aboutsummaryrefslogtreecommitdiffstats
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
parent65addfa212a2aef2f2d6de3cb49edc99a8f02f59 (diff)
save/load stuffs
-rw-r--r--Changelog6
-rw-r--r--include/entities.h35
-rw-r--r--include/inventory.h17
-rw-r--r--src/entities.cpp70
-rw-r--r--src/gameplay.cpp11
-rw-r--r--src/world.cpp57
6 files changed, 183 insertions, 13 deletions
diff --git a/Changelog b/Changelog
index e3ed5e0..68edc5e 100644
--- a/Changelog
+++ b/Changelog
@@ -476,3 +476,9 @@
- finished wrapping text for dialog boxes
- began working on world saving/loading again
- got some mad GLSL shaders running
+
+1/4/2015:
+=========
+
+ - fixed basic world save/load, working on entity saving
+ - GLSL shaders worked?
diff --git a/include/entities.h b/include/entities.h
index 6b8cc32..894dc16 100644
--- a/include/entities.h
+++ b/include/entities.h
@@ -45,6 +45,31 @@ enum BUILD_SUB{
FOUNTAIN
};
+typedef struct {
+ InventorySavePacket isp;
+ vec2 loc;
+ vec2 vel;
+ float width;
+ float height;
+ float speed;
+ float health;
+ float maxHealth;
+ int subtype;
+ int ticksToUse;
+ unsigned int randDialog;
+ unsigned char ground;
+ bool near;
+ bool canMove;
+ bool right,left;
+ bool alive;
+ bool hit;
+ _TYPE type;
+ GENDER gender;
+ size_t nameSize;
+ //char *name;
+ //Texturec *tex;
+} __attribute__ ((packed)) EntitySavePacket;
+
class World;
class Particles{
@@ -139,6 +164,9 @@ public:
virtual void interact(){}
virtual ~Entity(){}
+
+ char *baseSave(void);
+ void baseLoad(char *);
};
class Player : public Entity {
@@ -151,6 +179,10 @@ public:
void interact();
};
+typedef struct {
+ EntitySavePacket esp;
+} __attribute__ ((packed)) NPCSavePacket;
+
class NPC : public Entity{
public:
std::vector<int (*)(NPC *)>aiFunc;
@@ -161,6 +193,9 @@ public:
void addAIFunc(int (*func)(NPC *),bool preload);
void interact();
void wander(int);
+
+ char *save(unsigned int *size);
+ void load(char *b);
};
class Structures : public Entity{
diff --git a/include/inventory.h b/include/inventory.h
index 31b7d88..b035f91 100644
--- a/include/inventory.h
+++ b/include/inventory.h
@@ -60,6 +60,11 @@ struct item_t{
ITEM_ID id;
} __attribute__((packed));
+typedef struct {
+ unsigned int size;
+ int os;
+ unsigned int sel;
+} __attribute__ ((packed)) InventorySavePacket;
class Inventory {
private:
@@ -87,6 +92,18 @@ public:
void draw(void); // Draws a text list of items in this inventory (should only be called for the player for now)
+ char *save(void){
+ static InventorySavePacket *isp = new InventorySavePacket();
+ isp->size = size;
+ isp->os = os;
+ isp->sel = sel;
+ return (char *)isp;
+ }
+ void load(InventorySavePacket *isp){
+ size = isp->size;
+ os = isp->os;
+ sel = isp->sel;
+ }
};
void itemUse(void *p);
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){