diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index d4b216da..7c025b07 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -1594,10 +1594,10 @@ void Game::init(bool initForPreviewOnly) { printf("New game has started...\n"); } - if(isFlagType1BitEnabled(gameSettings.getFlagTypes1(),ft1_network_synch_checks_verbose) == true) { + if(isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == true) { printf("*Note: Monitoring Network CRC VERBOSE synchronization...\n"); } - else if(isFlagType1BitEnabled(gameSettings.getFlagTypes1(),ft1_network_synch_checks) == true) { + else if(isFlagType1BitEnabled(ft1_network_synch_checks) == true) { printf("*Note: Monitoring Network CRC NORMAL synchronization...\n"); } @@ -1735,8 +1735,8 @@ void Game::processNetworkSynchChecksIfRequired() { if(settings != NULL) { bool calculateNetworkCRC = false; - if(isFlagType1BitEnabled(settings->getFlagTypes1(),ft1_network_synch_checks) == true || - isFlagType1BitEnabled(settings->getFlagTypes1(),ft1_network_synch_checks_verbose) == true) { + if(isFlagType1BitEnabled(ft1_network_synch_checks) == true || + isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == true) { calculateNetworkCRC = true; } @@ -1751,10 +1751,10 @@ void Game::processNetworkSynchChecksIfRequired() { netIntf->setNetworkPlayerFactionCRC(index,faction->getCRC().getSum()); if(settings != NULL) { - if(isFlagType1BitEnabled(settings->getFlagTypes1(),ft1_network_synch_checks_verbose) == true) { + if(isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == true) { faction->addCRC_DetailsForWorldFrame(world.getFrameCount(),role == nrServer); } - else if(isFlagType1BitEnabled(settings->getFlagTypes1(),ft1_network_synch_checks) == true && + else if(isFlagType1BitEnabled(ft1_network_synch_checks) == true && world.getFrameCount() % 20 == 0) { faction->addCRC_DetailsForWorldFrame(world.getFrameCount(),role == nrServer); } @@ -5057,8 +5057,8 @@ void Game::DumpCRCWorldLogIfRequired(string fileSuffix) { GameSettings *settings = world.getGameSettingsPtr(); if(settings != NULL && - (isFlagType1BitEnabled(settings->getFlagTypes1(),ft1_network_synch_checks_verbose) == true || - isFlagType1BitEnabled(settings->getFlagTypes1(),ft1_network_synch_checks) == true)) { + (isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == true || + isFlagType1BitEnabled(ft1_network_synch_checks) == true)) { string debugCRCWorldLogFile = Config::getInstance().getString("DebugCRCWorldLogFile","debugCRCWorld.log"); debugCRCWorldLogFile += fileSuffix; @@ -5796,7 +5796,7 @@ void Game::checkWinnerStandardPlayer() { bool playerLostGame = true; // Team Shared units enabled? - if(isFlagType1BitEnabled(this->gameSettings.getFlagTypes1(),ft1_allow_shared_team_units) == true) { + if(isFlagType1BitEnabled(ft1_allow_shared_team_units) == true) { // Check if all team members have lost? for (int factionIndex = 0; factionIndex < world.getFactionCount(); ++factionIndex) { @@ -5956,6 +5956,10 @@ void Game::checkWinnerScripted() { } } +bool Game::isFlagType1BitEnabled(FlagTypes1 type) const { + return ((gameSettings.getFlagTypes1() & (uint32)type) == (uint32)type); +} + bool Game::factionLostGame(int factionIndex) { return factionLostGame(world.getFaction(factionIndex)); } diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 947c63e5..1ebfc6c6 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -224,6 +224,7 @@ public: ~Game(); void reInitGUI(); + bool isFlagType1BitEnabled(FlagTypes1 type) const; bool isMarkCellMode() const { return isMarkCellEnabled; } const Texture2D * getMarkCellTexture() const { return markCellTexture; } diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index af063368..3c519459 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -2339,40 +2339,66 @@ void Renderer::renderResourceStatus() { return; } - const Metrics &metrics= Metrics::getInstance(); - const World *world= game->getWorld(); + const Metrics &metrics = Metrics::getInstance(); + const World *world = game->getWorld(); - if(world->getThisFactionIndex() < 0 || world->getThisFactionIndex() >= world->getFactionCount()) { + if(world->getThisFactionIndex() < 0 || + world->getThisFactionIndex() >= world->getFactionCount()) { return; } - const Faction *thisFaction= world->getFaction(world->getThisFactionIndex()); - const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); + const Faction *thisFaction = world->getFaction(world->getThisFactionIndex()); + const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); assertGl(); glPushAttrib(GL_ENABLE_BIT); int resourceCountRendered = 0; - for(int i= 0; i < world->getTechTree()->getResourceTypeCount(); ++i) { - const ResourceType *rt = world->getTechTree()->getResourceType(i); - const Resource *r = thisFaction->getResource(rt); + for(int techTreeResourceTypeIndex = 0; + techTreeResourceTypeIndex < world->getTechTree()->getResourceTypeCount(); + ++techTreeResourceTypeIndex) { - if ( rt->getDisplayInHud() == false ){ + const ResourceType *rt = world->getTechTree()->getResourceType(techTreeResourceTypeIndex); + + if ( rt->getDisplayInHud() == false ) { continue; } - //if any unit produces the resource - bool showResource= false; - for(int k=0; k < thisFaction->getType()->getUnitTypeCount(); ++k) { - const UnitType *ut = thisFaction->getType()->getUnitType(k); - if(ut->getCost(rt) != NULL) { - showResource = true; - break; + + const Faction *factionForResourceView = thisFaction; + bool localFactionResourcesOnly = false; + + if(game != NULL && game->getGui() != NULL) { + + if(game->isFlagType1BitEnabled(ft1_allow_shared_team_units) == true || + game->isFlagType1BitEnabled(ft1_allow_shared_team_resources) == true) { + + const Gui *gui = game->getGui(); + if(gui != NULL) { + + const Selection *selection = gui->getSelection(); + if(selection != NULL && selection->getCount() > 0 && selection->getFrontUnit() != NULL) { + + const Unit *selectedUnit = selection->getFrontUnit(); + if(selectedUnit != NULL && selectedUnit->getType()->hasSkillClass(scBeBuilt) == true) { + + if(selectedUnit->getFactionIndex() == thisFaction->getIndex() || + selectedUnit->getFaction()->isAlly(thisFaction) == true) { + + factionForResourceView = selectedUnit->getFaction(); + localFactionResourcesOnly = true; + } + } + } + } } } + //if any unit produces the resource + bool showResource = world->showResourceTypeForFaction(rt, factionForResourceView, localFactionResourcesOnly); //draw resource status - if(showResource) { - string str= intToStr(r->getAmount()); + if(showResource == true) { + const Resource *r = factionForResourceView->getResource(rt,localFactionResourcesOnly); + string str = intToStr(r->getAmount()); glEnable(GL_TEXTURE_2D); @@ -2381,18 +2407,22 @@ void Renderer::renderResourceStatus() { bool isNegativeConsumableDisplayCycle = false; if(rt->getClass() == rcConsumable) { // Show in yellow/orange/red font if negative - if(r->getBalance()*5+r->getAmount()<0){ + if(r->getBalance() * 5 + r->getAmount() < 0) { if(time(NULL) % 2 == 0) { + isNegativeConsumableDisplayCycle = true; - if(r->getBalance()*1+r->getAmount()<0){ + if(r->getBalance() * 1 + r->getAmount() < 0) { + glColor3f(RED.x,RED.y,RED.z); resourceFontColor = RED; } - else if(r->getBalance()*3+r->getAmount()<0){ + else if(r->getBalance() * 3 + r->getAmount() < 0) { + glColor3f(ORANGE.x,ORANGE.y,ORANGE.z); resourceFontColor = ORANGE; } - else if(r->getBalance()*5+r->getAmount()<0){ + else if(r->getBalance() * 5 + r->getAmount() < 0) { + glColor3f(YELLOW.x,YELLOW.y,YELLOW.z); resourceFontColor = YELLOW; } @@ -2406,11 +2436,11 @@ void Renderer::renderResourceStatus() { const int MAX_RESOURCES_PER_ROW = 6; int resourceRow = (resourceCountRendered > 0 ? resourceCountRendered / MAX_RESOURCES_PER_ROW : 0); int resourceCol = resourceCountRendered % MAX_RESOURCES_PER_ROW; - //renderQuad(resourceCountRendered*100+200, metrics.getVirtualH()-30 - (30 * resourceRows), 16, 16, rt->getImage()); + renderQuad(resourceCol * 100 + 200, metrics.getVirtualH()-30 - (30 * resourceRow), 16, 16, rt->getImage()); if(rt->getClass() != rcStatic) { - str+= "/" + intToStr(thisFaction->getStoreAmount(rt)); + str+= "/" + intToStr(factionForResourceView->getStoreAmount(rt,localFactionResourcesOnly)); } if(rt->getClass() == rcConsumable) { str+= "("; diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 2d2783aa..ef54cbd2 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -135,7 +135,7 @@ void Gui::init(Game *game) { selection.init(this, world->getThisFactionIndex(), world->getThisTeamIndex(), - isFlagType1BitEnabled(game->getGameSettings()->getFlagTypes1(),ft1_allow_shared_team_units)); + game->isFlagType1BitEnabled(ft1_allow_shared_team_units)); } void Gui::end(){ diff --git a/source/glest_game/gui/selection.cpp b/source/glest_game/gui/selection.cpp index f05ee0d7..8e20633b 100644 --- a/source/glest_game/gui/selection.cpp +++ b/source/glest_game/gui/selection.cpp @@ -329,7 +329,7 @@ void Selection::loadGame(const XmlNode *rootNode, World *world) { factionIndex = selectionNode->getAttribute("factionIndex")->getIntValue(); teamIndex = selectionNode->getAttribute("teamIndex")->getIntValue(); if(selectionNode->hasAttribute("allowSharedTeamUnits") == true) { - allowSharedTeamUnits = selectionNode->getAttribute("allowSharedTeamUnits")->getIntValue(); + allowSharedTeamUnits = (selectionNode->getAttribute("allowSharedTeamUnits")->getIntValue() != 0); } vector selectedUnitsNodeList = selectionNode->getChildList("selectedUnits"); diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 6f8988a8..ed0b6e0c 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -1702,21 +1702,6 @@ void ServerInterface::updateKeyframe(int frameCount) { // We always need to broadcast when not pause as clients // look for broadcasts every network frame. sendBroadcastMessage = true; - - // If network CRC checking enabled we turn on broadcasting always -// bool calculateNetworkCRC = false; -// if(isFlagType1BitEnabled(this->gameSettings.getFlagTypes1(),ft1_network_synch_checks) == true || -// isFlagType1BitEnabled(this->gameSettings.getFlagTypes1(),ft1_network_synch_checks_verbose) == true) { -// -// calculateNetworkCRC = true; -// } -// -// if(calculateNetworkCRC == true || -// (lastBroadcastCommandsTimer.isStarted() == false || -// lastBroadcastCommandsTimer.getMillis() >= MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS)) { -// -// sendBroadcastMessage = true; -// } } // Auto pause is enabled due to client lagging, only send empty command // broadcasts every MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 89b57705..bfd740d4 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -710,11 +710,14 @@ void Faction::init( store.resize(techTree->getResourceTypeCount()); if(loadWorldNode == NULL) { - for(int i=0; igetResourceTypeCount(); ++i){ - const ResourceType *rt= techTree->getResourceType(i); - int resourceAmount= giveResources? factionType->getStartingResourceAmount(rt): 0; - resources[i].init(rt, resourceAmount); - store[i].init(rt, 0); + for(int index = 0; index < techTree->getResourceTypeCount(); ++index) { + const ResourceType *rt = techTree->getResourceType(index); + int resourceAmount = giveResources ? factionType->getStartingResourceAmount(rt): 0; + resources[index].init(rt, resourceAmount); + store[index].init(rt, 0); + + this->world->initTeamResource(rt,this->teamIndex,0); + this->updateUnitTypeWithResourceCostCache(rt); } } @@ -730,8 +733,6 @@ void Faction::init( } if( game->getGameSettings()->getPathFinderType() == pfBasic) { -// if( game->getGameSettings()->getPathFinderType() == pfBasic && -// Config::getInstance().getBool("EnableFactionWorkerThreads","true") == true) { if(workerThread != NULL) { workerThread->signalQuit(); if(workerThread->shutdownAndWait() == true) { @@ -750,12 +751,54 @@ void Faction::init( // ================== get ================== -const Resource *Faction::getResource(const ResourceType *rt) const{ - for(int i = 0; i < (int)resources.size(); ++i){ - if(rt==resources[i].getType()){ - return &resources[i]; +bool Faction::hasUnitTypeWithResourceCostInCache(const ResourceType *rt) const { + std::string resourceTypeName = rt->getName(false); + std::map::const_iterator iterFind = resourceTypeCostCache.find(resourceTypeName); + if(iterFind != resourceTypeCostCache.end()) { + return iterFind->second; + } + return false; +} +void Faction::updateUnitTypeWithResourceCostCache(const ResourceType *rt) { + std::string resourceTypeName = rt->getName(false); + + if(resourceTypeCostCache.find(resourceTypeName) == resourceTypeCostCache.end()) { + resourceTypeCostCache[resourceTypeName] = hasUnitTypeWithResouceCost(rt); + } +} + +bool Faction::hasUnitTypeWithResouceCost(const ResourceType *rt) { + for(int factionUnitTypeIndex = 0; + factionUnitTypeIndex < getType()->getUnitTypeCount(); + ++factionUnitTypeIndex) { + + const UnitType *ut = getType()->getUnitType(factionUnitTypeIndex); + if(ut->getCost(rt) != NULL) { + return true; + break; } } + return false; +} + +const Resource *Faction::getResource(const ResourceType *rt,bool localFactionOnly) const { + + if(localFactionOnly == false && + world != NULL && + world->getGame() != NULL) { + + Game *game = world->getGame(); + if(game->isFlagType1BitEnabled(ft1_allow_shared_team_resources) == true) { + return world->getResourceForTeam(rt, this->getTeam()); + } + } + + for(int index = 0; index < (int)resources.size(); ++index) { + if(rt == resources[index].getType()) { + return &resources[index]; + } + } + printf("ERROR cannot find resource type [%s] in list:\n",(rt != NULL ? rt->getName().c_str() : "null")); for(int i=0; i < (int)resources.size(); ++i){ printf("Index %d [%s]",i,resources[i].getType()->getName().c_str()); @@ -765,10 +808,21 @@ const Resource *Faction::getResource(const ResourceType *rt) const{ return NULL; } -int Faction::getStoreAmount(const ResourceType *rt) const{ - for(int i=0; i < (int)store.size(); ++i){ - if(rt==store[i].getType()){ - return store[i].getAmount(); +int Faction::getStoreAmount(const ResourceType *rt,bool localFactionOnly) const { + + if(localFactionOnly == false && + world != NULL && + world->getGame() != NULL) { + + Game *game = world->getGame(); + if(game->isFlagType1BitEnabled(ft1_allow_shared_team_resources) == true) { + return world->getStoreAmountForTeam(rt, this->getTeam()); + } + } + + for(int index =0 ; index < (int)store.size(); ++index) { + if(rt == store[index].getType()) { + return store[index].getAmount(); } } printf("ERROR cannot find store type [%s] in list:\n",(rt != NULL ? rt->getName().c_str() : "null")); diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index b799bba4..79db2a9a 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -167,6 +167,7 @@ private: std::map switchTeamVotes; int currentSwitchTeamVoteFactionIndex; + bool allowSharedTeamUnits; set livingUnits; set livingUnitsp; @@ -184,6 +185,8 @@ private: std::map mobileUnitListCache; std::map beingBuiltUnitListCache; + std::map resourceTypeCostCache; + public: Faction(); ~Faction(); @@ -251,9 +254,9 @@ public: void setFactionDisconnectHandled(bool value) { factionDisconnectHandled=value;} //get - const Resource *getResource(const ResourceType *rt) const; + const Resource *getResource(const ResourceType *rt,bool localFactionOnly=false) const; inline const Resource *getResource(int i) const {return &resources[i];} - int getStoreAmount(const ResourceType *rt) const; + int getStoreAmount(const ResourceType *rt,bool localFactionOnly=false) const; inline const FactionType *getType() const {return factionType;} inline int getIndex() const {return index;} @@ -375,9 +378,13 @@ public: string getCRC_DetailsForWorldFrames() const; uint64 getCRC_DetailsForWorldFrameCount() const; + void updateUnitTypeWithResourceCostCache(const ResourceType *rt); + bool hasUnitTypeWithResourceCostInCache(const ResourceType *rt) const; + private: void init(); void resetResourceAmount(const ResourceType *rt); + bool hasUnitTypeWithResouceCost(const ResourceType *rt); }; }}//end namespace diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 3a793be6..e99f715a 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -753,11 +753,11 @@ void Unit::dumpMemoryList() { bool Unit::isNetworkCRCEnabled() { bool isNetworkCRCEnabled = false; - if(game != NULL && game->getGameSettings() != NULL) { - if(isFlagType1BitEnabled(game->getGameSettings()->getFlagTypes1(),ft1_network_synch_checks_verbose) == true) { + if(game != NULL) { + if(game->isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == true) { isNetworkCRCEnabled = true; } - else if(isFlagType1BitEnabled(game->getGameSettings()->getFlagTypes1(),ft1_network_synch_checks) == true) { + else if(game->isFlagType1BitEnabled(ft1_network_synch_checks) == true) { isNetworkCRCEnabled = true; } } diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 0719cf28..f1799769 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -1033,6 +1033,7 @@ void World::tick() { //if consumable if(rt != NULL && rt->getClass() == rcConsumable) { + int balance= 0; for(int unitIndex = 0; unitIndex < faction->getUnitCount(); ++unitIndex) { @@ -1040,11 +1041,13 @@ void World::tick() { //if unit operative and has this cost const Unit *unit = faction->getUnit(unitIndex); if(unit != NULL && unit->isOperative()) { + const UnitType *ut = unit->getType(); const Resource *resource = NULL; std::map >::iterator iterFind = resourceCostCache.find(ut); if(iterFind != resourceCostCache.end() && iterFind->second.find(rt) != iterFind->second.end()) { + resource = iterFind->second.find(rt)->second; } else { @@ -2656,6 +2659,87 @@ void World::stopAllVideo() { this->game->stopAllVideo(); } +void World::initTeamResource(const ResourceType *rt,int teamIndex, int value) { + std::string resourceTypeName = rt->getName(false); + TeamResources[teamIndex][resourceTypeName].init(rt,value); +} + +const Resource *World::getResourceForTeam(const ResourceType *rt, int teamIndex) { + if(rt == NULL) { + return NULL; + } + + std::map &resourceList = TeamResources[teamIndex]; + std::string resourceTypeName = rt->getName(false); + resourceList[resourceTypeName].init(rt,0); + Resource &teamResource = resourceList[resourceTypeName]; + + for(int index = 0; index < (int)factions.size(); ++index) { + + Faction *faction = factions[index]; + if(faction != NULL && faction->getTeam() == teamIndex) { + + const Resource *factionResource = faction->getResource(rt,true); + if(factionResource != NULL && factionResource->getType() != NULL) { + + int teamResourceAmount = teamResource.getAmount(); + int teamResourceBalance = teamResource.getBalance(); + + teamResource.setAmount(teamResourceAmount + factionResource->getAmount()); + teamResource.setBalance(teamResourceBalance + factionResource->getBalance()); + } + } + } + + return &teamResource; +} + +int World::getStoreAmountForTeam(const ResourceType *rt, int teamIndex) const { + + int teamStoreAmount = 0; + for(int index = 0; index < (int)factions.size(); ++index) { + + Faction *faction = factions[index]; + if(faction != NULL && faction->getTeam() == teamIndex) { + + teamStoreAmount += faction->getStoreAmount(rt,true); + } + } + + return teamStoreAmount; +} + +bool World::showResourceTypeForFaction(const ResourceType *rt, const Faction *faction,bool localFactionOnly) const { + //if any unit produces the resource + bool showResource = false; + for(int factionUnitTypeIndex = 0; + factionUnitTypeIndex < faction->getType()->getUnitTypeCount(); + ++factionUnitTypeIndex) { + + const UnitType *ut = faction->getType()->getUnitType(factionUnitTypeIndex); + if(ut->getCost(rt) != NULL) { + showResource = true; + break; + } + } + if(localFactionOnly == false && showResource == false && + (game->isFlagType1BitEnabled(ft1_allow_shared_team_units) == true || + game->isFlagType1BitEnabled(ft1_allow_shared_team_resources) == true)) { + + for(int index = 0; showResource == false && index < (int)factions.size(); ++index) { + const Faction *teamFaction = factions[index]; + if(teamFaction != NULL && teamFaction->getTeam() == faction->getTeam()) { + + if(teamFaction->hasUnitTypeWithResourceCostInCache(rt) == true) { + showResource = true; + } + } + } + } + + return showResource; +} + void World::removeResourceTargetFromCache(const Vec2i &pos) { for(int i= 0; i < (int)factions.size(); ++i) { factions[i]->removeResourceTargetFromCache(pos); diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index bbcff6ba..101188d0 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -142,6 +142,8 @@ private: bool cacheFowAlphaTexture; bool cacheFowAlphaTextureFogOfWarValue; + std::map > TeamResources; + public: World(); ~World(); @@ -317,6 +319,12 @@ public: void refreshAllUnitExplorations(); bool factionLostGame(int factionIndex); + + void initTeamResource(const ResourceType *rt,int teamIndex, int value); + const Resource * getResourceForTeam(const ResourceType *rt, int teamIndex); + int getStoreAmountForTeam(const ResourceType *rt, int teamIndex) const; + bool showResourceTypeForFaction(const ResourceType *rt, const Faction *faction,bool localFactionOnly) const; + private: void initCells(bool fogOfWar);