Skip to content

Commit

Permalink
0.0.069: LOS idle processing
Browse files Browse the repository at this point in the history
* Add idle processing to LOS to improve performance under load. Works much better than it has any right to.
* Make forests more dense and grasslands more sparse.
  • Loading branch information
RyanBabij committed Feb 19, 2019
1 parent 934a48e commit 19a5be6
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 17 deletions.
51 changes: 49 additions & 2 deletions Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Character::Character()

worldX = -1;
worldY = -1;

idleCounter=0;

//enum enumCauseOfDeath { UNKNOWN=0, STARVATION=1, OLD_AGE=2 };

Expand Down Expand Up @@ -471,14 +473,16 @@ void Character::initialiseKnowledge()
{
if ( knowledge == 0 ) { return; }
if ( tribe == 0 ) { return; }

idleCounter=0;

//learn about all surrounding tiles.

// The basic 3x3 box of guaranteed sight.


//For now this simply wipes LOS from last turn.
knowledge->updateLOS();
//knowledge->updateLOS();

//knowledge->addTile(world(worldX,worldY), x,y);

Expand Down Expand Up @@ -515,7 +519,7 @@ void Character::initialiseKnowledge()

//std::cout<<"coordinates to raytrace from: "<<fullX<<", "<<fullY<<".\n";

Vector <HasXY2 <unsigned long int> *> * vVisibleTiles = world.rayTraceLOS(fullX,fullY,MAX_VIEW_RANGE);
Vector <HasXY2 <unsigned long int> *> * vVisibleTiles = world.rayTraceLOS(fullX,fullY,MAX_VIEW_RANGE/2);

if ( vVisibleTiles!=0 )
{
Expand All @@ -527,7 +531,50 @@ void Character::initialiseKnowledge()
}
}

// Add this tile for later processing when idle.
vMovesToProcess.push(new HasXY2 <unsigned long int> (fullX,fullY));

}

//Update knowledge with current instance.
void Character::updateKnowledgeIdle()
{
// Implement a basic delay to prevent idle flickering in.
++idleCounter;
if (idleCounter > 10) { idleCounter=10; }

if ( vMovesToProcess.size() == 0 ) { return; }
if ( knowledge == 0 ) { return; }
if ( tribe == 0 ) { return; }



//Update the LOS backlog during idle time.

//For now this simply wipes LOS from last turn.
//knowledge->updateLOS();

vMovesToProcess.shuffle();

auto moveToProcess = vMovesToProcess(0);

knowledge->addTile(moveToProcess->x,moveToProcess->y);


Vector <HasXY2 <unsigned long int> *> * vVisibleTiles = world.rayTraceLOS(moveToProcess->x,moveToProcess->y,MAX_VIEW_RANGE);

if ( vVisibleTiles!=0 )
{
for (int i=0; i<vVisibleTiles->size(); ++i)
{
//std::cout<<"ADDING\n";
knowledge->addTile((*vVisibleTiles)(i)->x, (*vVisibleTiles)(i)->y);
delete (*vVisibleTiles)(i);
}
}

delete moveToProcess;
vMovesToProcess.removeSlot(0);

}

Expand Down
6 changes: 6 additions & 0 deletions Character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Character: public WorldObject, public TableInterface, public SaveFileInter
// True if player has flagged this character as favourite.
bool isFavourite;


char idleCounter;
Vector <HasXY2 <unsigned long int> * > vMovesToProcess;

/* Right now each character has a biological mother and father. In the future we might also have guardians */
Character* father;
Character* mother;
Expand Down Expand Up @@ -171,6 +175,8 @@ class Character: public WorldObject, public TableInterface, public SaveFileInter

//Update knowledge with current instance.
void updateKnowledge();
// Extra processing available
void updateKnowledgeIdle();


};
Expand Down
2 changes: 1 addition & 1 deletion CompileCount.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
*/
#include <string>

const std::string COMPILE_COUNT = "6435";
const std::string COMPILE_COUNT = "6497";
#endif
4 changes: 2 additions & 2 deletions Driver_Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum class eLocalTerrain { G=0, GRASS=1, WATER=2 };


// STRINGS
const std::string VERSION = "0.0.068 Win32 dev";
const std::string VERSION = "0.0.069 Win32 dev";
const std::string G_WINDOW_TITLE = "WorldSim";
const std::string SAVE_FOLDER_PATH = "savedata";

Expand Down Expand Up @@ -83,7 +83,7 @@ bool FOG_OF_WAR = true;
const int LOCAL_MAP_SIZE = 65;
//const int LOCAL_MAP_SIZE = 129;
int TIME_SCALE = 60; /* How many seconds of gametime pass per logic tick. */
const int MAX_VIEW_RANGE = 100;
const int MAX_VIEW_RANGE = 80;

// The number of local maps to hold in memory at once. Minimum should be 4.
// (One for the player's current map, and three neighboring maps).
Expand Down
75 changes: 65 additions & 10 deletions World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Implementation of "World.hpp".
*/

//#include<set> /* For raytraceLOS */

#include <NameGen/NameGen.hpp>
#include <WorldGenerator/WorldGenerator2.hpp>

Expand Down Expand Up @@ -254,6 +256,9 @@ Vector <HasXY2 <unsigned long int> *> * World::rayTraceLOS (unsigned long int _x
unsigned long int tempY = rayY;

auto hXY = new HasXY2 <unsigned long int>;

//std::set <HasXY2 <unsigned long int> * > sInt;
//setOfNumbers.insert("first");

rayTraceCoordinates.push( new HasXY2 <unsigned long int> (tempX,tempY) );

Expand Down Expand Up @@ -315,7 +320,26 @@ void World::rayTrace (unsigned long int _x1, unsigned long int _y1, unsigned lon
if ( (_x1==_x2) && (_y1==_y2) )
{
if ( isSafe(_x1,_y2) )
{ vVisibleTiles->push(new HasXY2 <unsigned long int> (_x1,_y1) ); }
{
auto temp = new HasXY2 <unsigned long int> (_x1,_y1);

// Proper optimisation would use a set, however for now we'll just do a pre-check
// to prevent storing duplicates.
// Update: I removed the dupe detection as it significantly hurts performance.
// The renderer must iterate over the dupes, however it seems to be able to handle it better for now.
//if ( vVisibleTiles->contains(temp) == false )
if (true)
{
vVisibleTiles->push( temp );
}
else
{
//std::cout<<"DUPE DETECTED\n";
delete temp;
}


}
}

// SPECIAL CASE: UP/DOWN
Expand All @@ -325,17 +349,22 @@ void World::rayTrace (unsigned long int _x1, unsigned long int _y1, unsigned lon
{
if ( isSafe(_x1,_y1) )
{
vVisibleTiles->push(new HasXY2 <unsigned long int> (_x1,_y1) );
auto temp = new HasXY2 <unsigned long int> (_x1,_y1);

//if ( vVisibleTiles->containsPtr(temp) == false )
if (true)
{
vVisibleTiles->push( temp );
}
else
{
//std::cout<<"DUPE DETECTED\n";
delete temp;
}

LocalTile * lt = (*this)(_x1, _y1);
if (lt != 0 && lt->hasViewBlocker())
{ break; }

if (lt == 0)
{
std::cout<<"MAP ISNT LOADING\n";
}

}
if ( _y1 < _y2 )
{ ++_y1; }
Expand Down Expand Up @@ -379,7 +408,18 @@ void World::rayTrace (unsigned long int _x1, unsigned long int _y1, unsigned lon

if ( isSafe(_x1,roundedY) == true )
{
vVisibleTiles->push(new HasXY2 <unsigned long int> (_x1,roundedY) );
auto temp = new HasXY2 <unsigned long int> (_x1,roundedY);

//if ( vVisibleTiles->containsPtr(temp) == false )
if (true)
{
vVisibleTiles->push( temp );
}
else
{
//std::cout<<"DUPE DETECTED\n";
delete temp;
}

LocalTile * lt = (*this)(_x1, roundedY);
if (lt != 0 && lt->hasViewBlocker())
Expand Down Expand Up @@ -426,7 +466,18 @@ void World::rayTrace (unsigned long int _x1, unsigned long int _y1, unsigned lon

if ( isSafe(roundedX,_y1) == true )
{
vVisibleTiles->push(new HasXY2 <unsigned long int> (roundedX, _y1) );
auto temp = new HasXY2 <unsigned long int> (roundedX,_y1);

//if ( vVisibleTiles->containsPtr(temp) == false )
if (true)
{
vVisibleTiles->push( temp );
}
else
{
//std::cout<<"DUPE DETECTED\n";
delete temp;
}

LocalTile * lt = (*this)(roundedX, _y1);
if (lt != 0 && lt->hasViewBlocker())
Expand Down Expand Up @@ -744,6 +795,10 @@ void World::handleTickBacklog()
void World::idleTick()
{
handleTickBacklog();
if ( playerCharacter != 0 )
{
playerCharacter->updateKnowledgeIdle();
}
}


Expand Down
4 changes: 2 additions & 2 deletions World_Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ bool World_Local::generate()
{
aLocalTile(_x,_y).baseTerrain = baseBiome;

int baseTreeChance = 20;
int baseTreeChance = 30;

// Temporary hack to make forests look less bad.
if ( baseBiome == FOREST || baseBiome == JUNGLE )
{
aLocalTile(_x,_y).baseTerrain = GRASSLAND;
baseTreeChance/=3;
baseTreeChance/=8;
}


Expand Down

0 comments on commit 19a5be6

Please sign in to comment.