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(Player playerId, EntityType entType) {
00064     if(preReqResources(playerId, entType) && preReqEntities(playerId, entType))
00065         return true;
00066     else
00067         return false;
00068 }
00069 
00070 bool FastEcslent::GameMgr::preReqResources(Player playerId, EntityType entType) {
00071     if(this->resources[playerId].minerals >= this->entTypeData[entType].minerals)
00072         return true;
00073     else
00074         return false;
00075 }
00076 
00077 bool FastEcslent::GameMgr::preReqEntities(Player playerId, EntityType entType) {
00078     //return true; //stubbed out for now
00079     std::set<EntityType> reqEntTypes;
00080     for(int i = 0; i < NENTITYTYPES; i++)
00081     {
00082         if(this->entTypeData[entType].neededTypes[i])
00083         {
00084             reqEntTypes.insert((EntityType)i);
00085         }
00086     }
00087     
00088     //loop until all required entities have been found, or all living entities have been checked
00089     for(int i = 0; i < this->engine->entityMgr->nEnts && reqEntTypes.size() > 0; i++)
00090     {
00091         if(reqEntTypes.find(this->engine->entityMgr->ents[i]->entityType) != reqEntTypes.end())
00092             reqEntTypes.erase(reqEntTypes.find(this->engine->entityMgr->ents[i]->entityType)); //found the entity we required, no longer check for it
00093     }
00094     
00095     if(reqEntTypes.size() == 0) //found all the required ents
00096         return true;
00097     else
00098         return false;
00099 }
00100 
00101     bool FastEcslent::GameMgr::consumeResources(Player playerId, EntityType entType) {
00102         if(this->preReqResources(playerId, entType))
00103         {
00104             this->resources[playerId].minerals -= this->entTypeData[entType].minerals;
00105             this->resources[playerId].gas -= this->entTypeData[entType].gas;
00106             return true;
00107         }
00108         return false;
00109         
00110     }
00111     
00112     bool FastEcslent::GameMgr::returnResources(Player playerId, EntityType entType) {
00113         this->resources[playerId].minerals += this->entTypeData[entType].minerals;
00114         this->resources[playerId].gas += this->entTypeData[entType].gas;
00115         return true;
00116     }
00117 
00118 
00119 void FastEcslent::GameMgr::setupEntityBuildTimes(){
00120         this->entTypeData[SCV].buildTime = 17;
00121         this->entTypeData[MARINE].buildTime = 25;
00122         this->entTypeData[REAPER].buildTime = 45;
00123         this->entTypeData[TANK].buildTime = 45;
00124         this->entTypeData[THOR].buildTime = 60;
00125         this->entTypeData[MARAUDER].buildTime = 30;
00126         this->entTypeData[HELLION].buildTime = 30;
00127 
00128         this->entTypeData[COMMANDCENTER].buildTime = 100;
00129         this->entTypeData[BARRACKS].buildTime = 65;
00130         this->entTypeData[FACTORY].buildTime = 60;
00131         this->entTypeData[ARMORY].buildTime = 65;
00132         this->entTypeData[ENGINEERINGBAY].buildTime = 35;
00133 
00134         this->entTypeData[REFINERY].buildTime = 30;
00135         this->entTypeData[SUPPLYDEPOT].buildTime = 30;
00136 
00137         this->entTypeData[MINERALS].buildTime = 0;
00138         this->entTypeData[GAS].buildTime      = 0;
00139         
00140         this->entTypeData[SCV].minerals = 50;
00141         this->entTypeData[MARINE].minerals = 50;
00142         this->entTypeData[REAPER].minerals = 150;
00143         this->entTypeData[TANK].minerals = 150;
00144         this->entTypeData[THOR].minerals = 150;
00145         this->entTypeData[MARAUDER].minerals = 150;
00146         this->entTypeData[HELLION].minerals = 150;
00147 
00148         this->entTypeData[COMMANDCENTER].minerals = 500;
00149         this->entTypeData[BARRACKS].minerals = 30;
00150         this->entTypeData[FACTORY].minerals = 30;
00151         this->entTypeData[ARMORY].minerals = 30;
00152         this->entTypeData[ENGINEERINGBAY].minerals = 30;
00153 
00154         this->entTypeData[REFINERY].minerals = 75;
00155         this->entTypeData[SUPPLYDEPOT].minerals = 75;
00156 }
00157 
00158 void FastEcslent::GameMgr::setupEntityBuildables(){
00159     //init all to false
00160     for (int i = 0; i < NENTITYTYPES; i++)
00161     {
00162         for (int j = 0; j < NENTITYTYPES; j++)
00163         {
00164                 this->entTypeData[i].buildableEntities[j] = false;
00165                 this->entTypeData[i].neededTypes[j] = false;
00166         }        
00167     }
00168         //enable buildable units
00169         this->entTypeData[SCV].buildableEntities[BARRACKS] = true;
00170         this->entTypeData[SCV].buildableEntities[REFINERY] = true;
00171         this->entTypeData[SCV].buildableEntities[FACTORY] = true;
00172         this->entTypeData[COMMANDCENTER].buildableEntities[SCV] = true;
00173         this->entTypeData[BARRACKS].buildableEntities[MARINE] = true;
00174         this->entTypeData[FACTORY].buildableEntities[HELLION] = true;
00175         
00176         this->entTypeData[FACTORY].neededTypes[BARRACKS] = true;
00177 }
00178 
00179 void FastEcslent::GameMgr::setupEntitySupply(){
00180         for (int i = 0; i < NENTITYTYPES; i++){
00181                 this->entTypeData[i].supply = 0;
00182         }
00183         this->entTypeData[SCV].supply      = 1;
00184         this->entTypeData[MARINE].supply   = 1;
00185         this->entTypeData[REAPER].supply   = 1;
00186         this->entTypeData[TANK].supply     = 3;
00187         this->entTypeData[THOR].supply     = 5;
00188         this->entTypeData[MARAUDER].supply = 2;
00189         this->entTypeData[HELLION].supply  = 2;
00190 }
00191 
00192 void FastEcslent::GameMgr::game0(){
00193         for(int i = 0; i < NPLAYERS; i++){
00194                 this->popCap[i] = 200;
00195                 this->currentPopCap[i] = 10;
00196                 this->pop[i] = 0;
00197         }
00198         setupEntityBuildTimes();
00199 
00200         tester();
00201 }
00202 
00203 void FastEcslent::GameMgr::WaterCraft(){
00204         //Initialize before any entities being created.
00205         setupEntityBuildTimes();
00206         setupEntitySupply();
00207     setupEntityBuildables();
00208 
00209         for(int i = 0; i < NPLAYERS; i++){
00210                 this->popCap[i] = 200;
00211                 this->pop[i]    = 0;
00212                 this->currentPopCap[i] = 10;
00213                 this->resources[i].gas      = 0;
00214                 this->resources[i].minerals = 50;
00215                 this->playerNEnts[i] = 0;
00216         }
00217 
00218         if(engine->options.isServer){
00219                 this->mineralPatchID = 1;
00220                 this->mineralPatchInit = false;
00221                 this->initMining = false;
00222 
00223                 float offset = 3500.0f;
00224                 makeBaseForSidePlayer(RED, ONE, Ogre::Vector3(-offset, 0, -offset), 550.0f, 0.06f);
00225                 makeBaseForSidePlayer(BLUE, THREE, Ogre::Vector3(offset, 0, offset), 550.0f, 0.06f);
00226                 //makeBaseForSidePlayer(YELLOW, THREE, Ogre::Vector3(-offset, 0, offset), 550.0f, 0.06f);
00227                 //makeBaseForSidePlayer(GREEN, FOUR, Ogre::Vector3(offset, 0, -offset), 550.0f, 0.06f);
00228 
00229         }else{
00230                 clearClient();
00231         }
00232 //  setupEntityBuildTimes();
00233 //      setupEntitySupply();
00234 
00235 //      float offset = 3500.0f;
00236 //      makeBaseForSidePlayer(RED, ONE, Ogre::Vector3(-offset, 0, -offset), 550.0f, 0.06f);
00237 //      //makeBaseForSidePlayer(BLUE, TWO, Ogre::Vector3(offset, 0, offset), 550.0f, 0.06f);
00238 //      //makeBaseForSidePlayer(YELLOW, THREE, Ogre::Vector3(-offset, 0, offset), 550.0f, 0.06f);
00239 //      //makeBaseForSidePlayer(RED, FOUR, Ogre::Vector3(offset, 0, -offset), 550.0f, 0.06f);
00240 //      startMining(RED, ONE);
00241 //      startMining(engine->options.side, engine->options.player);
00242 //      startMining(BLUE, THREE);
00243 }
00244 
00245 void FastEcslent::GameMgr::clearClient(){
00246 
00247 }
00248 
00249 void FastEcslent::GameMgr::makeBaseForSidePlayer(Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset){
00250         //create command center
00251         Entity *ent;
00252         ent = engine->entityMgr->createEntityForPlayerAndSide(COMMANDCENTER, location, 0.0f, side, player);
00253         ent->pos = location;
00254         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00255         this->currentEntityCounts[player][ent->entityType]++;
00256         this->playerEnts[player][this->playerNEnts[player]++] = ent;
00257         ent->entityState = ALIVE;
00258 
00259         //create Minerals
00260         createMineralPatch(8, NEUTRAL, player, location, offset, yawOffset);
00261 //      createNEntitiesRadial(MINERALS, 8, side, player, location, offset, yawOffset);
00262         // create Gas
00263         createNEntitiesRadial(GAS, 2, NEUTRAL, player, location, offset, yawOffset * 5, 2.0f);
00264         //create SCVS
00265         createNEntitiesRadial(SCV, 5, side, player, location, offset/3.0f, yawOffset/2.0f);
00266         
00267 }
00268 
00269 std::vector<FastEcslent::Entity*> FastEcslent::GameMgr::createNEntitiesRadial(EntityType entType, int nEntities, Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset, int yawOffsetMultiplier){
00270         std::vector<Entity*> ents;
00271         Entity *ent;
00272         Ogre::Vector3 entityLocation(location.x, 0, location.z);
00273         float radius = entityLocation.length();
00274         radius = radius + fabs(offset);
00275         float yaw  = atan2(location.z, location.x);
00276         yaw = yaw - yawOffset*nEntities/2.0f;
00277 
00278         entityLocation.x = cos(yaw) * radius;
00279         entityLocation.z = sin(yaw) * radius;
00280         for (int i = 0; i < nEntities; i++) {
00281                 ent = engine->entityMgr->createEntityForPlayerAndSide(entType, entityLocation, yaw, side, player );
00282 
00283                 this->pop[player] += this->entTypeData[ent->entityType].supply;
00284                 this->currentEntityCounts[player][ent->entityType]++;
00285                 this->playerEnts[player][this->playerNEnts[player]++] = ent;
00286                 ent->entityState = ALIVE;
00287 
00288                 ents.push_back(ent);
00289 
00290                 yaw += yawOffset * yawOffsetMultiplier;
00291                 entityLocation.x = cos(yaw) * radius;
00292                 entityLocation.z = sin(yaw) * radius;
00293         }
00294 
00295         return ents;
00296 }
00297 
00298 void FastEcslent::GameMgr::createMineralPatch(int nEntities, Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset, int yawOffsetMultiplier){
00299         MineralPatch *mp = new MineralPatch(this->mineralPatchID++);
00300         mineralPatches.push_back(mp);
00301         std::vector<Entity*> ents = createNEntitiesRadial(MINERALS, nEntities, side, player, location, offset, yawOffset);
00302         for(std::vector<Entity*>::iterator i= ents.begin(); i!= ents.end();i++){
00303                 Minerals *m = dynamic_cast<Minerals*>(*i);
00304                 mp->addMineral(m);
00305                 m->mineralPatchId = mp->getID();
00306         }
00307 }
00308 
00309 FastEcslent::Entity *FastEcslent::GameMgr::findClosestEntityOfTypeWithinDistance(EntityType entityType, Ogre::Vector3 pos, float maxDistance, Side side, Player player){
00310         float minDistance = FLT_MAX;
00311         float distance;
00312         Entity *minEnt = 0;
00313         Entity *ent = 0;
00314         for (int i = 0; i < this->playerNEnts[player]; i++){
00315                 ent = this->playerEnts[player][i];
00316                 //if (ent->entityType == MINERALS && ent->entityId.side == side && ent->entityId.player == player){
00317                 if (ent->entityType == entityType && ent->entityState == ALIVE){
00318                         DEBUG(std::cout << "Found: " << ent->uiname << ": " << ent->entityState << std::endl;)
00319                         distance = pos.distance(ent->pos);
00320                         if (distance < maxDistance) {
00321                                 DEBUG(std::cout << ent->uiname << ", distance: " << distance << std::endl;)
00322                                 if (distance < minDistance) {
00323                                         minDistance = distance;
00324                                         minEnt      = ent;
00325                                 }
00326                         }
00327                 }
00328         }
00329         return minEnt;
00330 }
00331 
00332 void FastEcslent::GameMgr::startMining(Side side, Player player){
00333         Entity *ent;
00334         for (int i = 0; i < this->playerNEnts[player]; i++){
00335                 ent = this->playerEnts[player][i];
00336                 if (ent->entityType == SCV ){
00337                         SCVehicle *scv = dynamic_cast<SCVehicle *>(ent);
00338                         Entity *mineral = findClosestEntityOfTypeWithinDistance(MINERALS, scv->pos, FLT_MAX, side, player);
00339                         assert(mineral != 0);
00340                         Gather *g = createGatherForEntAndMineral(scv, mineral);
00341                         UnitAI *ai = dynamic_cast<UnitAI *> (scv->getAspect(UNITAI));
00342                         ai->setCommand(g);
00343                 }
00344         }
00345 }
00346 
00347 
00348 void FastEcslent::GameMgr::tester(){
00349         int x = 0;
00350         int z = 0;
00351 
00352         int rangex = 2000;
00353         int rangez = rangex;
00354         int nEnts  = 50;
00355         DEBUG(std::cout << "Game2...nEnts: " << nEnts << std::endl;)
00356         engine->selectionMgr->resetAll();
00357         //Group *group = engine->groupMgr->createGroup();
00358 
00359         Entity *ent;
00360         ent = engine->entityMgr->createEntityAfterTime(BARRACKS, Ogre::Vector3(0, 0, 0), 0.0f);
00361         ent->pos = Ogre::Vector3(x, 0.0f, z);
00362         x += 500;
00363         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00364 
00365         ent = engine->entityMgr->createEntityAfterTime(COMMANDCENTER, Ogre::Vector3(0, 0, 0), 0.0f);
00366         ent->pos = Ogre::Vector3(x, 0.0f, z);
00367         x += 500;
00368         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00369 
00370         ent = engine->entityMgr->createEntityAfterTime(FACTORY, Ogre::Vector3(0, 0, 0), 0.0f);
00371         ent->pos = Ogre::Vector3(x, 0.0f, z);
00372         x += 500;
00373         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00374 
00375         ent = engine->entityMgr->createEntityAfterTime(REFINERY, Ogre::Vector3(0, 0, 0), 0.0f);
00376         ent->pos = Ogre::Vector3(x, 0.0f, z);
00377         x += 500;
00378         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00379 
00380         ent = engine->entityMgr->createEntityAfterTime(SUPPLYDEPOT, Ogre::Vector3(0, 0, 0), 0.0f);
00381         ent->pos = Ogre::Vector3(x, 0.0f, z);
00382         x += 500;
00383         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00384 
00385         ent = engine->entityMgr->createEntityAfterTime(ENGINEERINGBAY, Ogre::Vector3(0, 0, 0), 0.0f);
00386         ent->pos = Ogre::Vector3(x, 0.0f, z);
00387         x += 500;
00388         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00389 
00390         ent = engine->entityMgr->createEntityAfterTime(ARMORY, Ogre::Vector3(0, 0, 0), 0.0f);
00391         ent->pos = Ogre::Vector3(x, 0.0f, z);
00392         x += 500;
00393         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00394 
00395         x = 0;
00396         z = 0;
00397         for(int i = 0; i < nEnts; i++){
00398                 ent = engine->entityMgr->createEntityAfterTime(static_cast<EntityType>(i%7), Ogre::Vector3(0, 0, 0), 0.0f);
00399                 DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00400                 ent->pos = Ogre::Vector3(x, 0.0f, z);
00401                 z = rangez - random() % (2 * rangez);
00402                 x = rangex - random() % (2 * rangex);
00403                 ent->heading = (random()%180) * 0.0174532925;
00404                 ent->yaw = ent->heading;
00405                 ent->desiredHeading = ent->heading;
00406                 ent->desiredSpeed = 0.0f;
00407         }
00408 
00409 }
00410 
00411 void FastEcslent::GameMgr::tick(double dtime){
00412 //      if(!initMining){
00413 //              for(int i = 0;i<engine->entityMgr->nEnts;i++){
00414 //                      if(engine->entityMgr->ents[i]->entityType == SCV && engine->entityMgr->ents[i]->speed == 0){
00415 //                              initMining = true;
00416 //                              startMining(engine->options.side, engine->options.player);
00417 //                      }
00418 //              }
00419 //      }
00420         return;
00421 }
00422 
00423 FastEcslent::MineralPatch* FastEcslent::GameMgr:: getMineralPatch(int id){
00424         if(mineralPatches.size() == 0 && !this->mineralPatchInit){
00425                 this->mineralPatchInit = true;
00426                 initMineralPatch();
00427         }
00428 
00429         for(std::vector<MineralPatch*>::iterator i = this->mineralPatches.begin(); i != this->mineralPatches.end();i++){
00430                 if((*i)->getID() == id){
00431                         return *i;
00432                 }
00433         }
00434 
00435         MineralPatch *mp = new MineralPatch(id);
00436         mineralPatches.push_back(mp);
00437         return mp;
00438 }
00439 
00440 
00441 FastEcslent::MineralPatch* FastEcslent::GameMgr:: initMineralPatch(){
00442         for(int i = 0;i<engine->entityMgr->nEnts;i++){
00443                 if(engine->entityMgr->ents[i]->entityType == MINERALS){
00444                         Minerals * mineral = dynamic_cast<Minerals*>(engine->entityMgr->ents[i]);
00445                         MineralPatch* patch = this->getMineralPatch(mineral->mineralPatchId);
00446                         patch->addMineral(mineral);
00447                 }
00448         }
00449 }

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