#include "Student.h"
#include "Splat.h"
#include <stdlib.h>
#include <fedtime.hh>
#ifndef _MSC_VER
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#else
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#endif
#define STUDENT_NUM 10
extern RTI::RTIambassador rtiAmb;
extern Student* myStudent;
extern RTIfedTime Lookahead;
extern RTIfedTime CurrentTime;
extern RTI::Boolean Regulating;
RTI::RTIambassador* Student::ms_rtiAmb=NULL;
RTI::Boolean Student::ms_doRegistry=RTI::RTI_FALSE;
RTI::ObjectClassHandle Student::m_StudentHandle=0;
RTI::AttributeHandle Student::m_LunchMoneyHandle=0;
RTI::AttributeHandle Student::m_AmmoAmountHandle=0;
RTI::AttributeHandle Student::m_CleanlinessHandle=0;
RTI::AttributeHandle Student::m_PrivilegeToDeleteHandle=0;
char* const Student::ms_StudentTypeStr="Student";
char* const Student::ms_LunchMoneyTypeStr="LunchMoney";
char* const Student::ms_AmmoAmountTypeStr="AmmoAmount";
char* const Student::ms_CleanlinessTypeStr="Cleanliness";
char* const Student::ms_PrivilegeToDeleteTypeStr="PrivilegeToDelete";
Student* Student::ms_StudentExtent[MAX_FIGHTGAME_FEDERATES]=
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
int Student::ms_extentCardinality=0;
Student::Student(char* name)
:ms_name(NULL),
m_LunchMoney(20),
m_AmmoAmount(0),
m_Cleanliness(100),
m_EnsuingMess(10),
ms_OwnLunchMoney(RTI::RTI_TRUE),
ms_OwnAmmoAmount(RTI::RTI_TRUE),
ms_OwnCleanliness(RTI::RTI_TRUE),
ms_OwnPrivilegeToDelete(RTI::RTI_TRUE),
ms_TransferLunchMoney(RTI::RTI_FALSE),
ms_TransferAmmoAmount(RTI::RTI_FALSE),
ms_TransferCleanliness(RTI::RTI_FALSE),
ms_TransferPrivilegeToDelete(RTI::RTI_FALSE),
ms_LunchMoneyUpdate(RTI::RTI_FALSE),
ms_AmmoAmountUpdate(RTI::RTI_FALSE),
ms_CleanlinessUpdate(RTI::RTI_FALSE),
ms_InstanceDeleted(RTI::RTI_FALSE)
{
cout<<"***Student() OK"<<endl<<endl;
Student::ms_StudentExtent[ Student::ms_extentCardinality++ ] = this;
this->SetName(name);
this->SetLunchMoney(20);
this->SetAmmoAmount(0);
this->SetCleanliness(100);
this->SetEnsuingMess(10);
}
Student::Student(RTI::ObjectHandle id)
:ms_name(NULL),
m_InstanceId(id),
m_LunchMoney(20),
m_AmmoAmount(0),
m_Cleanliness(100),
m_EnsuingMess(10),
ms_OwnLunchMoney(RTI::RTI_FALSE),
ms_OwnAmmoAmount(RTI::RTI_FALSE),
ms_OwnCleanliness(RTI::RTI_FALSE),
ms_OwnPrivilegeToDelete(RTI::RTI_FALSE),
ms_TransferLunchMoney(RTI::RTI_FALSE),
ms_TransferAmmoAmount(RTI::RTI_FALSE),
ms_TransferCleanliness(RTI::RTI_FALSE),
ms_TransferPrivilegeToDelete(RTI::RTI_FALSE),
ms_LunchMoneyUpdate(RTI::RTI_FALSE),
ms_AmmoAmountUpdate(RTI::RTI_FALSE),
ms_CleanlinessUpdate(RTI::RTI_FALSE),
ms_InstanceDeleted(RTI::RTI_FALSE)
{
cout<<"***Student(id) OK"<<endl<<endl;
Student::ms_StudentExtent[ Student::ms_extentCardinality++ ] = this;
this->SetName("two");
this->SetLunchMoney(20);
this->SetAmmoAmount(0);
this->SetCleanliness(100);
this->SetEnsuingMess(10);
}
Student::~Student()
{
cout<<"***~Student() OK"<<endl<<endl;
Student *pStudent = NULL;
int idx=0;
for(idx=0;idx<Student::ms_extentCardinality;idx++)
{
pStudent = Student::ms_StudentExtent[idx];
if (pStudent && pStudent->GetInstanceId() == this->GetInstanceId())
{
break;
}
}
if (pStudent)
{
for (int i=idx;(i<Student::ms_extentCardinality) && (Student::ms_StudentExtent[i]!=NULL); i++)
{
Student::ms_StudentExtent[i] = Student::ms_StudentExtent[i+1];
}
Student::ms_extentCardinality = Student::ms_extentCardinality - 1;
if (ms_rtiAmb && idx == 0)
{
ms_rtiAmb->queryLookahead(Lookahead);
(void)ms_rtiAmb->deleteObjectInstance(this->GetInstanceId(),CurrentTime+Lookahead,"Student Quits");
}
else
{
}
}
delete [] ms_name;
}
void Student::Init(RTI::RTIambassador* rtiAmb)
{
cout<<"***Init() OK"<<endl<<endl;
ms_rtiAmb = rtiAmb;
if(ms_rtiAmb)
{
try
{
m_StudentHandle = ms_rtiAmb->getObjectClassHandle(ms_StudentTypeStr);
m_AmmoAmountHandle= ms_rtiAmb->getAttributeHandle(ms_AmmoAmountTypeStr,m_StudentHandle);
m_LunchMoneyHandle = ms_rtiAmb->getAttributeHandle(ms_LunchMoneyTypeStr,m_StudentHandle);
m_CleanlinessHandle = ms_rtiAmb->getAttributeHandle(ms_CleanlinessTypeStr,m_StudentHandle);
m_PrivilegeToDeleteHandle = ms_rtiAmb->getAttributeHandle(ms_PrivilegeToDeleteTypeStr,m_StudentHandle);
cout<<"***acquired class attribute handles"<<endl;
cout<<"***StudentHandle:"<<m_StudentHandle<<endl;
cout<<"***AmmoAmountHandle:"<<m_AmmoAmountHandle<<endl;
cout<<"***LunchMoneyHandle:"<<m_LunchMoneyHandle<<endl;
cout<<"***CleanlinessHandle:"<<m_CleanlinessHandle<<endl;
cout<<"***PrivilegeToDeleteHandle:"<<m_PrivilegeToDeleteHandle<<endl<<endl;
}
catch ( RTI::Exception& e )
{
cout << e << endl;
exit(1);
}
}
}
void Student::Publishing()
{
cout<<"***Publishing() OK"<<endl<<endl;
if(ms_rtiAmb)
{
RTI::AttributeHandleSet* StudentAttributes;
try
{
StudentAttributes = RTI::AttributeHandleSetFactory::create(4);
StudentAttributes->add(m_AmmoAmountHandle);
StudentAttributes->add(m_LunchMoneyHandle);
StudentAttributes->add(m_CleanlinessHandle);
StudentAttributes->add(m_PrivilegeToDeleteHandle);
}
catch ( RTI::Exception& e )
{
cout << e << endl;
exit(1);
}
try
{
cout<<"*** published objectclass student"<<endl<<endl;
ms_rtiAmb->publishObjectClass(m_StudentHandle,*StudentAttributes);
}
catch ( RTI::Exception& e )
{
cout << e << endl;
exit(1);
}
if(StudentAttributes)
{
StudentAttributes->empty();
delete StudentAttributes;
}
}
}
void Student::Subscribing()
{
cout<<"***Subscribing() OK"<<endl<<endl;
if(ms_rtiAmb)
{
RTI::AttributeHandleSet* StudentAttributes;
try
{
StudentAttributes = RTI::AttributeHandleSetFactory::create(4);
StudentAttributes->add(m_AmmoAmountHandle);
StudentAttributes->add(m_LunchMoneyHandle);
StudentAttributes->add(m_CleanlinessHandle);
StudentAttributes->add(m_PrivilegeToDeleteHandle);
}
catch ( RTI::Exception& e )
{
cout << e << endl;
exit(1);
}
try
{
cout<<"*** Subscribe objectclass student"<<endl<<endl;
ms_rtiAmb->subscribeObjectClassAttributes(m_StudentHandle,*StudentAttributes);
}
catch ( RTI::Exception& e )
{
cout << e << endl;
exit(1);
}
if(StudentAttributes)
{
StudentAttributes->empty();
delete StudentAttributes;
}
}
}
void Student::Register()
{
cout<<"***Register() OK"<<endl<<endl;
if(ms_rtiAmb)
{
if(!ms_doRegistry)
{
cout<<"*** because no one subscrib student class,so tick time!"<<endl;
}
while(!ms_doRegistry)
{
ms_rtiAmb->tick(0.01,2.0);
}
for(int n=0;n<STUDENT_NUM;n++)
{
try
{
(this->ms_StudentExtent[n])->m_InstanceId =
ms_rtiAmb->registerObjectInstance(
(this->ms_StudentExtent[n])->GetStudentRtiId(),
(this->ms_StudentExtent[n])->GetName());
cout<<"*** registered student:"
<<(this->ms_StudentExtent[n])->GetInstanceId()
<<"Name:"<<(this->ms_StudentExtent[n])->GetName()<<endl;
ms_rtiAmb->tick(0.01,2.0);
}
catch ( RTI::Exception& e )
{
cout << e << endl;
exit(1);
}
}
}
}
void Student::DeleteInstance(Student* myStudent)
{
cout<<"***DeleteInstance() OK"<<endl<<endl;
Student* pStudent = NULL;
for(int idx=0;idx<myStudent->ms_extentCardinality;idx++)
{
pStudent = myStudent->ms_StudentExtent[idx];
if(pStud
- 1
- 2
- 3
- 4
- 5
前往页