- small bit of code cleanup in unit_updater

- added alarmcount to debug view
This commit is contained in:
Mark Vejvoda 2011-03-23 02:45:42 +00:00
parent 41fc6f661e
commit 6f523bb96a
3 changed files with 91 additions and 74 deletions

View File

@ -1790,9 +1790,11 @@ void Game::render2d(){
str+= "Cached surfacedata: " + intToStr(renderer.getCachedSurfaceDataSize())+"\n"; str+= "Cached surfacedata: " + intToStr(renderer.getCachedSurfaceDataSize())+"\n";
str+= "Time: " + floatToStr(world.getTimeFlow()->getTime(),2)+"\n"; str+= "Time: " + floatToStr(world.getTimeFlow()->getTime(),2)+"\n";
if(SystemFlags::getThreadedLoggerRunning() == true) { if(SystemFlags::getThreadedLoggerRunning() == true) {
str+= "Log buffer count: " + intToStr(SystemFlags::getLogEntryBufferCount())+"\n"; str+= "Log buffer count: " + intToStr(SystemFlags::getLogEntryBufferCount())+"\n";
} }
str+= "AttackWarningCount: " + intToStr(world.getUnitUpdater()->getAttackWarningCount()) + "\n";
str+= "Map: " + gameSettings.getMap() +"\n"; str+= "Map: " + gameSettings.getMap() +"\n";
str+= "Tileset: " + gameSettings.getTileset() +"\n"; str+= "Tileset: " + gameSettings.getTileset() +"\n";
str+= "Techtree: " + gameSettings.getTech() +"\n"; str+= "Techtree: " + gameSettings.getTech() +"\n";

View File

@ -290,16 +290,16 @@ void UnitUpdater::updateStop(Unit *unit, int frameIndex) {
return; return;
} }
Chrono chrono; //Chrono chrono;
chrono.start(); //chrono.start();
Command *command= unit->getCurrCommand(); Command *command= unit->getCurrCommand();
const StopCommandType *sct = static_cast<const StopCommandType*>(command->getCommandType()); const StopCommandType *sct = static_cast<const StopCommandType*>(command->getCommandType());
Unit *sighted; Unit *sighted=NULL;
unit->setCurrSkill(sct->getStopSkillType()); unit->setCurrSkill(sct->getStopSkillType());
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//we can attack any unit => attack it //we can attack any unit => attack it
if(unit->getType()->hasSkillClass(scAttack)) { if(unit->getType()->hasSkillClass(scAttack)) {
@ -310,44 +310,40 @@ void UnitUpdater::updateStop(Unit *unit, int frameIndex) {
//look for an attack skill //look for an attack skill
const AttackSkillType *ast= NULL; const AttackSkillType *ast= NULL;
if(ct->getClass()==ccAttack) { if(ct->getClass() == ccAttack) {
ast= static_cast<const AttackCommandType*>(ct)->getAttackSkillType(); ast= static_cast<const AttackCommandType*>(ct)->getAttackSkillType();
} }
else if(ct->getClass()==ccAttackStopped) { else if(ct->getClass() == ccAttackStopped) {
ast= static_cast<const AttackStoppedCommandType*>(ct)->getAttackSkillType(); ast= static_cast<const AttackStoppedCommandType*>(ct)->getAttackSkillType();
} }
//use it to attack //use it to attack
if(ast != NULL) { if(ast != NULL) {
if(attackableOnSight(unit, &sighted, ast)) { if(attackableOnSight(unit, &sighted, ast)) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
unit->giveCommand(new Command(ct, sighted->getPos())); unit->giveCommand(new Command(ct, sighted->getPos()));
break; break;
} }
} }
} }
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
} }
//see any unit and cant attack it => run //see any unit and cant attack it => run
else if(unit->getType()->hasCommandClass(ccMove)) { else if(unit->getType()->hasCommandClass(ccMove)) {
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(attackerOnSight(unit, &sighted)) { if(attackerOnSight(unit, &sighted)) {
//if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); Vec2i escapePos = unit->getPos() * 2 - sighted->getPos();
//SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
Vec2i escapePos= unit->getPos()*2-sighted->getPos();
SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
unit->giveCommand(new Command(unit->getType()->getFirstCtOfClass(ccMove), escapePos)); unit->giveCommand(new Command(unit->getType()->getFirstCtOfClass(ccMove), escapePos));
} }
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
} }
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
} }
@ -1899,7 +1895,6 @@ void UnitUpdater::findEnemiesForCell(const AttackSkillType *ast, Cell *cell, con
//check enemy //check enemy
if(possibleEnemy != NULL && possibleEnemy->isAlive()) { if(possibleEnemy != NULL && possibleEnemy->isAlive()) {
if((unit->isAlly(possibleEnemy) == false && commandTarget == NULL) || if((unit->isAlly(possibleEnemy) == false && commandTarget == NULL) ||
commandTarget == possibleEnemy) { commandTarget == possibleEnemy) {
@ -1912,9 +1907,8 @@ void UnitUpdater::findEnemiesForCell(const AttackSkillType *ast, Cell *cell, con
//if the unit has any enemy on range //if the unit has any enemy on range
bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr, bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr,
const AttackSkillType *ast){ const AttackSkillType *ast) {
vector<Unit*> enemies; vector<Unit*> enemies;
Unit* enemySeen = NULL;
bool result=false; bool result=false;
//we check command target //we check command target
const Unit *commandTarget = NULL; const Unit *commandTarget = NULL;
@ -1934,12 +1928,10 @@ bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr,
if(findCachedCellsEnemies(center,range,size,enemies,ast, if(findCachedCellsEnemies(center,range,size,enemies,ast,
unit,commandTarget) == false) { unit,commandTarget) == false) {
foundInCache = false; foundInCache = false;
//nearby cells //nearby cells
UnitRangeCellsLookupItem cacheItem; UnitRangeCellsLookupItem cacheItem;
for(int i=center.x-range; i<center.x+range+size; ++i){ for(int i = center.x - range; i < center.x + range + size; ++i) {
for(int j=center.y-range; j<center.y+range+size; ++j){ for(int j = center.y - range; j < center.y + range + size; ++j) {
//cells inside map and in range //cells inside map and in range
#ifdef USE_STREFLOP #ifdef USE_STREFLOP
if(map->isInside(i, j) && streflop::floor(floatCenter.dist(Vec2f((float)i, (float)j))) <= (range+1)){ if(map->isInside(i, j) && streflop::floor(floatCenter.dist(Vec2f((float)i, (float)j))) <= (range+1)){
@ -1956,24 +1948,47 @@ bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr,
// Ok update our caches with the latest info // Ok update our caches with the latest info
if(cacheItem.rangeCellList.size() > 0) { if(cacheItem.rangeCellList.size() > 0) {
cacheItem.UnitRangeCellsLookupItemCacheTimerCountIndex = UnitRangeCellsLookupItemCacheTimerCount++; //cacheItem.UnitRangeCellsLookupItemCacheTimerCountIndex = UnitRangeCellsLookupItemCacheTimerCount++;
UnitRangeCellsLookupItemCache[center][size][range] = cacheItem; UnitRangeCellsLookupItemCache[center][size][range] = cacheItem;
} }
} }
//attack enemies that can attack first //attack enemies that can attack first
Unit* enemySeen = NULL;
for(int i = 0; i< enemies.size(); ++i) { for(int i = 0; i< enemies.size(); ++i) {
if(enemies[i]->getType()->hasSkillClass(scAttack) && enemies[i]->isAlive() == true ) { if(enemies[i]->isAlive() == true) {
// Here we default to first enemy if no attackers found
if(enemySeen == NULL) {
*rangedPtr = enemies[i];
enemySeen = enemies[i];
result = true;
}
// Attackers get first priority
if(enemies[i]->getType()->hasSkillClass(scAttack) == true) {
*rangedPtr = enemies[i];
enemySeen = enemies[i];
result = true;
break;
}
}
}
/*
if(enemies[i]->getType()->hasSkillClass(scAttack) &&
enemies[i]->isAlive() == true ) {
*rangedPtr= enemies[i]; *rangedPtr= enemies[i];
enemySeen=enemies[i]; enemySeen=enemies[i];
result=true; result=true;
break; break;
} }
} }
if(!result){
/*
if(result == false) {
//any enemy //any enemy
for(int i= 0; i < enemies.size(); ++i){ for(int i= 0; i < enemies.size(); ++i) {
if(enemies[i]->isAlive() == true){ if(enemies[i]->isAlive() == true) {
*rangedPtr= enemies[i]; *rangedPtr= enemies[i];
enemySeen= enemies[i]; enemySeen= enemies[i];
result= true; result= true;
@ -1981,36 +1996,35 @@ bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr,
} }
} }
} }
*/
if(result == true) {
const Unit* teamUnit = NULL;
const Unit* enemyUnit = NULL;
bool onlyEnemyUnits = true;
if(result) if(unit->getTeam() == world->getThisTeamIndex()) {
{ teamUnit = unit;
const Unit* teamUnit; enemyUnit = enemySeen;
const Unit* enemyUnit; onlyEnemyUnits = false;
bool onlyEnemyUnits=true;
if(unit->getTeam()==world->getThisTeamIndex())
{
teamUnit=unit;
enemyUnit=enemySeen;
onlyEnemyUnits=false;
} }
else if(enemySeen->getTeam()==world->getThisTeamIndex() ) else if(enemySeen->getTeam() == world->getThisTeamIndex()) {
{ teamUnit = enemySeen;
teamUnit=enemySeen; enemyUnit = unit;
enemyUnit=unit; onlyEnemyUnits = false;
onlyEnemyUnits=false;
} }
if(!onlyEnemyUnits && enemyUnit->getTeam()!=world->getThisTeamIndex()) if(onlyEnemyUnits == false &&
{ enemyUnit->getTeam() != world->getThisTeamIndex()) {
Vec2f enemyFloatCenter = enemyUnit->getFloatCenteredPos(); Vec2f enemyFloatCenter = enemyUnit->getFloatCenteredPos();
// find nearest Attack and cleanup old dates // find nearest Attack and cleanup old dates
AttackWarningData *nearest=NULL; AttackWarningData *nearest = NULL;
float currentDistance; float currentDistance = 0.f;
float nearestDistance; float nearestDistance = 0.f;
for(int i = attackWarnings.size()-1; i>-1; --i) {
if(world->getFrameCount()-attackWarnings[i]->lastFrameCount>200) { //after 200 frames attack break we warn again for(int i = attackWarnings.size() - 1; i >= 0; --i) {
AttackWarningData *toDelete=attackWarnings[i]; if(world->getFrameCount() - attackWarnings[i]->lastFrameCount > 200) { //after 200 frames attack break we warn again
AttackWarningData *toDelete =attackWarnings[i];
attackWarnings.erase(attackWarnings.begin()+i); attackWarnings.erase(attackWarnings.begin()+i);
delete toDelete; // old one delete toDelete; // old one
} }
@ -2021,36 +2035,35 @@ bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr,
currentDistance = floor(enemyFloatCenter.dist(attackWarnings[i]->attackPosition)); // no need for streflops here! currentDistance = floor(enemyFloatCenter.dist(attackWarnings[i]->attackPosition)); // no need for streflops here!
#endif #endif
if( nearest==NULL ){ if(nearest == NULL) {
nearest=attackWarnings[i]; nearest = attackWarnings[i];
nearestDistance=currentDistance; nearestDistance = currentDistance;
} }
else { else {
if(currentDistance < nearestDistance) {
if( currentDistance< nearestDistance ){ nearest = attackWarnings[i];
nearest=attackWarnings[i]; nearestDistance = currentDistance;
nearestDistance=currentDistance;
}
} }
}
} }
} }
if(nearest!=NULL) if(nearest != NULL) {
{ // does it fit? // does it fit?
if(nearestDistance<attackWarnRange) if(nearestDistance < attackWarnRange) {
{// update entry with current values // update entry with current values
nearest->lastFrameCount=world->getFrameCount(); nearest->lastFrameCount=world->getFrameCount();
nearest->attackPosition.x=enemyFloatCenter.x; nearest->attackPosition.x=enemyFloatCenter.x;
nearest->attackPosition.y=enemyFloatCenter.y; nearest->attackPosition.y=enemyFloatCenter.y;
} }
else else {
{//Must be a different Attack! //Must be a different Attack!
nearest=NULL; //set to null to force a new entry in next step nearest=NULL; //set to null to force a new entry in next step
} }
} }
// add new attack // add new attack
if(nearest==NULL) // no else! if(nearest == NULL) {
{ // no else!
AttackWarningData* awd= new AttackWarningData(); AttackWarningData* awd= new AttackWarningData();
awd->lastFrameCount=world->getFrameCount(); awd->lastFrameCount=world->getFrameCount();
awd->attackPosition.x=enemyFloatCenter.x; awd->attackPosition.x=enemyFloatCenter.x;

View File

@ -43,7 +43,7 @@ class Cell;
class UnitRangeCellsLookupItem { class UnitRangeCellsLookupItem {
public: public:
int UnitRangeCellsLookupItemCacheTimerCountIndex; //int UnitRangeCellsLookupItemCacheTimerCountIndex;
std::vector<Cell *> rangeCellList; std::vector<Cell *> rangeCellList;
static time_t lastDebug; static time_t lastDebug;
@ -82,7 +82,7 @@ private:
std::map<Vec2i, std::map<int, std::map<int, UnitRangeCellsLookupItem > > > UnitRangeCellsLookupItemCache; std::map<Vec2i, std::map<int, std::map<int, UnitRangeCellsLookupItem > > > UnitRangeCellsLookupItemCache;
//std::map<int,ExploredCellsLookupKey> ExploredCellsLookupItemCacheTimer; //std::map<int,ExploredCellsLookupKey> ExploredCellsLookupItemCacheTimer;
int UnitRangeCellsLookupItemCacheTimerCount; //int UnitRangeCellsLookupItemCacheTimerCount;
bool findCachedCellsEnemies(Vec2i center, int range, bool findCachedCellsEnemies(Vec2i center, int range,
int size, vector<Unit*> &enemies, int size, vector<Unit*> &enemies,
@ -114,6 +114,8 @@ public:
void clearUnitPrecache(Unit *unit); void clearUnitPrecache(Unit *unit);
unsigned int getAttackWarningCount() const { return attackWarnings.size(); }
private: private:
//attack //attack
void hit(Unit *attacker); void hit(Unit *attacker);