00001 /* 00002 * engine.cpp 00003 * 00004 * Created on: Nov 20, 2011 00005 * Author: sushil 00006 */ 00007 00008 #include <boost/date_time/posix_time/posix_time.hpp> 00009 using namespace boost::posix_time; 00010 00011 #include <utils.h> 00012 00013 #include <timer.h> 00014 00015 //#include <entityMgr.h> 00016 #include <engine.h> 00017 00018 using namespace std; 00019 00020 void FastEcslent::Engine::constructManagers() { 00021 entityMgr = new EntityMgr(this, options); 00022 selectionMgr = new SelectionMgr(this, options); 00023 weaponMgr = new WeaponMgr(this, options); 00024 00025 gameMgr = new GameMgr(this, options); 00026 00027 //optional managers 00028 if(options.tacticalAI) { 00029 distanceMgr = new DistanceMgr(this, options); 00030 groupMgr = new GroupMgr(this, options); 00031 } 00032 00033 //optional threads 00034 if(options.enableNetworking) net = new NetThread(this); 00035 if(options.enableGfx) gfx = new GfxThread(this); 00036 00037 //construct others 00038 } 00039 void FastEcslent::Engine::init() { 00040 selectionTimer = new FastEcslent::MilliSecondTimer(200.0f); 00041 distanceTimer = new FastEcslent::MilliSecondTimer(500.0f); 00042 00043 sleepInterval = new boost::posix_time::milliseconds(300); 00044 00045 entityMgr->init(); 00046 selectionMgr->init(); 00047 weaponMgr->init(); 00048 00049 gameMgr->init(); 00050 00051 if (options.tacticalAI) { 00052 distanceMgr->init(); 00053 groupMgr->init(); 00054 } 00055 00056 //optional threads 00057 if (options.enableNetworking) net->init(); 00058 if (options.enableGfx) { 00059 gfx->init(); 00060 gameState = LOBBY; 00061 } 00062 } 00063 00064 void FastEcslent::Engine::loadLevel(){ 00065 if (options.enableNetworking) net->run(); 00066 if (options.enableGfx) { 00067 gfx->run(); 00068 //wait until lobby setting finished 00069 while(!gfx->gimPtr || this->gameState == LOBBY){ 00070 boost::this_thread::sleep(*sleepInterval); 00071 } 00072 } 00073 00074 gameMgr->loadLevel(); 00075 //sleep(2.0); // 00076 return; 00077 } 00078 00079 void FastEcslent::Engine::releaseLevel(){ 00080 //optional 00081 if (options.enableNetworking) net->stopAndJoin(); 00082 if (options.enableGfx) gfx->join(); 00083 00084 return; 00085 } 00086 00087 void FastEcslent::Engine::stop(){ 00088 // 00089 //std::cout << "Engine stopping" << std::endl; 00090 return; 00091 } 00092 00093 void FastEcslent::Engine::tickAll(float dt){ 00094 entityMgr->tick(dt); 00095 selectionMgr->tick(dt); 00096 weaponMgr->tick(dt); 00097 00098 gameMgr->tick(dt); 00099 // tick other managers 00100 00101 //optional Managers must be ticked 00102 if (options.tacticalAI) { 00103 distanceMgr->tick(dt); 00104 groupMgr->tick(dt); 00105 } 00106 //Threads are not ticked 00107 00108 } 00109 00110 void FastEcslent::Engine::run(){ 00111 //unsigned long int frame = 0; 00112 float dt = 0.0005; //1.0f/60.0f; 00113 double runTime = 0.0f; 00114 ptime oldTime = getCurrentTime(); 00115 ptime newTime; 00116 time_duration diff; 00117 double speedup = options.speedup; 00118 00119 float dtime = 0.0; 00120 while (runTime < 5000000.0 && !this->quit){ 00121 00122 tickAll(dtime * speedup); // tick all managers 00123 00124 //update times 00125 runTime += dt; 00126 00127 newTime = getCurrentTime(); 00128 diff = newTime - oldTime; 00129 oldTime = newTime; 00130 dtime = diff.total_microseconds() * 0.000001f; //inseconds 00131 /* 00132 if(options.runDebugTests){ 00133 runTests(); 00134 } 00135 */ 00136 /* 00137 if (frame%1000 == 0) { 00138 //cout << "Frame: " << frame << " Number of collisions: " << distanceMgr->collisionTotal << endl; 00139 this->selectionMgr->dump(); 00140 } 00141 frame++; 00142 */ 00143 00144 } 00145 cout << endl << "Finished, running. Dumping first entity..." << endl; 00146 entityMgr->dumpOne(0); 00147 //cout << " Number of collisions: " << distanceMgr->collisionTotal << endl; 00148 this->quit = true; 00149 00150 00151 } 00152 00153 void FastEcslent::Engine::runTests(){ 00154 int entIndex; 00155 Entity** selected; 00156 00157 00158 //test distance mgr 00159 if(options.tacticalAI){ 00160 if(distanceTimer->hasFired()){ 00161 distanceMgr->dumpAll(); 00162 } 00163 } 00164 00165 //test selection 00166 00167 if(selectionTimer->hasFired()){ 00168 entIndex = random()% this->entityMgr->nEnts; 00169 cout << "Unselected: " << entIndex << endl; 00170 this->selectionMgr->unselect(entIndex); 00171 selected = this->selectionMgr->getSelectedEnts(); 00172 cout << "Selected: "; 00173 for(int i = 0; i < this->selectionMgr->getNSelectedEnts(); i++){ 00174 cout << "(" << selected[i]->entityId.id << ": " << selected[i]->isSelected << ") "; 00175 } 00176 cout << endl; 00177 } 00178 //cout << "Iteration: " << n++ << endl;; 00179 //std::cout << "dtime: " << dtime << std::endl; 00180 00181 } 00182 00183 00184 00185