From 36ef7e42fb1b151c64d69d96df7e33a9f4c47b83 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 8 Feb 2016 08:48:33 -0500 Subject: quests? --- Changelog | 7 +++++ include/Quest.h | 10 +------- src/Quest.cpp | 67 ------------------------------------------------ src/entities.cpp | 6 +++-- src/gameplay.cpp | 39 ++++++++++++++++++++++++---- src/ui.cpp | 20 +++++++++------ xml/playerSpawnHill1.xml | 33 +++++++++++++----------- 7 files changed, 76 insertions(+), 106 deletions(-) diff --git a/Changelog b/Changelog index 2077ac0..5a94cac 100644 --- a/Changelog +++ b/Changelog @@ -625,3 +625,10 @@ - improved font rendering codes - added font selection to settings.xml - XML'd important text + +2/8/2016: +========= + + - partially fixed text drawing + - XML'd quest checking, working on quest requirements + - worked on XML'ing villages diff --git a/include/Quest.h b/include/Quest.h index cc01d27..7ba77c8 100644 --- a/include/Quest.h +++ b/include/Quest.h @@ -24,17 +24,9 @@ typedef struct { std::string title; std::string desc; struct item_t reward; + std::vector need; } Quest; -/*class Quest { -public: - char *title; - char *desc; - struct item_t reward; - Quest(const char *t,const char *d,struct item_t r); - ~Quest(); -};*/ - /** * The Quest Handler class. * diff --git a/src/Quest.cpp b/src/Quest.cpp index f0be63c..0151706 100644 --- a/src/Quest.cpp +++ b/src/Quest.cpp @@ -1,81 +1,14 @@ #include -/*const Quest QuestList[1] = { - Quest("Not a quest","Stop",(struct item_t){0,0}) -};*/ - - -/*Quest::Quest(const char *t,const char *d,struct item_t r){ - title = new char[strlen(t)+1]; - desc = new char[strlen(d)+1]; - strcpy(title,t); - strcpy(desc,d); - memcpy(&reward,&r,sizeof(struct item_t)); -} - -Quest::~Quest(){ - delete[] title; - delete[] desc; - memset(&reward,0,sizeof(struct item_t)); -}*/ - int QuestHandler::assign(const char *t){ - /*unsigned char i; - for(i=0;ititle,t)){ -#ifdef DEBUG - DEBUG_printf("The QuestHandler already has this quest: %s\n",t); -#endif // DEBUG - return -2; - } - } - for(i=0;i<0;i++){ // Add the quest (if it really exists) - if(!strcmp(QuestList[i].title,t)){ - current.push_back(&QuestList[i]); -#ifdef DEBUG - DEBUG_printf("Added quest %s, now have %u active quests.\n",t,current.size()); -#endif // DEBUG - return current.size(); - } -#ifdef DEBUG - DEBUG_printf("Finding quest: %s != %s\n",t,QuestList[i].title); -#endif // DEBUG - } -#ifdef DEBUG - DEBUG_printf("Quest %s does not exist.\n",t); -#endif // DEBUG*/ return strcmp(t,"h"); } int QuestHandler::drop(const char *t){ - /*unsigned char i; - for(i=0;ititle,t)){ - current.erase(current.begin()+i); - return current.size(); - } - }*/ return strcmp(t,"h"); } int QuestHandler::finish(const char *t,void *completer){ - /*unsigned char i; - for(i=0;ititle,t)){ -#ifdef DEBUG - DEBUG_printf("Completing quest %s.\n",t); -#endif // DEBUG - ((Entity *)completer)->inv->addItem(current[i]->reward.id,current[i]->reward.count); - current.erase(current.begin()+i); -#ifdef DEBUG - DEBUG_printf("QuestHandler now has %u active quests.\n",current.size()); -#endif // DEBUG - return 0; - } - } -#ifdef DEBUG - DEBUG_printf("QuestHandler never had quest %s.\n",t); -#endif // DEBUG*/ return strncmp(t,(char *)completer,1); } diff --git a/src/entities.cpp b/src/entities.cpp index 5a9c42a..f079b03 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -3,6 +3,8 @@ #include +#define RAND_DIALOG_COUNT 13 + extern std::istream *names; extern unsigned int loops; @@ -126,7 +128,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 = 6;//rand() % 12 - 1; + randDialog = rand() % RAND_DIALOG_COUNT - 1; dialogIndex = 0; } NPC::~NPC(){ @@ -392,7 +394,7 @@ void NPC::clearAIFunc(void){ aiFunc.clear(); } -const char *randomDialog[] = { +const char *randomDialog[RAND_DIALOG_COUNT] = { "What a beautiful day it is.", "Have you ever went fast? I have.", "I heard if you complete a quest, you'll get a special reward.", diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 00faf70..5633eb8 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -33,6 +33,8 @@ int commonAIFunc(NPC *speaker){ XMLDocument xml; XMLElement *exml,*oxml; + static unsigned int oldidx = 9999; + const char *name; unsigned int idx = 0; bool stop = false; @@ -67,9 +69,30 @@ int commonAIFunc(NPC *speaker){ if((oxml = exml->FirstChildElement("quest"))){ const char *qname; + Quest tmp; while(oxml){ - if((qname = oxml->Attribute("assign"))) - player->qh.current.push_back((Quest){qname,"None",(struct item_t){0,0}}); + if((qname = oxml->Attribute("assign"))){ + tmp.title = qname; + tmp.desc = "None"; + tmp.reward = (struct item_t){0,0}; + + if(oxml->GetText()){ + tmp.need.push_back(oxml->GetText()); + } + + player->qh.current.push_back(tmp); + }else if((qname = oxml->Attribute("check"))){ + if(player->qh.hasQuest(qname)){ + ui::dialogBox(speaker->name,NULL,false,"Nice meme"); + ui::waitForDialog(); + return 0; + }else{ + oldidx = speaker->dialogIndex; + speaker->dialogIndex = oxml->UnsignedAttribute("fail"); + return commonAIFunc(speaker); + } + } + oxml = oxml->NextSiblingElement(); } } @@ -177,10 +200,16 @@ int commonAIFunc(NPC *speaker){ return 1; }else return commonAIFunc(speaker); }else{ - speaker->dialogIndex = 9999; - return 0; + if(oldidx != 9999){ + speaker->dialogIndex = oldidx; + oldidx = 9999; + return 1; + }else{ + speaker->dialogIndex = 9999; + return 0; + } } - return 1; + //return 1; } } diff --git a/src/ui.cpp b/src/ui.cpp index 56a88a3..5adb7b5 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -224,9 +224,9 @@ namespace ui { buf = new char[ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4]; for(j=0;jglyph->bitmap.width*ftf->glyph->bitmap.rows;j++){ - buf[j*4 ]=fontColor[0]; - buf[j*4+1]=fontColor[1]; - buf[j*4+2]=fontColor[2]; + buf[j*4 ]=255;//fontColor[0]; + buf[j*4+1]=255;//fontColor[1]; + buf[j*4+2]=255;//fontColor[2]; buf[j*4+3]=ftf->glyph->bitmap.buffer[j] ? 255 : 0; //buf[j*4+3]=ftf->glyph->bitmap.buffer[j]; } @@ -258,9 +258,11 @@ namespace ui { * Draws a character at the specified coordinates, aborting if the character is unknown. */ - vec2 putChar(float x,float y,char c){ + vec2 putChar(float xx,float yy,char c){ vec2 c1,c2; + int x = xx, y = yy; + /* * Get the width and height of the rendered character. */ @@ -332,10 +334,12 @@ namespace ui { return xo; // i.e. the string width } - float putStringCentered(const float x,const float y,const char *s){ + float putStringCentered(const float xx,const float yy,const char *s){ unsigned int i = 0; float width = 0; + int x = xx,y = yy; + do{ if(s[i]=='\n'){ // Handle newlines // TODO @@ -626,13 +630,13 @@ namespace ui { if(player->inv->invOpen){ hub.y = player->loc.y + fontSize * 8; - hub.x = player->loc.x; + hub.x = player->loc.x;// + player->width / 2; putStringCentered(hub.x,hub.y,"Current Quests:"); for(auto &c : player->qh.current){ hub.y -= fontSize * 1.15; - putString(hub.x,hub.y,c.title.c_str()); + putStringCentered(hub.x,hub.y,c.title.c_str()); } } } @@ -726,7 +730,7 @@ namespace ui { void drawMenu(Menu *menu){ - setFontSize(20); + setFontSize(24); SDL_Event e; mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2); diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index 55612d3..cf42cda 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -10,7 +10,7 @@ - + @@ -22,30 +22,33 @@ And it wasn't stormy. - - Hello there! - - - Sup bro! - + + Sup bro! Have a quest. To complete it, just go talk to Ralph again. + + Dank Maymay + - - My name's Johnny. - + + Broooooooooooooo... -- cgit v1.2.3 From 1382f79d57060dc38f6c45fd4fc865e0e565f90b Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 9 Feb 2016 08:44:41 -0500 Subject: quest completion --- Changelog | 7 +++++++ config/items.xml | 2 +- include/Quest.h | 4 ++-- src/Quest.cpp | 24 +++++++++++++++++------- src/gameplay.cpp | 17 +++++++++-------- src/inventory.cpp | 22 +++++++++++++++++++--- src/ui.cpp | 29 ++++++++++++++--------------- xml/playerSpawnHill1.xml | 6 ++---- xml/playerSpawnHill1_Building1.xml | 9 ++++++++- 9 files changed, 79 insertions(+), 41 deletions(-) diff --git a/Changelog b/Changelog index 5a94cac..e8a0605 100644 --- a/Changelog +++ b/Changelog @@ -632,3 +632,10 @@ - partially fixed text drawing - XML'd quest checking, working on quest requirements - worked on XML'ing villages + +2/9/2016: +========= + + - quests can require one item for completion + - better-er text drawing + - can set world themes through XML diff --git a/config/items.xml b/config/items.xml index 02d199e..02382d6 100644 --- a/config/items.xml +++ b/config/items.xml @@ -1,7 +1,7 @@ - + diff --git a/include/Quest.h b/include/Quest.h index 7ba77c8..388b14d 100644 --- a/include/Quest.h +++ b/include/Quest.h @@ -55,13 +55,13 @@ public: * that gave the quest originally. */ - int finish(const char *t,void *completer); + int finish(std::string t); /** * Returns true if this handler is currently taking the quest. */ - bool hasQuest(const char *t); + bool hasQuest(std::string t); }; #endif // QUEST_H diff --git a/src/Quest.cpp b/src/Quest.cpp index 0151706..ac18021 100644 --- a/src/Quest.cpp +++ b/src/Quest.cpp @@ -1,5 +1,9 @@ #include +#include + +extern Player *player; + int QuestHandler::assign(const char *t){ return strcmp(t,"h"); } @@ -8,16 +12,22 @@ int QuestHandler::drop(const char *t){ return strcmp(t,"h"); } -int QuestHandler::finish(const char *t,void *completer){ - return strncmp(t,(char *)completer,1); +int QuestHandler::finish(std::string t){ + for(unsigned int i=0;iinv->takeItem(current[i].need.back(),1)){ + current.erase(current.begin()+i); + return 1; + }else return 0; + } + } + return 0; } -bool QuestHandler::hasQuest(const char *t){ - unsigned int i; - for(i=0;iqh.current.push_back(tmp); }else if((qname = oxml->Attribute("check"))){ - if(player->qh.hasQuest(qname)){ - ui::dialogBox(speaker->name,NULL,false,"Nice meme"); - ui::waitForDialog(); - return 0; + if(player->qh.hasQuest(qname) && player->qh.finish(qname)){ + player->qh.finish(qname); + goto CONT; }else{ oldidx = speaker->dialogIndex; speaker->dialogIndex = oxml->UnsignedAttribute("fail"); @@ -96,7 +95,9 @@ int commonAIFunc(NPC *speaker){ oxml = oxml->NextSiblingElement(); } } - + +CONT: + /* * Handle any 'give' requests. */ @@ -152,7 +153,7 @@ int commonAIFunc(NPC *speaker){ * Get the player's choice, then set the XMLElement to the option's block. */ - ui::dialogBox(speaker->name,optstr.c_str(),false,exml->GetText()); + ui::dialogBox(speaker->name,optstr.c_str(),false,exml->GetText()+1); ui::waitForDialog(); if(ui::dialogOptChosen) @@ -161,12 +162,12 @@ int commonAIFunc(NPC *speaker){ while(!dopt.empty()) dopt.pop_back(); }else{ - + /* * No options - simply print the text. */ - ui::dialogBox(speaker->name,"",false,exml->GetText()); + ui::dialogBox(speaker->name,NULL,false,exml->GetText()); ui::waitForDialog(); } diff --git a/src/inventory.cpp b/src/inventory.cpp index 93bdeb6..d52dbe8 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -53,22 +53,38 @@ int Inventory::addItem(std::string name,uint count){ int Inventory::takeItem(std::string name,uint count){ unsigned int id = 999999; + + /* + * Name to ID lookup + */ + for(unsigned int i=0;iname == name){ id = i; break; } } + + if(id == 999999) + return -1; + + /* + * Inventory lookup + */ + for(unsigned int i=0;i items[i].count) - items.erase(items.begin()+i); - else + return -(items[i].count - count); + else{ items[i].count -= count; + if(!items[i].count) + items.erase(items.begin()+i); + } return 0; } } - return -1; + return -2; } static GLuint *itemtex; diff --git a/src/ui.cpp b/src/ui.cpp index 5adb7b5..7c73192 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -258,17 +258,15 @@ namespace ui { * Draws a character at the specified coordinates, aborting if the character is unknown. */ - vec2 putChar(float xx,float yy,char c){ + vec2 putChar(float x,float y,char c){ vec2 c1,c2; - int x = xx, y = yy; - /* * Get the width and height of the rendered character. */ - c1={x+ftexbl[c-33].x, - y+ftexbl[c-33].y}; + c1={(float)floor(x)+ftexbl[c-33].x, + (float)floor(y)+ftexbl[c-33].y}; c2=ftexwh[c-33]; /* @@ -316,7 +314,10 @@ namespace ui { if(s[i] == ' ') i++; } - if(s[i] == '\n' || s[i] == '\r' || s[i] == '\t'){ + if(s[i] == '\n'){ + yo-=fontSize*1.05; + xo=x; + }else if(s[i] == '\r' || s[i] == '\t'){ /*if(s[i] == '\n'){ yo-=fontSize*1.05; xo=x; @@ -325,7 +326,7 @@ namespace ui { }else if(s[i]=='\b'){ // Handle backspaces? xo-=add.x; }else{ - add=putChar(xo,yo,s[i]); + add=putChar(floor(xo),floor(yo),s[i]); xo+=add.x; yo+=add.y; } @@ -334,12 +335,10 @@ namespace ui { return xo; // i.e. the string width } - float putStringCentered(const float xx,const float yy,const char *s){ + float putStringCentered(const float x,const float y,const char *s){ unsigned int i = 0; float width = 0; - int x = xx,y = yy; - do{ if(s[i]=='\n'){ // Handle newlines // TODO @@ -353,7 +352,7 @@ namespace ui { } }while(s[++i]); - putString(x-width/2,y,s); + putString(floor(x-width/2),y,s); return width; } @@ -530,15 +529,15 @@ namespace ui { } void importantText(const char *text,...){ va_list textArgs; - + //if(!player->ground)return; - + memset(dialogBoxText,0,512); - + va_start(textArgs,text); vsnprintf(dialogBoxText,512,text,textArgs); va_end(textArgs); - + dialogBoxExists = true; dialogImportant = true; //toggleBlack(); diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index cf42cda..387e1a9 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -31,8 +31,8 @@ And it wasn't stormy. - Niice. + @@ -43,9 +43,7 @@ And it wasn't stormy. Sup bro! Have a quest. To complete it, just go talk to Ralph again. - - Dank Maymay - + Dank MayMay diff --git a/xml/playerSpawnHill1_Building1.xml b/xml/playerSpawnHill1_Building1.xml index 54d217b..5c2b723 100644 --- a/xml/playerSpawnHill1_Building1.xml +++ b/xml/playerSpawnHill1_Building1.xml @@ -3,6 +3,13 @@