From cdd792bc8ba7337e7bd3a9d0b4141c63f17fb9a8 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 7 Jan 2016 08:35:51 -0500 Subject: world save/load done? --- src/entities.cpp | 145 +++++++++++++++++++++++++++++++++++++++++++++++++------ src/gameplay.cpp | 23 ++++----- src/world.cpp | 83 ++++++++++++++++++++++++++----- 3 files changed, 211 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/entities.cpp b/src/entities.cpp index f34abd6..eb653a9 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -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 sizeof(EntitySavePacket)){ + size2 = (size - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *)); + std::cout<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; } diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 94fb954..5b9f509 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -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 AIpreload; extern std::vector 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(); diff --git a/src/world.cpp b/src/world.cpp index 34e1870..8b09398 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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; - std::vector build; - std::vector mob; - std::vector entity; - std::vector object;*/ - size = npc.size(); + o->write((char *)&size,sizeof(unsigned int)); for(i=0;isave(&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;isave(); + o->write(bufptr,sizeof(StructuresSavePacket)); + delete[] bufptr; + } + size = object.size(); + o->write((char *)&size,sizeof(unsigned int)); + for(i=0;isave(); + o->write(bufptr,sizeof(ObjectSavePacket)); + delete[] bufptr; + } + size = mob.size(); + o->write((char *)&size,sizeof(unsigned int)); + for(i=0;isave(); + 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"<read(bgm,bgms); setBGM(bgm); - i->read(sig,2 * sizeof(char)); if(strncmp(sig,"NO",2)){ - std::cout<<"world.dat corrupt"<read((char *)&size,sizeof(unsigned int)); + for(j=0;jread((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;jread((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;jread((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;jread((char *)msp,sizeof(MobSavePacket)); + mob.push_back(new Mob(0)); + mob.back()->load((char *)msp); + entity.push_back(mob.back()); + } } World::World(void){ -- cgit v1.2.3