Mon, 30 Jul 2012 03:38:02 +0300
Added if() support
2 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
2 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
38 | * POSSIBILITY OF SUCH DAMAGE. | |
39 | */ | |
40 | ||
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
41 | #define __EVENTS_CXX__ |
2 | 42 | #include <stdlib.h> |
43 | #include <stdio.h> | |
44 | #include "common.h" | |
45 | #include "scriptreader.h" | |
46 | #include "str.h" | |
47 | #include "events.h" | |
48 | ||
49 | EventDef* g_EventDef; | |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
50 | |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
51 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
52 | // Read event definitions from file |
2 | 53 | void ReadEvents () { |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
54 | ScriptReader* r = new ScriptReader ("events.def"); |
2 | 55 | g_EventDef = NULL; |
56 | EventDef* curdef = g_EventDef; | |
57 | unsigned int numEventDefs = 0; | |
58 | while (r->Next()) { | |
59 | EventDef* e = new EventDef; | |
60 | e->name = r->token; | |
61 | e->number = numEventDefs; | |
62 | e->next = NULL; | |
63 | ||
64 | // g_EventDef becomes the first eventdef | |
65 | if (!g_EventDef) | |
66 | g_EventDef = e; | |
67 | ||
68 | if (!curdef) { | |
69 | curdef = e; | |
70 | } else { | |
71 | curdef->next = e; | |
72 | curdef = e; | |
73 | } | |
74 | numEventDefs++; | |
75 | } | |
76 | ||
77 | delete r; | |
78 | printf ("%d event definitions read.\n", numEventDefs); | |
79 | } | |
80 | ||
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
81 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
82 | // Delete event definitions recursively |
2 | 83 | void UnlinkEvents (EventDef* e) { |
84 | if (e->next) | |
85 | UnlinkEvents (e->next); | |
86 | delete e; | |
87 | } | |
88 | ||
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
89 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
90 | // Finds an event definition by index |
2 | 91 | EventDef* FindEventByIdx (unsigned int idx) { |
92 | EventDef* e = g_EventDef; | |
93 | while (idx > 0) { | |
94 | if (!e->next) | |
95 | return NULL; | |
96 | e = e->next; | |
97 | idx--; | |
98 | } | |
99 | return e; | |
100 | } | |
101 | ||
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
102 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
103 | // Finds an event definition by name |
2 | 104 | EventDef* FindEventByName (str a) { |
105 | EventDef* e; | |
106 | for (e = g_EventDef; e->next != NULL; e = e->next) { | |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
107 | if (!a.icompare (e->name)) |
2 | 108 | return e; |
109 | } | |
110 | ||
111 | return NULL; | |
112 | } |