diff options
author | drumsetmonkey <abelleisle@roadrunner.com> | 2016-01-07 08:48:03 -0500 |
---|---|---|
committer | drumsetmonkey <abelleisle@roadrunner.com> | 2016-01-07 08:48:03 -0500 |
commit | 42bd0ae7913af00ea5e330e45321e2bbe9e734d9 (patch) | |
tree | 0e420156d2cb71607a1df0aa541a02b0db35a6cb /src | |
parent | a85accc62dab6d3edc4fb4ffb991ce4c537394f6 (diff) | |
parent | 81cf23371b94baf608c98d0dd248acfbbd5d2c70 (diff) |
Merge branch 'master' of http://github.com/tcsullivan/gamedev
Diffstat (limited to 'src')
-rw-r--r-- | src/entities.cpp | 145 | ||||
-rw-r--r-- | src/gameplay.cpp | 27 | ||||
-rw-r--r-- | src/world.cpp | 83 |
3 files changed, 213 insertions, 42 deletions
diff --git a/src/entities.cpp b/src/entities.cpp index 7e8d55a..57a20ef 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -124,6 +124,8 @@ Structures::Structures(){ //sets the structure type inWorld = NULL; name = NULL; + + inv = NULL; } Structures::~Structures(){ delete tex; @@ -172,6 +174,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; @@ -188,6 +203,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; @@ -195,6 +211,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); @@ -514,8 +539,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; @@ -535,14 +564,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; @@ -562,23 +597,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; } diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 431d2cf..e8436e7 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()){ - // worldSpawnHill1->load(&i); - // i.close(); - // }else{ + /*if(!i.fail()){ + worldSpawnHill1->load(&i); + i.close(); + }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; @@ -186,7 +187,7 @@ void initEverything(void){ player = new Player(); player->spawn(200,100); - currentWorld = worldSpawnHill1; + currentWorld = worldSpawnHill1; currentWorld->bgmPlay(NULL); atexit(destroyEverything); } @@ -195,10 +196,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(); diff --git a/src/world.cpp b/src/world.cpp index 6fc84f8..a52e3f3 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -68,7 +68,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)); @@ -77,25 +77,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)); @@ -104,7 +120,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); } @@ -118,14 +134,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){ |