]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
world save/load done?
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 7 Jan 2016 13:35:51 +0000 (08:35 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 7 Jan 2016 13:35:51 +0000 (08:35 -0500)
Changelog
include/entities.h
src/entities.cpp
src/gameplay.cpp
src/world.cpp

index 68edc5e456d98d4b54d96bdfc7fa090de647a326..125aefca97591c67ce52d9a9e832cfe2f029a0bc 100644 (file)
--- a/Changelog
+++ b/Changelog
 
        - fixed basic world save/load, working on entity saving
        - GLSL shaders worked?
+
+1/5/2015:
+=========
+
+       - can save NPCs and Structures
+       - more shadering
index 894dc16e5cd8e47b2dea32fb9627e399983baeff..0f7ac9bea5e95b044b14d712193eabfb64b61759 100644 (file)
@@ -66,7 +66,7 @@ typedef struct {
        _TYPE type;
        GENDER  gender;
        size_t nameSize;
-       //char   *name;
+       char   name[32];
        //Texturec *tex;
 } __attribute__ ((packed)) EntitySavePacket;
 
@@ -179,10 +179,6 @@ public:
        void interact();
 };
 
-typedef struct {
-       EntitySavePacket esp;
-} __attribute__ ((packed)) NPCSavePacket;
-
 class NPC : public Entity{
 public:
        std::vector<int (*)(NPC *)>aiFunc;
@@ -195,9 +191,16 @@ public:
        void wander(int);
        
        char *save(unsigned int *size);
-       void load(char *b);
+       void load(unsigned int,char *b);
 };
 
+typedef struct {
+       EntitySavePacket esp;
+       World *inWorld;
+       World *inside;
+       BUILD_SUB bsubtype;
+} __attribute__ ((packed)) StructuresSavePacket;
+
 class Structures : public Entity{
 public:
        World *inWorld;
@@ -208,6 +211,9 @@ public:
        ~Structures();
        
        unsigned int spawn(_TYPE, BUILD_SUB, float, float, World *);
+       
+       char *save(void);
+       void load(char *s);
 };
 
 class Mob : public Entity{
@@ -220,19 +226,41 @@ public:
        ~Mob();
        
        void wander(int);
+       
+       char *save(void);
+       void load(char *);
 };
 
+typedef struct {
+       EntitySavePacket esp;
+       double init_y;
+       //void (*hey)(Mob *callee);
+} __attribute__ ((packed)) MobSavePacket;
+
+typedef struct {
+       EntitySavePacket esp;
+       ITEM_ID identifier;
+       bool questObject;
+       char pickupDialog[256];
+} __attribute__ ((packed)) ObjectSavePacket;
+
 class Object : public Entity{
 private:
-       int identifier;
+       ITEM_ID identifier;
 public:
        char *pickupDialog;
        bool questObject = false;
        
+       Object();
        Object(ITEM_ID id, bool qo, const char *pd);
        ~Object();
        
+       void reloadTexture(void);
+       
        void interact(void);
+       
+       char *save(void);
+       void load(char *);
 };
 #endif // ENTITIES_H
 
index f34abd6c690d7a4194480845671a213eb9ef20db..eb653a9e388a0b462a5b8a1d7f5156828b4fa2c6 100644 (file)
@@ -123,6 +123,8 @@ Structures::Structures(){ //sets the structure type
        
        inWorld = NULL;
        name = NULL;
+       
+       inv = NULL;
 }
 Structures::~Structures(){
        delete tex;
@@ -171,6 +173,19 @@ Mob::~Mob(){
        delete[] name;
 }
 
+Object::Object(){
+       type = OBJECTT;
+       alive = true;
+       near = false;
+       width  = 0;
+       height = 0;
+
+       maxHealth = health = 1;
+       
+       tex = NULL;
+       inv = NULL;
+}
+
 Object::Object(ITEM_ID id, bool qo, const char *pd){
        identifier = id;
        questObject = qo;
@@ -187,6 +202,7 @@ Object::Object(ITEM_ID id, bool qo, const char *pd){
 
        maxHealth = health = 1;
        tex = new Texturec(1,getItemTexturePath(id));
+       inv = NULL;
 }
 Object::~Object(){
        delete[] pickupDialog;
@@ -194,6 +210,15 @@ Object::~Object(){
        delete[] name;
 }
 
+void Object::reloadTexture(void){
+       if(tex)
+               delete tex;
+               
+       tex = new Texturec(1,getItemTexturePath(identifier));
+       width  = getItemWidth(identifier);
+       height = getItemHeight(identifier);
+}
+
 void Entity::draw(void){               //draws the entities
        glPushMatrix();
        glColor3ub(255,255,255);
@@ -492,8 +517,12 @@ void Mob::wander(int timeRun){
 }
 
 char *Entity::baseSave(void){
-       static EntitySavePacket *esp = new EntitySavePacket();
-       memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket));
+       static EntitySavePacket *esp;
+       esp = new EntitySavePacket();
+       if(inv)
+               memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket));
+       else
+               memset(&esp->isp,0,sizeof(InventorySavePacket));
        esp->loc = loc;
        esp->vel = vel;
        esp->width = width;
@@ -513,14 +542,20 @@ char *Entity::baseSave(void){
        esp->hit = hit;
        esp->type = type;
        esp->gender = gender;
-       esp->nameSize = strlen(name) + 1;
+       if(name){
+               esp->nameSize = strlen(name) + 1;
+               strncpy(esp->name,name,32);
+       }else{
+               esp->nameSize = 0;
+               strcpy(esp->name,"\0");
+       }
        return (char *)esp;
 }
 
 void Entity::baseLoad(char *e){
-       const char *tmpname = "GG\0";
        EntitySavePacket *esp = (EntitySavePacket *)e;
-       inv->load(&esp->isp);
+       if(esp->nameSize > 1)
+               inv->load(&esp->isp);
        loc = esp->loc;
        vel = esp->vel;
        width = esp->width;
@@ -540,23 +575,101 @@ void Entity::baseLoad(char *e){
        hit = esp->hit;
        type = esp->type;
        gender = esp->gender;
-       name = new char[esp->nameSize+1];
-       strcpy(name,tmpname);
+       if(esp->nameSize){
+               name = new char[esp->nameSize+1];
+               strcpy(name,esp->name);
+       }else{
+               name = new char[4];
+               strncpy(name,"\0\0\0\0",4);
+       }
 }
 
 char *NPC::save(unsigned int *size){
-       static char *buf = new char[(*size = sizeof(EntitySavePacket) + aiFunc.size() * sizeof(int(*)(NPC *)))],*esp;
+       static char *buf,*esp;
+       buf = new char[(*size = sizeof(EntitySavePacket) /*+ aiFunc.size() * sizeof(int(*)(NPC *))*/)];
        memcpy(buf,(esp = baseSave()),sizeof(EntitySavePacket));
        delete[] esp;
-       memcpy(buf+sizeof(EntitySavePacket),aiFunc.data(),aiFunc.size() * sizeof(int(*)(NPC *)));
+       //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
-       }
+void NPC::load(unsigned int size,char *b){
+       //unsigned int size2,i;
+       //int (*func)(NPC *);
+       baseLoad(b);
+       size--;
+       /*if(size > sizeof(EntitySavePacket)){
+               size2 = (size - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *));
+               std::cout<<size<<" "<<sizeof(EntitySavePacket)<<" "<<sizeof(int(*)(NPC *))<<" = "<<size2<<std::endl;
+               aiFunc.reserve(size2);
+               if(aiFunc.max_size() < size2){
+                       std::cout<<"what"<<std::endl;
+                       abort();
+               }
+               for(i=0;i<size2;i++){
+                       
+                       aiFunc.push_back(
+               }
+               memcpy(aiFunc.data(),b+sizeof(EntitySavePacket),size2 * sizeof(int(*)(NPC *)));
+               //aiFunc.erase(aiFunc.begin());
+               std::cout<<aiFunc.size()<<std::endl;
+       }*/
+}
+
+char *Structures::save(void){
+       static StructuresSavePacket *ssp;
+       char *esp;
+       ssp = new StructuresSavePacket();
+       esp = baseSave();
+       memcpy(&ssp->esp,esp,sizeof(EntitySavePacket));
+       delete[] esp;
+       ssp->inWorld = inWorld;
+       ssp->inside = inside;
+       ssp->bsubtype = bsubtype;
+       return (char *)ssp;
+}
+
+void Structures::load(char *s){
+       StructuresSavePacket *ssp = (StructuresSavePacket *)s;
+       baseLoad((char *)&ssp->esp);
+       inWorld = ssp->inWorld;
+       inside = ssp->inside;
+       bsubtype = ssp->bsubtype;
+}
+
+char *Object::save(void){
+       static ObjectSavePacket *osp;
+       char *esp;
+       osp = new ObjectSavePacket();
+       memcpy(&osp->esp,(esp = baseSave()),sizeof(EntitySavePacket));
+       delete[] esp;
+       osp->identifier = identifier;
+       osp->questObject = questObject;
+       strncpy(osp->pickupDialog,pickupDialog,256);
+       return (char *)osp;
+}
+
+void Object::load(char *buf){
+       ObjectSavePacket *osp = (ObjectSavePacket *)buf;
+       baseLoad((char *)&osp->esp);
+       identifier = osp->identifier;
+       questObject = osp->questObject;
+       pickupDialog = new char[256];
+       strcpy(pickupDialog,osp->pickupDialog);
+}
+
+char *Mob::save(void){
+       static MobSavePacket *msp;
+       char *esp;
+       msp = new MobSavePacket();
+       memcpy(&msp->esp,(esp = baseSave()),sizeof(MobSavePacket));
+       delete[] esp;
+       msp->init_y = init_y;
+       return (char *)msp;
+}
+
+void Mob::load(char *m){
+       MobSavePacket *msp = (MobSavePacket *)m;
+       baseLoad((char *)&msp->esp);
+       init_y = msp->init_y;
 }
index 94fb954fbf750505ba3b18716240ab5806bd6580..5b9f5097fed86dc26d8825631fd5231c60e106d9 100644 (file)
@@ -122,34 +122,35 @@ 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();
        worldSpawnHill1->setBackground(BG_FOREST);
-       if(!i.fail()){
+       /*if(!i.fail()){
                worldSpawnHill1->load(&i);
                i.close();
-       }else{
+       }else{*/
                worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1);
                worldSpawnHill1->setBGM("assets/music/embark.wav");
-       }
+       //}
        worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock);
+       worldSpawnHill1->addNPC(300,100);
 
        worldSpawnHill2 = new World();
-       worldSpawnHill2->generate(700);
        worldSpawnHill2->setBackground(BG_FOREST);
        worldSpawnHill2->setBGM("assets/music/ozone.wav");
+       worldSpawnHill2->generate(700);
        worldSpawnHill2->addMob(MS_PAGE,-400,0,worldSpawnHill2_infoSprint);
-
+       
        worldSpawnHill3 = new World();
        worldSpawnHill3->generateFunc(1000,gen_worldSpawnHill3);
        worldSpawnHill3->setBackground(BG_FOREST);
        worldSpawnHill3->setBGM("assets/music/ozone.wav");
        
        worldFirstVillage = new World();
-       worldFirstVillage->generate(1000);
        worldFirstVillage->setBackground(BG_FOREST);
        worldFirstVillage->setBGM("assets/music/embark.wav");
+       worldFirstVillage->generate(1000);
        
        worldSpawnHill1->toRight = worldSpawnHill2;
        worldSpawnHill2->toLeft = worldSpawnHill1;
@@ -184,7 +185,7 @@ void initEverything(void){
        player = new Player();
        player->spawn(200,100);
 
-       currentWorld = worldSpawnHill1; 
+       currentWorld = worldSpawnHill1;
        currentWorld->bgmPlay(NULL);
        atexit(destroyEverything);
 }
@@ -193,10 +194,10 @@ extern std::vector<int (*)(NPC *)> AIpreload;
 extern std::vector<NPC *> AIpreaddr;
 
 void destroyEverything(void){
-       static std::ofstream o;
+       /*static std::ofstream o;
        o.open("world.dat",std::ifstream::binary);
-       worldSpawnHill1->save(&o);
-       o.close();
+       worldSpawnHill2->save(&o);
+       o.close();*/
        
        while(!AIpreload.empty())
                AIpreload.pop_back();
index 34e187048f408fd28a12543a2e8de73bbc5aac19..8b09398a606fb41207a613ca9a7c647968813dac 100644 (file)
@@ -66,7 +66,7 @@ void World::save(std::ofstream *o){
        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("GG"              ,2         * sizeof(char));
@@ -75,25 +75,41 @@ void World::save(std::ofstream *o){
        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();
+       o->write((char *)&size,sizeof(unsigned int));
        for(i=0;i<size;i++){
                bufptr = npc[i]->save(&size2);
                o->write((char *)&size2,sizeof(unsigned int));
                o->write(bufptr,size2);
+               delete[] bufptr;
+       }
+       size = build.size();
+       o->write((char *)&size,sizeof(unsigned int));
+       for(i=0;i<size;i++){
+               bufptr = build[i]->save();
+               o->write(bufptr,sizeof(StructuresSavePacket));
+               delete[] bufptr;
+       }
+       size = object.size();
+       o->write((char *)&size,sizeof(unsigned int));
+       for(i=0;i<size;i++){
+               bufptr = object[i]->save();
+               o->write(bufptr,sizeof(ObjectSavePacket));
+               delete[] bufptr;
+       }
+       size = mob.size();
+       o->write((char *)&size,sizeof(unsigned int));
+       for(i=0;i<size;i++){
+               bufptr = mob[i]->save();
+               o->write(bufptr,sizeof(MobSavePacket));
+               delete[] bufptr;
        }
 }
 
 void World::load(std::ifstream *i){
-       //unsigned int size;
+       unsigned int size,size2,j;
        size_t bgms;
-       char sig[2];
+       char sig[2],*buf;
        
        i->read((char *)&lineCount,sizeof(unsigned int));
        
@@ -102,7 +118,7 @@ void World::load(std::ifstream *i){
        
        i->read(sig,2 * sizeof(char));
        if(strncmp(sig,"GG",2)){
-               std::cout<<"world.dat corrupt"<<std::endl;
+               std::cout<<"world.dat corrupt: GG"<<std::endl;
                exit(EXIT_FAILURE);
        }
 
@@ -116,14 +132,55 @@ void World::load(std::ifstream *i){
        i->read(bgm,bgms);
        setBGM(bgm);
 
-       
        i->read(sig,2 * sizeof(char));
        if(strncmp(sig,"NO",2)){
-               std::cout<<"world.dat corrupt"<<std::endl;
+               std::cout<<"world.dat corrupt: NO"<<std::endl;
                exit(EXIT_FAILURE);
        }
        
+       i->read((char *)&size,sizeof(unsigned int));
+       for(j=0;j<size;j++){
+               i->read((char *)&size2,sizeof(unsigned int));
+               buf = new char[size2];
+               i->read(buf,size2);
+               npc.push_back(new NPC());
+               npc.back()->load(size2,buf);
+               entity.push_back(npc.back());   
+               delete[] buf;
+       }
        
+       static StructuresSavePacket *ssp;
+       ssp = new StructuresSavePacket();
+       i->read((char *)&size,sizeof(unsigned int));
+       for(j=0;j<size;j++){
+               i->read((char *)ssp,sizeof(StructuresSavePacket));
+               build.push_back(new Structures());
+               build.back()->load((char *)ssp);
+               entity.push_back(build.back());
+       }
+       delete ssp;
+       
+       static ObjectSavePacket *osp;
+       osp = new ObjectSavePacket();
+       i->read((char *)&size,sizeof(unsigned int));
+       for(j=0;j<size;j++){
+               i->read((char *)osp,sizeof(ObjectSavePacket));
+               object.push_back(new Object());
+               object.back()->load((char *)osp);
+               object.back()->reloadTexture();
+               entity.push_back(object.back());
+       }
+       delete osp;
+       
+       static MobSavePacket *msp;
+       msp = new MobSavePacket();
+       i->read((char *)&size,sizeof(unsigned int));
+       for(j=0;j<size;j++){
+               i->read((char *)msp,sizeof(MobSavePacket));
+               mob.push_back(new Mob(0));
+               mob.back()->load((char *)msp);
+               entity.push_back(mob.back());
+       }
 }
 
 World::World(void){