From 0b9561febb7677de8792ba0feb056139ba7c94ea Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 18 Dec 2015 08:45:29 -0500 Subject: pages --- src/entities.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/entities.cpp') diff --git a/src/entities.cpp b/src/entities.cpp index c307a31..23a4ae6 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -1,7 +1,7 @@ #include #include -#include +//#include extern FILE* names; extern unsigned int loops; @@ -153,15 +153,22 @@ Mob::Mob(int sub){ width = HLINE * 8; height = HLINE * 8; tex = new Texturec(1, "assets/robin.png"); + break; case MS_TRIGGER: width = HLINE * 20; height = 2000; tex = new Texturec(0); + break; case MS_DOOR: width = HLINE * 12; height = HLINE * 19; tex = new Texturec(1,"assets/door.png"); break; + case MS_PAGE: + width = HLINE * 6; + height = HLINE * 4; + tex = new Texturec(1,"assets/items/ITEM_PAGE.png"); + break; } inv = new Inventory(NPC_INV_SIZE); @@ -248,6 +255,7 @@ void Entity::draw(void){ //draws the entities break; case MS_BIRD: case MS_DOOR: + case MS_PAGE: default: tex->bind(0); break; @@ -471,7 +479,17 @@ void Mob::wander(int timeRun){ } break; - case MS_DOOR: + case MS_PAGE: + if(ui::mouse.x > loc.x && + ui::mouse.x < loc.x + width && + SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)){ + if(speed != 666){ + speed = 666; + hey(this); + speed = 0; + } + } + break; default: break; } -- cgit v1.2.3 From 272a152b54a198a84f122ab8bedb1019708b7008 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 21 Dec 2015 08:46:35 -0500 Subject: pages, quests --- Changelog | 8 ++++++++ assets/items/ITEM_PAGE.png | Bin 331 -> 349 bytes include/common.h | 17 +++++++++++++++++ include/world.h | 6 ++++++ main.cpp | 12 ++++++++---- src/common.cpp | 17 +++++++++++++++++ src/entities.cpp | 44 ++++++++++++++++++++------------------------ src/gameplay.cpp | 37 ++++++++++++++++++++----------------- src/ui.cpp | 15 ++++++++------- src/world.cpp | 8 ++++++++ xcf/page.xcf | Bin 1059 -> 1059 bytes 11 files changed, 112 insertions(+), 52 deletions(-) (limited to 'src/entities.cpp') diff --git a/Changelog b/Changelog index 44ff8a4..c0d2b9e 100644 --- a/Changelog +++ b/Changelog @@ -452,3 +452,11 @@ - began working on pages, made sprite and handler - GLSL shaders are better - redid theme_jazz + +12/21/2015: +=========== + + - fixed dialog options issues, finished basic pages + - added World::getAvailableNPC() for easy quest assigner assigning + - added the Condition class, so that events and actions can be remembered by NPCs + - added functionality for multiple lights (GLSL) diff --git a/assets/items/ITEM_PAGE.png b/assets/items/ITEM_PAGE.png index ac132c6..4602afe 100644 Binary files a/assets/items/ITEM_PAGE.png and b/assets/items/ITEM_PAGE.png differ diff --git a/include/common.h b/include/common.h index 08ec73f..038bf42 100644 --- a/include/common.h +++ b/include/common.h @@ -141,6 +141,23 @@ extern vec2 offset; */ extern unsigned int loops; +/** + * This class contains a string for identification and a value. It can be used to + * save certain events for and decisions so that they can be recalled later. + */ + +class Condition { +private: + char *id; + void *value; +public: + Condition(const char *_id,void *val); + ~Condition(); + + bool sameID(const char *s); + void *getValue(void); +}; + /** * Prints a formatted debug message to the console, along with the callee's file and line * number. diff --git a/include/world.h b/include/world.h index 4b3a891..98ca54a 100644 --- a/include/world.h +++ b/include/world.h @@ -173,6 +173,12 @@ public: void addNPC(float x,float y); void addObject(ITEM_ID, bool, const char *, float, float); void addParticle(float, float, float, float, float, float, Color color, int); + + NPC *getAvailableNPC(void); + + /* + * Update coordinates of all entities. + */ void update(Player *p,unsigned int delta); diff --git a/main.cpp b/main.cpp index e5aba88..e6ff2eb 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,8 @@ The main game loop contains all of the global variables the game uses, and it runs the main game loop, the render loop, and the logic loop that control all of the entities. */ -#include // fopen +#include +#include #include #include @@ -123,7 +124,7 @@ Mix_Chunk *crickets; * referenced in src/entities.cpp for getting random names. */ -FILE *names; +std::istream *names; /* * loops is used for texture animation. It is believed to be passed to entity @@ -387,7 +388,10 @@ int main(/*int argc, char *argv[]*/){ * */ - names = fopen("assets/names_en-us", "r+"); + static std::filebuf fb; + fb.open("assets/names_en-us",std::ios::in); + names = new std::istream(&fb); + crickets=Mix_LoadWAV("assets/sounds/crickets.wav"); //Mix_Volume(2,25); @@ -427,7 +431,7 @@ int main(/*int argc, char *argv[]*/){ Mix_HaltMusic(); - fclose(names); + fb.close(); SDL_GL_DeleteContext(mainGLContext); SDL_DestroyWindow(window); diff --git a/src/common.cpp b/src/common.cpp index 7449a35..dbcef0b 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -11,6 +12,22 @@ unsigned int millis(void){ #endif // __WIN32__ +Condition::Condition(const char *_id,void *val){ + id = new char[strlen(_id)+1]; + strcpy(id,_id); + value = val; +} +Condition::~Condition(){ + delete[] id; +} + +bool Condition::sameID(const char *s){ + return !strcmp(id,s); +} +void *Condition::getValue(void){ + return value; +} + void DEBUG_prints(const char* file, int line, const char *s,...){ va_list args; printf("%s:%d: ",file,line); diff --git a/src/entities.cpp b/src/entities.cpp index 23a4ae6..3849040 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -1,9 +1,10 @@ #include #include +#include //#include -extern FILE* names; +extern std::istream *names; extern unsigned int loops; extern World *currentWorld; @@ -12,39 +13,30 @@ extern Player *player; extern const char *itemName; -extern - void getRandomName(Entity *e){ - int tempNum,max=0; + unsigned int tempNum,max=0; char *bufs; - rewind(names); + names->seekg(0,names->beg); - bufs = new char[16]; //(char *)malloc(16); + bufs = new char[32]; - for(;!feof(names);max++){ - fgets(bufs,16,(FILE*)names); - } + for(;!names->eof();max++) + names->getline(bufs,32); tempNum = rand() % max; - rewind(names); + names->seekg(0,names->beg); - for(int i=0;igetline(bufs,32); - switch(fgetc(names)){ + switch(bufs[0]){ + default : case 'm': e->gender = MALE; break; case 'f': e->gender = FEMALE;break; - default : break; } - if((fgets(bufs,16,(FILE*)names)) != NULL){ - bufs[strlen(bufs)] = '\0'; - strcpy(e->name,bufs); - if(e->name[strlen(e->name)-1] == '\n') - e->name[strlen(e->name)-1] = '\0'; - } + strcpy(e->name,bufs+1); delete[] bufs; } @@ -73,7 +65,7 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o } } - name = new char[16]; + name = new char[32]; getRandomName(this); } @@ -480,8 +472,12 @@ void Mob::wander(int timeRun){ } break; case MS_PAGE: - if(ui::mouse.x > loc.x && - ui::mouse.x < loc.x + width && + if(player->loc.x > loc.x - 100 && + player->loc.x < loc.x + 100 && + ui::mouse.x > loc.x && + ui::mouse.x < loc.x + width && + ui::mouse.y > loc.y - width / 2 && + ui::mouse.y < loc.y + width * 1.5 && SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)){ if(speed != 666){ speed = 666; diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 1670929..7dbe98e 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -28,6 +28,10 @@ void story(Mob *callee){ callee->alive = false; } +/* + * Gens + */ + float gen_worldSpawnHill1(float x){ return (float)(pow(2,(-x+200)/5) + GEN_MIN); } @@ -42,7 +46,6 @@ float gen_worldSpawnHill3(float x){ */ void worldSpawnHill1_hillBlock(Mob *callee){ - std::cout<<"oi"; player->vel.x = 0; player->loc.x = callee->loc.x + callee->width; ui::dialogBox(player->name,NULL,false,"This hill seems to steep to climb up..."); @@ -52,7 +55,7 @@ void worldSpawnHill1_hillBlock(Mob *callee){ static Arena *a; void worldSpawnHill2_infoSprint(Mob *callee){ - ui::dialogBox(player->name,":Nah:Sure",false,"This page would like to take you somewhere."); + ui::dialogBox(player->name,":Sure:Nah",false,"This page would like to take you somewhere."); ui::waitForDialog(); switch(ui::dialogOptChosen){ case 1: @@ -75,19 +78,22 @@ void worldSpawnHill2_infoSprint(Mob *callee){ //ui::dialogBox("B-) ",NULL,true,"Press \'Shift\' to run!"); } -void worldSpawnHill3_itemGet(Mob *callee){ - ui::dialogBox("B-) ",NULL,true,"Right click to pick up items!"); - callee->alive = false; -} - -void worldSpawnHill3_itemSee(Mob *callee){ - ui::dialogBox("B-) ",NULL,true,"Press \'e\' to open your inventory!"); - callee->alive = false; +int worldSpawnHill2_Quest2(NPC *callee){ + ui::dialogBox(callee->name,NULL,false,"Yo."); + ui::waitForDialog(); + return 0; } -void worldSpawnHill3_leave(Mob *callee){ - ui::dialogBox("B-) ",NULL,true,"Now jump in this hole, and let your journey begin :)"); - callee->alive = false; +int worldSpawnHill2_Quest1(NPC *callee){ + ui::dialogBox(callee->name,":Cool.",false,"Did you know that I\'m the coolest NPC in the world?"); + ui::waitForDialog(); + if(ui::dialogOptChosen == 1){ + ui::dialogBox(callee->name,NULL,false,"Yeah, it is."); + currentWorld->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest2,true); + ui::waitForDialog(); + return 0; + } + return 1; } /* @@ -132,10 +138,6 @@ void initEverything(void){ worldSpawnHill3->generateFunc(1000,gen_worldSpawnHill3); worldSpawnHill3->setBackground(BG_FOREST); worldSpawnHill3->setBGM("assets/music/ozone.wav"); - worldSpawnHill3->addMob(MS_TRIGGER,-500,0,worldSpawnHill3_itemGet); - worldSpawnHill3->addMob(MS_TRIGGER,0,0,worldSpawnHill3_itemSee); - worldSpawnHill3->addObject(TEST_ITEM,false,"",-200,300); - worldSpawnHill3->addMob(MS_TRIGGER,650,0,worldSpawnHill3_leave); worldSpawnHill3->addHole(800,1000); worldSpawnHill1->toRight = worldSpawnHill2; @@ -162,6 +164,7 @@ void initEverything(void){ worldSpawnHill2_Building1->setBGM("assets/music/theme_jazz.wav"); worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill2_Building1); + worldSpawnHill2->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest1,false); player = new Player(); player->spawn(200,100); diff --git a/src/ui.cpp b/src/ui.cpp index 6069ef9..f73f48a 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -300,7 +300,8 @@ namespace ui { } }while(s[++i]); - return putString(x-width/2,y,s); + putString(x-width/2,y,s); + return width; } /* @@ -487,7 +488,7 @@ namespace ui { } void draw(void){ unsigned char i; - float x,y; + float x,y,tmp; char *rtext; if(dialogBoxExists){ @@ -524,12 +525,12 @@ namespace ui { for(i=0;i dialogOptLoc[i][0] && - mouse.x < dialogOptLoc[i][0] + dialogOptLoc[i][2] && + mouse.x < dialogOptLoc[i][2] && mouse.y > dialogOptLoc[i][1] && mouse.y < dialogOptLoc[i][1] + 16 ){ // fontSize setFontColor(255,255,0); diff --git a/src/world.cpp b/src/world.cpp index 7f9b1c0..5663086 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -980,6 +980,14 @@ void World::addLayer(unsigned int width){ behind->bgTex=bgTex; } +NPC *World::getAvailableNPC(void){ + for(auto &n : npc){ + if(n->aiFunc.empty()) + return n; + } + return (NPC *)NULL; +} + World *World::goWorldLeft(Player *p){ if(toLeft&&p->loc.xloc.x=toLeft->x_start+getWidth(toLeft)-HLINE*10; diff --git a/xcf/page.xcf b/xcf/page.xcf index bf39c18..72a34cf 100644 Binary files a/xcf/page.xcf and b/xcf/page.xcf differ -- cgit v1.2.3 From 65addfa212a2aef2f2d6de3cb49edc99a8f02f59 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 4 Jan 2016 08:48:27 -0500 Subject: save/load tests --- Changelog | 16 ++++++++++++++++ Goals.txt | 4 ++-- include/ui.h | 2 ++ include/world.h | 10 +++++++--- main.cpp | 1 + src/entities.cpp | 4 ++-- src/gameplay.cpp | 25 ++++++++++++++++++++++--- src/ui.cpp | 19 ++++++++++++++----- src/world.cpp | 37 +++++++++++++++++++++++++++++-------- 9 files changed, 95 insertions(+), 23 deletions(-) (limited to 'src/entities.cpp') diff --git a/Changelog b/Changelog index c0d2b9e..e3ed5e0 100644 --- a/Changelog +++ b/Changelog @@ -460,3 +460,19 @@ - added World::getAvailableNPC() for easy quest assigner assigning - added the Condition class, so that events and actions can be remembered by NPCs - added functionality for multiple lights (GLSL) + +12/22/2015: +=========== + + - 3 month Changelog anniversary! + - fixed dialog quitting bug + - worked on village spawning + - worked on wrapping text for dialog boxes + - did more work on GLSL shaders + +1/3/2015: +========= + + - finished wrapping text for dialog boxes + - began working on world saving/loading again + - got some mad GLSL shaders running diff --git a/Goals.txt b/Goals.txt index 140cdd3..4848bac 100644 --- a/Goals.txt +++ b/Goals.txt @@ -35,7 +35,7 @@ End of December: - create first 'chapters' of story - create very first areas in the game (code it) to get an idea of what's gonna go down - + January - March: =============== @@ -45,7 +45,7 @@ January - March: February - End of March: ======================== - - design sound effects / background music? + - design or focus on sound effects / background music? March-ish: ====== diff --git a/include/ui.h b/include/ui.h index 6a81dab..b769bbf 100644 --- a/include/ui.h +++ b/include/ui.h @@ -33,6 +33,8 @@ namespace ui { extern unsigned char dialogOptChosen; extern bool dialogImportant; + extern unsigned int textWrapLimit; + /* * Initializes the FreeType system. */ diff --git a/include/world.h b/include/world.h index 98ca54a..cdcea3c 100644 --- a/include/world.h +++ b/include/world.h @@ -8,6 +8,9 @@ #ifndef WORLD_H #define WORLD_H +#include +#include + #include #include @@ -128,6 +131,7 @@ protected: */ Texturec *bgTex; + WORLD_BG_TYPE bgType; /** * The Mix_Music object that holds the background soundtrack for the world. @@ -167,7 +171,7 @@ public: std::vector particles; void addStructure(_TYPE t,BUILD_SUB sub,float x,float y,World *inside); - void addVillage(int bCount, int npcMin, int npcMax,_TYPE t,float x,float y,World *outside); + void addVillage(int bCount, int npcMin, int npcMax,_TYPE t,World *inside); void addMob(int t,float x,float y); void addMob(int t,float x,float y,void (*hey)(Mob *)); void addNPC(float x,float y); @@ -262,8 +266,8 @@ public: int getTheWidth(void); - void save(FILE *); - void load(FILE *); + void save(std::ofstream *); + void load(std::ifstream *); }; /* diff --git a/main.cpp b/main.cpp index e6ff2eb..2c088cb 100644 --- a/main.cpp +++ b/main.cpp @@ -480,6 +480,7 @@ void mainLoop(void){ if(prev != currentWorld){ currentWorld->bgmPlay(prev); + ui::dialogBoxExists = false; } if(prevPrevTime + MSEC_PER_TICK <= currentTime){ diff --git a/src/entities.cpp b/src/entities.cpp index 3849040..d424aba 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -101,7 +101,7 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation tex = new Texturec(1,"assets/NPC.png"); inv = new Inventory(NPC_INV_SIZE); - randDialog = rand() % 12 - 1; + randDialog = 6;//rand() % 12 - 1; } NPC::~NPC(){ while(!aiFunc.empty()){ @@ -341,7 +341,7 @@ const char *randomDialog[] = { "How much wood could a woodchuck chuck if a woodchuck could chuck wood?", "I don\'t think anyone has ever been able to climb up that hill.", "If you ever see a hole in the ground, watch out; it could mean the end for you.", - "Did you know this game has over 4000 lines of code? I didn\'t. I didn't even know I was in a game until now...", + "Did you know this game has over 5000 lines of code? I didn\'t. I didn't even know I was in a game until now...", "HELP MY CAPS LOCK IS STUCK", "You know, if anyone ever asked me who I wanted to be when I grow up, I would say Abby Ross.", "I want to have the wallpaper in our house changed. It doesn\'t really fit the environment.", diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 7dbe98e..ff77728 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -115,15 +115,21 @@ static World *worldSpawnHill3; static IndoorWorld *worldSpawnHill2_Building1; +static World *worldFirstVillage; /* * initEverything() start */ void destroyEverything(void); void initEverything(void){ - + //static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary); + worldSpawnHill1 = new World(); - worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1); + /*if(!i.fail()){ + worldSpawnHill1->load(&i); + i.close(); + }else*/ + worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1); worldSpawnHill1->setBackground(BG_FOREST); worldSpawnHill1->setBGM("assets/music/embark.wav"); worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock); @@ -138,12 +144,18 @@ void initEverything(void){ worldSpawnHill3->generateFunc(1000,gen_worldSpawnHill3); worldSpawnHill3->setBackground(BG_FOREST); worldSpawnHill3->setBGM("assets/music/ozone.wav"); - worldSpawnHill3->addHole(800,1000); + + worldFirstVillage = new World(); + worldFirstVillage->generate(1000); + worldFirstVillage->setBackground(BG_FOREST); + worldFirstVillage->setBGM("assets/music/embark.wav"); worldSpawnHill1->toRight = worldSpawnHill2; worldSpawnHill2->toLeft = worldSpawnHill1; worldSpawnHill2->toRight = worldSpawnHill3; worldSpawnHill3->toLeft = worldSpawnHill2; + worldSpawnHill3->toRight = worldFirstVillage; + worldFirstVillage->toLeft = worldSpawnHill3; /* * Spawn some entities. @@ -166,6 +178,8 @@ void initEverything(void){ worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill2_Building1); worldSpawnHill2->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest1,false); + worldFirstVillage->addVillage(5,0,0,STRUCTURET,worldSpawnHill2_Building1); + player = new Player(); player->spawn(200,100); @@ -178,6 +192,11 @@ extern std::vector AIpreload; extern std::vector AIpreaddr; void destroyEverything(void){ + static std::ofstream o; + o.open("world.dat",std::ifstream::binary); + worldSpawnHill1->save(&o); + o.close(); + while(!AIpreload.empty()) AIpreload.pop_back(); while(!AIpreaddr.empty()) diff --git a/src/ui.cpp b/src/ui.cpp index f73f48a..b827b43 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -95,6 +95,8 @@ namespace ui { bool dialogImportant = false; unsigned char dialogOptChosen = 0; + unsigned int textWrapLimit = 110; + /* * Current font size. Changing this WILL NOT change the font size, see setFontSize() for * actual font size changing. @@ -266,7 +268,13 @@ namespace ui { */ do{ - if(s[i]=='\n'){ // Handle newlines + if(i && ((i / 110.0) == (i / 110))){ + yo-=fontSize*1.05; + xo=x; + if(s[i] == ' ') + i++; + } + if(s[i] == '\n'){ yo-=fontSize*1.05; xo=x; }else if(s[i]==' '){ // Handle spaces @@ -308,12 +316,13 @@ namespace ui { * Draw a string in a typewriter-esque fashion. Each letter is rendered as calls are made * to this function. Passing a different string to the function will reset the counters. */ - + + static char *ret = NULL; char *typeOut(char *str){ static unsigned int sinc, // Acts as a delayer for the space between each character. linc=0, // Contains the number of letters that should be drawn. size=0; // Contains the full size of the current string. - static char *ret = NULL; + //static char *ret = NULL; /* * Create a well-sized buffer if we haven't yet. @@ -453,6 +462,8 @@ namespace ui { dialogBoxExists = true; dialogImportant = false; + if(ret) + ret[0] = '\0'; } void waitForDialog(void){ do{ @@ -667,7 +678,6 @@ DONE: memcpy(&player->loc,&tmppos,sizeof(vec2)); currentWorld = tmp; toggleBlackFast(); - dialogBoxExists = false; } } break; @@ -689,7 +699,6 @@ DONE: memcpy(&player->loc,&tmppos,sizeof(vec2)); currentWorld = tmp; toggleBlackFast(); - dialogBoxExists = false; } } break; diff --git a/src/world.cpp b/src/world.cpp index 5663086..2afbdf5 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -50,6 +50,7 @@ float worldGetYBase(World *w){ } void World::setBackground(WORLD_BG_TYPE bgt){ + bgType = bgt; switch(bgt){ case BG_FOREST: bgTex = new Texturec(7,bgPaths[0]); @@ -60,12 +61,28 @@ void World::setBackground(WORLD_BG_TYPE bgt){ } } -void World::save(FILE *s){ - fclose(s); +void World::save(std::ofstream *o){ + o->write((char *)&lineCount, sizeof(unsigned int)); + o->write((char *)&line ,lineCount * sizeof(struct line_t)); + o->write("GG" ,2 * sizeof(char)); + o->write((char *)&star ,100 * sizeof(vec2)); } -void World::load(FILE *s){ - fclose(s); +void World::load(std::ifstream *i){ + static char end[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(end ,2 * sizeof(char)); + if(strncmp(end,"GG",2)){ + std::cout<<"world.dat corrupt"<read((char *)&star,100 * sizeof(vec2)); + + x_start = 0 - getWidth(this) / 2; } World::World(void){ @@ -913,16 +930,20 @@ void World::addStructure(_TYPE t,BUILD_SUB sub, float x,float y,World *inside){ entity.push_back(build.back()); } -void World::addVillage(int bCount, int npcMin, int npcMax,_TYPE t,float x,float y,World *outside){ +void World::addVillage(int bCount, int npcMin, int npcMax,_TYPE t,World *inside){ std::cout << npcMin << ", " << npcMax << std::endl; - int xwasd; + //int xwasd; for(int i = 0; i < bCount; i++){ - xwasd = (rand()%(int)x+1000*HLINE); + addStructure(t,HOUSE,x_start + (i * 300),100,inside); + /*std::cout<<"1\n"; HERE: + xwasd = (rand()%(int)x+1000*HLINE); for(auto &bu : build){ if(xwasd > bu->loc.x && xwasd < bu->loc.x+bu->width)goto HERE; } - addStructure(t,HOUSE,xwasd,y,outside); + std::cout<<"2\n"; + addStructure(t,HOUSE,xwasd,y,inside); + std::cout<<"3\n";*/ } } void World::addMob(int t,float x,float y){ -- cgit v1.2.3 From 45edad31559852d306d59b50f380cb79c9f27dcc Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 5 Jan 2016 08:48:29 -0500 Subject: save/load stuffs --- Changelog | 6 +++++ include/entities.h | 35 +++++++++++++++++++++++++++ include/inventory.h | 17 +++++++++++++ src/entities.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gameplay.cpp | 11 +++++---- src/world.cpp | 57 +++++++++++++++++++++++++++++++++++++------ 6 files changed, 183 insertions(+), 13 deletions(-) (limited to 'src/entities.cpp') 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::vectoraiFunc; @@ -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;isetBackground(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; + std::vector build; + std::vector mob; + std::vector entity; + std::vector object;*/ + + size = npc.size(); + for(i=0;isave(&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"<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"<