aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-01-05 08:48:29 -0500
committerClyne Sullivan <tullivan99@gmail.com>2016-01-05 08:48:29 -0500
commit45edad31559852d306d59b50f380cb79c9f27dcc (patch)
tree2a8b7ac460bfae33f517f3b24904be7158ab0d7a /src
parent65addfa212a2aef2f2d6de3cb49edc99a8f02f59 (diff)
save/load stuffs
Diffstat (limited to 'src')
-rw-r--r--src/entities.cpp70
-rw-r--r--src/gameplay.cpp11
-rw-r--r--src/world.cpp57
3 files changed, 125 insertions, 13 deletions
diff --git a/src/entities.cpp b/src/entities.cpp
index d424aba..f34abd6 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -490,3 +490,73 @@ void Mob::wander(int timeRun){
break;
}
}
+
+char *Entity::baseSave(void){
+ static EntitySavePacket *esp = new EntitySavePacket();
+ memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket));
+ esp->loc = loc;
+ esp->vel = vel;
+ esp->width = width;
+ esp->height = height;
+ esp->speed = speed;
+ esp->health = health;
+ esp->maxHealth = maxHealth;
+ esp->subtype = subtype;
+ esp->ticksToUse = ticksToUse;
+ esp->randDialog = randDialog;
+ esp->ground = ground;
+ esp->near = near;
+ esp->canMove = canMove;
+ esp->right = right;
+ esp->left = left;
+ esp->alive = alive;
+ esp->hit = hit;
+ esp->type = type;
+ esp->gender = gender;
+ esp->nameSize = strlen(name) + 1;
+ return (char *)esp;
+}
+
+void Entity::baseLoad(char *e){
+ const char *tmpname = "GG\0";
+ EntitySavePacket *esp = (EntitySavePacket *)e;
+ inv->load(&esp->isp);
+ loc = esp->loc;
+ vel = esp->vel;
+ width = esp->width;
+ height = esp->height;
+ speed = esp->speed;
+ health = esp->health;
+ maxHealth = esp->maxHealth;
+ subtype = esp->subtype;
+ ticksToUse = esp->ticksToUse;
+ randDialog = esp->randDialog;
+ ground = esp->ground;
+ near = esp->near;
+ canMove = esp->canMove;
+ right = esp->right;
+ left = esp->left;
+ alive = esp->alive;
+ hit = esp->hit;
+ type = esp->type;
+ gender = esp->gender;
+ name = new char[esp->nameSize+1];
+ strcpy(name,tmpname);
+}
+
+char *NPC::save(unsigned int *size){
+ static char *buf = new char[(*size = sizeof(EntitySavePacket) + aiFunc.size() * sizeof(int(*)(NPC *)))],*esp;
+ memcpy(buf,(esp = baseSave()),sizeof(EntitySavePacket));
+ delete[] esp;
+ memcpy(buf+sizeof(EntitySavePacket),aiFunc.data(),aiFunc.size() * sizeof(int(*)(NPC *)));
+ return buf;
+}
+
+void NPC::load(char *b){
+ unsigned int size = *(unsigned int *)b,size2,i;
+ baseLoad(b + sizeof(unsigned int));
+ size2 = (size - sizeof(unsigned int) - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *));
+ for(i=0;i<size2;i++){
+ //aiFunc.push_back
+ }
+}
diff --git a/src/gameplay.cpp b/src/gameplay.cpp
index ff77728..94fb954 100644
--- a/src/gameplay.cpp
+++ b/src/gameplay.cpp
@@ -122,16 +122,17 @@ static World *worldFirstVillage;
void destroyEverything(void);
void initEverything(void){
- //static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary);
+ static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary);
worldSpawnHill1 = new World();
- /*if(!i.fail()){
+ worldSpawnHill1->setBackground(BG_FOREST);
+ if(!i.fail()){
worldSpawnHill1->load(&i);
i.close();
- }else*/
+ }else{
worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1);
- worldSpawnHill1->setBackground(BG_FOREST);
- worldSpawnHill1->setBGM("assets/music/embark.wav");
+ worldSpawnHill1->setBGM("assets/music/embark.wav");
+ }
worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock);
worldSpawnHill2 = new World();
diff --git a/src/world.cpp b/src/world.cpp
index 2afbdf5..34e1870 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -62,27 +62,68 @@ void World::setBackground(WORLD_BG_TYPE bgt){
}
void World::save(std::ofstream *o){
+ static unsigned int size2;
+ unsigned int size,i;
+ size_t bgms = strlen(bgm) + 1;
+ char *bufptr;
+
o->write((char *)&lineCount, sizeof(unsigned int));
- o->write((char *)&line ,lineCount * sizeof(struct line_t));
+ o->write((char *)line ,lineCount * sizeof(struct line_t));
o->write("GG" ,2 * sizeof(char));
- o->write((char *)&star ,100 * sizeof(vec2));
+ o->write((char *)star ,100 * sizeof(vec2));
+ o->write((char *)&bgType , sizeof(WORLD_BG_TYPE));
+ o->write((char *)&bgms , sizeof(size_t));
+ o->write(bgm ,strlen(bgm)+1);
+ o->write("NO" ,2 * sizeof(char));
+
+ /*std::vector<NPC *> npc;
+ std::vector<Structures *> build;
+ std::vector<Mob *> mob;
+ std::vector<Entity *> entity;
+ std::vector<Object *> object;*/
+
+ size = npc.size();
+ for(i=0;i<size;i++){
+ bufptr = npc[i]->save(&size2);
+ o->write((char *)&size2,sizeof(unsigned int));
+ o->write(bufptr,size2);
+ }
}
void World::load(std::ifstream *i){
- static char end[2];
+ //unsigned int size;
+ size_t bgms;
+ char sig[2];
i->read((char *)&lineCount,sizeof(unsigned int));
+
line = new struct line_t[lineCount];
+ i->read((char *)line,lineCount * sizeof(struct line_t));
- i->read((char *)&line,lineCount * sizeof(struct line_t));
- i->read(end ,2 * sizeof(char));
- if(strncmp(end,"GG",2)){
+ i->read(sig,2 * sizeof(char));
+ if(strncmp(sig,"GG",2)){
std::cout<<"world.dat corrupt"<<std::endl;
exit(EXIT_FAILURE);
}
- i->read((char *)&star,100 * sizeof(vec2));
-
+
x_start = 0 - getWidth(this) / 2;
+
+ i->read((char *)star,100 * sizeof(vec2));
+ i->read((char *)&bgType,sizeof(WORLD_BG_TYPE));
+
+ i->read((char *)&bgms,sizeof(size_t));
+ bgm = new char[bgms];
+ i->read(bgm,bgms);
+ setBGM(bgm);
+
+
+ i->read(sig,2 * sizeof(char));
+ if(strncmp(sig,"NO",2)){
+ std::cout<<"world.dat corrupt"<<std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+
}
World::World(void){