Fri, 13 Jul 2012 17:20:51 +0300
Initial commit
Makefile | file | annotate | diff | comparison | revisions | |
botcommands.h | file | annotate | diff | comparison | revisions | |
bots.h | file | annotate | diff | comparison | revisions | |
common.h | file | annotate | diff | comparison | revisions | |
main.cxx | file | annotate | diff | comparison | revisions | |
objwriter.cxx | file | annotate | diff | comparison | revisions | |
objwriter.h | file | annotate | diff | comparison | revisions | |
parser.cxx | file | annotate | diff | comparison | revisions | |
parser.h | 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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,10 @@ +all: + g++ -Wall -c -o scriptreader.o scriptreader.cxx + g++ -Wall -c -o objwriter.o objwriter.cxx + 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 + +clean: + rm -f *.o *~ botc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/botcommands.h Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,183 @@ +//----------------------------------------------------------------------------- +// +// Skulltag Source +// Copyright (C) 2002 Brad Carney +// Copyright (C) 2007-2012 Skulltag Development Team +// 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. +// +// Date created: 5/18/04 +// +// +// Filename: botcommands.h +// +// Description: Contains bot structures and prototypes +// [Dusk] Clipped stuff that botc doesn't need. +// +//----------------------------------------------------------------------------- + +#ifndef __BOTCOMMANDS_H__ +#define __BOTCOMMANDS_H__ + +#include "bots.h" + +//***************************************************************************** +// DEFINES + +#define SETENEMY_LASTSEEN 0 +#define SETENEMY_LASTSHOTBY 1 + +// Different results for pathing commands. +#define PATH_UNREACHABLE -1 +#define PATH_INCOMPLETE 0 +#define PATH_COMPLETE 1 +#define PATH_REACHEDGOAL 2 + +// This is the size of the return string for the bot command functions. +#define BOTCMD_RETURNSTRING_SIZE 256 + +//***************************************************************************** +typedef enum +{ + BOTCMD_CHANGESTATE, // Basic botcmd utility functions. + BOTCMD_DELAY, + BOTCMD_RAND, + BOTCMD_STRINGSAREEQUAL, + BOTCMD_LOOKFORPOWERUPS, // Search functions. + BOTCMD_LOOKFORWEAPONS, + BOTCMD_LOOKFORAMMO, + BOTCMD_LOOKFORBASEHEALTH, + BOTCMD_LOOKFORBASEARMOR, + BOTCMD_LOOKFORSUPERHEALTH, + BOTCMD_LOOKFORSUPERARMOR, /* 10 */ + BOTCMD_LOOKFORPLAYERENEMIES, + BOTCMD_GETCLOSESTPLAYERENEMY, + BOTCMD_MOVELEFT, // Movement functions. + BOTCMD_MOVERIGHT, + BOTCMD_MOVEFORWARD, + BOTCMD_MOVEBACKWARDS, + BOTCMD_STOPMOVEMENT, + BOTCMD_STOPFORWARDMOVEMENT, + BOTCMD_STOPSIDEWAYSMOVEMENT, + BOTCMD_CHECKTERRAIN, /* 20 */ + BOTCMD_PATHTOGOAL, // Pathing functions. + BOTCMD_PATHTOLASTKNOWNENEMYPOSITION, + BOTCMD_PATHTOLASTHEARDSOUND, + BOTCMD_ROAM, + BOTCMD_GETPATHINGCOSTTOITEM, + BOTCMD_GETDISTANCETOITEM, + BOTCMD_GETITEMNAME, + BOTCMD_ISITEMVISIBLE, + BOTCMD_SETGOAL, + BOTCMD_BEGINAIMINGATENEMY, /* 30 */ // Aiming functions. + BOTCMD_STOPAIMINGATENEMY, + BOTCMD_TURN, + BOTCMD_GETCURRENTANGLE, + BOTCMD_SETENEMY, // Enemy functions. + BOTCMD_CLEARENEMY, + BOTCMD_ISENEMYALIVE, + BOTCMD_ISENEMYVISIBLE, + BOTCMD_GETDISTANCETOENEMY, + BOTCMD_GETPLAYERDAMAGEDBY, + BOTCMD_GETENEMYINVULNERABILITYTICKS, /* 40 */ + BOTCMD_FIREWEAPON, // Weapon functions. + BOTCMD_BEGINFIRINGWEAPON, + BOTCMD_STOPFIRINGWEAPON, + BOTCMD_GETCURRENTWEAPON, + BOTCMD_CHANGEWEAPON, + BOTCMD_GETWEAPONFROMITEM, + BOTCMD_ISWEAPONOWNED, + BOTCMD_ISFAVORITEWEAPON, + BOTCMD_SAY, // Chat functions. + BOTCMD_SAYFROMFILE, /* 50 */ + BOTCMD_SAYFROMCHATFILE, + BOTCMD_BEGINCHATTING, + BOTCMD_STOPCHATTING, + BOTCMD_CHATSECTIONEXISTS, + BOTCMD_CHATSECTIONEXISTSINFILE, + BOTCMD_GETLASTCHATSTRING, + BOTCMD_GETLASTCHATPLAYER, + BOTCMD_GETCHATFREQUENCY, + BOTCMD_JUMP, // Jumping functions. + BOTCMD_BEGINJUMPING, /* 60 */ + BOTCMD_STOPJUMPING, + BOTCMD_TAUNT, // Other action functions. + BOTCMD_RESPAWN, + BOTCMD_TRYTOJOINGAME, + BOTCMD_ISDEAD, // Information about self functions. + BOTCMD_ISSPECTATING, + BOTCMD_GETHEALTH, + BOTCMD_GETARMOR, + BOTCMD_GETBASEHEALTH, + BOTCMD_GETBASEARMOR, /* 70 */ + BOTCMD_GETBOTSKILL, // Botskill functions. + BOTCMD_GETACCURACY, + BOTCMD_GETINTELLECT, + BOTCMD_GETANTICIPATION, + BOTCMD_GETEVADE, + BOTCMD_GETREACTIONTIME, + BOTCMD_GETPERCEPTION, + BOTCMD_SETSKILLINCREASE, // Botskill modifying functions functions. + BOTCMD_ISSKILLINCREASED, + BOTCMD_SETSKILLDECREASE, /* 80 */ + BOTCMD_ISSKILLDECREASED, + BOTCMD_GETGAMEMODE, // Other functions. + BOTCMD_GETSPREAD, + BOTCMD_GETLASTJOINEDPLAYER, + BOTCMD_GETPLAYERNAME, + BOTCMD_GETRECEIVEDMEDAL, + BOTCMD_ACS_EXECUTE, + BOTCMD_GETFAVORITEWEAPON, + BOTCMD_SAYFROMLUMP, + BOTCMD_SAYFROMCHATLUMP, /* 90 */ + BOTCMD_CHATSECTIONEXISTSINLUMP, + BOTCMD_CHATSECTIONEXISTSINCHATLUMP, + + NUM_BOTCMDS + +} BOTCMD_e; + +//***************************************************************************** +typedef enum +{ + RETURNVAL_VOID, + RETURNVAL_INT, + RETURNVAL_BOOLEAN, + RETURNVAL_STRING, + +} RETURNVAL_e; + +#endif // __BOTCOMMANDS_H__ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bots.h Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,272 @@ +//----------------------------------------------------------------------------- +// +// Skulltag Source +// Copyright (C) 2002 Brad Carney +// Copyright (C) 2007-2012 Skulltag Development Team +// 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. +// +// +// +// Filename: bots.h +// +// Description: Contains bot structures and prototypes +// [Dusk] Cropped out the stuff botc doesn't need. +// +//----------------------------------------------------------------------------- + +#ifndef __BOTS_H__ +#define __BOTS_H__ + +//***************************************************************************** +// DEFINES + +// Maximum number of variables on the bot evaluation stack. +#define BOTSCRIPT_STACK_SIZE 8 + +// Maximum number of botinto structures that can be defines. +#define MAX_BOTINFO 128 + +// Maximum number of states that can appear in a script. +#define MAX_NUM_STATES 256 + +// Maximum number of bot events that can be defined. +#define MAX_NUM_EVENTS 32 + +// Maximum number of global bot events that can be defined. +#define MAX_NUM_GLOBAL_EVENTS 32 + +// Maximum number of global variables that can be defined in a script. +#define MAX_SCRIPT_VARIABLES 128 + +// Maximum number of global arrays that can be defined in a script. +#define MAX_SCRIPT_ARRAYS 16 + +// Maximum number of global arrays that can be defined in a script. +#define MAX_SCRIPTARRAY_SIZE 65536 + +// Maxmimum number of state (local) variables that can appear in a script. +#define MAX_STATE_VARIABLES 16 + +// Maximum number of strings that can appear in a script stringlist. +#define MAX_LIST_STRINGS 128 + +// Maximum length of those strings in the stringlist. +#define MAX_STRING_LENGTH 256 + +// Maximum reaction time for a bot. +#define MAX_REACTION_TIME 52 + +// Maximum number of events the bots can store up that it's waiting to react to. +#define MAX_STORED_EVENTS 64 + +//***************************************************************************** +typedef enum +{ + // Bot skill ratings. + BOTSKILL_VERYPOOR, + BOTSKILL_POOR, + BOTSKILL_LOW, + BOTSKILL_MEDIUM, + BOTSKILL_HIGH, + BOTSKILL_EXCELLENT, + BOTSKILL_SUPREME, + BOTSKILL_GODLIKE, + BOTSKILL_PERFECT, + + NUM_BOT_SKILLS + +} BOTSKILL_e; + +//***************************************************************************** +// STRUCTURES +// + +//***************************************************************************** +// These are the botscript data headers that it writes out. +typedef enum +{ + DH_COMMAND, + DH_STATEIDX, + DH_STATENAME, + DH_ONENTER, + DH_MAINLOOP, + DH_ONEXIT, + DH_EVENT, + DH_ENDONENTER, + DH_ENDMAINLOOP, + DH_ENDONEXIT, + DH_ENDEVENT, + DH_IFGOTO, + DH_IFNOTGOTO, + DH_GOTO, + DH_ORLOGICAL, + DH_ANDLOGICAL, + DH_ORBITWISE, + DH_EORBITWISE, + DH_ANDBITWISE, + DH_EQUALS, + DH_NOTEQUALS, + DH_LESSTHAN, + DH_LESSTHANEQUALS, + DH_GREATERTHAN, + DH_GREATERTHANEQUALS, + DH_NEGATELOGICAL, + DH_LSHIFT, + DH_RSHIFT, + DH_ADD, + DH_SUBTRACT, + DH_UNARYMINUS, + DH_MULTIPLY, + DH_DIVIDE, + DH_MODULUS, + DH_PUSHNUMBER, + DH_PUSHSTRINGINDEX, + DH_PUSHGLOBALVAR, + DH_PUSHLOCALVAR, + DH_DROPSTACKPOSITION, + DH_SCRIPTVARLIST, + DH_STRINGLIST, + DH_INCGLOBALVAR, + DH_DECGLOBALVAR, + DH_ASSIGNGLOBALVAR, + DH_ADDGLOBALVAR, + DH_SUBGLOBALVAR, + DH_MULGLOBALVAR, + DH_DIVGLOBALVAR, + DH_MODGLOBALVAR, + DH_INCLOCALVAR, + DH_DECLOCALVAR, + DH_ASSIGNLOCALVAR, + DH_ADDLOCALVAR, + DH_SUBLOCALVAR, + DH_MULLOCALVAR, + DH_DIVLOCALVAR, + DH_MODLOCALVAR, + DH_CASEGOTO, + DH_DROP, + DH_INCGLOBALARRAY, + DH_DECGLOBALARRAY, + DH_ASSIGNGLOBALARRAY, + DH_ADDGLOBALARRAY, + DH_SUBGLOBALARRAY, + DH_MULGLOBALARRAY, + DH_DIVGLOBALARRAY, + DH_MODGLOBALARRAY, + DH_PUSHGLOBALARRAY, + DH_SWAP, + DH_DUP, + DH_ARRAYSET, + + NUM_DATAHEADERS + +} DATAHEADERS_e; + +//***************************************************************************** +// These are the different bot events that can be posted to a bot's state. +typedef enum +{ + BOTEVENT_KILLED_BYENEMY, + BOTEVENT_KILLED_BYPLAYER, + BOTEVENT_KILLED_BYSELF, + BOTEVENT_KILLED_BYENVIORNMENT, + BOTEVENT_REACHED_GOAL, + BOTEVENT_GOAL_REMOVED, + BOTEVENT_DAMAGEDBY_PLAYER, + BOTEVENT_PLAYER_SAY, + BOTEVENT_ENEMY_KILLED, + BOTEVENT_RESPAWNED, + BOTEVENT_INTERMISSION, + BOTEVENT_NEWMAP, + BOTEVENT_ENEMY_USEDFIST, + BOTEVENT_ENEMY_USEDCHAINSAW, + BOTEVENT_ENEMY_FIREDPISTOL, + BOTEVENT_ENEMY_FIREDSHOTGUN, + BOTEVENT_ENEMY_FIREDSSG, + BOTEVENT_ENEMY_FIREDCHAINGUN, + BOTEVENT_ENEMY_FIREDMINIGUN, + BOTEVENT_ENEMY_FIREDROCKET, + BOTEVENT_ENEMY_FIREDGRENADE, + BOTEVENT_ENEMY_FIREDRAILGUN, + BOTEVENT_ENEMY_FIREDPLASMA, + BOTEVENT_ENEMY_FIREDBFG, + BOTEVENT_ENEMY_FIREDBFG10K, + BOTEVENT_PLAYER_USEDFIST, + BOTEVENT_PLAYER_USEDCHAINSAW, + BOTEVENT_PLAYER_FIREDPISTOL, + BOTEVENT_PLAYER_FIREDSHOTGUN, + BOTEVENT_PLAYER_FIREDSSG, + BOTEVENT_PLAYER_FIREDCHAINGUN, + BOTEVENT_PLAYER_FIREDMINIGUN, + BOTEVENT_PLAYER_FIREDROCKET, + BOTEVENT_PLAYER_FIREDGRENADE, + BOTEVENT_PLAYER_FIREDRAILGUN, + BOTEVENT_PLAYER_FIREDPLASMA, + BOTEVENT_PLAYER_FIREDBFG, + BOTEVENT_PLAYER_FIREDBFG10K, + BOTEVENT_USEDFIST, + BOTEVENT_USEDCHAINSAW, + BOTEVENT_FIREDPISTOL, + BOTEVENT_FIREDSHOTGUN, + BOTEVENT_FIREDSSG, + BOTEVENT_FIREDCHAINGUN, + BOTEVENT_FIREDMINIGUN, + BOTEVENT_FIREDROCKET, + BOTEVENT_FIREDGRENADE, + BOTEVENT_FIREDRAILGUN, + BOTEVENT_FIREDPLASMA, + BOTEVENT_FIREDBFG, + BOTEVENT_FIREDBFG10K, + BOTEVENT_PLAYER_JOINEDGAME, + BOTEVENT_JOINEDGAME, + BOTEVENT_DUEL_STARTINGCOUNTDOWN, + BOTEVENT_DUEL_FIGHT, + BOTEVENT_DUEL_WINSEQUENCE, + BOTEVENT_SPECTATING, + BOTEVENT_LMS_STARTINGCOUNTDOWN, + BOTEVENT_LMS_FIGHT, + BOTEVENT_LMS_WINSEQUENCE, + BOTEVENT_WEAPONCHANGE, + BOTEVENT_ENEMY_BFGEXPLODE, + BOTEVENT_PLAYER_BFGEXPLODE, + BOTEVENT_BFGEXPLODE, + BOTEVENT_RECEIVEDMEDAL, + + NUM_BOTEVENTS + +} BOTEVENT_e; + +#endif // __BOTS_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common.h Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,66 @@ +/* + * 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 __COMMON_H__ +#define __COMMON_H__ + +#include <stdarg.h> +#include "bots.h" +#include "str.h" + +typedef unsigned long qbyte; + +#define CHECK_FILE(path,action) if (!fp) { \ + fprintf (stderr, "error: couldn't open %s for %s!\n", (char*)path, action); \ + exit (1); \ + } + +#define PERFORM_FORMAT(in, out) va_list v; \ + va_start (v, in); \ + char* out = vdynformat (in, v, 256); \ + va_end (v); + +void error (const char* text, ...); + +struct EventDef { + str name; +}; + +#endif // __COMMON_H__ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cxx Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,77 @@ +/* + * 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "common.h" + +#include "str.h" +#include "scriptreader.h" +#include "objwriter.h" +#include "parser.h" + +#include "bots.h" +#include "botcommands.h" + +int main (int argc, char** argv) { + if (argc != 3) { + fprintf (stderr, "usage: %s: <infile> <outFile>\n", argv[0]); + exit (1); + } + + str infile = argv[1]; + str outfile = argv[2]; + + ScriptReader *r = new ScriptReader (infile); + ObjWriter *w = new ObjWriter (outfile); + + // Alrite, we're set, begin parsing :) + r->BeginParse (w); + + delete r; + delete w; +} + +void error (const char* text, ...) { + PERFORM_FORMAT (text, c); + fprintf (stderr, "error: %s", c); + exit (1); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/objwriter.cxx Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,84 @@ +/* + * 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "common.h" +#include "str.h" +#include "objwriter.h" + +#include "bots.h" + +ObjWriter::ObjWriter (str path) { + numstates = 0; + + fp = fopen (path, "w"); + CHECK_FILE (path, "writing"); +} + +ObjWriter::~ObjWriter () { + fclose (fp); +} + +void ObjWriter::WriteString (char* s) { + for (unsigned int u = 0; u < strlen (s); u++) + Write<char> (s[u]); +} + +void ObjWriter::WriteString (const char* s) { + WriteString (const_cast<char*> (s)); +} + +void ObjWriter::WriteString (str s) { + WriteString (s.chars()); +} + +// Writes a state label +void ObjWriter::WriteState (str name) { + printf ("write state %s\n", (char*)name); + Write (DH_STATENAME); + Write (name.len()); + WriteString (name); + Write (DH_STATEIDX); + Write (numstates); + + numstates++; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/objwriter.h Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,68 @@ +/* + * 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 __OBJWRITER_H__ +#define __OBJWRITER_H__ + +#include <stdio.h> +#include "common.h" +#include "str.h" + +class ObjWriter { +public: + FILE* fp; + int numstates; + + ObjWriter (str path); + ~ObjWriter (); + void WriteString (char* s); + void WriteString (const char* s); + void WriteString (str s); + void WriteState (str name); + + template <class T> void Write (T stuff) { + fwrite (&stuff, sizeof (T), 1, fp); + } + + // Cannot use default arguments in function templates.. + void Write (qbyte stuff) {Write<qbyte> (stuff);} +}; + +#endif // __OBJWRITER_H__ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parser.cxx Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,139 @@ +/* + * 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 <stdio.h> +#include <stdlib.h> +#include "common.h" +#include "str.h" +#include "objwriter.h" +#include "scriptreader.h" + +#define TOKEN_CHARS token.chars() +#define TOKEN_IS(s) !token.compare (s) +#define MUST_TOPLEVEL if (curmode != MODE_TOPLEVEL) \ + ParseError ("%ss may only be defined at top level!", token.chars()); + +void ScriptReader::BeginParse (ObjWriter* w) { + // Script starts at top level + curmode = MODE_TOPLEVEL; + curstate = ""; + + while (Next()) { + printf ("got token %s\n", (char*)token); + if (TOKEN_IS ("state")) { + MUST_TOPLEVEL + + MustNext (); + + str statename = token; + + // State name must be a word. + if (statename.first (" ") != statename.len()) + ParseError ("state name must be a single word! got `%s`", (char*)statename); + + + // Must end in a colon + MustNext (":"); + + w->WriteState (statename); + curstate = statename; + } else if (TOKEN_IS ("event")) { + MUST_TOPLEVEL + + // 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) + ParseError ("bad event! got `%s`\n", token.chars()); + +#endif + + MustNext ("{"); + + curmode = MODE_EVENT; + + w->Write (DH_EVENT); + // w->Write<long> (u); + numevents++; + } else if (TOKEN_IS ("}")) { + // Closing brace.. + switch (curmode) { + case MODE_EVENT: + // Brace closes event. + w->Write (DH_ENDEVENT); + curmode = MODE_TOPLEVEL; + break; + default: + ParseError ("unexpected `}`"); + } + } else { + ParseError ("unknown keyword `%s`!", TOKEN_CHARS); + } + } + + if (curmode != MODE_TOPLEVEL) + ParseError ("script did not end at top level! did you forget a `}`?\n"); + + /* + // State + w->WriteState ("stateSpawn"); + + w->Write (DH_ONENTER); + w->Write (DH_ENDONENTER); + + w->Write (DH_MAINLOOP); + w->Write (DH_ENDMAINLOOP); + + w->Write (DH_EVENT); + w->Write (BOTEVENT_PLAYER_FIREDSSG); + w->Write (DH_COMMAND); + w->Write (BOTCMD_BEGINJUMPING); + w->Write (0); + w->Write (DH_ENDEVENT); + */ +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parser.h Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,2 @@ +#include "scriptreader.h" +#include "objwriter.h"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scriptreader.cxx Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,167 @@ +/* + * 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 <stdio.h> +#include <stdlib.h> +#include "string.h" +#include "str.h" +#include "common.h" +#include "scriptreader.h" + +bool IsWhitespace (char c) { + // These characters are invisible, thus considered whitespace + if (c <= 32 || c == 127 || c == 255) + return true; + + return false; +} + +ScriptReader::ScriptReader (str path) { + fp = fopen (path, "r"); + CHECK_FILE (path, "reading"); + + curline = 1; + pos = 0; + token = ""; +} + +char ScriptReader::ReadChar () { + char* c = (char*)malloc (sizeof (char)); + if (!fread (c, sizeof (char), 1, fp)) + return 0; + + // If we just read a new line, increase current line number counter. + if (c[0] == '\n') + curline++; + + return c[0]; +} + +// true if was found, false if not. +bool ScriptReader::Next () { + str tmp = ""; + bool quote = false; + tokenquoted = false; + while (!feof (fp)) { + char c = ReadChar (); + + // Quotation mark. + if (c == '"') { + // End-quote ends the token. + if (quote) + break; + + tokenquoted = true; + quote = !quote; + continue; + } + + if (IsWhitespace (c) && !quote) { + // Don't break if we haven't gathered anything yet. + if (tmp.len()) + break; + } else { + tmp += c; + } + } + + // If we got nothing here, read failed. + if (!tmp.len()) { + token = ""; + return false; + } + + pos++; + token = tmp; + return true; +} + +// Returns the next token without advancing the cursor. +str ScriptReader::PeekNext () { + // Store current position + int cpos = ftell (fp); + + // Advance on the token. + if (!Next ()) + return ""; + + str tmp = token; + + // Restore position + fseek (fp, cpos, SEEK_SET); + pos--; + + return tmp; +} + +void ScriptReader::Seek (unsigned int n, int origin) { + switch (origin) { + case SEEK_SET: + fseek (fp, 0, SEEK_SET); + pos = 0; + break; + case SEEK_CUR: + break; + case SEEK_END: + printf ("ScriptReader::Seek: SEEK_END not yet supported.\n"); + break; + } + + for (unsigned int i = 0; i < n+1; i++) + Next(); +} + +void ScriptReader::MustNext (const char* c) { + if (!Next()) { + if (strlen (c)) + ParseError ("expected `%s`, reached end of file instead\n", c); + else + ParseError ("expected a token, reached end of file instead\n"); + } + + if (strlen (c) && token.compare (c) != 0) { + ParseError ("expected `%s`, got `%s` instead", c, token.chars()); + } +} + +void ScriptReader::ParseError (const char* message, ...) { + PERFORM_FORMAT (message, outmessage); + error ("Parse error on line %d:\n%s\n", curline, outmessage); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scriptreader.h Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,78 @@ +/* + * 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 __SCRIPTREADER_H__ +#define __SCRIPTREADER_H__ + +#include <stdio.h> +#include "str.h" +#include "objwriter.h" + +// Where is the parser at? +enum parsermode { + MODE_TOPLEVEL, // at top level + MODE_EVENT // inside event definition +}; + +class ScriptReader { +public: + FILE* fp; + unsigned int pos; + unsigned int curline; + str token; + bool tokenquoted; + + parsermode curmode; + str curstate; + + unsigned int numevents; + + ScriptReader (str path); + char ReadChar (); + bool Next (); + str PeekNext (); + void Seek (unsigned int n, int origin); + void MustNext (const char* c = ""); + void ParseError (const char* message, ...); + + void BeginParse (ObjWriter* w); +}; + +#endif // __SCRIPTREADER_H__ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/str.cxx Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,381 @@ +/* + * 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include "str.h" +#include "common.h" + +#define ITERATE_STRING(u) for (unsigned int u = 0; u < strlen (text); u++) + +// ============================================================================ +// vdynformat: Try to write to a formatted string with size bytes first, if +// that fails, double the size and keep recursing until it works. +char* vdynformat (const char* c, va_list v, unsigned int size) { + char* buffer = new char[size]; + int r = vsnprintf (buffer, size-1, c, v); + if (r > (int)size-1 || r < 0) { + delete buffer; + buffer = vdynformat (c, v, size*2); + } + return buffer; +} + +// ============================================================================ +str::str () { + text = new char[1]; + clear(); + alloclen = strlen (text); +} + +str::str (const char* c) { + text = new char[1]; + clear (); + alloclen = strlen (text); + append (c); +} + +str::str (char c) { + text = new char[1]; + clear (); + alloclen = strlen (text); + append (c); +} + +// ============================================================================ +void str::clear () { + delete text; + text = new char[1]; + text[0] = '\0'; + curs = 0; +} + +unsigned int str::len () {return strlen(text);} + +// ============================================================================ +void str::resize (unsigned int len) { + unsigned int oldlen = strlen (text); + char* oldtext = new char[oldlen]; + strncpy (oldtext, text, oldlen); + + delete text; + text = new char[len+1]; + strncpy (text, oldtext, len); + + alloclen = len; +} + +// ============================================================================ +void str::dump () { + for (unsigned int u = 0; u <= alloclen; u++) + printf ("\t%u. %d (%c)\n", u, text[u], text[u]); +} + +// ============================================================================ +// Adds a new character at the end of the string. +void str::append (char c) { + // Out of space, thus resize + if (curs == alloclen) + resize (alloclen+1); + + text[curs] = c; + curs++; +} + +void str::append (const char* c) { + resize (alloclen + strlen (c)); + + for (unsigned int u = 0; u < strlen (c); u++) { + if (c[u] != 0) + append (c[u]); + } +} + +void str::append (str c) { + append (c.chars()); +} + +// ============================================================================ +void str::appendformat (const char* c, ...) { + va_list v; + + va_start (v, c); + char* buf = vdynformat (c, v, 256); + va_end (v); + + append (buf); +} + +void str::appendformat (str c, ...) { + va_list v; + + va_start (v, c); + char* buf = vdynformat (c.chars(), v, 256); + va_end (v); + + append (buf); +} + +// ============================================================================ +char* str::chars () { + return text; +} + +// ============================================================================ +unsigned int str::first (const char* c, unsigned int a) { + unsigned int r = 0; + unsigned int index = 0; + for (; a < alloclen; a++) { + if (text[a] == c[r]) { + if (r == 0) + index = a; + + r++; + if (r == strlen (c)) + return index; + } else { + if (r != 0) { + // If the string sequence broke at this point, we need to + // check this character again, for a new sequence just + // might start right here. + a--; + } + + r = 0; + } + } + + return len (); +} + +// ============================================================================ +unsigned int str::last (const char* c, int a) { + if (a == -1) + a = len(); + + int max = strlen (c)-1; + + int r = max; + for (; a >= 0; a--) { + if (text[a] == c[r]) { + r--; + if (r == -1) + return a; + } else { + if (r != max) + a++; + + r = max; + } + } + + return len (); +} + +// ============================================================================ +str str::substr (unsigned int a, unsigned int b) { + if (a > len()) a = len(); + if (b > len()) b = len(); + + if (b == a) + return ""; + + if (b < a) { + printf ("str::substr: indices %u and %u given, should be the other way around, swapping..\n", a, b); + + // Swap the variables + unsigned int c = a; + a = b; + b = c; + } + + char* s = new char[b-a]; + strncpy (s, text+a, b-a); + return str(s); +} + +// ============================================================================ +void str::remove (unsigned int idx, unsigned int dellen) { + str s1 = substr (0, idx); + str s2 = substr (idx + dellen, len()); + + clear(); + + append (s1); + append (s2); +} + +// ============================================================================ +void str::trim (int dellen) { + if (!dellen) + return; + + str s; + // If dellen is positive, trim from the end, + // if negative, trim from beginning. + if (dellen > 0) + s = substr (0, len()-dellen); + else + s = substr (-dellen, len()); + + clear(); + append (s); +} + +// ============================================================================ +void str::replace (const char* o, const char* n, unsigned int a) { + unsigned int idx; + + while ((idx = first (o, a)) != len()) { + str s1 = substr (0, idx); + str s2 = substr (idx + strlen (o), len()); + + clear(); + + append (s1); + append (n); + append (s2); + } +} + +void str::insert (char* c, unsigned int pos) { + str s1 = substr (0, pos); + str s2 = substr (pos, len()); + + clear(); + append (s1); + append (c); + append (s2); +} + +void str::reverse () { + char* tmp = new char[alloclen]; + strcpy (tmp, text); + + clear(); + curs = 0; + resize (alloclen); + for (int i = alloclen-1; i >= 0; i--) + append (tmp[i]); +} + +void str::repeat (unsigned int n) { + char* tmp = new char[alloclen]; + strcpy (tmp, text); + + for (; n > 0; n--) + append (tmp); +} + +// ============================================================================ +bool str::isnumber () { + return contentcheck (SCCF_NUMBER); +} + +// ============================================================================ +bool str::isword () { + return contentcheck (SCCF_WORD); +} + +bool str::contentcheck (int flags) { + ITERATE_STRING (u) { + if (flags & SCCF_WORD) { + // lowercase letters + if (text[u] >= 'a' || text[u] <= 'z') + continue; + + // uppercase letters + if (text[u] >= 'A' || text[u] <= 'Z') + continue; + } + + if (flags & SCCF_NUMBER) { + if (text[u] < '0' || text[u] > '9') + return false; + } + + return false; + } + return true; +} + +// ============================================================================ +int str::compare (const char* c) { + return strcmp (text, c); +} + +int str::compare (str c) { + return compare (c.chars()); +} + +// ============================================================================ +// OPERATORS +str str::operator + (str& c) { + append (c); + return *this; +} + +str& str::operator += (char c) { + append (c); + return *this; +} + +str& str::operator += (const char* c) { + append (c); + return *this; +} + +str& str::operator += (const str c) { + append (c); + return *this; +} + +char str::operator [] (unsigned int pos) { + return text[pos]; +} + +str::operator char* () const { + return text; +} + +str::operator int () const {return atoi(text);} +str::operator unsigned int () const {return atoi(text);} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/str.h Fri Jul 13 17:20:51 2012 +0300 @@ -0,0 +1,146 @@ +/* + * 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 __STR_H__ +#define __STR_H__ + +char* vdynformat (const char* c, va_list v, unsigned int size); + +#define SCCF_NUMBER 1<<0 +#define SCCF_WORD 1<<1 + +// Dynamic string object, allocates memory when needed and +// features a good bunch of manipulation methods +class str { +private: + // The actual message + char* text; + + // Where will append() place new characters? + unsigned int curs; + + // Allocated length + unsigned int alloclen; + + // Resize the text buffer to len characters + void resize (unsigned int len); +public: + // ====================================================================== + // CONSTRUCTORS + str (); + str (const char* c); + str (char c); + + // ====================================================================== + // METHODS + + // Empty the string + void clear (); + + // Length of the string + unsigned int len (); + + // The char* form of the string + char* chars (); + + // Dumps the character table of the string + void dump (); + + // Appends text to the string + void append (char c); + void append (const char* c); + void append (str c); + + // Appends formatted text to the string. + void appendformat (const char* c, ...); + void appendformat (str c, ...); + + // Returns the first occurrence of c in the string, optionally starting + // from a certain position rather than the start. + unsigned int first (const char* c, unsigned int a = 0); + + // Returns the last occurrence of c in the string, optionally starting + // from a certain position rather than the end. + unsigned int last (const char* c, int a = -1); + + // Returns a substring of the string, from a to b. + str substr (unsigned int a, unsigned int b); + + // Replace a substring with another substring. + void replace (const char* o, const char* n, unsigned int a = 0); + + // Removes a given index from the string, optionally more characters than just 1. + void remove (unsigned int idx, unsigned int dellen=1); + + void trim (int dellen); + + // Inserts a substring into a certain position. + void insert (char* c, unsigned int pos); + + // Reverses the string. + void reverse (); + + // Repeats the string a given amount of times. + void repeat (unsigned int n); + + // Is the string a number? + bool isnumber (); + + // Is the string a word, i.e consists only of alphabetic letters? + bool isword (); + + bool contentcheck (int flags); + + int compare (const char* c); + int compare (str c); + + // ====================================================================== + // OPERATORS + str operator + (str& c); + str& operator += (char c); + str& operator += (const char* c); + str& operator += (const str c); + char operator [] (unsigned int pos); + operator char* () const; + operator int () const; + operator unsigned int () const; +}; + +#endif // __STR_H__ \ No newline at end of file