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