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         this->currentEntityCounts[player][ent->entityType]++;
00148         this->playerEnts[player][this->playerNEnts[player]++] = ent;
00149         ent->entityState = ALIVE;
00150 
00151         //create Minerals
00152         createMineralPatch(8, side, player, location, offset, yawOffset);
00153 //      createNEntitiesRadial(MINERALS, 8, side, player, location, offset, yawOffset);
00154         // create Gas
00155         createNEntitiesRadial(GAS, 2, side, player, location, offset, yawOffset * 5, 2.0f);
00156         //create SCVS
00157         createNEntitiesRadial(SCV, 5, side, player, location, offset/3.0f, yawOffset/2.0f);
00158 
00159 }
00160 
00161 std::vector<FastEcslent::Entity*> FastEcslent::GameMgr::createNEntitiesRadial(EntityType entType, int nEntities, Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset, int yawOffsetMultiplier){
00162         std::vector<Entity*> ents;
00163         Entity *ent;
00164         Ogre::Vector3 entityLocation(location.x, 0, location.z);
00165         float radius = entityLocation.length();
00166         radius = radius + fabs(offset);
00167         float yaw  = atan2(location.z, location.x);
00168         yaw = yaw - yawOffset*nEntities/2.0f;
00169 
00170         entityLocation.x = cos(yaw) * radius;
00171         entityLocation.z = sin(yaw) * radius;
00172         for (int i = 0; i < nEntities; i++) {
00173                 ent = engine->entityMgr->createEntityForPlayerAndSide(entType, entityLocation, yaw, side, player );
00174 
00175                 this->pop[player] += this->entTypeData[ent->entityType].supply;
00176                 this->currentEntityCounts[player][ent->entityType]++;
00177                 this->playerEnts[player][this->playerNEnts[player]++] = ent;
00178                 ent->entityState = ALIVE;
00179 
00180                 ents.push_back(ent);
00181 
00182                 yaw += yawOffset * yawOffsetMultiplier;
00183                 entityLocation.x = cos(yaw) * radius;
00184                 entityLocation.z = sin(yaw) * radius;
00185         }
00186 
00187         return ents;
00188 }
00189 
00190 void FastEcslent::GameMgr::createMineralPatch(int nEntities, Side side, Player player, Ogre::Vector3 location, float offset, float yawOffset, int yawOffsetMultiplier){
00191         MineralPatch *mp = new MineralPatch();
00192         mineralPatches.push_back(mp);
00193         std::vector<Entity*> ents = createNEntitiesRadial(MINERALS, nEntities, side, player, location, offset, yawOffset);
00194         for(std::vector<Entity*>::iterator i= ents.begin(); i!= ents.end();i++){
00195                 Minerals *m = dynamic_cast<Minerals*>(*i);
00196                 mp->addMineral(m);
00197                 m->patch = mp;
00198         }
00199 }
00200 
00201 FastEcslent::Entity *FastEcslent::GameMgr::findClosestEntityOfTypeWithinDistance(EntityType entityType, Ogre::Vector3 pos, float maxDistance, Side side, Player player){
00202         float minDistance = FLT_MAX;
00203         float distance;
00204         Entity *minEnt = 0;
00205         Entity *ent = 0;
00206         for (int i = 0; i < this->playerNEnts[player]; i++){
00207                 ent = this->playerEnts[player][i];
00208                 //if (ent->entityType == MINERALS && ent->entityId.side == side && ent->entityId.player == player){
00209                 if (ent->entityType == entityType && ent->entityState == ALIVE){
00210                         DEBUG(std::cout << "Found: " << ent->uiname << ": " << ent->entityState << std::endl;)
00211                         distance = pos.distance(ent->pos);
00212                         if (distance < maxDistance) {
00213                                 DEBUG(std::cout << ent->uiname << ", distance: " << distance << std::endl;)
00214                                 if (distance < minDistance) {
00215                                         minDistance = distance;
00216                                         minEnt      = ent;
00217                                 }
00218                         }
00219                 }
00220         }
00221         return minEnt;
00222 }
00223 
00224 void FastEcslent::GameMgr::startMining(Side side, Player player){
00225         Entity *ent;
00226         for (int i = 0; i < this->playerNEnts[player]; i++){
00227                 ent = this->playerEnts[player][i];
00228                 if (ent->entityType == SCV ){
00229                         SCVehicle *scv = dynamic_cast<SCVehicle *>(ent);
00230                         Entity *mineral = findClosestEntityOfTypeWithinDistance(MINERALS, scv->pos, FLT_MAX, side, player);
00231                         assert(mineral != 0);
00232                         Gather *g = createGatherForEntAndMineral(scv, dynamic_cast<Minerals *>(mineral));
00233                         UnitAI *ai = dynamic_cast<UnitAI *> (scv->getAspect(UNITAI));
00234                         ai->setCommand(g);
00235                 }
00236         }
00237 }
00238 
00239 
00240 void FastEcslent::GameMgr::tester(){
00241         int x = 0;
00242         int z = 0;
00243 
00244         int rangex = 2000;
00245         int rangez = rangex;
00246         int nEnts  = 50;
00247         DEBUG(std::cout << "Game2...nEnts: " << nEnts << std::endl;)
00248         engine->selectionMgr->resetAll();
00249         //Group *group = engine->groupMgr->createGroup();
00250 
00251         Entity *ent;
00252         ent = engine->entityMgr->createEntityAfterTime(BARRACKS, Ogre::Vector3(0, 0, 0), 0.0f);
00253         ent->pos = Ogre::Vector3(x, 0.0f, z);
00254         x += 500;
00255         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00256 
00257         ent = engine->entityMgr->createEntityAfterTime(COMMANDCENTER, Ogre::Vector3(0, 0, 0), 0.0f);
00258         ent->pos = Ogre::Vector3(x, 0.0f, z);
00259         x += 500;
00260         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00261 
00262         ent = engine->entityMgr->createEntityAfterTime(FACTORY, Ogre::Vector3(0, 0, 0), 0.0f);
00263         ent->pos = Ogre::Vector3(x, 0.0f, z);
00264         x += 500;
00265         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00266 
00267         ent = engine->entityMgr->createEntityAfterTime(REFINERY, Ogre::Vector3(0, 0, 0), 0.0f);
00268         ent->pos = Ogre::Vector3(x, 0.0f, z);
00269         x += 500;
00270         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00271 
00272         ent = engine->entityMgr->createEntityAfterTime(SUPPLYDEPOT, 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 
00277         ent = engine->entityMgr->createEntityAfterTime(ENGINEERINGBAY, Ogre::Vector3(0, 0, 0), 0.0f);
00278         ent->pos = Ogre::Vector3(x, 0.0f, z);
00279         x += 500;
00280         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00281 
00282         ent = engine->entityMgr->createEntityAfterTime(ARMORY, Ogre::Vector3(0, 0, 0), 0.0f);
00283         ent->pos = Ogre::Vector3(x, 0.0f, z);
00284         x += 500;
00285         DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00286 
00287         x = 0;
00288         z = 0;
00289         for(int i = 0; i < nEnts; i++){
00290                 ent = engine->entityMgr->createEntityAfterTime(static_cast<EntityType>(i%7), Ogre::Vector3(0, 0, 0), 0.0f);
00291                 DEBUG(std::cout << "Game Manager: " << ent->uiname << std::endl;)
00292                 ent->pos = Ogre::Vector3(x, 0.0f, z);
00293                 z = rangez - random() % (2 * rangez);
00294                 x = rangex - random() % (2 * rangex);
00295                 ent->heading = (random()%180) * 0.0174532925;
00296                 ent->yaw = ent->heading;
00297                 ent->desiredHeading = ent->heading;
00298                 ent->desiredSpeed = 0.0f;
00299         }
00300 
00301 }
00302 
00303 
00304 
00305 void FastEcslent::GameMgr::tick(double dtime){
00306         return;
00307 }
00308 
00309 
00310 
00311 

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