1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include "chunkmanager.h"
#include "chunk.h"
#include <string>
#include <distance.h>
#include <cmath>
#include <fstream>
#include "config.h"
#include <iostream>
#include <time.h>
#include <string>
std::string convertInt(int number)
{
std::string temp="";
std::string returnvalue="";
if (number == 0)
return "0";
if (number < 0){
returnvalue += "-";
number = -number;}
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (unsigned int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}
chunkmanager::chunkmanager()
{
m_config.chunksRange = 2;
}
//----------------debut manage----------------//
void chunkmanager::manage(Player player, std::list<chunk>& list)
{
for(int x(-m_config.chunksRange+1); x != m_config.chunksRange;++x)
{
for(int z(-m_config.chunksRange+1); z != m_config.chunksRange;++z)
{
//test chunk chargé
bool loaded(false);
for(std::list<chunk>::iterator it = list.begin(); it != list.end();++it)
{
if(it->getPos().X == x+player.getPos().X && it->getPos().Z == z+player.getPos().Z)
loaded = true;
}
if(loaded == false)
{
//chargement du chunk
std::string file("C:\\Users\\Administrateur\\AppData\\Roaming\\OpenGL\\saves\\chunks\\");
int X(x+player.getPos().divide(16).X);
int Z(z+player.getPos().divide(16).Z);
std::cout << X << " " << Z << std::endl;
file += convertInt(X);
file += ".";
file += convertInt(Z);
file += ".chunk";
point2D coords(X, Z);
chunk c(coords, file);
c.save();
list.push_back(c);
std::cout << "new chunk " << file << std::endl;
}
}
}
for(std::list<chunk>::iterator it = list.begin(); it != list.end();++it){
point2D point = it->getPos();
std::cout << "distance " << distance::getDistance32((player.getPos()).divide(16), it->getPos()) <<std::endl;
//si le chunk est trop loin
if(distance::getDistance32((player.getPos()).divide(16), it->getPos()) > (m_config.chunksRange))
{
//----sauvegarde et supression de la liste----//
std::string file("C:\\Users\\Administrateur\\AppData\\Roaming\\OpenGL\\saves\\chunks\\");
file += convertInt(point.X);
file += ".";
file += convertInt(point.Z);
file += ".chunk";
std::cout << "delete " << file << std::endl;
it->save();
list.erase(it);
//--------------------------------------------//
}
}
}
//---------------------------------------------// |