aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authordrumsetmonkey <abelleisle@roadrunner.com>2016-02-11 08:28:27 -0500
committerdrumsetmonkey <abelleisle@roadrunner.com>2016-02-11 08:28:27 -0500
commit2a4a514b654ef0e08134a116c658d49a8f16cfe7 (patch)
tree8f33af3d9508c370a27719d3bc2089ada1abeffb /src
parenta0afc9a192016b2bb4c185895a4082cc965e1179 (diff)
parent3033594b89f23e65b6152daa6610f991307f2f67 (diff)
Worlds can be themed
Diffstat (limited to 'src')
-rw-r--r--src/Quest.cpp117
-rw-r--r--src/entities.cpp6
-rw-r--r--src/gameplay.cpp37
-rw-r--r--src/inventory.cpp51
-rw-r--r--src/ui.cpp33
5 files changed, 144 insertions, 100 deletions
diff --git a/src/Quest.cpp b/src/Quest.cpp
index f0be63c..535afc5 100644
--- a/src/Quest.cpp
+++ b/src/Quest.cpp
@@ -1,90 +1,71 @@
#include <Quest.h>
-/*const Quest QuestList[1] = {
- Quest("Not a quest","Stop",(struct item_t){0,0})
-};*/
+#include <entities.h>
+extern Player *player;
-/*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));
-}
+int QuestHandler::assign(std::string title,std::string desc,std::string req){
+ Quest tmp;
+ char *tok;
+
+ tmp.title = title;
+ tmp.desc = desc;
-Quest::~Quest(){
- delete[] title;
- delete[] desc;
- memset(&reward,0,sizeof(struct item_t));
-}*/
+ std::unique_ptr<char[]> buf (new char[req.size()]);
-int QuestHandler::assign(const char *t){
- /*unsigned char i;
- for(i=0;i<current.size();i++){ // Make sure we don't already have this quest
- if(!strcmp(current[i]->title,t)){
-#ifdef DEBUG
- DEBUG_printf("The QuestHandler already has this quest: %s\n",t);
-#endif // DEBUG
- return -2;
- }
+ strcpy(buf.get(),req.c_str());
+ tok = strtok(buf.get(),"\n\r\t,");
+ tmp.need.push_back({"\0",0});
+
+ while(tok){
+ if(tmp.need.back().name != "\0"){
+ tmp.need.back().n = atoi(tok);
+ tmp.need.push_back({"\0",0});
+ }else
+ tmp.need.back().name = tok;
+
+ tok = strtok(NULL,"\n\r\t,");
}
- 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");
+
+ tmp.need.pop_back();
+ current.push_back(tmp);
+
+ return 0;
}
-int QuestHandler::drop(const char *t){
- /*unsigned char i;
- for(i=0;i<current.size();i++){
- if(!strcmp(current[i]->title,t)){
+int QuestHandler::drop(std::string title){
+ for(unsigned int i=0;i<current.size();i++){
+ if(current[i].title == title){
current.erase(current.begin()+i);
- return current.size();
+ return 0;
}
- }*/
- return strcmp(t,"h");
+ }
+ return -1;
}
-int QuestHandler::finish(const char *t,void *completer){
- /*unsigned char i;
- for(i=0;i<current.size();i++){
- if(!strcmp(current[i]->title,t)){
-#ifdef DEBUG
- DEBUG_printf("Completing quest %s.\n",t);
-#endif // DEBUG
- ((Entity *)completer)->inv->addItem(current[i]->reward.id,current[i]->reward.count);
+int QuestHandler::finish(std::string t){
+ for(unsigned int i=0;i<current.size();i++){
+ if(current[i].title == t){
+ for(auto &n : current[i].need){
+ if(player->inv->hasItem(n.name) < n.n)
+ return 0;
+ }
+
+ for(auto &n : current[i].need){
+ player->inv->takeItem(n.name,n.n);
+ }
+
current.erase(current.begin()+i);
-#ifdef DEBUG
- DEBUG_printf("QuestHandler now has %u active quests.\n",current.size());
-#endif // DEBUG
- return 0;
+ return 1;
}
}
-#ifdef DEBUG
- DEBUG_printf("QuestHandler never had quest %s.\n",t);
-#endif // DEBUG*/
- return strncmp(t,(char *)completer,1);
+ return 0;
}
-bool QuestHandler::hasQuest(const char *t){
- unsigned int i;
- for(i=0;i<current.size();i++){
- if(!strcmp(current[i].title.c_str(),t)){
+bool QuestHandler::hasQuest(std::string t){
+ for(unsigned int i=0;i<current.size();i++){
+ if(current[i].title == t)
return true;
- }
}
return false;
}
diff --git a/src/entities.cpp b/src/entities.cpp
index 0469ad2..e020f8b 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -3,6 +3,8 @@
#include <istream>
+#define RAND_DIALOG_COUNT 13
+
extern std::istream *names;
extern unsigned int loops;
@@ -117,7 +119,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 = rand() % RAND_DIALOG_COUNT - 1;
dialogIndex = 0;
}
NPC::~NPC(){
@@ -383,7 +385,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..e252c26 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,13 +69,26 @@ int commonAIFunc(NPC *speaker){
if((oxml = exml->FirstChildElement("quest"))){
const char *qname;
+
while(oxml){
if((qname = oxml->Attribute("assign")))
- player->qh.current.push_back((Quest){qname,"None",(struct item_t){0,0}});
+ player->qh.assign(qname,"None",(std::string)oxml->GetText());
+ else if((qname = oxml->Attribute("check"))){
+ if(player->qh.hasQuest(qname) && player->qh.finish(qname)){
+ goto CONT;
+ }else{
+ oldidx = speaker->dialogIndex;
+ speaker->dialogIndex = oxml->UnsignedAttribute("fail");
+ return commonAIFunc(speaker);
+ }
+ }
+
oxml = oxml->NextSiblingElement();
}
}
-
+
+CONT:
+
/*
* Handle any 'give' requests.
*/
@@ -129,7 +144,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)
@@ -138,12 +153,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();
}
@@ -177,10 +192,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/inventory.cpp b/src/inventory.cpp
index 93bdeb6..b32b56f 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -13,7 +13,7 @@ static bool swing = false;
static vec2 itemLoc;
Mix_Chunk* swordSwing;
-std::vector<Item *> itemMap;
+static std::vector<Item *> itemMap;
void items(void){
XMLDocument xml;
@@ -32,7 +32,7 @@ void items(void){
itemMap.back()->texloc = exml->Attribute("sprite");
exml = exml->NextSiblingElement();
- }
+ }
}
int Inventory::addItem(std::string name,uint count){
@@ -44,7 +44,7 @@ int Inventory::addItem(std::string name,uint count){
return 0;
}
}
- items.push_back((item_t){i,count});
+ items.push_back((item_t){count,i});
return 0;
}
}
@@ -53,22 +53,59 @@ 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;i<itemMap.size();i++){
if(itemMap[i]->name == name){
id = i;
break;
}
}
+
+ if(id == 999999)
+ return -1;
+
+ /*
+ * Inventory lookup
+ */
+
for(unsigned int i=0;i<items.size();i++){
if(items[i].id == id){
if(count > 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;
+}
+
+int Inventory::hasItem(std::string name){
+ unsigned int id = 999999;
+
+ for(unsigned int i=0;i<itemMap.size();i++){
+ if(itemMap[i]->name == name){
+ id = i;
+ break;
+ }
+ }
+
+ if(id == 999999)
+ return 0;
+
+ for(auto &i : items){
+ if(i.id == id)
+ return i.count;
+ }
+
+ return 0;
}
static GLuint *itemtex;
@@ -211,7 +248,7 @@ void Inventory::draw(void){
}
glEnd();
glDisable(GL_TEXTURE_2D);
- ui::putText(r.end.x-(itemWide/2),r.end.y-(itemWide*.9),"%s",itemMap[items[a].id]->name);
+ ui::putText(r.end.x-(itemWide/2),r.end.y-(itemWide*.9),"%s",itemMap[items[a].id]->name.c_str());
ui::putText(r.end.x-(itemWide/2)+(itemWide*.85),r.end.y-(itemWide/2),"%d",items[a].count);
}
diff --git a/src/ui.cpp b/src/ui.cpp
index ead4932..e6ee988 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;j<ftf->glyph->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];
}
@@ -267,8 +267,8 @@ namespace ui {
* 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 +316,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 +328,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;
}
@@ -351,7 +354,7 @@ namespace ui {
}
}while(s[++i]);
- putString(x-width/2,y,s);
+ putString(floor(x-width/2),y,s);
return width;
}
@@ -528,15 +531,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();
@@ -628,13 +631,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());
}
}
}
@@ -727,7 +730,7 @@ namespace ui {
*/
void drawMenu(Menu *menu){
- setFontSize(18);
+ setFontSize(24);
SDL_Event e;
mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2);