- attempt to add more randomness to pathfinding and faster AI decsions

This commit is contained in:
Mark Vejvoda 2011-02-04 06:34:32 +00:00
parent ab9d2f1bf2
commit 6ec92f2127
8 changed files with 218 additions and 184 deletions

View File

@ -244,6 +244,10 @@ const ResourceType *Ai::getNeededResource(int unitIndex) {
}
bool Ai::beingAttacked(Vec2i &pos, Field &field, int radius){
const Unit *enemy = aiInterface->getFirstOnSightEnemyUnit(pos, field, radius);
return (enemy != NULL);
/*
int count= aiInterface->onSightUnitCount();
const Unit *unit;
@ -259,6 +263,7 @@ bool Ai::beingAttacked(Vec2i &pos, Field &field, int radius){
}
}
return false;
*/
}
bool Ai::isStableBase(){

View File

@ -587,4 +587,31 @@ bool AiInterface::isFreeCells(const Vec2i &pos, int size, Field field){
return world->getMap()->isFreeCells(pos, size, field);
}
const Unit *AiInterface::getFirstOnSightEnemyUnit(Vec2i &pos, Field &field, int radius) {
Map *map= world->getMap();
for(int i = 0; i < world->getFactionCount(); ++i) {
for(int j = 0; j < world->getFaction(i)->getUnitCount(); ++j) {
Unit * unit= world->getFaction(i)->getUnit(j);
SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getPos()));
bool cannotSeeUnit = (unit->getType()->hasCellMap() == true &&
unit->getType()->getAllowEmptyCellMap() == true &&
unit->getType()->hasEmptyCellMap() == true);
if(sc->isVisible(teamIndex) && cannotSeeUnit == false &&
isAlly(unit) == false && unit->isAlive() == true) {
pos= unit->getPos();
field= unit->getCurrField();
if(pos.dist(getHomeLocation()) < radius) {
printLog(2, "Being attacked at pos "+intToStr(pos.x)+","+intToStr(pos.y)+"\n");
return unit;
}
}
}
}
return NULL;
}
}}//end namespace

View File

@ -93,6 +93,7 @@ public:
bool reqsOk(const CommandType *ct);
bool checkCosts(const ProducibleType *pt);
bool isFreeCells(const Vec2i &pos, int size, Field field);
const Unit *getFirstOnSightEnemyUnit(Vec2i &pos, Field &field, int radius);
private:
string getLogFilename() const {return "ai"+intToStr(factionIndex)+".log";}

View File

@ -46,16 +46,11 @@ const int PathFinder::pathFindBailoutRadius = 20;
PathFinder::PathFinder() {
nodePool.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
map=NULL;
}
PathFinder::PathFinder(const Map *map) {
nodePool.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
map=NULL;
init(map);
@ -63,30 +58,19 @@ PathFinder::PathFinder(const Map *map) {
void PathFinder::init(const Map *map) {
nodePool.resize(pathFindNodesMax);
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
this->map= map;
}
PathFinder::~PathFinder(){
nodePool.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
map=NULL;
}
TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck,bool clearLookupCache) {
TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck) {
if(map == NULL) {
throw runtime_error("map == NULL");
}
if(clearLookupCache == true) {
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[findPath] unit->getPos() [%s] finalPos [%s]",
@ -326,6 +310,29 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
// ==================== PRIVATE ====================
void PathFinder::processNode(Unit *unit, Node *node,const Vec2i finalPos, int i, int j, bool &nodeLimitReached) {
Vec2i sucPos= node->pos + Vec2i(i, j);
bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos);
if(openPos(sucPos) == false && canUnitMoveToCell == true) {
//if node is not open and canMove then generate another node
Node *sucNode= newNode();
if(sucNode != NULL) {
sucNode->pos= sucPos;
sucNode->heuristic= heuristic(sucNode->pos, finalPos);
sucNode->prev= node;
sucNode->next= NULL;
sucNode->exploredCell= map->getSurfaceCell(Map::toSurfCoords(sucPos))->isExplored(unit->getTeam());
openNodesList[sucNode->heuristic].push_back(sucNode);
openPosList[sucNode->pos] = true;
}
else {
nodeLimitReached= true;
}
}
}
//route a unit using A* algorithm
TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout) {
Chrono chrono;
@ -342,7 +349,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
const Vec2i finalPos= computeNearestFreePos(unit, targetPos);
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//path find algorithm
@ -366,9 +373,8 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
bool pathFound = true;
bool nodeLimitReached = false;
Node *node = NULL;
int64 lastPerfTick = 0;
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
int whileLoopCount = 0;
while(nodeLimitReached == false) {
@ -395,64 +401,38 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
closedNodesList[node->heuristic].push_back(node);
openPosList[node->pos] = true;
int tryDirection = random.randRange(0,1);
if(tryDirection > 0) {
int tryDirection = random.randRange(0,3);
if(tryDirection == 3) {
for(int i = 1; i >= -1 && nodeLimitReached == false; --i) {
for(int j = -1; j <= 1 && nodeLimitReached == false; ++j) {
processNode(unit, node, finalPos, i, j, nodeLimitReached);
}
}
}
else if(tryDirection == 2) {
for(int i = -1; i <= 1 && nodeLimitReached == false; ++i) {
for(int j = 1; j >= -1 && nodeLimitReached == false; --j) {
processNode(unit, node, finalPos, i, j, nodeLimitReached);
}
}
}
else if(tryDirection == 1) {
for(int i = -1; i <= 1 && nodeLimitReached == false; ++i) {
for(int j = -1; j <= 1 && nodeLimitReached == false; ++j) {
Vec2i sucPos= node->pos + Vec2i(i, j);
//bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos, &moveLookupCacheApproxCanMove);
bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos);
if(openPos(sucPos) == false && canUnitMoveToCell == true) {
//if node is not open and canMove then generate another node
Node *sucNode= newNode();
if(sucNode != NULL) {
sucNode->pos= sucPos;
sucNode->heuristic= heuristic(sucNode->pos, finalPos);
sucNode->prev= node;
sucNode->next= NULL;
sucNode->exploredCell= map->getSurfaceCell(Map::toSurfCoords(sucPos))->isExplored(unit->getTeam());
openNodesList[sucNode->heuristic].push_back(sucNode);
openPosList[sucNode->pos] = true;
}
else {
nodeLimitReached= true;
}
}
processNode(unit, node, finalPos, i, j, nodeLimitReached);
}
}
}
else {
for(int i = 1; i >= -1 && nodeLimitReached == false; --i) {
for(int j = 1; j >= -1 && nodeLimitReached == false; --j) {
Vec2i sucPos= node->pos + Vec2i(i, j);
//bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos, &moveLookupCacheApproxCanMove);
bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos);
if(openPos(sucPos) == false && canUnitMoveToCell == true) {
//if node is not open and canMove then generate another node
Node *sucNode= newNode();
if(sucNode != NULL) {
sucNode->pos= sucPos;
sucNode->heuristic= heuristic(sucNode->pos, finalPos);
sucNode->prev= node;
sucNode->next= NULL;
sucNode->exploredCell= map->getSurfaceCell(Map::toSurfCoords(sucPos))->isExplored(unit->getTeam());
openNodesList[sucNode->heuristic].push_back(sucNode);
openPosList[sucNode->pos] = true;
}
else {
nodeLimitReached= true;
}
}
processNode(unit, node, finalPos, i, j, nodeLimitReached);
}
}
}
} //while
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
Node *lastNode= node;
@ -466,7 +446,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
}
}
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//check results of path finding
TravelState ts = tsImpossible;
@ -497,7 +477,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
unit->logSynchData(__FILE__,__LINE__,szBuf);
}
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
}
else {
//on the way
@ -510,7 +490,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
currNode= currNode->prev;
}
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//store path
path->clear();
@ -520,7 +500,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
path->add(currNode->next->pos);
}
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
@ -541,14 +521,14 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
unit->setCurrentUnitTitle(szBuf);
}
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
}
openNodesList.clear();
openPosList.clear();
closedNodesList.clear();
if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
return ts;
}
@ -562,6 +542,25 @@ PathFinder::Node *PathFinder::newNode() {
return NULL;
}
void PathFinder::processNearestFreePos(const Vec2i &finalPos, int i, int j, int size, Field field, int teamIndex,Vec2i unitPos, Vec2i &nearestPos, float &nearestDist) {
Vec2i currPos= finalPos + Vec2i(i, j);
if(map->isAproxFreeCells(currPos, size, field, teamIndex)) {
float dist= currPos.dist(finalPos);
//if nearer from finalPos
if(dist < nearestDist){
nearestPos= currPos;
nearestDist= dist;
}
//if the distance is the same compare distance to unit
else if(dist == nearestDist){
if(currPos.dist(unitPos) < nearestPos.dist(unitPos)) {
nearestPos= currPos;
}
}
}
}
Vec2i PathFinder::computeNearestFreePos(const Unit *unit, const Vec2i &finalPos) {
if(map == NULL) {
throw runtime_error("map == NULL");
@ -581,24 +580,10 @@ Vec2i PathFinder::computeNearestFreePos(const Unit *unit, const Vec2i &finalPos)
Vec2i unitPos= unit->getPos();
Vec2i nearestPos= unitPos;
float nearestDist= unitPos.dist(finalPos);
for(int i= -maxFreeSearchRadius; i <= maxFreeSearchRadius; ++i) {
for(int j= -maxFreeSearchRadius; j <= maxFreeSearchRadius; ++j) {
Vec2i currPos= finalPos + Vec2i(i, j);
if(map->isAproxFreeCells(currPos, size, field, teamIndex)) {
float dist= currPos.dist(finalPos);
//if nearer from finalPos
if(dist < nearestDist){
nearestPos= currPos;
nearestDist= dist;
}
//if the distance is the same compare distance to unit
else if(dist == nearestDist){
if(currPos.dist(unitPos) < nearestPos.dist(unitPos)) {
nearestPos= currPos;
}
}
}
processNearestFreePos(finalPos, i, j, size, field, teamIndex, unitPos, nearestPos, nearestDist);
}
}
return nearestPos;

View File

@ -68,15 +68,12 @@ private:
const Map *map;
RandomGen random;
//std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<Field,bool> > > > lookupCacheCanMove;
std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<int, std::map<Field,bool> > > > > moveLookupCacheApproxCanMove;
public:
PathFinder();
PathFinder(const Map *map);
~PathFinder();
void init(const Map *map);
TravelState findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck=NULL,bool clearLookupCache=true);
TravelState findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck=NULL);
private:
TravelState aStar(Unit *unit, const Vec2i &finalPos, bool inBailout);
@ -86,6 +83,9 @@ private:
bool openPos(const Vec2i &sucPos);
Node * minHeuristicFastLookup();
void processNode(Unit *unit, Node *node,const Vec2i finalPos, int i, int j, bool &nodeLimitReached);
void processNearestFreePos(const Vec2i &finalPos, int i, int j, int size, Field field, int teamIndex,Vec2i unitPos, Vec2i &nearestPos, float &nearestDist);
};
}}//end namespace

View File

@ -34,6 +34,8 @@ using namespace Shared::Graphics::Gl;
using namespace Shared::Util;
using namespace Shared::Graphics;
//#define ENABLE_VBO_CODE 1
namespace Glest { namespace Game{
// =====================================================
@ -1799,17 +1801,6 @@ void Renderer::ReleaseSurfaceVBOs() {
#endif
class SurfaceData {
public:
SurfaceData(){};
int textureHandle;
vector<Vec2f> texCoords;
vector<Vec2f> texCoordsSurface;
vector<Vec3f> vertices;
vector<Vec3f> normals;
};
void Renderer::renderSurface(const int renderFps) {
IF_DEBUG_EDITION(
if (getDebugRenderer().willRenderSurface()) {
@ -1865,90 +1856,95 @@ void Renderer::renderSurface(const int renderFps) {
#if defined(ENABLE_VBO_CODE)
int lastSurfaceDataIndex = -1;
std::vector<SurfaceData> surface;
//surface.reserve(qCache.visibleScaledCellList.size());
if(mapSurfaceData.find(visibleQuad) == mapSurfaceData.end()) {
//std::vector<SurfaceData> surface;
std::vector<SurfaceData> &surface = mapSurfaceData[visibleQuad];
for(int visibleIndex = 0;
visibleIndex < qCache.visibleScaledCellList.size(); ++visibleIndex) {
Vec2i &pos = qCache.visibleScaledCellList[visibleIndex];
for(int visibleIndex = 0;
visibleIndex < qCache.visibleScaledCellList.size(); ++visibleIndex) {
Vec2i &pos = qCache.visibleScaledCellList[visibleIndex];
SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y);
SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y);
SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1);
SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1);
SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y);
SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y);
SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1);
SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1);
if(tc00 == NULL) {
throw runtime_error("tc00 == NULL");
}
if(tc10 == NULL) {
throw runtime_error("tc10 == NULL");
}
if(tc01 == NULL) {
throw runtime_error("tc01 == NULL");
}
if(tc11 == NULL) {
throw runtime_error("tc11 == NULL");
if(tc00 == NULL) {
throw runtime_error("tc00 == NULL");
}
if(tc10 == NULL) {
throw runtime_error("tc10 == NULL");
}
if(tc01 == NULL) {
throw runtime_error("tc01 == NULL");
}
if(tc11 == NULL) {
throw runtime_error("tc11 == NULL");
}
triangleCount+= 2;
pointCount+= 4;
//set texture
if(tc00->getSurfaceTexture() == NULL) {
throw runtime_error("tc00->getSurfaceTexture() == NULL");
}
vector<VisibleQuadContainerVBOCache> &vboCache = GetSurfaceVBOs(pos);
int surfaceDataIndex = -1;
currTex= static_cast<const Texture2DGl*>(tc00->getSurfaceTexture())->getHandle();
if(currTex != lastTex) {
lastTex = currTex;
}
else {
surfaceDataIndex = lastSurfaceDataIndex;
}
if(surfaceDataIndex < 0) {
SurfaceData newData;
newData.textureHandle = currTex;
surface.push_back(newData);
surfaceDataIndex = surface.size()-1;
//surface[surfaceDataIndex].texCoords.reserve(100);
//surface[surfaceDataIndex].texCoordsSurface.reserve(100);
//surface[surfaceDataIndex].vertices.reserve(100);
//surface[surfaceDataIndex].normals.reserve(100);
}
lastSurfaceDataIndex = surfaceDataIndex;
const Vec2f &surfCoord= tc00->getSurfTexCoord();
//int dataIndex = surface[surfaceDataIndex].texCoords.size();
surface[surfaceDataIndex].texCoords.push_back(tc01->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep));
surface[surfaceDataIndex].vertices.push_back(tc01->getVertex());
surface[surfaceDataIndex].normals.push_back(tc01->getNormal());
surface[surfaceDataIndex].texCoords.push_back(tc00->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y));
surface[surfaceDataIndex].vertices.push_back(tc00->getVertex());
surface[surfaceDataIndex].normals.push_back(tc00->getNormal());
surface[surfaceDataIndex].texCoords.push_back(tc11->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep));
surface[surfaceDataIndex].vertices.push_back(tc11->getVertex());
surface[surfaceDataIndex].normals.push_back(tc11->getNormal());
surface[surfaceDataIndex].texCoords.push_back(tc10->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y));
surface[surfaceDataIndex].vertices.push_back(tc10->getVertex());
surface[surfaceDataIndex].normals.push_back(tc10->getNormal());
}
triangleCount+= 2;
pointCount+= 4;
//set texture
if(tc00->getSurfaceTexture() == NULL) {
throw runtime_error("tc00->getSurfaceTexture() == NULL");
}
vector<VisibleQuadContainerVBOCache> &vboCache = GetSurfaceVBOs(pos);
int surfaceDataIndex = -1;
currTex= static_cast<const Texture2DGl*>(tc00->getSurfaceTexture())->getHandle();
if(currTex != lastTex) {
lastTex = currTex;
}
else {
surfaceDataIndex = lastSurfaceDataIndex;
}
if(surfaceDataIndex < 0) {
SurfaceData newData;
newData.textureHandle = currTex;
surface.push_back(newData);
surfaceDataIndex = surface.size()-1;
//surface[surfaceDataIndex].texCoords.reserve(100);
//surface[surfaceDataIndex].texCoordsSurface.reserve(100);
//surface[surfaceDataIndex].vertices.reserve(100);
//surface[surfaceDataIndex].normals.reserve(100);
}
lastSurfaceDataIndex = surfaceDataIndex;
const Vec2f &surfCoord= tc00->getSurfTexCoord();
//int dataIndex = surface[surfaceDataIndex].texCoords.size();
surface[surfaceDataIndex].texCoords.push_back(tc01->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep));
surface[surfaceDataIndex].vertices.push_back(tc01->getVertex());
surface[surfaceDataIndex].normals.push_back(tc01->getNormal());
surface[surfaceDataIndex].texCoords.push_back(tc00->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y));
surface[surfaceDataIndex].vertices.push_back(tc00->getVertex());
surface[surfaceDataIndex].normals.push_back(tc00->getNormal());
surface[surfaceDataIndex].texCoords.push_back(tc11->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep));
surface[surfaceDataIndex].vertices.push_back(tc11->getVertex());
surface[surfaceDataIndex].normals.push_back(tc11->getNormal());
surface[surfaceDataIndex].texCoords.push_back(tc10->getFowTexCoord());
surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y));
surface[surfaceDataIndex].vertices.push_back(tc10->getVertex());
surface[surfaceDataIndex].normals.push_back(tc10->getNormal());
//mapSurfaceData[visibleQuad] = surface;
}
//printf("\nsurface.size() = %d vs qCache.visibleScaledCellList.size() = %d \n",surface.size(),qCache.visibleScaledCellList.size());
std::vector<SurfaceData> &surface = mapSurfaceData[visibleQuad];
for(int i = 0; i < surface.size(); ++i) {
SurfaceData &data = surface[i];

View File

@ -42,8 +42,6 @@
#include "leak_dumper.h"
//#define ENABLE_VBO_CODE 1
namespace Glest{ namespace Game{
using namespace Shared::Graphics;
@ -136,7 +134,7 @@ public:
std::vector<Vec2i> visibleScaledCellList;
};
#if defined(ENABLE_VBO_CODE)
//#if defined(ENABLE_VBO_CODE)
class VisibleQuadContainerVBOCache {
public:
// Vertex Buffer Object Names
@ -147,7 +145,7 @@ public:
uint32 m_nVBONormals; // Normal VBO Name
//uint32 m_nVBOIndexes; // Indexes VBO Name
};
#endif
//#endif
class Renderer : public RendererInterface, public BaseRenderer, public SimpleTaskCallbackInterface {
@ -265,13 +263,25 @@ private:
std::map<Vec3f,Vec3f> worldToScreenPosCache;
#if defined(ENABLE_VBO_CODE)
//#if defined(ENABLE_VBO_CODE)
std::map<Vec2i,std::vector<VisibleQuadContainerVBOCache> > mapSurfaceVBOCache;
VisibleQuadContainerVBOCache SetupSurfaceVBO(Vec2i &pos, SurfaceCell *cell, Vec2f surfCoord);
vector<VisibleQuadContainerVBOCache> & GetSurfaceVBOs(Vec2i &pos);
void ReleaseSurfaceVBOs();
#endif
//#endif
class SurfaceData {
public:
SurfaceData(){};
int textureHandle;
vector<Vec2f> texCoords;
vector<Vec2f> texCoordsSurface;
vector<Vec3f> vertices;
vector<Vec3f> normals;
};
std::map<Quad2i,std::vector<SurfaceData> > mapSurfaceData;
private:
Renderer();

View File

@ -86,6 +86,16 @@ public:
}
}
}
std::string getString() const {
std::ostringstream streamOut;
streamOut << "#1: " << this->p[0].getString();
streamOut << "#2: " << this->p[1].getString();
std::string result = streamOut.str();
streamOut.str(std::string());
return result;
}
};
typedef Rect2<int> Rect2i;