From d3792c499ce9e33f314e06d102d2a0119108e8d3 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <tullivan99@gmail.com>
Date: Tue, 12 Jan 2016 07:38:57 -0500
Subject: world linking xml'd

---
 Changelog                |  7 +++++
 include/common.h         |  1 +
 include/world.h          |  4 +--
 main.cpp                 |  1 +
 src/common.cpp           | 14 ++++++++++
 src/gameplay.cpp         | 69 ++++++++++++++++++++++++++++++++++++++----------
 src/world.cpp            | 22 ++++++++-------
 test.frag                |  4 +--
 xml/playerSpawnHill1.xml |  4 ++-
 xml/playerSpawnHill2.xml |  9 +++++++
 10 files changed, 106 insertions(+), 29 deletions(-)
 create mode 100644 xml/playerSpawnHill2.xml

diff --git a/Changelog b/Changelog
index 02dd436..30c1a50 100644
--- a/Changelog
+++ b/Changelog
@@ -508,3 +508,10 @@
 	- xml'd npc dialogs
 	- drafted page xml
 	
+1/11/2015:
+==========
+
+	- improved npc dialog xmling
+	- WIP xml'd world linking
+	- textures?
+	- music?
diff --git a/include/common.h b/include/common.h
index 8552720..22ecb22 100644
--- a/include/common.h
+++ b/include/common.h
@@ -201,5 +201,6 @@ unsigned int millis(void);
 #endif // __WIN32__
 
 int getdir(const char *dir, std::vector<std::string> &files);
+void strVectorSortAlpha(std::vector<std::string> *v);
 
 #endif // COMMON_H
diff --git a/include/world.h b/include/world.h
index 0e85462..28d7a09 100644
--- a/include/world.h
+++ b/include/world.h
@@ -157,8 +157,8 @@ public:
 	 * ui.cpp for world jumping.
 	 */
 
-	World *toLeft,
-		  *toRight,
+	World **toLeft,
+		  **toRight,
 		  *behind,
 		  *infront;
 	
diff --git a/main.cpp b/main.cpp
index 3ae9787..79c7250 100644
--- a/main.cpp
+++ b/main.cpp
@@ -376,6 +376,7 @@ int main(/*int argc, char *argv[]*/){
 	    std::vector<char> programError( (logLength > 1) ? logLength : 1 );
 	    glGetProgramInfoLog(shaderProgram, logLength, NULL, &programError[0]);
 	    std::cout << &programError[0] << std::endl;
+			
 	#endif //SHADERS
 	
 	//glEnable(GL_DEPTH_TEST); //THIS DOESN'T WORK ON LINUX
diff --git a/src/common.cpp b/src/common.cpp
index 8defd61..6307790 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -77,3 +77,17 @@ int getdir(const char *dir, std::vector<std::string> &files){
     closedir(dp);
     return 0;
 }
+
+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]){
+				std::cout<<"swap "<<v[0][i]<<" <-> "<<v[0][i+1]<<std::endl;
+				std::swap(v[0][i],v[0][i+1]);
+				change = true;
+			}
+		}
+	}while(change);
+}
diff --git a/src/gameplay.cpp b/src/gameplay.cpp
index a257ad3..55161f9 100644
--- a/src/gameplay.cpp
+++ b/src/gameplay.cpp
@@ -32,16 +32,21 @@ typedef struct {
 	char *file;
 } WorldXML;
 
+typedef struct {
+	char *name;
+	World *ptr;
+} WorldLink;
 
 typedef struct {
 	NPC *npc;
 	unsigned int index;
 } NPCDialog;
 
-std::vector<NPCDialog>		npcd;
-std::vector<WorldXML>		earthxml;
-std::vector<World *>		earth;
-std::vector<XMLElement *>	dopt;
+std::vector<NPCDialog>			npcd;
+std::vector<WorldXML>			earthxml;
+static std::vector<WorldLink>	earthlnk;
+std::vector<World *>			earth;
+std::vector<XMLElement *>		dopt;
 
 int commonAIFunc(NPC *speaker){
 	XMLDocument xml;
@@ -68,7 +73,7 @@ int commonAIFunc(NPC *speaker){
 			
 			do{
 				if(!strcmp(exml->Name(),"text")){
-					if(/*!strcmp(exml->Attribute("name"),speaker->name) &&*/ exml->UnsignedAttribute("id") == idx){
+					if(exml->UnsignedAttribute("id") == idx){
 						
 						if((oxml = exml->FirstChildElement("option"))){
 							const char *op;
@@ -158,22 +163,53 @@ void initEverything(void){
 		abort();
 	}
 	
-	for(auto x : xmlFiles){
-		if(strncmp(x.c_str(),".",1) && strncmp(x.c_str(),"..",2)){
-			file = new char[5 + x.size()];
-			memset(file,0,5 + x.size());
+	strVectorSortAlpha(&xmlFiles);
+	
+	for(unsigned int i=0;i<xmlFiles.size();i++){
+		if(strncmp(xmlFiles[i].c_str(),".",1) && strncmp(xmlFiles[i].c_str(),"..",2)){
+			earthlnk.push_back((WorldLink){new char[strlen(xmlFiles[i].c_str() + 1)],NULL});
+			strcpy(earthlnk.back().name,xmlFiles[i].c_str());
+		}
+	}
+	
+	for(unsigned int i=0;i<xmlFiles.size();i++){
+		if(strncmp(xmlFiles[i].c_str(),".",1) && strncmp(xmlFiles[i].c_str(),"..",2)){
+			file = new char[5 + xmlFiles[i].size()];
+			memset(file,0,5 + xmlFiles[i].size());
 			strncpy(file,"xml/",4);
-			strcpy(file+4,x.c_str());
+			strcpy(file+4,xmlFiles[i].c_str());
+			std::cout<<std::endl<<"Loading "<<file<<" ..."<<std::endl<<std::endl;
 			xml.LoadFile(file);
 
 			wxml = xml.FirstChildElement("World")->FirstChildElement();
 
 			earth.push_back(new World());
 			
-			do{
+			for(auto &l : earthlnk){
+				if(!strcmp(file+4,l.name)){
+					l.ptr = earth.back();
+					break;
+				}
+			}
+			
+			while(wxml){
 				name = wxml->Name();
-				
-				if(!strcmp(name,"style")){
+
+				if(!strcmp(name,"link") && wxml->Attribute("left")){
+					for(auto &l : earthlnk){
+						if(!strcmp(l.name,wxml->Attribute("left"))){
+							earth.back()->toLeft = &l.ptr;
+							break;
+						}
+					}
+				}else if(!strcmp(name,"link") && wxml->Attribute("right")){
+					for(auto &l : earthlnk){
+						if(!strcmp(l.name,wxml->Attribute("right"))){
+							earth.back()->toRight = &l.ptr;
+							break;
+						}
+					}
+				}else if(!strcmp(name,"style")){
 					earth.back()->setBackground((WORLD_BG_TYPE)wxml->UnsignedAttribute("background"));
 					earth.back()->setBGM(wxml->Attribute("bgm"));
 				}else if(!strcmp(name,"generation")){
@@ -208,10 +244,15 @@ SKIP:
 						earth.back()->npc.back()->addAIFunc(commonAIFunc,false);
 						npcd.push_back((NPCDialog){earth.back()->npc.back(),0});
 					}
+				}else if(!strcmp(name,"structure")){
+					/*if(wxml->QueryFloatAttribute("x",&spawnx) != XML_NO_ERROR)
+						earth.back()->addStructure((getRand() % earth.back()->getTheWidth() / 2.0f,100);
+					else
+						earth.back()->addNPC(wxml->FloatAttribute("x"),wxml->FloatAttribute("y"));*/
 				}
 				
 				wxml = wxml->NextSiblingElement();
-			}while(wxml);
+			}
 			
 			delete[] file;
 		}
diff --git a/src/world.cpp b/src/world.cpp
index e66fb0d..e2c6aa9 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -185,6 +185,8 @@ void World::load(std::ifstream *i){
 	}
 }
 
+void *NULLPTR = NULL;
+
 World::World(void){
 	
 	bgm = NULL;
@@ -195,9 +197,9 @@ World::World(void){
 	*/
 	
 	behind	=
-	infront	=
+	infront	= NULL;
 	toLeft	=
-	toRight	= NULL;
+	toRight	= (World **)&NULLPTR;
 	
 	/*
 	 *	Allocate and clear an array for star coordinates.
@@ -1151,19 +1153,19 @@ NPC *World::getAvailableNPC(void){
 }
 
 World *World::goWorldLeft(Player *p){
-	if(toLeft&&p->loc.x<x_start+HLINE*15){
-		p->loc.x=toLeft->x_start+getWidth(toLeft)-HLINE*10;
-		p->loc.y=toLeft->line[toLeft->lineCount-GEN_INC-1].y;
-		return toLeft;
+	if(toLeft[0]&&p->loc.x<x_start+HLINE*15){
+		p->loc.x=toLeft[0]->x_start+getWidth(toLeft[0])-HLINE*10;
+		p->loc.y=toLeft[0]->line[toLeft[0]->lineCount-GEN_INC-1].y;
+		return toLeft[0];
 	}
 	return this;
 }
 
 World *World::goWorldRight(Player *p){
-	if(toRight&&p->loc.x+p->width>x_start+getWidth(this)-HLINE*10){
-		p->loc.x=toRight->x_start+HLINE*10;
-		p->loc.y=toRight->line[0].y;
-		return toRight;
+	if(toRight[0]&&p->loc.x+p->width>x_start+getWidth(this)-HLINE*10){
+		p->loc.x=toRight[0]->x_start+HLINE*10;
+		p->loc.y=toRight[0]->line[0].y;
+		return toRight[0];
 	}
 	return this;
 }
diff --git a/test.frag b/test.frag
index 1c1e77b..c6ff3c9 100644
--- a/test.frag
+++ b/test.frag
@@ -1,4 +1,4 @@
-#version 130
+#version 120
 uniform sampler2D sampler;
 
 uniform int numLight;
@@ -24,4 +24,4 @@ void main(){
 
 	color += vec4(amb,amb,amb,1.0f+amb);
 	gl_FragColor = tex * vec4(color)*tex.a;
-}
\ No newline at end of file
+}
diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml
index 5a0e51f..fbf5d8d 100644
--- a/xml/playerSpawnHill1.xml
+++ b/xml/playerSpawnHill1.xml
@@ -1,7 +1,9 @@
 <?xml version="1.0"?>
 <World>
 	<style background="0" bgm="assets/music/embark.wav" />
-	<generation type="Random" width="2000" />
+	<generation type="Random" width="800" />
+	
+	<link right="playerSpawnHill2.xml" />
 	
 	<mob type="1" />
 	
diff --git a/xml/playerSpawnHill2.xml b/xml/playerSpawnHill2.xml
new file mode 100644
index 0000000..d6a966f
--- /dev/null
+++ b/xml/playerSpawnHill2.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<World>
+	<style background="0" bgm="assets/music/embark.wav" />
+	<generation type="Random" width="500" />
+	
+	<link left="playerSpawnHill1.xml" />
+
+</World>
+
-- 
cgit v1.2.3