From 41dfe298d3bf5720ebde0604d06595a990137668 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 9 Sep 2011 17:49:10 +0000 Subject: [PATCH] - re-enable grouped commands for better unit moving (was previously causing out of synch but i think it works properly now) --- source/glest_game/type_instances/faction.cpp | 73 +++++++++++++++----- source/glest_game/type_instances/faction.h | 2 + source/glest_game/world/world.cpp | 16 ++++- 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index cdd78ff5..e7156f43 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -38,48 +38,89 @@ CommandGroupSorter::CommandGroupSorter(Unit *unit) { this->unit = unit; } -bool CommandGroupSorter::operator< (const CommandGroupSorter &j) const { +bool CommandGroupSorter::operator< (const CommandGroupSorter *j) const { + return operator<(*j); +} - if(j.unit == NULL || j.unit->isAlive() == false) { - return true; - } - else if((this->unit == NULL || this->unit->isAlive() == false)) { - return false; - } + +bool CommandGroupSorter::compare(const CommandGroupSorter *l, const CommandGroupSorter *r) { + return (*l < *r); +} + +bool CommandGroupSorter::operator< (const CommandGroupSorter &j) const { + bool result = false; + // If comparer if null or dead +// if(j.unit == NULL || j.unit->isAlive() == false) { +// // if source is null or dead also +// if((this->unit == NULL || this->unit->isAlive() == false)) { +// return false; +// } +// return true; +// } +// else if((this->unit == NULL || this->unit->isAlive() == false)) { +// return false; +// } Command *command= this->unit->getCurrrentCommandThreadSafe(); + Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); //Command *command= this->unit->getCurrCommand(); + + // Are we moving or attacking if( command != NULL && (command->getCommandType()->getClass() == ccMove || command->getCommandType()->getClass() == ccAttack) && command->getUnitCommandGroupId() > 0) { int curCommandGroupId = command->getUnitCommandGroupId(); - Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); + //Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); //Command *commandPeer = j.unit->getCurrCommand(); + + // is comparer a valid command if(commandPeer == NULL) { - return true; + result = true; } + // is comparer command the same type? else if(commandPeer->getCommandType()->getClass() != command->getCommandType()->getClass()) { - return true; + result = true; } + // is comparer command groupid invalid? else if(commandPeer->getUnitCommandGroupId() < 0) { - return true; + result = true; } - else if(curCommandGroupId > commandPeer->getUnitCommandGroupId()) { - return false; + // If comparer command group id is less than current group id + else if(curCommandGroupId != commandPeer->getUnitCommandGroupId()) { + result = curCommandGroupId < commandPeer->getUnitCommandGroupId(); } else { float unitDist = this->unit->getCenteredPos().dist(command->getPos()); float unitDistPeer = j.unit->getCenteredPos().dist(commandPeer->getPos()); - - return unitDist < unitDistPeer; + // Closest unit in commandgroup + result = (unitDist < unitDistPeer); + } + } + else if(command == NULL && j.unit->getCurrrentCommandThreadSafe() != NULL) { + result = false; + } +// else if(command == NULL && j.unit->getCurrrentCommandThreadSafe() == NULL) { +// return this->unit->getId() < j.unit->getId(); +// } + else { + //Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); + if( commandPeer != NULL && + (commandPeer->getCommandType()->getClass() != ccMove && + commandPeer->getCommandType()->getClass() != ccAttack)) { + result = this->unit->getId() < j.unit->getId(); + } + else { + result = (this->unit->getId() < j.unit->getId()); } } - return false; + //printf("Sorting, unit [%d - %s] cmd [%s] | unit2 [%d - %s] cmd [%s] result = %d\n",this->unit->getId(),this->unit->getFullName().c_str(),(this->unit->getCurrCommand() == NULL ? "NULL" : this->unit->getCurrCommand()->toString().c_str()),j.unit->getId(),j.unit->getFullName().c_str(),(j.unit->getCurrCommand() == NULL ? "NULL" : j.unit->getCurrCommand()->toString().c_str()),result); + + return result; } // ===================================================== diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index f5652480..83d5025c 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -56,6 +56,8 @@ public: CommandGroupSorter(); CommandGroupSorter(Unit *unit); bool operator< (const CommandGroupSorter &j) const; + bool operator< (const CommandGroupSorter *j) const; + bool static compare(const CommandGroupSorter *l, const CommandGroupSorter *r); }; class FactionThread : public BaseThread { diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index d1ad5941..c7c8cde4 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -281,7 +281,7 @@ void World::updateAllFactionUnits() { // Prioritize grouped command units so closest units to target go first // units Config &config= Config::getInstance(); - bool sortedUnitsAllowed = config.getBool("AllowGroupedUnitCommands","false"); + bool sortedUnitsAllowed = config.getBool("AllowGroupedUnitCommands","true"); std::map > unitsInFactionsSorted; int factionCount = getFactionCount(); @@ -305,7 +305,19 @@ void World::updateAllFactionUnits() { unitListToSort.push_back(new CommandGroupSorter(unit)); } if(unitListToSort.empty() == false) { - std::sort(unitListToSort.begin(),unitListToSort.end()); + //printf("About to Sort...\n"); + std::sort(unitListToSort.begin(),unitListToSort.end(),CommandGroupSorter::compare); + } + + if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { + char szBuf[4096]=""; + + for(unsigned int x = 0; x < unitListToSort.size(); ++x) { + CommandGroupSorter *sorter = unitListToSort[x]; + + sprintf(szBuf,"List %d / %ld unit [%d - %s] cmd [%s]",x,unitListToSort.size(),sorter->unit->getId(),sorter->unit->getFullName().c_str(),(sorter->unit->getCurrCommand() == NULL ? "NULL" : sorter->unit->getCurrCommand()->toString().c_str())); + sorter->unit->logSynchData(__FILE__,__LINE__,szBuf); + } } } }