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