Added event definitions

Fri, 13 Jul 2012 20:10:08 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Fri, 13 Jul 2012 20:10:08 +0300
changeset 2
bb2c45522eb6
parent 1
f0c61c204bc8
child 3
08cab2b67dce

Added event definitions

Makefile file | annotate | diff | comparison | revisions
common.h file | annotate | diff | comparison | revisions
events.cxx file | annotate | diff | comparison | revisions
events.def file | annotate | diff | comparison | revisions
events.h file | annotate | diff | comparison | revisions
main.cxx file | annotate | diff | comparison | revisions
parser.cxx file | annotate | diff | comparison | revisions
scriptreader.cxx file | annotate | diff | comparison | revisions
scriptreader.h file | annotate | diff | comparison | revisions
str.cxx file | annotate | diff | comparison | revisions
str.h file | annotate | diff | comparison | revisions
--- a/Makefile	Fri Jul 13 18:41:40 2012 +0300
+++ b/Makefile	Fri Jul 13 20:10:08 2012 +0300
@@ -4,7 +4,8 @@
 	g++ -Wall -c -o str.o str.cxx
 	g++ -Wall -c -o main.o main.cxx
 	g++ -Wall -c -o parser.o parser.cxx
-	g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o
+	g++ -Wall -c -o events.o events.cxx
+	g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o events.o
 
 clean:
 	rm -f *.o *~ botc
--- a/common.h	Fri Jul 13 18:41:40 2012 +0300
+++ b/common.h	Fri Jul 13 20:10:08 2012 +0300
@@ -66,10 +66,6 @@
 
 void error (const char* text, ...);
 
-struct EventDef {
-	str name;
-};
-
 #ifndef __PARSER_CXX__
 extern int g_NumStates;
 extern int g_NumEvents;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/events.cxx	Fri Jul 13 20:10:08 2012 +0300
@@ -0,0 +1,107 @@
+/*
+ *	botc source code
+ *	Copyright (C) 2012 Santeri `Dusk` Piippo
+ *	All rights reserved.
+ *	
+ *	Redistribution and use in source and binary forms, with or without
+ *	modification, are permitted provided that the following conditions are met:
+ *	
+ *	1. Redistributions of source code must retain the above copyright notice,
+ *	   this list of conditions and the following disclaimer.
+ *	2. Redistributions in binary form must reproduce the above copyright notice,
+ *	   this list of conditions and the following disclaimer in the documentation
+ *	   and/or other materials provided with the distribution.
+ *	3. Neither the name of the Skulltag Development Team nor the names of its
+ *	   contributors may be used to endorse or promote products derived from this
+ *	   software without specific prior written permission.
+ *	4. Redistributions in any form must be accompanied by information on how to
+ *	   obtain complete source code for the software and any accompanying
+ *	   software that uses the software. The source code must either be included
+ *	   in the distribution or be available for no more than the cost of
+ *	   distribution plus a nominal fee, and must be freely redistributable
+ *	   under reasonable conditions. For an executable file, complete source
+ *	   code means the source code for all modules it contains. It does not
+ *	   include source code for modules or files that typically accompany the
+ *	   major components of the operating system on which the executable file
+ *	   runs.
+ *	
+ *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ *	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ *	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ *	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ *	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ *	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ *	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ *	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ *	POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "common.h"
+#include "scriptreader.h"
+#include "str.h"
+#include "events.h"
+
+EventDef* g_EventDef;
+void ReadEvents () {
+	ScriptReader* r = new ScriptReader ((char*)"events.def");
+	g_EventDef = NULL;
+	EventDef* curdef = g_EventDef;
+	unsigned int numEventDefs = 0;
+	while (r->Next()) {
+		EventDef* e = new EventDef;
+		e->name = r->token;
+		e->number = numEventDefs;
+		e->next = NULL;
+		
+		// g_EventDef becomes the first eventdef
+		if (!g_EventDef)
+			g_EventDef = e;
+		
+		if (!curdef) {
+			curdef = e;
+		} else {
+			curdef->next = e;
+			curdef = e;
+		}
+		numEventDefs++;
+	}
+	
+	delete r;
+	printf ("%d event definitions read.\n", numEventDefs);
+}
+
+void UnlinkEvents (EventDef* e) {
+	if (e->next)
+		UnlinkEvents (e->next);
+	delete e;
+}
+
+EventDef* FindEventByIdx (unsigned int idx) {
+	EventDef* e = g_EventDef;
+	while (idx > 0) {
+		if (!e->next)
+			return NULL;
+		e = e->next;
+		idx--;
+	}
+	return e;
+}
+
+EventDef* FindEventByName (str a) {
+	a.tolower();
+	
+	EventDef* e;
+	for (e = g_EventDef; e->next != NULL; e = e->next) {
+		str b = e->name;
+		b.tolower();
+		
+		if (!a.compare (b))
+			return e;
+	}
+	
+	return NULL;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/events.def	Fri Jul 13 20:10:08 2012 +0300
@@ -0,0 +1,65 @@
+KilledByEnemy
+KilledByPlayer
+KilledBySelf
+KilledByEnvironment
+ReachedGoal
+GoalRemoved
+DamagedByPlayer
+PlayerSay
+EnemyKilled
+Respawned
+Intermission
+NewMap
+EnemyUsedFist
+EnemyUsedChainsaw
+EnemyFiredPistol
+EnemyFiredShotgun
+EnemyFiredSSG
+EnemyFiredChaingun
+EnemyFiredMinigun
+EnemyFiredRocket
+EnemyFiredGrenade
+EnemyFiredRailgun
+EnemyFiredPlasma
+EnemyFiredBFG
+EnemyFiredBFG10k
+PlayerUsedFist
+PlayerUsedChainsaw
+PlayerFiredPistol
+PlayerFiredShotgun
+PlayerFiredSSG
+PlayerFiredChaingun
+PlayerFiredMinigun
+PlayerFiredRocket
+PlayerFiredGrenade
+PlayerFiredRailgun
+PlayerFiredPlasma
+PlayerFiredBFG
+PlayerFiredBFG10k
+UsedFist
+UsedChainsaw
+FiredPistol
+FiredShotgun
+FiredSSG
+FiredChaingun
+FiredMinigun
+FiredRocket
+FiredGrenade
+FiredRailgun
+FiredPlasma
+FiredBFG
+FiredBFG10k
+PlayerJoinedGame
+JoinedGame
+DuelStartingCountdown
+DuelFight
+DuelWinSequence
+Spectating
+LMSStartingCountdown
+LMSFight
+LMSWinSequence
+WeaponChange
+EnemyBFGExplode
+PlayerBFGExplode
+BFGExplode
+ReceivedMedal
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/events.h	Fri Jul 13 20:10:08 2012 +0300
@@ -0,0 +1,57 @@
+/*
+ *	botc source code
+ *	Copyright (C) 2012 Santeri `Dusk` Piippo
+ *	All rights reserved.
+ *	
+ *	Redistribution and use in source and binary forms, with or without
+ *	modification, are permitted provided that the following conditions are met:
+ *	
+ *	1. Redistributions of source code must retain the above copyright notice,
+ *	   this list of conditions and the following disclaimer.
+ *	2. Redistributions in binary form must reproduce the above copyright notice,
+ *	   this list of conditions and the following disclaimer in the documentation
+ *	   and/or other materials provided with the distribution.
+ *	3. Neither the name of the Skulltag Development Team nor the names of its
+ *	   contributors may be used to endorse or promote products derived from this
+ *	   software without specific prior written permission.
+ *	4. Redistributions in any form must be accompanied by information on how to
+ *	   obtain complete source code for the software and any accompanying
+ *	   software that uses the software. The source code must either be included
+ *	   in the distribution or be available for no more than the cost of
+ *	   distribution plus a nominal fee, and must be freely redistributable
+ *	   under reasonable conditions. For an executable file, complete source
+ *	   code means the source code for all modules it contains. It does not
+ *	   include source code for modules or files that typically accompany the
+ *	   major components of the operating system on which the executable file
+ *	   runs.
+ *	
+ *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ *	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ *	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ *	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ *	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ *	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ *	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ *	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ *	POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __EVENT_H__
+#define __EVENT_H__
+
+#include "str.h"
+
+struct EventDef {
+	str name;
+	int number;
+	EventDef* next;
+};
+
+void ReadEvents ();
+void UnlinkEvents (EventDef* e);
+EventDef* FindEventByIdx (unsigned int idx);
+EventDef* FindEventByName (str a);
+
+#endif // __EVENT_H__
\ No newline at end of file
--- a/main.cxx	Fri Jul 13 18:41:40 2012 +0300
+++ b/main.cxx	Fri Jul 13 20:10:08 2012 +0300
@@ -47,6 +47,7 @@
 #include "scriptreader.h"
 #include "objwriter.h"
 #include "parser.h"
+#include "events.h"
 
 #include "bots.h"
 #include "botcommands.h"
@@ -64,11 +65,12 @@
 	headerline.repeat ((header.len()/2)-1);
 	printf ("%s\n%s\n", header.chars(), headerline.chars());
 	
+	// Read the event definitions
+	ReadEvents ();
+	
 	// Prepare reader and writer
-	str infile = argv[1];
-	str outfile = argv[2];
-	ScriptReader *r = new ScriptReader (infile);
-	ObjWriter *w = new ObjWriter (outfile);
+	ScriptReader *r = new ScriptReader (argv[1]);
+	ObjWriter *w = new ObjWriter (argv[2]);
 	
 	// We're set, begin parsing :)
 	r->BeginParse (w);
--- a/parser.cxx	Fri Jul 13 18:41:40 2012 +0300
+++ b/parser.cxx	Fri Jul 13 20:10:08 2012 +0300
@@ -46,6 +46,7 @@
 #include "str.h"
 #include "objwriter.h"
 #include "scriptreader.h"
+#include "events.h"
 
 #define TOKEN_CHARS token.chars()
 #define TOKEN_IS(s) !token.compare (s)
@@ -97,25 +98,16 @@
 			// Event definition
 			MustNext ();
 			
-			// TODO: make a data file for bot events and read it
-#if 0
-			unsigned int u;
-			for (u = 0; u < NUM_BOTEVENTS; u++) {
-				if (!BotEvents[u].name.compare (token))
-					break;
-			}
-			
-			if (u == NUM_BOTEVENTS)
+			EventDef* e = FindEventByName (token);
+			if (!e)
 				ParseError ("bad event! got `%s`\n", token.chars());
 			
-#endif
-			
 			MustNext ("{");
 			
 			g_CurMode = MODE_EVENT;
 			
 			w->Write (DH_EVENT);
-			// w->Write<long> (u);
+			w->Write<long> (e->number);
 			g_NumEvents++;
 		} else if (TOKEN_IS ("}")) {
 			// Closing brace..
--- a/scriptreader.cxx	Fri Jul 13 18:41:40 2012 +0300
+++ b/scriptreader.cxx	Fri Jul 13 20:10:08 2012 +0300
@@ -53,9 +53,11 @@
 	return false;
 }
 
-ScriptReader::ScriptReader (str path) {
-	fp = fopen (path, "r");
-	CHECK_FILE (fp, path, "reading");
+ScriptReader::ScriptReader (char* path) {
+	if (!(fp = fopen (path, "r"))) {
+		error ("couldn't open %s for reading!\n", path);
+		exit (1);
+	}
 	
 	curline = 1;
 	pos = 0;
--- a/scriptreader.h	Fri Jul 13 18:41:40 2012 +0300
+++ b/scriptreader.h	Fri Jul 13 20:10:08 2012 +0300
@@ -63,7 +63,7 @@
 	
 	// ====================================================================
 	// METHODS
-	ScriptReader (str path);
+	ScriptReader (char* path);
 	~ScriptReader ();
 	char ReadChar ();
 	bool Next ();
--- a/str.cxx	Fri Jul 13 18:41:40 2012 +0300
+++ b/str.cxx	Fri Jul 13 20:10:08 2012 +0300
@@ -99,6 +99,8 @@
 	
 	delete text;
 	text = new char[len+1];
+	for (unsigned int u = 0; u < len+1; u++)
+		text[u] = 0;
 	strncpy (text, oldtext, len);
 	
 	alloclen = len;
@@ -107,7 +109,7 @@
 // ============================================================================
 void str::dump () {
 	for (unsigned int u = 0; u <= alloclen; u++)
-		printf ("\t%u. %d (%c)\n", u, text[u], text[u]);
+		printf ("\t%u. %u (%c)\n", u, text[u], text[u]);
 }
 
 // ============================================================================
@@ -116,7 +118,6 @@
 	// Out of space, thus resize
 	if (curs == alloclen)
 		resize (alloclen+1);
-	
 	text[curs] = c;
 	curs++;
 }
@@ -348,6 +349,30 @@
 }
 
 // ============================================================================
+str str::tolower () {
+	str n = text;
+	
+	for (uint u = 0; u < len(); u++) {
+		if (n[u] > 'A' && n[u] < 'Z')
+			n.text[u] += ('a' - 'A');
+	}
+	
+	return n;
+}
+
+// ============================================================================
+str str::toupper () {
+	str n = text;
+	
+	for (uint u = 0; u < len(); u++) {
+		if (n[u] > 'a' && n[u] < 'z')
+			n.text[u] -= ('A' - 'a');
+	}
+	
+	return n;
+}
+
+// ============================================================================
 // OPERATORS
 str str::operator + (str& c) {
 	append (c);
--- a/str.h	Fri Jul 13 18:41:40 2012 +0300
+++ b/str.h	Fri Jul 13 20:10:08 2012 +0300
@@ -126,6 +126,12 @@
 	// Is the string a word, i.e consists only of alphabetic letters?
 	bool isword ();
 	
+	// Convert string to lower case
+	str tolower ();
+	
+	// Convert string to upper case
+	str toupper ();
+	
 	bool contentcheck (int flags);
 	
 	int compare (const char* c);

mercurial