From 77f6086bedaa0cbd945fb727a9e82a4c93e6255c Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 21 Jan 2016 08:20:27 -0500 Subject: save/load positions --- include/entities.h | 1 + main.cpp | 4 ++-- src/common.cpp | 3 ++- src/entities.cpp | 4 ++++ src/gameplay.cpp | 9 ++++++--- src/ui.cpp | 5 ++++- src/world.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 7 files changed, 61 insertions(+), 16 deletions(-) diff --git a/include/entities.h b/include/entities.h index fa98285..1fcb896 100644 --- a/include/entities.h +++ b/include/entities.h @@ -190,6 +190,7 @@ public: ~NPC(); void addAIFunc(int (*func)(NPC *),bool preload); + void clearAIFunc(void); void interact(); void wander(int); }; diff --git a/main.cpp b/main.cpp index cb5a20a..1df2c00 100644 --- a/main.cpp +++ b/main.cpp @@ -110,7 +110,7 @@ extern bool worldInside; * referenced somewhere. */ -unsigned int tickCount = 0; +unsigned int tickCount = DAY_CYCLE; unsigned int deltaTime = 0; /* @@ -340,7 +340,7 @@ int main(/*int argc, char *argv[]*/){ const GLchar *shaderSource = readFile("test.frag"); - GLint bufferln = 0; + GLint bufferln = GL_FALSE; int logLength; diff --git a/src/common.cpp b/src/common.cpp index 334671e..faa7012 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -104,9 +104,10 @@ const char *readFile(const char *path){ } in.seekg(0,in.end); - buf = new GLchar[(size = in.tellg())]; + buf = new GLchar[(size = in.tellg()) + 1]; in.seekg(0,in.beg); in.read(buf,size); + buf[size] = '\0'; in.close(); return buf; diff --git a/src/entities.cpp b/src/entities.cpp index 65085ae..0507376 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -393,6 +393,10 @@ void NPC::addAIFunc(int (*func)(NPC *),bool preload){ else aiFunc.push_back(func); } +void NPC::clearAIFunc(void){ + aiFunc.clear(); +} + const char *randomDialog[] = { "What a beautiful day it is.", "Have you ever went fast? I have.", diff --git a/src/gameplay.cpp b/src/gameplay.cpp index b8e51dc..7730cf6 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -145,14 +145,17 @@ int commonAIFunc(NPC *speaker){ speaker->dialogIndex = idx; if(exml->QueryBoolAttribute("stop",&stop) == XML_NO_ERROR && stop){ - speaker->dialogIndex = -1; + speaker->dialogIndex = 9999; return 0; }else if(exml->QueryBoolAttribute("pause",&stop) == XML_NO_ERROR && stop){ - speaker->dialogIndex = -1; + speaker->dialogIndex = 9999; return 1; }else return commonAIFunc(speaker); + }else{ + speaker->dialogIndex = 9999; + return 0; } - return 0; + return 1; } } diff --git a/src/ui.cpp b/src/ui.cpp index 76e2777..49acd4f 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -730,7 +730,10 @@ DONE: toggleBlackFast(); } }else{*/ - currentWorld=currentWorld->goInsideStructure(player); + if((tmp = currentWorld->goInsideStructure(player)) != currentWorld){ + delete currentWorld; + currentWorld = tmp; + } //}*/ break; case SDLK_i: diff --git a/src/world.cpp b/src/world.cpp index ed1b522..af960d5 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1154,17 +1154,29 @@ int World::getTheWidth(void){ } void World::save(void){ - static std::string data; + std::string data; std::string save = (std::string)currentXML + ".dat"; std::ofstream out (save,std::ios::out | std::ios::binary); std::cout<<"Saving to "<dialogIndex) + "\n"); - + data.append(std::to_string((int)n->loc.x) + "\n"); + data.append(std::to_string((int)n->loc.y) + "\n"); + } + + for(auto &b : build){ + data.append(std::to_string((int)b->loc.x) + "\n"); + data.append(std::to_string((int)b->loc.y) + "\n"); + } + + for(auto &m : mob){ + data.append(std::to_string((int)m->loc.x) + "\n"); + data.append(std::to_string((int)m->loc.y) + "\n"); + } + data.append("dOnE\0"); out.write(data.c_str(),data.size()); @@ -1184,15 +1196,35 @@ void World::load(void){ for(auto &n : npc){ std::getline(iss,line); - if((n->dialogIndex = std::stoi(line)) != -1) + if((n->dialogIndex = std::stoi(line)) != 9999) n->addAIFunc(commonAIFunc,false); + else n->clearAIFunc(); + + std::getline(iss,line); + n->loc.x = std::stoi(line); + std::getline(iss,line); + n->loc.y = std::stoi(line); + } + + for(auto &b : build){ + std::getline(iss,line); + b->loc.x = std::stoi(line); + std::getline(iss,line); + b->loc.y = std::stoi(line); + } + + for(auto &m : mob){ + std::getline(iss,line); + m->loc.x = std::stoi(line); + std::getline(iss,line); + m->loc.y = std::stoi(line); } while(std::getline(iss,line)){ if(line == "dOnE") break; - std::cout<QueryBoolAttribute("hasDialog",&dialog) == XML_NO_ERROR && dialog) tmp->npc.back()->addAIFunc(commonAIFunc,false); - else tmp->npc.back()->dialogIndex = -1; + else tmp->npc.back()->dialogIndex = 9999; }else if(!strcmp(name,"structure")){ ptr = wxml->Attribute("inside"); @@ -1455,6 +1488,6 @@ World *loadWorldFromXML(const char *path){ dat.close(); tmp->load(); } - + return tmp; } -- cgit v1.2.3