00001
00002
00003
00004
00005
00006
00007 #include <ent.h>
00008 #include <buildings.h>
00009 #include <unitBuilder.h>
00010 #include <engine.h>
00011
00012 namespace FastEcslent {
00013 const int Builder::MAX_QUEUE = 5;
00014
00015
00016 Builder::Builder(Entity *ent, UnitAspectType aspectType,Ogre::Vector3 minBuildRadius,Ogre::Vector3 maxBuildRadius) : UnitAspect(ent, aspectType), entityBeingBuilt(NULL),minBuildRadius(minBuildRadius),maxBuildRadius(maxBuildRadius), waitNetworkResponse(false) {
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 }
00027
00028 Builder::~Builder() {
00029
00030 }
00031
00032 void Builder::init() {
00033
00034
00035 }
00036
00037 Ogre::Vector3 Builder::selectRandPosInRadius(int attempts)
00038 {
00039 Ogre::Vector3 newPos = Ogre::Vector3(0,0,0);
00040 for(int i = 0; i < attempts; i++)
00041 {
00042 newPos = this->entity->pos;
00043 newPos.x += (Ogre::Math::RangeRandom(minBuildRadius.x, maxBuildRadius.x));
00044 newPos.z += (Ogre::Math::RangeRandom(minBuildRadius.z, maxBuildRadius.z));
00045
00046 if(!(entity->engine->distanceMgr->wouldCollide(newPos, 200.0)))
00047 {
00048
00049 break;
00050 }
00051 }
00052 return newPos;
00053
00054 }
00055
00056 void Builder::startBuildingNextEntity()
00057 {
00058 if (this->entity->engine->gameMgr->notExceedPopCap(this->entity->entityId)) {
00059 Ogre::Vector3 newPos = selectRandPosInRadius(1000);
00060 if(this->entity->engine->options.isServer)
00061 {
00062 this->entityBeingBuilt = this->entity->engine->entityMgr->createEntityForPlayerAndSide(this->entityQueue.front(), newPos, 0.0f, this->entity->entityId.side,this->entity->entityId.player);
00063 }
00064 else
00065 {
00066 this->entity->engine->net->createEntFromClientReq(this->entity->entityId.id, this->entityQueue.front(), newPos);
00067 this->waitNetworkResponse = true;
00068 }
00069 }
00070 }
00071
00072 void Builder::tick(double dt) {
00073
00074
00075
00076 if (!this->entityQueue.empty() && !this->waitNetworkResponse ) {
00077
00078
00079 if(this->entityBeingBuilt == NULL)
00080 {
00081 this->startBuildingNextEntity();
00082 }
00083
00084 else if (this->entityBeingBuilt->timeLeftToBirth <=0) {
00085
00086 this->entityQueue.pop_front();
00087 this->entityBeingBuilt = NULL;
00088
00089
00090
00091
00092
00093
00094 }
00095 }
00096
00097
00098
00099
00100 }
00101
00102 bool Builder::enqueue(EntityType entType) {
00103 if (this->entity->engine->gameMgr->entTypeData[this->entity->entityType].buildableEntities[entType] && this->entityQueue.size() < MAX_QUEUE) {
00104 if (this->entity->engine->gameMgr->preReqExists(this->entity->entityId.player, entType)) {
00105 this->entity->engine->gameMgr->consumeResources(this->entity->entityId.player, entType);
00106 this->entityQueue.push_back(entType);
00107 }
00108 return true;
00109 } else {
00110 return false;
00111 }
00112 }
00113
00114
00115 bool Builder::cancelEntityBeingBuilt() {
00116 return this->entity->engine->entityMgr->cancelGestatingEntity(this->entityBeingBuilt);
00117 }
00118
00119 bool Builder::removeAtIndex(unsigned int pos) {
00120 if (pos < this->entityQueue.size()) {
00121 this->entityQueue.erase(this->entityQueue.begin() + pos);
00122 return true;
00123 } else {
00124 return false;
00125 }
00126 }
00127
00128 bool Builder::removeTail(void) {
00129 if (this->entityQueue.empty()) {
00130 return false;
00131 } else {
00132 this->entityQueue.pop_back();
00133 return true;
00134 }
00135 }
00136
00137
00138
00139
00140 }