diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/common.hpp | 23 | ||||
-rw-r--r-- | include/entities.hpp | 25 | ||||
-rw-r--r-- | include/inventory.hpp | 40 | ||||
-rw-r--r-- | include/ui.hpp | 35 | ||||
-rw-r--r-- | include/ui_menu.hpp | 34 | ||||
-rw-r--r-- | include/world.hpp | 8 | ||||
-rw-r--r-- | main.cpp | 7 | ||||
-rw-r--r-- | src/common.cpp | 48 | ||||
-rw-r--r-- | src/config.cpp | 16 | ||||
-rw-r--r-- | src/entities.cpp | 161 | ||||
-rw-r--r-- | src/gameplay.cpp | 62 | ||||
-rw-r--r-- | src/inventory.cpp | 225 | ||||
-rw-r--r-- | src/quest.cpp | 12 | ||||
-rw-r--r-- | src/texture.cpp | 56 | ||||
-rwxr-xr-x | src/tinyxml2.cpp | 2 | ||||
-rw-r--r-- | src/ui.cpp | 152 | ||||
-rw-r--r-- | src/ui_menu.cpp | 70 | ||||
-rw-r--r-- | src/world.cpp | 168 | ||||
-rw-r--r-- | xml/playerSpawnHill1.xml | 11 |
20 files changed, 617 insertions, 540 deletions
@@ -12,7 +12,7 @@ ifeq ($(TARGET_OS),win32) -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer -lfreetype endif -CXXFLAGS = -m$(TARGET_BITS) -std=c++1z +CXXFLAGS = -g -m$(TARGET_BITS) -std=c++1z CXXINC = -Iinclude -Iinclude/freetype CXXWARN = -Wall -Wextra -Werror -pedantic-errors diff --git a/include/common.hpp b/include/common.hpp index 65ac47b..5968272 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -48,6 +48,11 @@ typedef unsigned int uint; #define MSEC_PER_TICK (1000 / TICKS_PER_SEC) +/** + * This flag lets the compuler know that we are testing for segfault locations. + * If this flag is enabled, the function C(x) will print 'x' to terminal + */ + //#define SEGFAULT /** @@ -57,17 +62,17 @@ typedef unsigned int uint; #define SHADERS template<typename N> -N abso(N v){ - if(v < 0){ +N abso(N v) { + if (v < 0) { return v * -1; }else return v; } template<class A> -float averagef(A v){ +float averagef(A v) { float avg = 0; - for(auto &a : v){ + for(auto &a : v) { avg += a; } avg /= v.size(); @@ -112,16 +117,16 @@ struct col { float red; float green; float blue; - col operator-=(float a){ + col operator-=(float a) { red-=a; green-=a; blue-=a; return{red+a,green+a,blue+a}; } - col operator+=(float a){ + col operator+=(float a) { return{red+a,green+a,blue+a}; } - col operator=(float a){ + col operator=(float a) { return{red=a,green=a,blue=a}; } }; @@ -195,7 +200,7 @@ extern float VOLUME_SFX; #define DEBUG_printf(message, ...) DEBUG_prints(__FILE__, __LINE__, message, __VA_ARGS__) #ifdef SEGFAULT -#define C(x) std::cout << m << std::endl +#define C(x) std::cout << x << std::endl #else #define C(x) #endif @@ -268,7 +273,7 @@ const char *readFile(const char *path); int strCreateFunc(const char *equ); template<typename N, size_t s> -size_t arrAmt(N (&)[s]){return s;} +size_t arrAmt(N (&)[s]) {return s;} void UserError(std::string reason); diff --git a/include/entities.hpp b/include/entities.hpp index 6cd5731..ae23714 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -58,7 +58,7 @@ public: std::string item[2]; int quantity[2]; Trade(int qo, std::string o, int qt, std::string t); - Trade(){} + Trade() {} }; class World; @@ -77,7 +77,7 @@ public: bool gravity; bool behind; bool bounce; - Particles(float x, float y, float w, float h, float vx, float vy, Color c, float d){ + Particles(float x, float y, float w, float h, float vx, float vy, Color c, float d) { loc.x = x; loc.y = y; vel.x = vx; @@ -92,9 +92,9 @@ public: bounce = false; index = Texture::getIndex(c); } - ~Particles(){ + ~Particles() { } - void draw(){ + void draw() { glColor3ub(255,255,255); glBegin(GL_QUADS); glTexCoord2f(.25*index.x, .125*index.y); glVertex2i(loc.x, loc.y); @@ -117,7 +117,7 @@ public: } else if (gravity && vel.y > -1) vel.y -= _gravity * deltaTime; } - bool kill(float delta){ + bool kill(float delta) { return (duration -= delta) <= 0; } }; @@ -184,15 +184,15 @@ public: int ticksToUse; // Used by wander() - virtual void wander(int){} - virtual void interact(){} + virtual void wander(int) {} + virtual void interact() {} void follow(Entity *e); bool isNear(Entity e); bool isInside(vec2 coord) const; - virtual ~Entity(){} + virtual ~Entity() {} }; class Player : public Entity{ @@ -234,11 +234,18 @@ public: virtual void wander(int); }; -class Merchant : public NPC { +class Merchant : public NPC{ public: std::vector<Trade>trade; uint currTrade; + std::string text[4]; + std::string *toSay; + //greeting + //accept + //deny + //farewell + void interact(); Structures *inside; diff --git a/include/inventory.hpp b/include/inventory.hpp index e38f89a..4fb45f9 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -10,7 +10,7 @@ class Item{ public: - std::string name,type; + std::string name, type; float width; float height; @@ -20,7 +20,26 @@ public: std::string texloc; Texturec *tex; - GLuint rtex(){ + GLuint rtex() + { + return tex->image[0]; + } +}; + +class Currency{ +public: + std::string name; + + float width; + float height; + + std::string texloc; + Texturec *tex; + + float value; + + GLuint rtex() + { return tex->image[0]; } }; @@ -32,17 +51,18 @@ struct item_t{ class Inventory { private: - unsigned int size; + unsigned int size; //how many slots our inventory has + unsigned int sel; //what item is currently selected int os = 0; public: std::vector<item_t> items; - unsigned int sel; - bool invOpen = false; - bool invOpening = false; - bool invHover = false; - bool selected = false; - bool mouseSel = false; - bool usingi = false; + + bool invOpen = false; //is the inventory open + bool invOpening = false; //is the opening animation playing + bool invHover = false; //are we using the fancy hover inventory + bool selected = false; //used in hover inventory to show which item has been selected + bool mouseSel = false; //the location of the temperary selection for the hover inv + bool usingi = false; //bool used to tell if inventory owner is using selected item Inventory(unsigned int s); // Creates an inventory of size 's' ~Inventory(void); // Free's allocated memory diff --git a/include/ui.hpp b/include/ui.hpp index 5281d31..df81bab 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -22,30 +22,25 @@ #define DEBUG -typedef uint8_t BYTE; -typedef uint16_t WORD; -typedef uint32_t DWORD; -typedef int32_t LONG; - typedef struct{ - WORD bfType; - DWORD bfSize; - WORD bfReserved1, bfReserved2; - DWORD bfOffBits; //how many bytes before the image data + uint16_t bfType; + uint32_t bfSize; + uint16_t bfReserved1, bfReserved2; + uint32_t bfOffBits; //how many bytes before the image data } __attribute__ ((packed)) BITMAPFILEHEADER; typedef struct{ - DWORD biSize; //size of header in bytes - LONG biWidth; - LONG biHeight; - WORD biPlanes; - WORD biBitCount; //how many bits are in a pixel - DWORD biCompression; - DWORD biSizeImage; //size of image in bytes - LONG biXPelsPerMeter; - LONG biYPelsPerMeter; - DWORD biClrUsed; //how many colors there are - DWORD biClrImportant; //important colors + uint32_t biSize; //size of header in bytes + int32_t biWidth; + int32_t biHeight; + uint16_t biPlanes; + uint16_t biBitCount; //how many bits are in a pixel + uint32_t biCompression; + uint32_t biSizeImage; //size of image in bytes + int32_t biXPelsPerMeter; + int32_t biYPelsPerMeter; + uint32_t biClrUsed; //how many colors there are + uint32_t biClrImportant; //important colors } __attribute__ ((packed)) BITMAPINFOHEADER; namespace ui { diff --git a/include/ui_menu.hpp b/include/ui_menu.hpp index bfeecba..7c6f2ad 100644 --- a/include/ui_menu.hpp +++ b/include/ui_menu.hpp @@ -6,14 +6,15 @@ typedef void (*menuFunc)(void); -struct menuItem { +class menuItem { +public: int member; union { struct { vec2 loc; dim2 dim; Color color; - + const char *text; menuFunc func; } button; @@ -21,28 +22,37 @@ struct menuItem { vec2 loc; dim2 dim; Color color; - + float minValue; float maxValue; float sliderLoc; - + const char *text; float *var; } slider; }; + ~menuItem(){ + //button.text = NULL; + //slider.text = NULL; + + //delete[] button.text; + //delete[] slider.text; + //delete slider.var; + } }; class Menu { public: std::vector<menuItem> items; - Menu *child, *parent; - - ~Menu() { - // TODO you CANNOT delete null pointers! - /*child = NULL; + Menu *parent, *child; + + ~Menu() + { + items.clear(); + //delete child; + //delete parent; + child = NULL; parent = NULL; - delete child; - delete parent;*/ } void gotoChild(void); @@ -55,7 +65,7 @@ namespace ui { menuItem createChildButton(vec2 l, dim2 d, Color c, const char* t); menuItem createParentButton(vec2 l, dim2 d, Color c, const char* t); menuItem createSlider(vec2 l, dim2 d, Color c, float min, float max, const char* t, float* v); - + void draw(void); } } diff --git a/include/world.hpp b/include/world.hpp index bee2bcf..3625849 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -102,7 +102,7 @@ public: float fireFlicker; vec2 fireLoc; - Light(vec2 l, Color c, float r){ + Light(vec2 l, Color c, float r) { loc = l; color = c; radius = r; @@ -113,11 +113,11 @@ public: flame = false; } - void makeFlame(void){ + void makeFlame(void) { flame = true; } - void follow(Entity *f){ + void follow(Entity *f) { following=f; belongsTo = true; } @@ -137,7 +137,7 @@ public: std::vector<Structures *> build; Village(const char *meme, World *w); - ~Village(void){} + ~Village(void) {} }; /** @@ -67,12 +67,6 @@ World *currentWorld = NULL, Player *player; /** - * TODO - */ - -extern Menu *currentMenu; - -/** * The current number of ticks, used for logic operations and day/night cycles. */ @@ -137,6 +131,7 @@ unsigned int loops = 0; vec2 offset; +//std::shared_ptr<Menu>currentMenu; Menu *currentMenu; Menu optionsMenu; Menu pauseMenu; diff --git a/src/common.cpp b/src/common.cpp index 8f59fba..34b2a61 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -13,14 +13,14 @@ #ifndef __WIN32__ -unsigned int millis(void){ +unsigned int millis(void) { std::chrono::system_clock::time_point now=std::chrono::system_clock::now(); return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count(); } #endif // __WIN32__ -void DEBUG_prints(const char* file, int line, const char *s,...){ +void DEBUG_prints(const char* file, int line, const char *s,...) { va_list args; printf("%s:%d: ",file,line); va_start(args,s); @@ -28,32 +28,32 @@ void DEBUG_prints(const char* file, int line, const char *s,...){ va_end(args); } -void safeSetColor(int r,int g,int b){ // safeSetColor() is an alternative to directly using glColor3ub() to set - if(r>255)r=255; // the color for OpenGL drawing. safeSetColor() checks for values that are - if(g>255)g=255; // outside the range of an unsigned character and sets them to a safer value. - if(b>255)b=255; - if(r<0)r=0; - if(g<0)g=0; - if(b<0)b=0; +void safeSetColor(int r,int g,int b) { // safeSetColor() is an alternative to directly using glColor3ub() to set + if (r>255)r=255; // the color for OpenGL drawing. safeSetColor() checks for values that are + if (g>255)g=255; // outside the range of an unsigned character and sets them to a safer value. + if (b>255)b=255; + if (r<0)r=0; + if (g<0)g=0; + if (b<0)b=0; glColor3ub(r,g,b); } -void safeSetColorA(int r,int g,int b,int a){ - if(r>255)r=255; - if(g>255)g=255; - if(b>255)b=255; - if(a>255)a=255; - if(r<0)r=0; - if(g<0)g=0; - if(b<0)b=0; - if(a<0)a=0; +void safeSetColorA(int r,int g,int b,int a) { + if (r>255)r=255; + if (g>255)g=255; + if (b>255)b=255; + if (a>255)a=255; + if (r<0)r=0; + if (g<0)g=0; + if (b<0)b=0; + if (a<0)a=0; glColor4ub(r,g,b,a); } -int getdir(std::string dir, std::vector<std::string> &files){ +int getdir(std::string dir, std::vector<std::string> &files) { DIR *dp; struct dirent *dirp; - if(!(dp = opendir(dir.c_str()))){ + if (!(dp = opendir(dir.c_str()))) { std::cout <<"Error ("<<errno<<") opening "<<dir<<std::endl; return errno; } @@ -63,12 +63,12 @@ int getdir(std::string dir, std::vector<std::string> &files){ return 0; } -void strVectorSortAlpha(std::vector<std::string> *v){ +void strVectorSortAlpha(std::vector<std::string> *v) { static bool change; do{ change = false; - for(unsigned int i=0;i<v->size()-1;i++){ - if(v[0][i] > v[0][i+1]){ + for(unsigned int i=0;i<v->size()-1;i++) { + if (v[0][i] > v[0][i+1]) { std::swap(v[0][i],v[0][i+1]); change = true; } @@ -76,7 +76,7 @@ void strVectorSortAlpha(std::vector<std::string> *v){ }while(change); } -const char *readFile(const char *path){ +const char *readFile(const char *path) { std::ifstream in (path,std::ios::in); unsigned int size; GLchar *buf; diff --git a/src/config.cpp b/src/config.cpp index a2acc03..752b365 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -29,33 +29,33 @@ namespace config { xml.LoadFile("config/settings.xml"); scr = xml.FirstChildElement("screen"); - if(scr->QueryUnsignedAttribute("width",&uval) == XML_NO_ERROR) + if (scr->QueryUnsignedAttribute("width",&uval) == XML_NO_ERROR) SCREEN_WIDTH = uval; else SCREEN_WIDTH = 1280; - if(scr->QueryUnsignedAttribute("height",&uval) == XML_NO_ERROR) + if (scr->QueryUnsignedAttribute("height",&uval) == XML_NO_ERROR) SCREEN_HEIGHT = uval; else SCREEN_HEIGHT = 800; - if(scr->QueryBoolAttribute("fullscreen",&bval) == XML_NO_ERROR) + if (scr->QueryBoolAttribute("fullscreen",&bval) == XML_NO_ERROR) FULLSCREEN = bval; else FULLSCREEN = false; - if(xml.FirstChildElement("hline")->QueryUnsignedAttribute("size",&uval) == XML_NO_ERROR) + if (xml.FirstChildElement("hline")->QueryUnsignedAttribute("size",&uval) == XML_NO_ERROR) HLINE = uval; else HLINE = 3; vol = xml.FirstChildElement("volume"); - if(vol->FirstChildElement("master")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + if (vol->FirstChildElement("master")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) VOLUME_MASTER = fval; else VOLUME_MASTER = 50; - if(vol->FirstChildElement("music")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + if (vol->FirstChildElement("music")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) VOLUME_MUSIC = fval; else VOLUME_MUSIC = 50; - if(vol->FirstChildElement("sfx")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + if (vol->FirstChildElement("sfx")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) VOLUME_SFX = fval; else VOLUME_SFX = 50; xmlFolder = xml.FirstChildElement("world")->Attribute("start"); - if(xmlFolder=="\0")xmlFolder = "xml/"; + if (xmlFolder=="\0")xmlFolder = "xml/"; std::cout << "Folder: " << xmlFolder << std::endl; ui::initFonts(); diff --git a/src/entities.cpp b/src/entities.cpp index 02be6fe..c52ff4e 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -36,11 +36,11 @@ const char *randomDialog[RAND_DIALOG_COUNT] = { "What's a bagel? I don't know because I'm mormon" }; -void initEntity(){ +void initEntity() { waterTex = Texture::loadTexture("assets/waterTex.png"); } -void getRandomName(Entity *e){ +void getRandomName(Entity *e) { unsigned int tempNum,max=0; char *bufs; @@ -61,7 +61,7 @@ void getRandomName(Entity *e){ names.close(); - switch(bufs[0]){ + switch(bufs[0]) { default : case 'm': e->gender = MALE; @@ -76,7 +76,7 @@ void getRandomName(Entity *e){ delete[] bufs; } -Trade::Trade(int qo, std::string o, int qt, std::string t){ +Trade::Trade(int qo, std::string o, int qt, std::string t) { item[0] = o; item[1] = t; @@ -86,7 +86,7 @@ Trade::Trade(int qo, std::string o, int qt, std::string t){ std::cout << "Trading: " << quantity[0] << " " << item[0] << " for " << quantity[1] << " " << item[1] << std::endl; } -void Entity::spawn(float x, float y){ //spawns the entity you pass to it based off of coords and global entity settings +void Entity::spawn(float x, float y) { //spawns the entity you pass to it based off of coords and global entity settings loc.x = x; loc.y = y; vel.x = 0; @@ -105,10 +105,10 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o ticksToUse = 0; hitCooldown = 0; - if(!maxHealth)health = maxHealth = 1; + if (!maxHealth)health = maxHealth = 1; - if(type==MOBT){ - if(Mobp(this)->subtype == MS_BIRD){ + if (type==MOBT) { + if (Mobp(this)->subtype == MS_BIRD) { Mobp(this)->init_y=loc.y; } } @@ -119,7 +119,7 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o followee = NULL; } -Player::Player(){ //sets all of the player specific traits on object creation +Player::Player() { //sets all of the player specific traits on object creation width = HLINE * 10; height = HLINE * 16; @@ -140,13 +140,13 @@ Player::Player(){ //sets all of the player specific traits on object creation "assets/player/playerk8.png"); inv = new Inventory(PLAYER_INV_SIZE); } -Player::~Player(){ +Player::~Player() { delete inv; delete tex; delete[] name; } -NPC::NPC(){ //sets all of the NPC specific traits on object creation +NPC::NPC() { //sets all of the NPC specific traits on object creation width = HLINE * 10; height = HLINE * 16; @@ -165,8 +165,8 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation dialogIndex = 0; } -NPC::~NPC(){ - while(!aiFunc.empty()){ +NPC::~NPC() { + while(!aiFunc.empty()) { aiFunc.pop_back(); } @@ -175,7 +175,7 @@ NPC::~NPC(){ delete[] name; } -Merchant::Merchant(){ //sets all of the Merchant specific traits on object creation +Merchant::Merchant() { //sets all of the Merchant specific traits on object creation width = HLINE * 10; height = HLINE * 16; @@ -200,8 +200,8 @@ Merchant::Merchant(){ //sets all of the Merchant specific traits on object creat dialogIndex = 0; } -Merchant::~Merchant(){ - /*while(!aiFunc.empty()){ +Merchant::~Merchant() { + /*while(!aiFunc.empty()) { aiFunc.pop_back(); }*/ delete inside; @@ -210,7 +210,7 @@ Merchant::~Merchant(){ //delete[] name; } -Structures::Structures(){ //sets the structure type +Structures::Structures() { //sets the structure type health = maxHealth = 1; alive = false; @@ -221,21 +221,21 @@ Structures::Structures(){ //sets the structure type //inv = NULL; canMove = false; } -Structures::~Structures(){ +Structures::~Structures() { delete tex; - if(name) + if (name) delete[] name; } -Mob::Mob(int sub){ +Mob::Mob(int sub) { type = MOBT; aggressive = false; maxHealth = health = 50; canMove = true; - switch((subtype = sub)){ + switch((subtype = sub)) { case MS_RABBIT: width = HLINE * 10; height = HLINE * 8; @@ -265,13 +265,13 @@ Mob::Mob(int sub){ inv = new Inventory(NPC_INV_SIZE); } -Mob::~Mob(){ +Mob::~Mob() { delete inv; delete tex; delete[] name; } -Object::Object(){ +Object::Object() { type = OBJECTT; alive = true; near = false; @@ -285,7 +285,7 @@ Object::Object(){ inv = NULL; } -Object::Object(std::string in, std::string pd){ +Object::Object(std::string in, std::string pd) { iname = in; pickupDialog = pd; @@ -301,13 +301,13 @@ Object::Object(std::string in, std::string pd){ tex = new Texturec(1,getItemTexturePath(in)); inv = NULL; } -Object::~Object(){ +Object::~Object() { delete tex; delete[] name; } -void Object::reloadTexture(void){ - if(tex) +void Object::reloadTexture(void) { + if (tex) delete tex; tex = new Texturec(1,getItemTexturePath(iname)); @@ -315,47 +315,47 @@ void Object::reloadTexture(void){ height = getItemHeight(iname); } -bool Entity::isNear(Entity e){ +bool Entity::isNear(Entity e) { return pow(e.loc.x - loc.x, 2) + pow(e.loc.y - loc.y, 2) <= pow(40 * HLINE, 2); } -void Entity::draw(void){ //draws the entities +void Entity::draw(void) { //draws the entities glPushMatrix(); glColor3ub(255,255,255); if (!alive) return; - if(type==NPCT){ - if(NPCp(this)->aiFunc.size()){ + if (type==NPCT) { + if (NPCp(this)->aiFunc.size()) { glColor3ub(255,255,0); glRectf(loc.x+width/3,loc.y+height,loc.x+width*2/3,loc.y+height+width/3); } - if(gender == MALE){ + if (gender == MALE) { glColor3ub(255,255,255); - }else if(gender == FEMALE){ + }else if (gender == FEMALE) { glColor3ub(255,105,180); } } - if(left){ + if (left) { glScalef(-1.0f,1.0f,1.0f); glTranslatef(0-width-loc.x*2,0,0); } glMatrixMode(GL_TEXTURE); glLoadIdentity(); glEnable(GL_TEXTURE_2D); - switch(type){ + switch(type) { case PLAYERT: static int texState = 0; - if(speed && !(loops % ((2.0f/speed) < 1 ? 1 : (int)((float)2.0f/(float)speed)))){ - if(++texState==9)texState=1; + if (speed && !(loops % ((2.0f/speed) < 1 ? 1 : (int)((float)2.0f/(float)speed)))) { + if (++texState==9)texState=1; glActiveTexture(GL_TEXTURE0); tex->bind(texState); } - if(!ground){ + if (!ground) { glActiveTexture(GL_TEXTURE0 + 0); tex->bind(0); - }else if(vel.x){ + }else if (vel.x) { glActiveTexture(GL_TEXTURE0 + 0); tex->bind(texState); }else{ @@ -364,7 +364,7 @@ void Entity::draw(void){ //draws the entities } break; case MOBT: - switch(subtype){ + switch(subtype) { case MS_RABBIT: glActiveTexture(GL_TEXTURE0 + 0); tex->bind(!ground); @@ -382,8 +382,8 @@ void Entity::draw(void){ //draws the entities } break; case STRUCTURET: - for(auto &strt : currentWorld->build){ - if(this == strt){ + for(auto &strt : currentWorld->build) { + if (this == strt) { glActiveTexture(GL_TEXTURE0); tex->bind(0); break; @@ -465,14 +465,14 @@ wander(int timeRun) vel.x *= direction; } - if(vel.x < 0) + if (vel.x < 0) currentWorld->goWorldLeft(this); ticksToUse--; } -void NPC::addAIFunc(int (*func)(NPC *),bool preload){ - if(preload){ // Preload AI functions so that they're given after +void NPC::addAIFunc(int (*func)(NPC *),bool preload) { + if (preload) { // Preload AI functions so that they're given after // the current dialog box is closed AIpreload.push_back(func); AIpreaddr.push_back(this); @@ -480,11 +480,11 @@ void NPC::addAIFunc(int (*func)(NPC *),bool preload){ else aiFunc.push_back(func); } -void NPC::clearAIFunc(void){ +void NPC::clearAIFunc(void) { aiFunc.clear(); } -void NPC::interact(){ //have the npc's interact back to the player +void NPC::interact() { //have the npc's interact back to the player std::thread([this]{ int (*func)(NPC *); loc.y += 5; @@ -493,11 +493,11 @@ void NPC::interact(){ //have the npc's interact back to the player left = (player->loc.x < loc.x); right = !left; - if(aiFunc.size()){ + if (aiFunc.size()) { func=aiFunc.front(); - if(!func(this)){ - if(aiFunc.size())aiFunc.erase(aiFunc.begin()); + if (!func(this)) { + if (aiFunc.size())aiFunc.erase(aiFunc.begin()); } }else{ ui::dialogBox(name,NULL,false,randomDialog[randDialog]); @@ -507,7 +507,7 @@ void NPC::interact(){ //have the npc's interact back to the player }).detach(); } -void Merchant::wander(int timeRun){ +void Merchant::wander(int timeRun) { static int direction; if (forcedMove) @@ -525,7 +525,7 @@ void Merchant::wander(int timeRun){ vel.x *= direction; } - if(vel.x < 0) + if (vel.x < 0) currentWorld->goWorldLeft(this); if (inside != nullptr) { loc.y = inside->loc.y + HLINE * 2; @@ -538,17 +538,23 @@ void Merchant::wander(int timeRun){ ticksToUse--; } -void Merchant::interact(){ +void Merchant::interact() { std::thread([this]{ - ui::merchantBox(name, trade[currTrade], ":Accept:Good-Bye", false, "Welcome to Smithy\'s. Buy your sausages here you freaking meme lording screw-face"); + ui::merchantBox(name, trade[currTrade], ":Accept:Good-Bye", false, toSay->c_str()); ui::waitForDialog(); // handle normal dialog options switch (ui::dialogOptChosen) { // Accept case 1: - if (!(player->inv->takeItem(trade[currTrade].item[1], trade[currTrade].quantity[1]))) + if (!(player->inv->takeItem(trade[currTrade].item[1], trade[currTrade].quantity[1]))) { player->inv->addItem(trade[currTrade].item[0],trade[currTrade].quantity[0]); + toSay = &text[1]; + interact(); + } else { + toSay = &text[2]; + interact(); + } break; // Good-bye @@ -579,16 +585,17 @@ void Merchant::interact(){ default: break; + toSay = &text[0]; } }).detach(); } -void Object::interact(void){ +void Object::interact(void) { std::thread([this]{ - if(questObject && alive){ + if (questObject && alive) { ui::dialogBox(player->name, ":Yes:No", false, pickupDialog.c_str()); ui::waitForDialog(); - if(ui::dialogOptChosen == 1){ + if (ui::dialogOptChosen == 1) { player->inv->addItem(iname, 1); alive = false; } @@ -606,7 +613,7 @@ bool Entity::isInside(vec2 coord) const { coord.y <= loc.y + height; } -void Entity::follow(Entity *e){ +void Entity::follow(Entity *e) { followee = e; } @@ -621,7 +628,7 @@ void Entity::follow(Entity *e){ * point to have non-normal traits so it could be invisible or invincible... */ -unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ +unsigned int Structures::spawn(BUILD_SUB sub, float x, float y) { loc.x = x; loc.y = y; type = STRUCTURET; @@ -642,7 +649,7 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ if (textureLoc.empty()) textureLoc = inWorld->getSTextureLocation(sub); - switch(sub){ + switch(sub) { case STALL_MARKET: tex = new Texturec({ textureLoc }); dim = Texture::imageDim(textureLoc); @@ -667,7 +674,7 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ * See NPC::wander for the explaination of the argument's variable */ -void Mob::wander(int timeRun){ +void Mob::wander(int timeRun) { static int direction; //variable to decide what direction the entity moves static unsigned int heya=0,hi=0; static bool YAYA = false; @@ -687,9 +694,9 @@ void Mob::wander(int timeRun){ return; } - if(aggressive && !YAYA && + if (aggressive && !YAYA && player->loc.x + (width / 2) > loc.x && player->loc.x + (width / 2) < loc.x + width && - player->loc.y + (height / 3) > loc.y && player->loc.y + (height / 3) < loc.y + height){ + player->loc.y + (height / 3) > loc.y && player->loc.y + (height / 3) < loc.y + height) { if (!ui::dialogBoxExists) { Arena *a = new Arena(currentWorld,player,this); a->setStyle(""); @@ -705,16 +712,16 @@ void Mob::wander(int timeRun){ } } - switch(subtype){ + switch(subtype) { case MS_RABBIT: - if(!ticksToUse){ + if (!ticksToUse) { ticksToUse = timeRun; direction = (getRand() % 3 - 1); //sets the direction to either -1, 0, 1 //this lets the entity move left, right, or stay still - if(direction==0)ticksToUse/=2; + if (direction==0)ticksToUse/=2; vel.x *= direction; //changes the velocity based off of the direction } - if(ground && direction){ + if (ground && direction) { vel.y=.15; loc.y+=HLINE*.25; ground=false; @@ -723,19 +730,19 @@ void Mob::wander(int timeRun){ ticksToUse--; //removes one off of the entities timer break; case MS_BIRD: - if(loc.y<=init_y-.2)vel.y=.02*deltaTime; // TODO handle direction + if (loc.y<=init_y-.2)vel.y=.02*deltaTime; // TODO handle direction vel.x=.02*deltaTime; - if(++heya==200){heya=0;hi^=1;} - if(hi)vel.x*=-1; + if (++heya==200) {heya=0;hi^=1;} + if (hi)vel.x*=-1; break; case MS_TRIGGER: - if(player->loc.x + player->width / 2 > loc.x && + if (player->loc.x + player->width / 2 > loc.x && player->loc.x + player->width / 2 < loc.x + width) std::thread([this]{hey(this);}).detach(); //hey(this); break; case MS_PAGE: - if(player->loc.x > loc.x - 100 && player->loc.x < loc.x + 100 && // within player ranger + if (player->loc.x > loc.x - 100 && player->loc.x < loc.x + 100 && // within player ranger ui::mouse.x > loc.x && ui::mouse.x < loc.x + width && // mouse x ui::mouse.y > loc.y - width / 2 && ui::mouse.y < loc.y + width * 1.5 && // mouse y SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)) // right click @@ -746,7 +753,7 @@ void Mob::wander(int timeRun){ } } -void Player::save(void){ +void Player::save(void) { std::string data; std::ofstream out ("xml/main.dat",std::ios::out | std::ios::binary); std::cout<<"Saving player data..."<<std::endl; @@ -767,13 +774,13 @@ void Player::save(void){ out.close(); } -void Player::sspawn(float x,float y){ +void Player::sspawn(float x,float y) { unsigned int i; uint count; std::ifstream in (std::string(xmlFolder + "main.dat"),std::ios::in | std::ios::binary); spawn(x,y); - if(in.good()){ + if (in.good()) { std::istringstream data; std::string ddata; std::streampos len; @@ -799,7 +806,7 @@ void Player::sspawn(float x,float y){ tickCount = std::stoi(ddata); std::getline(data,ddata); - for(i = std::stoi(ddata);i;i--){ + for(i = std::stoi(ddata);i;i--) { std::getline(data,ddata); count = std::stoi(ddata); std::getline(data,ddata); diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 66b7f1d..e314be4 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -9,7 +9,6 @@ using namespace tinyxml2; extern Player *player; // main.cpp extern World *currentWorld; // main.cpp -extern Menu *currentMenu; extern Menu pauseMenu; extern Menu optionsMenu; @@ -28,7 +27,7 @@ inline void segFault() { (*((int *)NULL))++; } -int commonAIFunc(NPC *speaker){ +int commonAIFunc(NPC *speaker) { XMLDocument xml; XMLElement *exml,*oxml; @@ -59,21 +58,21 @@ int commonAIFunc(NPC *speaker){ */ do{ - if(!strcmp(exml->Name(),"text")){ - if(exml->UnsignedAttribute("id") == (unsigned)speaker->dialogIndex){ + if (!strcmp(exml->Name(),"text")) { + if (exml->UnsignedAttribute("id") == (unsigned)speaker->dialogIndex) { /* * Handle any quest tags */ - if((oxml = exml->FirstChildElement("quest"))){ + if ((oxml = exml->FirstChildElement("quest"))) { std::string qname; while (oxml) { if (!(qname = oxml->StrAttribute("assign")).empty()) player->qh.assign(qname,"None",(std::string)oxml->GetText()); - else if(!(qname = oxml->StrAttribute("check")).empty()){ - if(player->qh.hasQuest(qname) && player->qh.finish(qname)){ + else if (!(qname = oxml->StrAttribute("check")).empty()) { + if (player->qh.hasQuest(qname) && player->qh.finish(qname)) { goto CONT; }else{ oldidx = speaker->dialogIndex; @@ -92,8 +91,8 @@ CONT: * Handle any 'give' requests. */ - if((oxml = exml->FirstChildElement("give"))){ - while(oxml){ + if ((oxml = exml->FirstChildElement("give"))) { + while(oxml) { player->inv->addItem(oxml->Attribute("id"),oxml->UnsignedAttribute("count")); oxml = oxml->NextSiblingElement(); } @@ -103,8 +102,8 @@ CONT: * Handle any 'take' requests. */ - if((oxml = exml->FirstChildElement("take"))){ - while(oxml){ + if ((oxml = exml->FirstChildElement("take"))) { + while(oxml) { player->inv->takeItem(oxml->Attribute("id"),oxml->UnsignedAttribute("count")); oxml = oxml->NextSiblingElement(); } @@ -121,7 +120,7 @@ CONT: * Handle dialog options. */ - if((oxml = exml->FirstChildElement("option"))){ + if ((oxml = exml->FirstChildElement("option"))) { /* * Convert the list of options into a single colon-separated string. @@ -129,7 +128,7 @@ CONT: std::string optstr; - while(oxml){ + while(oxml) { /* * Create a buffer big enough for the next option. @@ -153,7 +152,7 @@ CONT: ui::dialogBox(speaker->name,optstr.c_str(),false,exml->GetText()+1); ui::waitForDialog(); - if(ui::dialogOptChosen) + if (ui::dialogOptChosen) exml = dopt[ui::dialogOptChosen-1]; while(!dopt.empty()) @@ -172,10 +171,10 @@ CONT: * Give another NPC dialog if requested. */ - if((name = exml->Attribute("call"))){ - for(auto &n : currentWorld->npc){ - if(!strcmp(n->name,name)){ - if(exml->QueryUnsignedAttribute("callid",&idx) == XML_NO_ERROR) + if ((name = exml->Attribute("call"))) { + for(auto &n : currentWorld->npc) { + if (!strcmp(n->name,name)) { + if (exml->QueryUnsignedAttribute("callid",&idx) == XML_NO_ERROR) n->dialogIndex = idx; n->addAIFunc(commonAIFunc,false); break; @@ -187,18 +186,18 @@ CONT: * Handle the next dialog block if this one leads to another. */ - if(exml->QueryUnsignedAttribute("nextid",&idx) == XML_NO_ERROR){ + if (exml->QueryUnsignedAttribute("nextid",&idx) == XML_NO_ERROR) { speaker->dialogIndex = idx; - if(exml->QueryBoolAttribute("stop",&stop) == XML_NO_ERROR && stop){ + if (exml->QueryBoolAttribute("stop",&stop) == XML_NO_ERROR && stop) { speaker->dialogIndex = 9999; return 0; - }else if(exml->QueryBoolAttribute("pause",&stop) == XML_NO_ERROR && stop){ + }else if (exml->QueryBoolAttribute("pause",&stop) == XML_NO_ERROR && stop) { //speaker->dialogIndex = 9999; return 1; }else return commonAIFunc(speaker); }else{ - if(oldidx != 9999){ + if (oldidx != 9999) { speaker->dialogIndex = oldidx; oldidx = 9999; return 1; @@ -218,19 +217,19 @@ CONT: return 0; } -void commonPageFunc(Mob *callee){ +void commonPageFunc(Mob *callee) { ui::drawPage(callee->heyid); ui::waitForDialog(); callee->health = 0; } -void commonTriggerFunc(Mob *callee){ +void commonTriggerFunc(Mob *callee) { static bool lock = false; XMLDocument xml; XMLElement *exml; char *text,*pch; - if(!lock){ + if (!lock) { lock = true; xml.LoadFile(currentXML.c_str()); @@ -248,7 +247,7 @@ void commonTriggerFunc(Mob *callee){ strcpy(text,exml->GetText()); pch = strtok(text,"\n"); - while(pch){ + while(pch) { ui::importantText(pch); ui::waitForDialog(); @@ -264,7 +263,7 @@ void commonTriggerFunc(Mob *callee){ } } -void initEverything(void){ +void initEverything(void) { std::vector<std::string> xmlFiles; XMLDocument xml; @@ -285,8 +284,8 @@ void initEverything(void){ * Load the first file found as currentWorld. */ - for(unsigned int i=0;i<xmlFiles.size();i++){ - if(xmlFiles[i] != "." && xmlFiles[i] != ".." && strcmp(xmlFiles[i].c_str()+xmlFiles[i].size()-3,"dat")){ + for(unsigned int i=0;i<xmlFiles.size();i++) { + if (xmlFiles[i] != "." && xmlFiles[i] != ".." && strcmp(xmlFiles[i].c_str()+xmlFiles[i].size()-3,"dat")) { /* * Read in the XML file. @@ -304,15 +303,12 @@ void initEverything(void){ pauseMenu.items.push_back(ui::menu::createButton({-256/2,-200},{256,75},{0.0f,0.0f,0.0f}, "Save and Quit", ui::quitGame)); pauseMenu.items.push_back(ui::menu::createButton({-256/2,-300},{256,75},{0.0f,0.0f,0.0f}, "Segfault", segFault)); pauseMenu.child = &optionsMenu; - pauseMenu.parent = NULL; optionsMenu.items.push_back(ui::menu::createSlider({0-(float)SCREEN_WIDTH/4,0-(512/2)}, {50,512}, {0.0f, 0.0f, 0.0f}, 0, 100, "Master", &VOLUME_MASTER)); optionsMenu.items.push_back(ui::menu::createSlider({-200,100}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "Music", &VOLUME_MUSIC)); optionsMenu.items.push_back(ui::menu::createSlider({-200,000}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "SFX", &VOLUME_SFX)); - optionsMenu.child = NULL; optionsMenu.parent = &pauseMenu; - // optionsMenu.push_back(ui::menu::createButton({-256/2,-200},{256,75},{0.0f,0.0f,0.0f}, (const char*)("Save and Quit"),); /* * Spawn the player and begin the game. @@ -325,7 +321,7 @@ void initEverything(void){ atexit(destroyEverything); } -void destroyEverything(void){ +void destroyEverything(void) { currentWorld->save(); //delete currentWorld; //delete[] currentXML; diff --git a/src/inventory.cpp b/src/inventory.cpp index 1b2d378..8bde00e 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -14,15 +14,29 @@ static const unsigned char numSlot = 7; Mix_Chunk* swordSwing; static std::vector<Item *> itemMap; +static std::vector<Currency *> currencyMap; static GLuint *itemtex; void itemDraw(Player *p,uint id); -void items(void){ +void items(void) { XMLDocument xml; - XMLElement *exml; xml.LoadFile("config/items.xml"); - exml = xml.FirstChildElement("item"); - while(exml){ + XMLElement *exml = xml.FirstChildElement("item"); + XMLElement *cxml = xml.FirstChildElement("currency"); + while (cxml) { + + currencyMap.push_back(new Currency()); + + currencyMap.back()->width = 7*HLINE; + currencyMap.back()->height = 7*HLINE; + + currencyMap.back()->name = cxml->Attribute("name"); + currencyMap.back()->value = cxml->FloatAttribute("value"); + + + cxml = cxml->NextSiblingElement(); + } + while (exml) { itemMap.push_back(new Item()); @@ -40,11 +54,11 @@ void items(void){ } } -int Inventory::addItem(std::string name,uint count){ - for(unsigned int i=0;i<itemMap.size();i++){ - if(itemMap[i]->name == name){ - for(auto &in : items){ - if(in.id == i){ +int Inventory::addItem(std::string name,uint count) { + for(unsigned int i=0;i<itemMap.size();i++) { + if (itemMap[i]->name == name) { + for(auto &in : items) { + if (in.id == i) { in.count += count; return 0; } @@ -56,34 +70,34 @@ int Inventory::addItem(std::string name,uint count){ return -1; } -int Inventory::takeItem(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){ + for(unsigned int i=0;i<itemMap.size();i++) { + if (itemMap[i]->name == name) { id = i; break; } } - if(id == 999999) + if (id == 999999) return -1; //if no such item exists /* * Inventory lookup */ - for(unsigned int i=0;i<items.size();i++){ - if(items[i].id == id){ - if(count > items[i].count) + for(unsigned int i=0;i<items.size();i++) { + if (items[i].id == id) { + if (count > items[i].count) return -(items[i].count - count); else{ items[i].count -= count; - if(!items[i].count) + if (!items[i].count) items.erase(items.begin()+i); } return 0; @@ -92,33 +106,33 @@ int Inventory::takeItem(std::string name,uint count){ return -2; } -int Inventory::hasItem(std::string name){ +int Inventory::hasItem(std::string name) { unsigned int id = 999999; - for(unsigned int i=0;i<itemMap.size();i++){ - if(itemMap[i]->name == name){ + for(unsigned int i=0;i<itemMap.size();i++) { + if (itemMap[i]->name == name) { id = i; break; } } - if(id == 999999) + if (id == 999999) return 0; - for(auto &i : items){ - if(i.id == id) + for(auto &i : items) { + if (i.id == id) return i.count; } return 0; } -void initInventorySprites(void){ +void initInventorySprites(void) { items(); itemtex = new GLuint[itemMap.size()]; - for(unsigned int i = 0;i<itemMap.size();i++){ + for(unsigned int i = 0;i<itemMap.size();i++) { itemtex[i] = Texture::loadTexture(getItemTexturePath(itemMap[i]->name)); } @@ -126,9 +140,9 @@ void initInventorySprites(void){ Mix_Volume(2,100); } -void destroyInventory(void){ +void destroyInventory(void) { - while(!itemMap.empty()){ + while(!itemMap.empty()) { delete itemMap.front(); itemMap.erase(itemMap.begin()); } @@ -136,15 +150,15 @@ void destroyInventory(void){ Mix_FreeChunk(swordSwing); } -const char *getItemTexturePath(std::string name){ - for(auto &i : itemMap){ - if(i->name == name) +const char *getItemTexturePath(std::string name) { + for(auto &i : itemMap) { + if (i->name == name) return i->texloc.c_str(); } return NULL; } -GLuint getItemTexture(std::string name){ +GLuint getItemTexture(std::string name) { for (int i = itemMap.size(); i--;) { if (itemMap[i]->name == name) return itemtex[i]; @@ -153,45 +167,45 @@ GLuint getItemTexture(std::string name){ return 0; } -float getItemWidth(std::string name){ - for(auto &i : itemMap){ - if(i->name == name) +float getItemWidth(std::string name) { + for(auto &i : itemMap) { + if (i->name == name) return i->width; } return 0; } -float getItemHeight(std::string name){ - for(auto &i : itemMap){ - if(i->name == name) +float getItemHeight(std::string name) { + for(auto &i : itemMap) { + if (i->name == name) return i->height; } return 0; } -Inventory::Inventory(unsigned int s){ +Inventory::Inventory(unsigned int s) { sel=0; size=s; } -Inventory::~Inventory(void){ +Inventory::~Inventory(void) { } -void Inventory::setSelection(unsigned int s){ +void Inventory::setSelection(unsigned int s) { sel=s; } -void Inventory::setSelectionUp(){ +void Inventory::setSelectionUp() { if (!sel--) sel++; } -void Inventory::setSelectionDown(){ +void Inventory::setSelectionDown() { if (++sel >= numSlot) sel = numSlot - 1; } -void Inventory::draw(void){ +void Inventory::draw(void) { static std::vector<int>dfp(numSlot); static std::vector<Ray>iray(numSlot); static std::vector<vec2>curCoord(numSlot); @@ -257,11 +271,12 @@ void Inventory::draw(void){ a++; } a = 0; - while (++a < massOrder.size()) { - if (!a || massDfp[ massOrder[a - 1] ] > massRange * 0.75f) - massDfp[ massOrder[a] ] += 5.0f * deltaTime; - if (massDfp[ massOrder[a] ] > massRange) - massDfp[ massOrder[a] ] = massRange; + while (a < massOrder.size()) { + if (!a || massDfp[massOrder[a - 1]] > massRange * 0.75f) + massDfp[massOrder[a]] += 5.0f * deltaTime; + if (massDfp[massOrder[a]] > massRange) + massDfp[massOrder[a]] = massRange; + a++; } a = 0; if (numSlot > 0) @@ -276,15 +291,15 @@ void Inventory::draw(void){ cd -= 1.0f * deltaTime; } - a = 0; - while (++a < massRay.size()) { - if (!a || massDfp[ massOrderClosing[a - 1] ] <= 0) - massDfp[ massOrderClosing[a] ] -= 10.0f * deltaTime; - if (massDfp[ massOrderClosing[a - 1] ] < 0) - massDfp[ massOrderClosing[a - 1] ] = 0; + while (a < massRay.size()) { + if (!a || massDfp[massOrderClosing[a - 1]] <= 0) + massDfp[massOrderClosing[a]] -= 10.0f * deltaTime; + else if (massDfp[massOrderClosing[a - 1]] < 0) + massDfp[massOrderClosing[a - 1]] = 0; + a++; } a = 0; - if (std::all_of(std::begin(massDfp), std::end(massDfp), [](auto d){ return d <= 0; })) { + if (std::all_of(std::begin(massDfp), std::end(massDfp), [](auto d) { return d <= 0; })) { invOpen = false; for (auto &md : massDfp) { if (md < 0) @@ -299,9 +314,9 @@ void Inventory::draw(void){ */ C("Start drawing inventory"); - if(invOpen){ + if (invOpen) { - for(auto &mr : massRay){ + for(auto &mr : massRay) { glColor4f(0.0f,0.0f,0.0f, ((float)massDfp[a]/(float)massRange)*.5f); glBegin(GL_QUADS); glVertex2i(mr.x-(itemWide/2), mr.y-(itemWide/2)); @@ -309,12 +324,12 @@ void Inventory::draw(void){ glVertex2i(mr.x-(itemWide/2)+itemWide,mr.y-(itemWide/2)+itemWide); glVertex2i(mr.x-(itemWide/2), mr.y-(itemWide/2)+itemWide); glEnd(); - if(!items.empty() && a < items.size() && items[a+numSlot].count){ + if (!items.empty() && a+numSlot < items.size() && items[a+numSlot].count) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, itemtex[items[a+numSlot].id]); glColor4f(1.0f, 1.0f, 1.0f, ((float)massDfp[a]/(float)(massRange?massRange:1))*0.8f); glBegin(GL_QUADS); - if(itemMap[items[a].id]->height > itemMap[items[a+numSlot].id]->width){ + if (itemMap[items[a+numSlot].id]->height > itemMap[items[a+numSlot].id]->width) { glTexCoord2i(0,1);glVertex2i(mr.x-((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)), mr.y-(itemWide/2)); glTexCoord2i(1,1);glVertex2i(mr.x+((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)), mr.y-(itemWide/2)); glTexCoord2i(1,0);glVertex2i(mr.x+((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)), mr.y+(itemWide/2)); @@ -334,7 +349,7 @@ void Inventory::draw(void){ a++; }a=0; - for(auto &cr : curRay){ + for(auto &cr : curRay) { curCurCoord[a].x -= float((curdfp[a]) * cos(-1)); curCurCoord[a].y += float((curdfp[a]) * sin(0)); cr.end = curCurCoord[a]; @@ -349,7 +364,7 @@ void Inventory::draw(void){ a++; }a=0; - for(auto &r : iray){ + for(auto &r : iray) { angle = 180 - (angleB * a) - angleB / 2.0f; curCoord[a].x += float((dfp[a]) * cos(angle*PI/180)); curCoord[a].y += float((dfp[a]) * sin(angle*PI/180)); @@ -363,12 +378,12 @@ void Inventory::draw(void){ glVertex2i(r.end.x-(itemWide/2), r.end.y-(itemWide/2)+itemWide); glEnd(); - if(!items.empty() && a < items.size() && items[a].count){ + if (!items.empty() && a < numSlot && items[a].count) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, itemtex[items[a].id]); glColor4f(1.0f, 1.0f, 1.0f, ((float)dfp[a]/(float)(range?range:1))*0.8f); glBegin(GL_QUADS); - if(itemMap[items[a].id]->height > itemMap[items[a].id]->width){ + if (itemMap[items[a].id]->height > itemMap[items[a].id]->width) { glTexCoord2i(0,1);glVertex2i(r.end.x-((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)), r.end.y-(itemWide/2)); glTexCoord2i(1,1);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)), r.end.y-(itemWide/2)); glTexCoord2i(1,0);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)), r.end.y+(itemWide/2)); @@ -387,15 +402,15 @@ void Inventory::draw(void){ ui::setFontColor(255,255,255,255); } - if(sel == a){ + if (sel == a) { static float sc = 1; static bool up; up ? sc += .0005*deltaTime : sc -= .0005*deltaTime; - if(sc > 1.2){ + if (sc > 1.2) { up = false; sc = 1.2; } - if(sc < 1.0){ + if (sc < 1.0) { up = true; sc = 1.0; } @@ -425,13 +440,13 @@ void Inventory::draw(void){ a++; } C("Done drawing standard inv"); - }else if(invHover){ + }else if (invHover) { static unsigned int highlight = 0; static unsigned int thing = 0; std::cout<<"Inventory2???"<<std::endl; - if(!mouseSel){ + if (!mouseSel) { mouseStart.x = ui::mouse.x - offset.x; std::cout << "Setting highlight" << std::endl; highlight = sel; @@ -442,26 +457,26 @@ void Inventory::draw(void){ std::cout << "Done" << std::endl; }else{ std::cout << "Is mousex greater than the start" << std::endl; - if((ui::mouse.x - offset.x) >= mouseStart.x){ + if ((ui::mouse.x - offset.x) >= mouseStart.x) { std::cout << "Thing" << std::endl; thing = (ui::mouse.x - offset.x - mouseStart.x)/80; std::cout << "Highlight" << std::endl; highlight=sel+thing; std::cout << "Highlight Check" << std::endl; - if(highlight>numSlot-1)highlight=numSlot-1; + if (highlight>numSlot-1)highlight=numSlot-1; std::cout << "Left Click" << std::endl; - if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){ + if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) { sel = highlight; mouseSel=false; invHover=false; selected = true; } } - if((ui::mouse.x - offset.x) < mouseStart.x){ + if ((ui::mouse.x - offset.x) < mouseStart.x) { thing = (mouseStart.x - (ui::mouse.x - offset.x))/80; - if((int)sel-(int)thing<0)highlight=0; + if ((int)sel-(int)thing<0)highlight=0; else highlight=sel-thing; - if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){ + if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) { sel = highlight; mouseSel=false; invHover=false; @@ -470,7 +485,7 @@ void Inventory::draw(void){ } } std::cout << "Rays" << std::endl; - for(auto &r : iray){ + for(auto &r : iray) { std::cout << "Setting angle" << std::endl; angle=180-(angleB*a) - angleB/2.0f; std::cout << "Currcourd" << std::endl; @@ -488,21 +503,18 @@ void Inventory::draw(void){ glVertex2i(r.end.x-(itemWide/2), r.end.y+(itemWide/2)); glEnd(); - std::cout << "Draw items" << std::endl; - if(!items.empty() && a < items.size() && items[a].count){ - std::cout << "Jamie" << std::endl; + std::cout << "Draw items," << a << std::endl; + if (!items.empty() && a < numSlot && items[a].count) { + std::cout << "drawing" << std::endl; glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, itemtex[items[a].id]); - glColor4f(1.0f, 1.0f, 1.0f, a == highlight ? 0.8f : 0.2f); - std::cout << "Done Binding" << std::endl; + glColor4f(1.0f, 1.0f, 1.0f, 1.0fa); glBegin(GL_QUADS); - std::cout << "jdjdjd" << std::endl; - if(itemMap[items[a].id]->height > itemMap[items[a].id]->width){ - std::cout << "map" << std::endl; - glTexCoord2i(0,1);glVertex2i(r.end.x-((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),r.end.y-(itemWide/2)); - glTexCoord2i(1,1);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),r.end.y-(itemWide/2)); - glTexCoord2i(1,0);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),r.end.y+(itemWide/2)); - glTexCoord2i(0,0);glVertex2i(r.end.x-((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),r.end.y+(itemWide/2)); + if (itemMap[items[a].id]->height > itemMap[items[a].id]->width) { + glTexCoord2i(0,1);glVertex2i(r.end.x-((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)), r.end.y-(itemWide/2)); + glTexCoord2i(1,1);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)), r.end.y-(itemWide/2)); + glTexCoord2i(1,0);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)), r.end.y+(itemWide/2)); + glTexCoord2i(0,0);glVertex2i(r.end.x-((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)), r.end.y+(itemWide/2)); }else{ glTexCoord2i(0,1);glVertex2i(r.end.x-(itemWide/2),r.end.y-(itemWide/2)*((float)itemMap[items[a].id]->height/(float)itemMap[items[a].id]->width)); glTexCoord2i(1,1);glVertex2i(r.end.x+(itemWide/2),r.end.y-(itemWide/2)*((float)itemMap[items[a].id]->height/(float)itemMap[items[a].id]->width)); @@ -511,34 +523,37 @@ void Inventory::draw(void){ } glEnd(); glDisable(GL_TEXTURE_2D); - std::cout << "Adding a" << std::endl; - a++; + + if (highlight == a) { + std::cout << "Shitting" << std::endl; + std::cout << itemMap[items[a].id]->name << std::endl; + ui::putStringCentered(player->loc.x+player->width/2, player->loc.y + range*.75,itemMap[items[a].id]->name); + } } + a++; } - ui::putStringCentered(player->loc.x+player->width/2, player->loc.y + range*.75,itemMap[items[highlight].id]->name.c_str()); } - - if(!items.empty() && items.size() > sel && items[sel].count) + if (!items.empty() && items.size() > sel && items[sel].count) itemDraw(player,items[sel].id); } -void itemDraw(Player *p,uint id){ +void itemDraw(Player *p,uint id) { static unsigned char inc = 0; itemLoc.y = p->loc.y+(p->height/3); itemLoc.x = p->left?p->loc.x:p->loc.x+p->width; glPushMatrix(); - if(!id)return; + if (!id)return; - if(itemMap[id]->type == "Sword"){ - if(p->left){ - if(hangle < 15){ + if (itemMap[id]->type == "Sword") { + if (p->left) { + if (hangle < 15) { hangle=15.0f; p->inv->usingi = false; } }else{ - if(hangle > -15){ + if (hangle > -15) { hangle=-15.0f; p->inv->usingi = false; } @@ -600,7 +615,7 @@ int Inventory::useItem(void) swing = true; Mix_PlayChannel(2, swordSwing, 0); } - }else if(itemMap[items[sel].id]->type == "Cooked Food"){ + }else if (itemMap[items[sel].id]->type == "Cooked Food") { player->health += itemMap[items[sel].id]->attribValue; usingi = false; } @@ -608,22 +623,22 @@ int Inventory::useItem(void) return 0; } -bool Inventory::detectCollision(vec2 one, vec2 two){ +bool Inventory::detectCollision(vec2 one, vec2 two) { (void)one; (void)two; float xc, yc; float i = 0.0f; - if(items.empty() || !items[sel].count) + if (items.empty() || !items[sel].count) return false; - if(itemMap[items[sel].id]->type == "Sword"){ - while(i<itemMap[items[sel].id]->height){ + if (itemMap[items[sel].id]->type == "Sword") { + while(i<itemMap[items[sel].id]->height) { xc = itemLoc.x; yc = itemLoc.y; xc += float(i) * cos((hangle+90)*PI/180); yc += float(i) * sin((hangle+90)*PI/180); - if(xc >= one.x && xc <= two.x){ - if(yc >= one.y && yc <= two.y){ + if (xc >= one.x && xc <= two.x) { + if (yc >= one.y && yc <= two.y) { return true; } } diff --git a/src/quest.cpp b/src/quest.cpp index afeec6b..aa0bb2e 100644 --- a/src/quest.cpp +++ b/src/quest.cpp @@ -5,7 +5,7 @@ extern Player *player;
-int QuestHandler::assign(std::string title,std::string desc,std::string req){
+int QuestHandler::assign(std::string title,std::string desc,std::string req) {
Quest tmp;
char *tok;
@@ -31,16 +31,16 @@ int QuestHandler::assign(std::string title,std::string desc,std::string req){ return 0;
}
-int QuestHandler::drop(std::string title){
- current.erase(std::remove_if(current.begin(),
+int QuestHandler::drop(std::string title) {
+ current.erase(std::remove_if (current.begin(),
current.end(),
- [&](Quest q){ return q.title == title; }),
+ [&](Quest q) { return q.title == title; }),
current.end());
return 0;
}
-int QuestHandler::finish(std::string t){
+int QuestHandler::finish(std::string t) {
for (auto c = current.begin(); c != current.end(); c++) {
if ((*c).title == t) {
for (auto &n : (*c).need) {
@@ -58,7 +58,7 @@ int QuestHandler::finish(std::string t){ return 0;
}
-bool QuestHandler::hasQuest(std::string t){
+bool QuestHandler::hasQuest(std::string t) {
for (auto &c : current) {
if (c.title == t)
return true;
diff --git a/src/texture.cpp b/src/texture.cpp index a61d426..86038e2 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -31,13 +31,13 @@ static std::vector<texture_t> LoadedTexture; namespace Texture{ Color pixels[8][4]; - GLuint loadTexture(std::string fileName){ + GLuint loadTexture(std::string fileName) { SDL_Surface *image; GLuint object = 0; // check if texture is already loaded - for(auto &t : LoadedTexture){ - if(t.name == fileName){ + for(auto &t : LoadedTexture) { + if (t.name == fileName) { #ifdef DEBUG DEBUG_printf("Reusing loaded texture for %s\n", fileName.c_str()); @@ -48,7 +48,7 @@ namespace Texture{ } // load SDL_surface of texture - if(!(image = IMG_Load(fileName.c_str()))) + if (!(image = IMG_Load(fileName.c_str()))) return 0; #ifdef DEBUG @@ -89,23 +89,23 @@ namespace Texture{ return object; } - dim2 imageDim(std::string fileName){ - for(auto &t : LoadedTexture){ - if(t.name == fileName) + dim2 imageDim(std::string fileName) { + for(auto &t : LoadedTexture) { + if (t.name == fileName) return t.dim; } return {0,0}; } - void freeTextures(void){ - while(!LoadedTexture.empty()){ + void freeTextures(void) { + while(!LoadedTexture.empty()) { glDeleteTextures(1, &LoadedTexture.back().tex); LoadedTexture.pop_back(); } } #define CINDEX_WIDTH (8*4*3) - void initColorIndex(){ + void initColorIndex() { unsigned int i; GLubyte *buffer; GLfloat *bufferf; @@ -122,9 +122,9 @@ namespace Texture{ bufferf[i] = (float)buffer[i] / 255.0f; i = 0; - for(unsigned int y = 0; y < 8; y++){ - for(unsigned int x = 0; x < 4; x++){ - if(i >= CINDEX_WIDTH){ + for(unsigned int y = 0; y < 8; y++) { + for(unsigned int x = 0; x < 4; x++) { + if (i >= CINDEX_WIDTH) { delete[] buffer; delete[] bufferf; return; @@ -140,21 +140,21 @@ namespace Texture{ //sqrt((255-145)^2+(90-145)^2+(0-0)^2); std::vector<index_t>ind; - vec2 getIndex(Color c){ - for(auto &i : ind){ - if(c.red == i.color.red && c.green == i.color.green && c.blue == i.color.blue){ + vec2 getIndex(Color c) { + for(auto &i : ind) { + if (c.red == i.color.red && c.green == i.color.green && c.blue == i.color.blue) { return {float(i.indexx), float(i.indexy)}; } } uint buf[2]; float buff = 999; float shit = 999; - for(uint y = 0; y < 8; y++){ - for(uint x = 0; x < 4; x++){ + for(uint y = 0; y < 8; y++) { + for(uint x = 0; x < 4; x++) { buff = sqrt(pow((pixels[y][x].red- c.red), 2)+ pow((pixels[y][x].green-c.green),2)+ pow((pixels[y][x].blue- c.blue), 2)); - if(buff < shit){ + if (buff < shit) { shit = buff; buf[0] = y; buf[1] = x; @@ -166,7 +166,7 @@ namespace Texture{ } } -Texturec::Texturec(uint amt, ...){ +Texturec::Texturec(uint amt, ...) { va_list fNames; texState = 0; va_start(fNames, amt); @@ -178,32 +178,32 @@ Texturec::Texturec(uint amt, ...){ Texturec::Texturec(std::initializer_list<std::string> l) { texState = 0; - std::for_each(l.begin(), l.end(), [&](std::string s){ image.push_back(Texture::loadTexture(s)); }); + std::for_each(l.begin(), l.end(), [&](std::string s) { image.push_back(Texture::loadTexture(s)); }); } -Texturec::Texturec(std::vector<std::string>v){ +Texturec::Texturec(std::vector<std::string>v) { texState = 0; - std::for_each(v.begin(), v.end(), [&](std::string s){ image.push_back(Texture::loadTexture(s)); }); + std::for_each(v.begin(), v.end(), [&](std::string s) { image.push_back(Texture::loadTexture(s)); }); } -Texturec::Texturec(uint amt,const char **paths){ +Texturec::Texturec(uint amt,const char **paths) { texState = 0; for(unsigned int i = 0; i < amt; i++) image.push_back(Texture::loadTexture(paths[i])); } -Texturec::~Texturec(){ +Texturec::~Texturec() { } -void Texturec::bind(unsigned int bn){ +void Texturec::bind(unsigned int bn) { texState = bn; glBindTexture(GL_TEXTURE_2D,image[(int)texState]); } -void Texturec::bindNext(){ +void Texturec::bindNext() { bind(++texState); } -void Texturec::bindPrev(){ +void Texturec::bindPrev() { bind(--texState); } diff --git a/src/tinyxml2.cpp b/src/tinyxml2.cpp index 1388489..096ff5b 100755 --- a/src/tinyxml2.cpp +++ b/src/tinyxml2.cpp @@ -614,7 +614,7 @@ char* XMLDocument::Identify(char* p, XMLNode** node) TIXMLASSERT(p);
char* const start = p;
p = XMLUtil::SkipWhiteSpace(p);
- if(!*p) {
+ if (!*p) {
*node = 0;
TIXMLASSERT(p);
return p;
@@ -1,7 +1,7 @@ #include <ui.hpp> extern std::vector<menuItem> optionsMenu; -extern Menu *currentMenu; +extern Menu* currentMenu; extern Menu pauseMenu; extern SDL_Window *window; @@ -99,7 +99,7 @@ void loadFontSize(unsigned int size, std::vector<GLuint> &tex, std::vector<FT_In glDeleteTextures(93, tex.data()); glGenTextures(93, tex.data()); // Generate new texture name/locations? - for(char i=33;i<126;i++){ + for(char i=33;i<126;i++) { /* * Load the character from the font family file. @@ -194,7 +194,7 @@ namespace ui { * Initialises the Freetype library, and sets a font size. */ - void initFonts(void){ + void initFonts(void) { if (FT_Init_FreeType(&ftl)) UserError("Couldn't initialize freetype."); @@ -209,7 +209,7 @@ namespace ui { fontSize = 0; } - void destroyFonts(void){ + void destroyFonts(void) { FT_Done_Face(ftf); FT_Done_FreeType(ftl); @@ -222,7 +222,7 @@ namespace ui { * Sets a new font family to use (*.ttf). */ - void setFontFace(const char *ttf){ + void setFontFace(const char *ttf) { if (FT_New_Face(ftl, ttf, 0, &ftf)) UserError("Error! Couldn't open " + (std::string)ttf + "."); @@ -237,9 +237,9 @@ namespace ui { * Sets a new font size (default: 12). */ - void setFontSize(unsigned int size){ + void setFontSize(unsigned int size) { if (size == 16) { - if(!ft16loaded) { + if (!ft16loaded) { loadFontSize(fontSize = size, ftex16, ftdat16); ft16loaded = true; } @@ -261,14 +261,14 @@ namespace ui { * Set a color for font rendering (default: white). */ - void setFontColor(unsigned char r,unsigned char g,unsigned char b){ + void setFontColor(unsigned char r,unsigned char g,unsigned char b) { fontColor[0]=r; fontColor[1]=g; fontColor[2]=b; fontColor[3]=255; } - void setFontColor(unsigned char r,unsigned char g,unsigned char b, unsigned char a){ + void setFontColor(unsigned char r,unsigned char g,unsigned char b, unsigned char a) { fontColor[0]=r; fontColor[1]=g; fontColor[2]=b; @@ -279,7 +279,7 @@ 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 xx,float yy,char c) { vec2 c1,c2; int x = xx, y = yy; @@ -327,19 +327,19 @@ namespace ui { */ do{ - if(i && ((i / 110.0) == (i / 110))){ + if (i && ((i / 110.0) == (i / 110))) { o.y -= fontSize * 1.05f; o.x = x; - if(s[i] == ' ') + if (s[i] == ' ') i++; } - if(i && (i / (float)textWrapLimit == i / textWrapLimit)){ + if (i && (i / (float)textWrapLimit == i / textWrapLimit)) { o.y -= fontSize * 1.05f; o.x = x; // skip a space if it's there since we just newline'd - if(s[i] == ' ') + if (s[i] == ' ') i++; } @@ -490,7 +490,7 @@ namespace ui { char *sopt = strtok(&soptbuf[0], ":"); // cycle through options - while(sopt){ + while(sopt) { dialogOptText.push_back(std::make_pair((std::string)sopt, vec3 {0,0,0})); sopt = strtok(NULL,":"); } @@ -506,7 +506,7 @@ namespace ui { ret.clear(); } - void merchantBox(const char *name,Trade trade,const char *opt,bool passive,const char *text,...){ + void merchantBox(const char *name,Trade trade,const char *opt,bool passive,const char *text,...) { va_list dialogArgs; std::unique_ptr<char[]> printfbuf (new char[512]); @@ -529,12 +529,12 @@ namespace ui { merchOptChosen = 0; // handle options if desired - if(opt){ + if (opt) { std::string soptbuf = opt; char *sopt = strtok(&soptbuf[0], ":"); // cycle through options - while(sopt){ + while(sopt) { dialogOptText.push_back(std::make_pair((std::string)sopt, vec3 {0,0,0})); sopt = strtok(NULL,":"); } @@ -549,7 +549,7 @@ namespace ui { ret.clear(); } - void merchantBox(){ + void merchantBox() { textWrapLimit = 50; dialogMerchant = true; } @@ -573,7 +573,7 @@ namespace ui { while (millis() < target); } - void importantText(const char *text,...){ + void importantText(const char *text,...) { va_list textArgs; char *printfbuf; @@ -590,7 +590,7 @@ namespace ui { dialogImportant = true; } - void passiveImportantText(int duration, const char *text, ...){ + void passiveImportantText(int duration, const char *text, ...) { va_list textArgs; char *printfbuf; @@ -615,7 +615,7 @@ namespace ui { pageTexReady = true; } - void draw(void){ + void draw(void) { unsigned char i; float x,y,tmp; std::string rtext; @@ -635,17 +635,17 @@ namespace ui { rtext=typeOut(dialogBoxText); - if(dialogImportant){ + if (dialogImportant) { setFontColor(255,255,255); - if(dialogPassive){ + if (dialogPassive) { dialogPassiveTime -= deltaTime; - if(dialogPassiveTime < 0){ + if (dialogPassiveTime < 0) { dialogPassive = false; dialogImportant = false; dialogBoxExists = false; } } - if(fadeIntensity == 255 || dialogPassive){ + if (fadeIntensity == 255 || dialogPassive) { setFontSize(24); putStringCentered(offset.x,offset.y,rtext.c_str()); setFontSize(16); @@ -710,8 +710,8 @@ namespace ui { merchArrowLoc[0].z = offset.x - (SCREEN_WIDTH / 8.5); merchArrowLoc[1].z = offset.x + (SCREEN_WIDTH / 8.5); - for(i = 0; i < 2; i++){ - if(((merchArrowLoc[i].x < merchArrowLoc[i].z) ? + for(i = 0; i < 2; i++) { + if (((merchArrowLoc[i].x < merchArrowLoc[i].z) ? (mouse.x > merchArrowLoc[i].x && mouse.x < merchArrowLoc[i].z) : (mouse.x < merchArrowLoc[i].x && mouse.x > merchArrowLoc[i].z)) && mouse.y > merchArrowLoc[i].y - 8 && mouse.y < merchArrowLoc[i].y + 8) { @@ -728,7 +728,7 @@ namespace ui { // draw / handle dialog options if they exist - for(i = 0; i < dialogOptText.size(); i++){ + for(i = 0; i < dialogOptText.size(); i++) { setFontColor(255, 255, 255); // draw option @@ -740,8 +740,8 @@ namespace ui { dialogOptText[i].second.x = offset.x - tmp; // make text yellow if the mouse hovers over the text - if(mouse.x > dialogOptText[i].second.x && mouse.x < dialogOptText[i].second.z && - mouse.y > dialogOptText[i].second.y && mouse.y < dialogOptText[i].second.y + 16){ + if (mouse.x > dialogOptText[i].second.x && mouse.x < dialogOptText[i].second.z && + mouse.y > dialogOptText[i].second.y && mouse.y < dialogOptText[i].second.y + 16) { setFontColor(255, 255, 0); putStringCentered(offset.x, dialogOptText[i].second.y, dialogOptText[i].first); } @@ -771,16 +771,16 @@ namespace ui { putString(x+HLINE,y-fontSize-HLINE,rtext); - for(i=0;i<dialogOptText.size();i++){ + for(i=0;i<dialogOptText.size();i++) { setFontColor(255,255,255); tmp = putStringCentered(offset.x,dialogOptText[i].second.y,dialogOptText[i].first); dialogOptText[i].second.z = offset.x + tmp; dialogOptText[i].second.x = offset.x - tmp; dialogOptText[i].second.y = y - SCREEN_HEIGHT / 4 + (fontSize + HLINE) * (i + 1); - if(mouse.x > dialogOptText[i].second.x && + if (mouse.x > dialogOptText[i].second.x && mouse.x < dialogOptText[i].second.z && mouse.y > dialogOptText[i].second.y && - mouse.y < dialogOptText[i].second.y + 16){ // fontSize + mouse.y < dialogOptText[i].second.y + 16) { // fontSize setFontColor(255,255,0); putStringCentered(offset.x,dialogOptText[i].second.y,dialogOptText[i].first); } @@ -794,7 +794,7 @@ namespace ui { Mix_PlayChannel(1, dialogClick, 0); } - }if(!fadeIntensity){ + }if (!fadeIntensity) { vec2 hub = { (SCREEN_WIDTH/2+offset.x)-fontSize*10, (offset.y+SCREEN_HEIGHT/2)-fontSize @@ -803,7 +803,7 @@ namespace ui { putText(hub.x,hub.y,"Health: %u/%u",player->health>0?(unsigned)player->health:0, (unsigned)player->maxHealth ); - if(player->alive){ + if (player->alive) { glColor3ub(150,0,0); hub.y-=fontSize*1.15; glRectf(hub.x, @@ -822,13 +822,13 @@ namespace ui { */ setFontColor(255,255,255,fontTransInv); - if(player->inv->invOpen){ + if (player->inv->invOpen) { hub.y = player->loc.y + fontSize * 8; hub.x = player->loc.x;// + player->width / 2; putStringCentered(hub.x,hub.y,"Current Quests:"); - for(auto &c : player->qh.current){ + for(auto &c : player->qh.current) { hub.y -= fontSize * 1.15; putStringCentered(hub.x,hub.y,c.title.c_str()); } @@ -847,7 +847,7 @@ namespace ui { } } - void quitGame(){ + void quitGame() { dialogBoxExists = false; currentMenu = NULL; gameRunning = false; @@ -855,12 +855,12 @@ namespace ui { config::save(); } - void closeBox(){ + void closeBox() { dialogBoxExists = false; dialogMerchant = false; } - void dialogAdvance(void){ + void dialogAdvance(void) { unsigned char i; if (pageTex) { @@ -870,16 +870,16 @@ namespace ui { return; } - /*if(!typeOutDone){ + /*if (!typeOutDone) { typeOutDone = true; return; }*/ - for(i=0;i<dialogOptText.size();i++){ - if(mouse.x > dialogOptText[i].second.x && + for(i=0;i<dialogOptText.size();i++) { + if (mouse.x > dialogOptText[i].second.x && mouse.x < dialogOptText[i].second.z && mouse.y > dialogOptText[i].second.y && - mouse.y < dialogOptText[i].second.y + 16){ // fontSize + mouse.y < dialogOptText[i].second.y + 16) { // fontSize dialogOptChosen = i + 1; goto EXIT; } @@ -890,10 +890,10 @@ namespace ui { // TODO neaten this if statement - if(((merchArrowLoc[i].x < merchArrowLoc[i].z) ? + if (((merchArrowLoc[i].x < merchArrowLoc[i].z) ? (mouse.x > merchArrowLoc[i].x && mouse.x < merchArrowLoc[i].z) : (mouse.x < merchArrowLoc[i].x && mouse.x > merchArrowLoc[i].z) && - mouse.y > merchArrowLoc[i].y - 8 && mouse.y < merchArrowLoc[i].y + 8)){ + mouse.y > merchArrowLoc[i].y - 8 && mouse.y < merchArrowLoc[i].y + 8)) { merchOptChosen = i + 1; goto EXIT; } @@ -902,20 +902,20 @@ namespace ui { EXIT: - //if(!dialogMerchant)closeBox(); + //if (!dialogMerchant)closeBox(); dialogBoxExists = false; dialogMerchant = false; //DONE: // handle important text - if(dialogImportant){ + if (dialogImportant) { dialogImportant = false; setFontSize(16); } } - void handleEvents(void){ + void handleEvents(void) { static bool left=true,right=false; static int heyOhLetsGo = 0; static int mouseWheelUpCount = 0, mouseWheelDownCount = 0; @@ -930,8 +930,8 @@ EXIT: static vec2 fr; static Entity *ig; - while(SDL_PollEvent(&e)){ - switch(e.type){ + while(SDL_PollEvent(&e)) { + switch(e.type) { // escape - quit game case SDL_QUIT: @@ -964,14 +964,14 @@ EXIT: if ((e.button.button & SDL_BUTTON_LEFT) && !dialogBoxExists) player->inv->usingi = true; - if(mouse.x > player->loc.x && mouse.x < player->loc.x + player->width && + if (mouse.x > player->loc.x && mouse.x < player->loc.x + player->width && mouse.y > player->loc.y && mouse.y < player->loc.y + player->height) { player->vel.y = .05; fr = mouse; ig = player; } else { for (auto &e : currentWorld->entity) { - if(mouse.x > e->loc.x && mouse.x < e->loc.x + e->width && + if (mouse.x > e->loc.x && mouse.x < e->loc.x + e->width && mouse.y > e->loc.y && mouse.y < e->loc.y + e->height) { e->vel.y = .05; fr = mouse; @@ -983,13 +983,13 @@ EXIT: break; case SDL_MOUSEWHEEL: - if (e.wheel.y < 0){ - if(mouseWheelUpCount++ && mouseWheelUpCount%5==0){ + if (e.wheel.y < 0) { + if (mouseWheelUpCount++ && mouseWheelUpCount%5==0) { player->inv->setSelectionUp(); mouseWheelUpCount = 0; } }else{ - if(mouseWheelDownCount-- && mouseWheelDownCount%5==0){ + if (mouseWheelDownCount-- && mouseWheelDownCount%5==0) { player->inv->setSelectionDown(); mouseWheelDownCount = 0; } @@ -1010,18 +1010,18 @@ EXIT: // only let other keys be handled if dialog allows it } else if (!dialogBoxExists || dialogPassive) { tmp = currentWorld; - switch(SDL_KEY){ + switch(SDL_KEY) { case SDLK_t: tickCount += 50; break; case SDLK_a: - if(fadeEnable)break; + if (fadeEnable)break; player->vel.x = -PLAYER_SPEED_CONSTANT; player->left = left = true; player->right = right = false; if (currentWorldToLeft) { oldpos = player->loc; - if((tmp = currentWorld->goWorldLeft(player)) != currentWorld){ + if ((tmp = currentWorld->goWorldLeft(player)) != currentWorld) { tmppos = player->loc; player->loc = oldpos; @@ -1035,13 +1035,13 @@ EXIT: } break; case SDLK_d: - if(fadeEnable)break; + if (fadeEnable)break; player->vel.x = PLAYER_SPEED_CONSTANT; player->right = right = true; player->left = left = false; if (currentWorldToRight) { oldpos = player->loc; - if((tmp = currentWorld->goWorldRight(player)) != currentWorld){ + if ((tmp = currentWorld->goWorldRight(player)) != currentWorld) { tmppos = player->loc; player->loc = oldpos; @@ -1066,7 +1066,7 @@ EXIT: currentWorld = tmp; break; case SDLK_LSHIFT: - if(debug){ + if (debug) { Mix_PlayChannel(1,sanic,-1); player->speed = 4.0f; }else @@ -1077,17 +1077,17 @@ EXIT: break; case SDLK_e: edown=true; - if(!heyOhLetsGo){ + if (!heyOhLetsGo) { heyOhLetsGo = loops; player->inv->mouseSel = false; } - if(loops - heyOhLetsGo >= 2 && !(player->inv->invOpen) && !(player->inv->selected)) + if (loops - heyOhLetsGo >= 2 && !(player->inv->invOpen) && !(player->inv->selected)) player->inv->invHover=true; break; default: break; } - if(tmp != currentWorld){ + if (tmp != currentWorld) { std::swap(tmp,currentWorld); toggleBlackFast(); waitForCover(); @@ -1132,7 +1132,7 @@ EXIT: right = false; break; case SDLK_LSHIFT: - if(player->speed == 4){ + if (player->speed == 4) { Mix_FadeOutChannel(1,2000); } player->speed = 1; @@ -1142,10 +1142,10 @@ EXIT: break; case SDLK_e: edown=false; - if(player->inv->invHover){ + if (player->inv->invHover) { player->inv->invHover = false; }else{ - if(!player->inv->selected)player->inv->invOpening ^= true; + if (!player->inv->selected)player->inv->invOpening ^= true; else player->inv->selected = false; player->inv->mouseSel = false; } @@ -1188,7 +1188,7 @@ EXIT: break; } - if(!left&&!right) + if (!left&&!right) player->vel.x=0; break; @@ -1207,22 +1207,22 @@ EXIT: } } - void toggleBlack(void){ + void toggleBlack(void) { fadeEnable ^= true; fadeWhite = false; fadeFast = false; } - void toggleBlackFast(void){ + void toggleBlackFast(void) { fadeEnable ^= true; fadeWhite = false; fadeFast = true; } - void toggleWhite(void){ + void toggleWhite(void) { fadeEnable ^= true; fadeWhite = true; fadeFast = false; } - void toggleWhiteFast(void){ + void toggleWhiteFast(void) { fadeEnable ^= true; fadeWhite = true; fadeFast = true; @@ -1230,10 +1230,10 @@ EXIT: Mix_PlayChannel(1, battleStart, 0); } - void takeScreenshot(GLubyte* pixels){ + void takeScreenshot(GLubyte* pixels) { std::vector<GLubyte> bgr (SCREEN_WIDTH * SCREEN_HEIGHT * 3, 0); - for(uint x = 0; x < SCREEN_WIDTH*SCREEN_HEIGHT*3; x+=3){ + for(uint x = 0; x < SCREEN_WIDTH*SCREEN_HEIGHT*3; x+=3) { bgr[x] = pixels[x+2]; bgr[x+1] = pixels[x+1]; bgr[x+2] = pixels[x]; diff --git a/src/ui_menu.cpp b/src/ui_menu.cpp index 9c7fc9d..cd11dae 100644 --- a/src/ui_menu.cpp +++ b/src/ui_menu.cpp @@ -5,27 +5,27 @@ extern bool gameRunning; extern Menu *currentMenu; extern Menu pauseMenu; -void Menu:: -gotoParent(void) +void Menu::gotoParent(void) { if (!parent) { - currentMenu = NULL; + currentMenu = nullptr; config::update(); - } else + } else { currentMenu = parent; + } } -void Menu:: -gotoChild(void) +void Menu::gotoChild(void) { currentMenu = child; } namespace ui { namespace menu { - menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f){ + menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f) + { menuItem temp; - + temp.member = 0; temp.button.loc = l; temp.button.dim = d; @@ -36,9 +36,10 @@ namespace ui { return temp; } - menuItem createChildButton(vec2 l, dim2 d, Color c, const char* t){ + menuItem createChildButton(vec2 l, dim2 d, Color c, const char* t) + { menuItem temp; - + temp.member = -1; temp.button.loc = l; temp.button.dim = d; @@ -49,7 +50,8 @@ namespace ui { return temp; } - menuItem createParentButton(vec2 l, dim2 d, Color c, const char* t){ + menuItem createParentButton(vec2 l, dim2 d, Color c, const char* t) + { menuItem temp; temp.member = -2; @@ -62,7 +64,8 @@ namespace ui { return temp; } - menuItem createSlider(vec2 l, dim2 d, Color c, float min, float max, const char* t, float* v){ + menuItem createSlider(vec2 l, dim2 d, Color c, float min, float max, const char* t, float* v) + { menuItem temp; temp.member = 1; @@ -78,9 +81,10 @@ namespace ui { return temp; } - void draw(void) { + void draw(void) + { SDL_Event e; - + setFontSize(24); config::update(); @@ -88,8 +92,8 @@ namespace ui { mouse.y = (offset.y+SCREEN_HEIGHT/2)-ui::premouse.y; //custom event polling for menu's so all other events are ignored - while(SDL_PollEvent(&e)){ - switch(e.type){ + while(SDL_PollEvent(&e)) { + switch (e.type) { case SDL_QUIT: gameRunning = false; return; @@ -99,7 +103,7 @@ namespace ui { premouse.y=e.motion.y; break; case SDL_KEYUP: - if(SDL_KEY == SDLK_ESCAPE){ + if (SDL_KEY == SDLK_ESCAPE) { currentMenu->gotoParent(); return; } @@ -113,9 +117,9 @@ namespace ui { glRectf(offset.x-SCREEN_WIDTH/2,0,offset.x+SCREEN_WIDTH/2,SCREEN_HEIGHT); //loop through all elements of the menu - for(auto &m : currentMenu->items){ + for (auto &m : currentMenu->items) { //if the menu is any type of button - if(m.member == 0 || m.member == -1 || m.member == -2){ + if (m.member == 0 || m.member == -1 || m.member == -2) { //draw the button background glColor3f(m.button.color.red,m.button.color.green,m.button.color.blue); @@ -129,8 +133,8 @@ namespace ui { m.button.text); //tests if the mouse is over the button - if(mouse.x >= offset.x+m.button.loc.x && mouse.x <= offset.x+m.button.loc.x + m.button.dim.x){ - if(mouse.y >= offset.y+m.button.loc.y && mouse.y <= offset.y+m.button.loc.y + m.button.dim.y){ + if (mouse.x >= offset.x+m.button.loc.x && mouse.x <= offset.x+m.button.loc.x + m.button.dim.x) { + if (mouse.y >= offset.y+m.button.loc.y && mouse.y <= offset.y+m.button.loc.y + m.button.dim.y) { //if the mouse if over the button, it draws this white outline glColor3f(1.0f,1.0f,1.0f); @@ -143,8 +147,8 @@ namespace ui { glEnd(); //if the mouse is over the button and clicks - if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){ - switch(m.member){ + if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) { + switch(m.member) { case 0: //normal button m.button.func(); break; @@ -160,14 +164,14 @@ namespace ui { } //if element is a slider - }else if(m.member == 1){ + }else if (m.member == 1) { //combining slider text with variable amount char outSV[32]; sprintf(outSV, "%s: %.1f",m.slider.text, *m.slider.var); float sliderW, sliderH; - if(m.slider.dim.y > m.slider.dim.x){ + if (m.slider.dim.y > m.slider.dim.x) { //width of the slider handle sliderW = m.slider.dim.x; sliderH = m.slider.dim.y * .05; @@ -189,7 +193,7 @@ namespace ui { //draw the slider handle glColor4f(m.slider.color.red,m.slider.color.green,m.slider.color.blue, 1.0f); - if(m.slider.dim.y > m.slider.dim.x){ + if (m.slider.dim.y > m.slider.dim.x) { glRectf(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05), offset.x+m.slider.loc.x + sliderW, @@ -207,8 +211,8 @@ namespace ui { putStringCentered(offset.x + m.slider.loc.x + (m.slider.dim.x/2), (offset.y + m.slider.loc.y + (m.slider.dim.y/2)) - ui::fontSize/2, outSV); } //test if mouse is inside of the slider's borders - if(mouse.x >= offset.x+m.slider.loc.x && mouse.x <= offset.x+m.slider.loc.x + m.slider.dim.x){ - if(mouse.y >= offset.y+m.slider.loc.y && mouse.y <= offset.y+m.slider.loc.y + m.slider.dim.y){ + if (mouse.x >= offset.x+m.slider.loc.x && mouse.x <= offset.x+m.slider.loc.x + m.slider.dim.x) { + if (mouse.y >= offset.y+m.slider.loc.y && mouse.y <= offset.y+m.slider.loc.y + m.slider.dim.y) { //if it is we draw a white border around it glColor3f(1.0f,1.0f,1.0f); @@ -219,7 +223,7 @@ namespace ui { glVertex2f(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y+m.slider.dim.y); glVertex2f(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y); - if(m.slider.dim.y > m.slider.dim.x){ + if (m.slider.dim.y > m.slider.dim.x) { //and a border around the slider handle glVertex2f(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)); glVertex2f(offset.x+m.slider.loc.x + sliderW, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)); @@ -238,9 +242,9 @@ namespace ui { glEnd(); //if we are inside the slider and click it will set the slider to that point - if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){ + if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) { //change handle location - if(m.slider.dim.y > m.slider.dim.x){ + if (m.slider.dim.y > m.slider.dim.x) { *m.slider.var = (((mouse.y-offset.y) - m.slider.loc.y)/m.slider.dim.y)*100; //draw a white box over the handle glColor3f(1.0f,1.0f,1.0f); @@ -261,8 +265,8 @@ namespace ui { } //makes sure handle can't go below or above min and max values - if(*m.slider.var >= m.slider.maxValue)*m.slider.var = m.slider.maxValue; - else if(*m.slider.var <= m.slider.minValue)*m.slider.var = m.slider.minValue; + if (*m.slider.var >= m.slider.maxValue)*m.slider.var = m.slider.maxValue; + else if (*m.slider.var <= m.slider.minValue)*m.slider.var = m.slider.minValue; } } } diff --git a/src/world.cpp b/src/world.cpp index 434f619..be8ba94 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -171,7 +171,7 @@ deleteEntities(void) mob.clear(); merchant.clear(); - while(!npc.empty()){ + while(!npc.empty()) { delete npc.back(); npc.pop_back(); } @@ -209,7 +209,7 @@ World:: ~World(void) { // sdl2_mixer's object - if(bgmObj) + if (bgmObj) Mix_FreeMusic(bgmObj); delete bgTex; @@ -250,7 +250,7 @@ generate(unsigned int width) // create slopes from the points that were just defined, populate the rest of the WorldData structure - for(wditer = worldData.begin() + 1; wditer != worldData.end(); wditer++){ + for(wditer = worldData.begin() + 1; wditer != worldData.end(); wditer++) { if ((*wditer).groundHeight && wditer + GROUND_HILLINESS < worldData.end()) // wditer + GROUND_HILLINESS can go out of bounds (invalid read) geninc = ((*(wditer + GROUND_HILLINESS)).groundHeight - (*wditer).groundHeight) / (float)GROUND_HILLINESS; @@ -268,7 +268,7 @@ generate(unsigned int width) else if ((*wditer).groundHeight > GROUND_HEIGHT_MAXIMUM) (*wditer).groundHeight = GROUND_HEIGHT_MAXIMUM; - if((*wditer).groundHeight <= 0) + if ((*wditer).groundHeight <= 0) (*wditer).groundHeight = GROUND_HEIGHT_MINIMUM; } @@ -317,7 +317,7 @@ update(Player *p, unsigned int delta) e->loc.y += e->vel.y * delta; } // iterate through particles - particles.erase(std::remove_if(particles.begin(), particles.end(), [&delta](Particles &part){return part.kill(delta);}), particles.end()); + particles.erase(std::remove_if (particles.begin(), particles.end(), [&delta](Particles &part) {return part.kill(delta);}), particles.end()); for (auto part = particles.begin(); part != particles.end(); part++) { if ((*part).canMove) { (*part).loc.y += (*part).vel.y * delta; @@ -338,7 +338,7 @@ update(Player *p, unsigned int delta) // handle music fades if (ui::dialogImportant) { //Mix_FadeOutMusic(2000); - } else if(!Mix_PlayingMusic()) + } else if (!Mix_PlayingMusic()) Mix_FadeInMusic(bgmObj,-1,2000); } @@ -352,7 +352,7 @@ update(Player *p, unsigned int delta) void World:: setBGM(std::string path) { - if(!path.empty()) + if (!path.empty()) bgmObj = Mix_LoadMUS((bgm = path).c_str()); } @@ -385,7 +385,7 @@ bgmPlay(World *prev) const * screen. */ -void World::draw(Player *p){ +void World::draw(Player *p) { // iterators int i, iStart, iEnd; @@ -450,7 +450,7 @@ void World::draw(Player *p){ // draw the stars if the time deems it appropriate //if ((((weather == WorldWeather::Dark) & (tickCount % DAY_CYCLE)) < DAY_CYCLE / 2) || - // (((weather == WorldWeather::Sunny) & (tickCount % DAY_CYCLE)) > DAY_CYCLE * .75)){ + // (((weather == WorldWeather::Sunny) & (tickCount % DAY_CYCLE)) > DAY_CYCLE * .75)) { if (worldShade > 0) { safeSetColorA(255, 255, 255, 255 - (getRand() % 30 - 15)); @@ -485,7 +485,7 @@ void World::draw(Player *p){ safeSetColorA(bgDraw[i][0] + shadeBackground * 2, bgDraw[i][0] + shadeBackground * 2, bgDraw[i][0] + shadeBackground * 2, bgDraw[i][1]); glBegin(GL_QUADS); - for(int j = worldStart; j <= -worldStart; j += 600){ + for(int j = worldStart; j <= -worldStart; j += 600) { glTexCoord2i(0, 1); glVertex2i(j + offset.x * bgDraw[i][2], GROUND_HEIGHT_MINIMUM); glTexCoord2i(1, 1); glVertex2i((j + 600) + offset.x * bgDraw[i][2], GROUND_HEIGHT_MINIMUM); glTexCoord2i(1, 0); glVertex2i((j + 600) + offset.x * bgDraw[i][2], GROUND_HEIGHT_MINIMUM + 400); @@ -548,12 +548,12 @@ void World::draw(Player *p){ glActiveTexture(GL_TEXTURE0); bgTex->bindNext(); - for(auto &l : light){ - if(l.belongsTo){ + for(auto &l : light) { + if (l.belongsTo) { l.loc.x = l.following->loc.x + SCREEN_WIDTH/2; l.loc.y = l.following->loc.y; } - if(l.flame){ + if (l.flame) { l.fireFlicker = .9+((rand()%2)/10.0f); l.fireLoc.x = l.loc.x + (rand()%2-1)*3; l.fireLoc.y = l.loc.y + (rand()%2-1)*3; @@ -567,7 +567,7 @@ void World::draw(Player *p){ GLfloat flameArray[64]; for (uint i = 0; i < light.size(); i++) { - if(light[i].flame){ + if (light[i].flame) { pointArray[2 * i ] = light[i].fireLoc.x - offset.x; pointArray[2 * i + 1] = light[i].fireLoc.y; }else{ @@ -651,7 +651,7 @@ void World::draw(Player *p){ memset(cgh, 0 , 2 * sizeof(float)); // flatten the grass if the player is standing on it. - if(!worldData[i].grassUnpressed){ + if (!worldData[i].grassUnpressed) { cgh[0] /= 4; cgh[1] /= 4; } @@ -746,12 +746,12 @@ singleDetect(Entity *e) e->alive = false; e->health = 0; for (i = 0; i < entity.size(); i++) { - if (entity[i] == e){ + if (entity[i] == e) { switch (e->type) { case STRUCTURET: killed = "structure"; - for(j=0;j<build.size();j++){ - if(build[j]==e){ + for(j=0;j<build.size();j++) { + if (build[j]==e) { delete build[j]; build.erase(build.begin()+j); break; @@ -760,8 +760,8 @@ singleDetect(Entity *e) break; case NPCT: killed = "NPC"; - for(j=0;j<npc.size();j++){ - if(npc[j]==e){ + for(j=0;j<npc.size();j++) { + if (npc[j]==e) { delete npc[j]; npc.erase(npc.begin()+j); break; @@ -770,8 +770,8 @@ singleDetect(Entity *e) break; case MOBT: killed = "mob"; - /*for(j=0;j<mob.size();j++){ - if(mob[j]==e){ + /*for(j=0;j<mob.size();j++) { + if (mob[j]==e) { delete mob[j]; mob.erase(mob.begin()+j); break; @@ -851,10 +851,10 @@ singleDetect(Entity *e) * Insure that the entity doesn't fall off either edge of the world. */ - if(e->loc.x < worldStart){ // Left bound + if (e->loc.x < worldStart) { // Left bound e->vel.x=0; e->loc.x=(float)worldStart + HLINE / 2; - }else if(e->loc.x + e->width + HLINE > worldStart + worldStart * -2){ // Right bound + }else if (e->loc.x + e->width + HLINE > worldStart + worldStart * -2) { // Right bound e->vel.x=0; e->loc.x=worldStart + worldStart * -2 - e->width - HLINE; } @@ -949,7 +949,7 @@ detect(Player *p) } } -void World::addStructure(BUILD_SUB sub, float x,float y, std::string tex, std::string inside){ +void World::addStructure(BUILD_SUB sub, float x,float y, std::string tex, std::string inside) { build.push_back(new Structures()); build.back()->inWorld = this; build.back()->textureLoc = tex; @@ -968,14 +968,14 @@ addVillage(std::string name, World *world) return &village.back(); } -void World::addMob(int t,float x,float y){ +void World::addMob(int t,float x,float y) { mob.emplace_back(t); mob.back().spawn(x,y); entity.push_back(&mob.back()); } -void World::addMob(int t,float x,float y,void (*hey)(Mob *)){ +void World::addMob(int t,float x,float y,void (*hey)(Mob *)) { mob.emplace_back(t); mob.back().spawn(x,y); mob.back().hey = hey; @@ -983,14 +983,14 @@ void World::addMob(int t,float x,float y,void (*hey)(Mob *)){ entity.push_back(&mob.back()); } -void World::addNPC(float x,float y){ +void World::addNPC(float x,float y) { npc.push_back(new NPC()); npc.back()->spawn(x,y); entity.push_back(npc.back()); } -void World::addMerchant(float x, float y){ +void World::addMerchant(float x, float y) { merchant.push_back(new Merchant()); merchant.back()->spawn(x,y); @@ -998,7 +998,7 @@ void World::addMerchant(float x, float y){ entity.push_back(npc.back()); } -void World::addObject(std::string in, std::string p, float x, float y){ +void World::addObject(std::string in, std::string p, float x, float y) { object.emplace_back(in, p); object.back().spawn(x, y); @@ -1079,7 +1079,7 @@ goWorldLeft(Player *p) World *tmp; // check if player is at world edge - if(!toLeft.empty() && p->loc.x < worldStart + HLINE * 15.0f) { + if (!toLeft.empty() && p->loc.x < worldStart + HLINE * 15.0f) { // load world (`toLeft` conditional confirms existance) tmp = loadWorldFromPtr(currentWorldToLeft); @@ -1098,7 +1098,7 @@ bool World:: goWorldLeft(NPC *e) { // check if entity is at world edge - if(!toLeft.empty() && e->loc.x < worldStart + HLINE * 15.0f) { + if (!toLeft.empty() && e->loc.x < worldStart + HLINE * 15.0f) { currentWorldToLeft->addNPC(e->loc.x,e->loc.y); e->alive = false; @@ -1117,7 +1117,7 @@ goWorldRight(Player *p) { World *tmp; - if(!toRight.empty() && p->loc.x + p->width > -worldStart - HLINE * 15) { + if (!toRight.empty() && p->loc.x + p->width > -worldStart - HLINE * 15) { tmp = loadWorldFromPtr(currentWorldToRight); p->loc.x = tmp->worldStart - HLINE * -15.0f; @@ -1218,7 +1218,7 @@ getTheWidth(void) const return worldStart * -2; } -void World::save(void){ +void World::save(void) { std::string data; std::string save = (std::string)currentXML + ".dat"; @@ -1226,18 +1226,18 @@ void World::save(void){ std::cout<<"Saving to "<<save<<" ..."<<std::endl; - for(auto &n : npc){ + for(auto &n : npc) { data.append(std::to_string(n->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){ + 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){ + 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(std::to_string((int)m.alive) + "\n"); @@ -1248,7 +1248,7 @@ void World::save(void){ out.close(); } -void World::load(void){ +void World::load(void) { std::string save,data,line; const char *filedata; @@ -1257,44 +1257,44 @@ void World::load(void){ data = filedata; std::istringstream iss (data); - for(auto &n : npc){ + for(auto &n : npc) { std::getline(iss,line); - if(line == "dOnE")return; - if((n->dialogIndex = std::stoi(line)) != 9999) + if (line == "dOnE")return; + if ((n->dialogIndex = std::stoi(line)) != 9999) n->addAIFunc(commonAIFunc,false); else n->clearAIFunc(); std::getline(iss,line); - if(line == "dOnE")return; + if (line == "dOnE")return; n->loc.x = std::stoi(line); std::getline(iss,line); - if(line == "dOnE")return; + if (line == "dOnE")return; n->loc.y = std::stoi(line); } - for(auto &b : build){ + for(auto &b : build) { std::getline(iss,line); - if(line == "dOnE")return; + if (line == "dOnE")return; b->loc.x = std::stoi(line); std::getline(iss,line); - if(line == "dOnE")return; + if (line == "dOnE")return; b->loc.y = std::stoi(line); } - for(auto &m : mob){ + for(auto &m : mob) { std::getline(iss,line); - if(line == "dOnE")return; + if (line == "dOnE")return; m.loc.x = std::stoi(line); std::getline(iss,line); - if(line == "dOnE")return; + if (line == "dOnE")return; m.loc.y = std::stoi(line); std::getline(iss,line); - if(line == "dOnE")return; + if (line == "dOnE")return; m.alive = std::stoi(line); } - while(std::getline(iss,line)){ - if(line == "dOnE") + while(std::getline(iss,line)) { + if (line == "dOnE") break; } @@ -1310,10 +1310,10 @@ bool isCurrentWorldIndoors(void) { return !inside.empty(); } -IndoorWorld::IndoorWorld(void){ +IndoorWorld::IndoorWorld(void) { } -IndoorWorld::~IndoorWorld(void){ +IndoorWorld::~IndoorWorld(void) { delete bgTex; deleteEntities(); @@ -1436,7 +1436,7 @@ draw(Player *p) GLfloat flameArray[64]; for (i = 0; i < light.size(); i++) { - if(light[i].flame){ + if (light[i].flame) { pointArray[2 * i ] = light[i].fireLoc.x - offset.x; pointArray[2 * i + 1] = light[i].fireLoc.y; }else{ @@ -1522,7 +1522,7 @@ draw(Player *p) p->draw(); } -Arena::Arena(World *leave,Player *p,Mob *m){ +Arena::Arena(World *leave,Player *p,Mob *m) { generate(800); addMob(MS_DOOR,100,100); @@ -1537,11 +1537,11 @@ Arena::Arena(World *leave,Player *p,Mob *m){ battleNestLoc.push_back(p->loc); } -Arena::~Arena(void){ +Arena::~Arena(void) { deleteEntities(); } -World *Arena::exitArena(Player *p){ +World *Arena::exitArena(Player *p) { World *tmp; if (!mmob->alive && p->loc.x + p->width / 2 > mob[0].loc.x && @@ -1586,7 +1586,7 @@ std::string getWorldWeatherStr(WorldWeather ww) static bool loadedLeft = false; static bool loadedRight = false; -World *loadWorldFromXML(std::string path){ +World *loadWorldFromXML(std::string path) { if (!currentXML.empty()) currentWorld->save(); @@ -1642,7 +1642,7 @@ loadWorldFromXMLNoSave(std::string path) { } // attempt to load an <IndoorWorld> tag - else if((wxml = xml.FirstChildElement("IndoorWorld"))) { + else if ((wxml = xml.FirstChildElement("IndoorWorld"))) { wxml = wxml->FirstChildElement(); vil = NULL; tmp = new IndoorWorld(); @@ -1815,10 +1815,9 @@ loadWorldFromXMLNoSave(std::string path) { vil = vil->FirstChildElement(); } - while(vil){ + while(vil) { name = vil->Name(); randx = 0; - //static BuySell bs; /** * READS DATA ABOUT STRUCTURE CONTAINED IN VILLAGE @@ -1864,17 +1863,38 @@ loadWorldFromXMLNoSave(std::string path) { while (sxml) { tag = sxml->Name(); - if (tag == "buy") { + if (tag == "buy") { //converts price to the currencies determined in items.xml // TODO - } else if (tag == "sell") { + } else if (tag == "sell") { //converts price so the player can sell // TODO - } else if (tag == "trade") { + } else if (tag == "trade") { //doesn't have to convert anything, we just trade multiple items tmp->merchant.back()->trade.push_back(Trade(sxml->IntAttribute("quantity"), - sxml->StrAttribute("item"), - sxml->IntAttribute("quantity1"), - sxml->StrAttribute("item1") - )); - } + sxml->StrAttribute("item"), + sxml->IntAttribute("quantity1"), + sxml->StrAttribute("item1"))); + } else if (tag == "text") { //this is what the merchant says + std::cout << "text" << std::endl; + + XMLElement *txml = sxml->FirstChildElement(); + std::string textOption; + + while (txml) { + textOption = txml->Name(); + const char* buf = txml->GetText(); + + if (textOption == "greet") { //when you talk to him + tmp->merchant.back()->text[0] = std::string(buf, strlen(buf)); + tmp->merchant.back()->toSay = &tmp->merchant.back()->text[0]; + } else if (textOption == "accept") { //when he accepts the trade + tmp->merchant.back()->text[1] = std::string(buf, strlen(buf)); + } else if (textOption == "deny") { //when you don't have enough money + tmp->merchant.back()->text[2] = std::string(buf, strlen(buf)); + } else if (textOption == "leave") { //when you leave the merchant + tmp->merchant.back()->text[3] = std::string(buf, strlen(buf)); + } + txml = txml->NextSiblingElement(); + } + } sxml = sxml->NextSiblingElement(); } @@ -1882,11 +1902,11 @@ loadWorldFromXMLNoSave(std::string path) { vptr->build.push_back(tmp->build.back()); - if(vptr->build.back()->loc.x < vptr->start.x){ + if (vptr->build.back()->loc.x < vptr->start.x) { vptr->start.x = vptr->build.back()->loc.x; } - if(vptr->build.back()->loc.x + vptr->build.back()->width > vptr->end.x){ + if (vptr->build.back()->loc.x + vptr->build.back()->width > vptr->end.x) { vptr->end.x = vptr->build.back()->loc.x + vptr->build.back()->width; } @@ -1895,7 +1915,7 @@ loadWorldFromXMLNoSave(std::string path) { } std::ifstream dat (((std::string)currentXML + ".dat").c_str()); - if(dat.good()){ + if (dat.good()) { dat.close(); tmp->load(); } @@ -1903,7 +1923,7 @@ loadWorldFromXMLNoSave(std::string path) { return tmp; } -Village::Village(const char *meme, World *w){ +Village::Village(const char *meme, World *w) { name = meme; start.x = w->getTheWidth() / 2.0f; end.x = -start.x; diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index 53f5ad2..6518c25 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -7,7 +7,7 @@ <hill peakx="0" peaky="1000" width="50" /> - <mob x="300" type="1" aggressive="false" health="1000" /> + <!-- <mob x="300" type="1" aggressive="false" health="1000" /> --> <!--<trigger x="-300" id="Test" />--> @@ -21,13 +21,16 @@ <structure type="0" x="-300" inside="playerSpawnHill1_Building1.xml"/> <structure type="5" x="-500" /> <stall type="market" texture="assets/style/classic/stall.png"> - <buy item="Dank MayMay" cost="420"/> - <sell item="Dank MayMay" cost="666"/> + <text> + <greet>Welcome to Smithy's! What would you like to purchase today?</greet> + <accept>Thanks!</accept> + <deny>Come back with more money to get this item!</deny> + <leave>Have a great day, and watch out for my rabbit Mr. Cuddles, he might try to attack you.</leave> + </text> <trade quantity="420" item="Dank MayMay" quantity1="1" item1="Wood Sword"/> <trade quantity="666" item="Wood Sword" quantity1="420" item1="Dank MayMay"/> </stall> </village> - </World> <Trigger id="Test">It was a dark and stormy night... |