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