- added a small cache to improve pathfinding a bit

- added logic to IGNORE the following characters when doing CRC checks in XML files
 (space,tab,CR,LF)
This commit is contained in:
Mark Vejvoda 2010-11-07 01:06:14 +00:00
parent b52ca80388
commit 9e55d8e46b
4 changed files with 81 additions and 13 deletions

View File

@ -370,6 +370,8 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
//if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<int, std::map<Field,bool> > > > > moveLookupCache;
// This cache stores the units free cell movement calcs during the looping below
//std::map<Vec2i,std::map<Vec2i, bool> > localCacheForUnitCellMovement;
int whileLoopCount = 0;
@ -412,7 +414,9 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
// canUnitMoveToCell = iterFind->second.find(sucPos)->second;
//}
//else {
canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos);
canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos,&moveLookupCache);
//if(Config::getInstance().getBool("DisableCaching","false") == false) {
// localCacheForUnitCellMovement[node->pos][sucPos] = canUnitMoveToCell;
//}

View File

@ -488,53 +488,108 @@ bool Map::canMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2) const
}
//checks if a unit can move from between 2 cells using only visible cells (for pathfinding)
bool Map::aproxCanMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2) const{
bool Map::aproxCanMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2, std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<int, std::map<Field,bool> > > > > *lookupCache) const {
int size= unit->getType()->getSize();
int teamIndex= unit->getTeam();
Field field= unit->getCurrField();
if(lookupCache != NULL) {
std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<int, std::map<Field,bool> > > > >::const_iterator iterFind1 = lookupCache->find(pos1);
if(iterFind1 != lookupCache->end()) {
std::map<Vec2i, std::map<int, std::map<int, std::map<Field,bool> > > >::const_iterator iterFind2 = iterFind1->second.find(pos2);
if(iterFind2 != iterFind1->second.end()) {
std::map<int, std::map<int, std::map<Field,bool> > >::const_iterator iterFind3 = iterFind2->second.find(teamIndex);
if(iterFind3 != iterFind2->second.end()) {
std::map<int, std::map<Field,bool> >::const_iterator iterFind4 = iterFind3->second.find(size);
if(iterFind4 != iterFind3->second.end()) {
std::map<Field,bool>::const_iterator iterFind5 = iterFind4->second.find(field);
if(iterFind5 != iterFind4->second.end()) {
// Found this result in the cache
return iterFind5->second;
}
}
}
}
}
}
//single cell units
if(size==1) {
if(isAproxFreeCell(pos2, field, teamIndex) == false){
if(size == 1) {
if(isAproxFreeCell(pos2, field, teamIndex) == false) {
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=false;
}
return false;
}
if(pos1.x != pos2.x && pos1.y != pos2.y){
if(pos1.x != pos2.x && pos1.y != pos2.y) {
if(isAproxFreeCell(Vec2i(pos1.x, pos2.y), field, teamIndex) == false) {
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=false;
}
return false;
}
if(isAproxFreeCell(Vec2i(pos2.x, pos1.y), field, teamIndex) == false) {
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=false;
}
return false;
}
}
if(unit == NULL || unit->isBadHarvestPos(pos2) == true) {
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=false;
}
return false;
}
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=true;
}
return true;
}
//multi cell units
else {
for(int i=pos2.x; i<pos2.x+size; ++i) {
for(int j=pos2.y; j<pos2.y+size; ++j) {
if(isInside(i, j)){
if(getCell(i, j)->getUnit(unit->getCurrField())!=unit) {
for(int i = pos2.x; i < pos2.x + size; ++i) {
for(int j = pos2.y; j < pos2.y + size; ++j) {
if(isInside(i, j)) {
if(getCell(i, j)->getUnit(unit->getCurrField()) != unit) {
if(isAproxFreeCell(Vec2i(i, j), field, teamIndex) == false) {
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=false;
}
return false;
}
}
}
else {
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=false;
}
return false;
}
}
}
if(unit == NULL || unit->isBadHarvestPos(pos2) == true) {
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=false;
}
return false;
}
if(lookupCache != NULL) {
(*lookupCache)[pos1][pos2][teamIndex][size][field]=true;
}
return true;
}
}

View File

@ -216,7 +216,7 @@ public:
bool canOccupy(const Vec2i &pos, Field field, const UnitType *ut, CardinalDir facing);
//unit placement
bool aproxCanMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2) const;
bool aproxCanMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2, std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<int, std::map<Field,bool> > > > > *lookupCache=NULL) const;
bool canMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2) const;
void putUnitCells(Unit *unit, const Vec2i &pos);
void clearUnitCells(Unit *unit, const Vec2i &pos);

View File

@ -22,9 +22,11 @@
#include <sys/stat.h> // for open()
#include "util.h"
#include "platform_common.h"
#include "leak_dumper.h"
using namespace std;
using namespace Shared::PlatformCommon;
namespace Shared{ namespace Util{
@ -115,13 +117,14 @@ void Checksum::addFileToSum(const string &path){
FILE* file= fopen(path.c_str(), "rb");
if(file!=NULL){
if(file!=NULL) {
addString(lastFile(path));
bool isXMLFile = (EndsWith(path, ".xml") == true);
char buf[4096]=""; /* Should be large enough. */
int bufSize = sizeof buf;
while(!feof(file)){
while(!feof(file)) {
//int8 byte= 0;
//size_t readBytes = fread(&byte, 1, 1, file);
@ -129,6 +132,12 @@ void Checksum::addFileToSum(const string &path){
if(fgets(buf, bufSize, file) != NULL) {
//addByte(byte);
for(int i = 0; buf[i] != 0 && i < bufSize; i++) {
// Ignore Spaces in XML files as they are
// ONLY for formatting
if(isXMLFile == true &&
(buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n' || buf[i] == '\r')) {
continue;
}
addByte(buf[i]);
}
}