gameMgr.cpp

Go to the documentation of this file.
00001 /*
00002  * gameMgr.cpp
00003  *
00004  *  Created on: Jan 9, 2012
00005  *      Author: sushil
00006  */
00007 #include <assert.h>
00008 
00009 #include <const.h>
00010 #include <utils.h>
00011 #include <engine.h>
00012 #include <ent.h>
00013 #include <aspect.h>
00014 #include <ai/gather.h>
00015 #include <commandHelp.h>
00016 #include <unitAI.h>
00017 
00018 #include <groupAI.h>
00019 #include <flock.h>
00020 
00021 #include <gameMgr.h>
00022 
00023 #include <OgreVector3.h>
00024 #include <cfloat>
00025 #include "DEBUG.h"
00026 
00027 //FastEcslent::GameMgr::GameMgr() {
00028 //      gameNumber = 0;
00029         //reset();
00030 //}
00031 
00032 FastEcslent::GameMgr::GameMgr(Engine* engine, Options opts): Mgr(engine) {
00033         //reset();
00034         options = opts;
00035 
00036 }
00037 
00038 void FastEcslent::GameMgr::init() {
00039 }
00040 
00041 void FastEcslent::GameMgr::loadLevel(){
00042         switch (engine->options.gameNumber) {
00043         case 0:
00044                 game0();
00045                 break;
00046         case 1:
00047                 WaterCraft();
00048                 break;
00049         case 2:
00050                 tester();
00051                 break;
00052         default:
00053                 tester();
00054         }
00055 }
00056 
00057 bool FastEcslent::GameMgr::notExceedPopCap(Identity entId) {
00058         Entity *ent = this->engine->entityMgr->ents[entId.id];
00059         return (this->pop[ent->entityId.player] + this->entTypeData[ent->entityType].supply <= this->currentPopCap[ent->entityId.player]);
00060         //return true;
00061 }
00062 
00063 bool FastEcslent::GameMgr::preReqExists(EntityType entType) {
00064         return true;
00065 }
00066 
00067 void FastEcslent::GameMgr::setupEntityBuildTimes(){
00068         this->entTypeData[SCV].buildTime = 17;
00069         this->entTypeData[MARINE].buildTime = 25;
00070         this->entTypeData[REAPER].buildTime = 45;
00071         this->entTypeData[TANK].buildTime = 45;
00072         this->entTypeData[THOR].buildTime = 60;
00073         this->entTypeData[MARAUDER].buildTime = 30;
00074         this->entTypeData[HELLION].buildTime = 30;
00075 
00076         this->entTypeData[COMMANDCENTER].buildTime = 100;
00077         this->entTypeData[BARRACKS].buildTime = 65;
00078         this->entTypeData[FACTORY].buildTime = 60;
00079         this->entTypeData[ARMORY].buildTime = 65;
00080         this->entTypeData[ENGINEERINGBAY].buildTime = 35;
00081 
00082         this->entTypeData[REFINERY].buildTime = 30;
00083         this->entTypeData[SUPPLYDEPOT].buildTime = 30;
00084 
00085         this->entTypeData[MINERALS].buildTime = 0;
00086         this->entTypeData[GAS].buildTime      = 0;
00087 }
00088 
00089 void FastEcslent::GameMgr::setupEntitySupply(){
00090         for (int i = 0; i < NENTITYTYPES; i++){
00091                 this->entTypeData[i].supply = 0;
00092         }
00093         this->entTypeData[SCV].supply      = 1;
00094         this->entTypeData[MARINE].supply   = 1;
00095         this->entTypeData[REAPER].supply   = 1;
00096         this->entTypeData[TANK].supply     = 3;
00097         this->entTypeData[THOR].supply     = 5;
00098         this->entTypeData[MARAUDER].supply = 2;
00099         this->entTypeData[HELLION].supply  = 2;
00100 }
00101 
00102 void FastEcslent::GameMgr::game0(){
00103         for(int i = 0; i < NPLAYERS; i++){
00104                 this->popCap[i] = 200;
00105                 this->currentPopCap[i] = 10;
00106                 this->pop[i] = 0;
00107         }
00108         setupEntityBuildTimes();
00109 
00110         tester();
00111 }
00112 
00113 void FastEcslent::GameMgr::WaterCraft(){
00114         if(engine->options.isServer){
00115                 for(int i = 0; i < NPLAYERS; i++){
00116                         this->popCap[i] = 200;
00117                         this->pop[i]    = 0;
00118                         this->currentPopCap[i] = 10;
00119                         this->resources[i].gas      = 0;
00120                         this->resources[i].minerals = 50;
00121                         this->playerNEnts[i] = 0;
00122                 }
00123                 setupEntityBuildTimes();
00124                 setupEntitySupply();
00125                 float offset = 3500.0f;
00126                 makeBaseForSidePlayer(RED, ONE, Ogre::Vector3(-offset, 0, -offset), 550.0f, 0.06f);
00127                 makeBaseForSidePlayer(BLUE, THREE, Ogre::Vector3(offset, 0, offset), 550.0f, 0.06f);
00128                 //makeBaseForSidePlayer(YELLOW, THREE, Ogre::Vector3(-offset, 0, offset), 550.0f, 0.06f);
00129                 //makeBaseForSidePlayer(GREEN, FOUR, Ogre::Vector3(offset, 0, -offset), 550.0f, 0.06f);
00130                 startMining(RED, ONE);
00131                 startMining(BLUE, THREE);
00132         }else{
00133                 clearClient();
00134         }
00135 }
00136 
00137 void FastEcslent::GameMgr::clearClient(){
00138 
00139 }
00140 
00141 void FastEcslent::GameMgr::makeBaseForSidePlayer(Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset){
00142         //create command center
00143         Entity *ent;
00144         ent = engine->entityMgr->createEntityForPlayerAndSide(COMMANDCENTER, location, 0.0f, side, player);
00145         ent->pos = location;
00146         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00147         ent->init();
00148         this->currentEntityCounts[player][ent->entityType]++;
00149         this->playerEnts[player][this->playerNEnts[player]++] = ent;
00150         ent->entityState = ALIVE;
00151 
00152         //create Minerals
00153         createMineralPatch(8, side, player, location, offset, yawOffset);
00154 //      createNEntitiesRadial(MINERALS, 8, side, player, location, offset, yawOffset);
00155         // create Gas
00156         createNEntitiesRadial(GAS, 2, side, player, location, offset, yawOffset * 5, 2.0f);
00157         //create SCVS
00158         createNEntitiesRadial(SCV, 5, side, player, location, offset/3.0f, yawOffset/2.0f);
00159 
00160 }
00161 
00162 std::vector<FastEcslent::Entity*> FastEcslent::GameMgr::createNEntitiesRadial(EntityType entType, int nEntities, Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset, int yawOffsetMultiplier){
00163         std::vector<Entity*> ents;
00164         Entity *ent;
00165         Ogre::Vector3 entityLocation(location.x, 0, location.z);
00166         float radius = entityLocation.length();
00167         radius = radius + fabs(offset);
00168         float yaw  = atan2(location.z, location.x);
00169         yaw = yaw - yawOffset*nEntities/2.0f;
00170 
00171         entityLocation.x = cos(yaw) * radius;
00172         entityLocation.z = sin(yaw) * radius;
00173         for (int i = 0; i < nEntities; i++) {
00174                 ent = engine->entityMgr->createEntityForPlayerAndSide(entType, entityLocation, yaw, side, player );
00175                 ent->init();
00176 
00177                 this->pop[player] += this->entTypeData[ent->entityType].supply;
00178                 this->currentEntityCounts[player][ent->entityType]++;
00179                 this->playerEnts[player][this->playerNEnts[player]++] = ent;
00180                 ent->entityState = ALIVE;
00181 
00182                 ents.push_back(ent);
00183 
00184                 yaw += yawOffset * yawOffsetMultiplier;
00185                 entityLocation.x = cos(yaw) * radius;
00186                 entityLocation.z = sin(yaw) * radius;
00187         }
00188 
00189         return ents;
00190 }
00191 
00192 void FastEcslent::GameMgr::createMineralPatch(int nEntities, Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset, int yawOffsetMultiplier){
00193         MineralPatch *mp = new MineralPatch();
00194         mineralPatches.push_back(mp);
00195         std::vector<Entity*> ents = createNEntitiesRadial(MINERALS, nEntities, side, player, location, offset, yawOffset);
00196         for(std::vector<Entity*>::iterator i= ents.begin(); i!= ents.end();i++){
00197                 Minerals *m = dynamic_cast<Minerals*>(*i);
00198                 mp->addMineral(m);
00199                 m->patch = mp;
00200         }
00201 }
00202 
00203 FastEcslent::Entity *FastEcslent::GameMgr::findClosestEntityOfTypeWithinDistance(EntityType entityType, Ogre::Vector3 pos, float maxDistance, Side side, Player player){
00204         float minDistance = FLT_MAX;
00205         float distance;
00206         Entity *minEnt = 0;
00207         Entity *ent = 0;
00208         for (int i = 0; i < this->playerNEnts[player]; i++){
00209                 ent = this->playerEnts[player][i];
00210                 //if (ent->entityType == MINERALS && ent->entityId.side == side && ent->entityId.player == player){
00211                 if (ent->entityType == entityType && ent->entityState == ALIVE){
00212                         DEBUG(std::cout << "Found: " << ent->uiname << ": " << ent->entityState << std::endl;)
00213                         distance = pos.distance(ent->pos);
00214                         if (distance < maxDistance) {
00215                                 DEBUG(std::cout << ent->uiname << ", distance: " << distance << std::endl;)
00216                                 if (distance < minDistance) {
00217                                         minDistance = distance;
00218                                         minEnt      = ent;
00219                                 }
00220                         }
00221                 }
00222         }
00223         return minEnt;
00224 }
00225 
00226 void FastEcslent::GameMgr::startMining(Side side, Player player){
00227         Entity *ent;
00228         for (int i = 0; i < this->playerNEnts[player]; i++){
00229                 ent = this->playerEnts[player][i];
00230                 if (ent->entityType == SCV ){
00231                         SCVehicle *scv = dynamic_cast<SCVehicle *>(ent);
00232                         Entity *mineral = findClosestEntityOfTypeWithinDistance(MINERALS, scv->pos, FLT_MAX, side, player);
00233                         assert(mineral != 0);
00234                         Gather *g = createGatherForEntAndMineral(scv, dynamic_cast<Minerals *>(mineral));
00235                         UnitAI *ai = dynamic_cast<UnitAI *> (scv->getAspect(UNITAI));
00236                         ai->setCommand(g);
00237                 }
00238         }
00239 }
00240 
00241 
00242 void FastEcslent::GameMgr::tester(){
00243         int x = 0;
00244         int z = 0;
00245 
00246         int rangex = 2000;
00247         int rangez = rangex;
00248         int nEnts  = 50;
00249         DEBUG(std::cout << "Game2...nEnts: " << nEnts << std::endl;)
00250         engine->selectionMgr->resetAll();
00251         //Group *group = engine->groupMgr->createGroup();
00252 
00253         Entity *ent;
00254         ent = engine->entityMgr->createEntityAfterTime(BARRACKS, Ogre::Vector3(0, 0, 0), 0.0f);
00255         ent->pos = Ogre::Vector3(x, 0.0f, z);
00256         x += 500;
00257         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00258         ent->init();
00259 
00260         ent = engine->entityMgr->createEntityAfterTime(COMMANDCENTER, Ogre::Vector3(0, 0, 0), 0.0f);
00261         ent->pos = Ogre::Vector3(x, 0.0f, z);
00262         x += 500;
00263         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00264         ent->init();
00265 
00266         ent = engine->entityMgr->createEntityAfterTime(FACTORY, Ogre::Vector3(0, 0, 0), 0.0f);
00267         ent->pos = Ogre::Vector3(x, 0.0f, z);
00268         x += 500;
00269         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00270         ent->init();
00271 
00272         ent = engine->entityMgr->createEntityAfterTime(REFINERY, Ogre::Vector3(0, 0, 0), 0.0f);
00273         ent->pos = Ogre::Vector3(x, 0.0f, z);
00274         x += 500;
00275         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00276         ent->init();
00277 
00278         ent = engine->entityMgr->createEntityAfterTime(SUPPLYDEPOT, Ogre::Vector3(0, 0, 0), 0.0f);
00279         ent->pos = Ogre::Vector3(x, 0.0f, z);
00280         x += 500;
00281         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00282         ent->init();
00283 
00284         ent = engine->entityMgr->createEntityAfterTime(ENGINEERINGBAY, Ogre::Vector3(0, 0, 0), 0.0f);
00285         ent->pos = Ogre::Vector3(x, 0.0f, z);
00286         x += 500;
00287         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00288         ent->init();
00289 
00290         ent = engine->entityMgr->createEntityAfterTime(ARMORY, Ogre::Vector3(0, 0, 0), 0.0f);
00291         ent->pos = Ogre::Vector3(x, 0.0f, z);
00292         x += 500;
00293         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00294         ent->init();
00295 
00296         x = 0;
00297         z = 0;
00298         for(int i = 0; i < nEnts; i++){
00299                 ent = engine->entityMgr->createEntityAfterTime(static_cast<EntityType>(i%7), Ogre::Vector3(0, 0, 0), 0.0f);
00300                 DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00301                 ent->init();
00302                 ent->pos = Ogre::Vector3(x, 0.0f, z);
00303                 z = rangez - random() % (2 * rangez);
00304                 x = rangex - random() % (2 * rangex);
00305                 ent->heading = (random()%180) * 0.0174532925;
00306                 ent->yaw = ent->heading;
00307                 ent->desiredHeading = ent->heading;
00308                 ent->desiredSpeed = 0.0f;
00309         }
00310 
00311 }
00312 
00313 
00314 
00315 void FastEcslent::GameMgr::tick(double dtime){
00316         return;
00317 }
00318 
00319 
00320 
00321 

Generated on Fri Dec 13 14:54:17 2013 for FastECSLent by  doxygen 1.5.4