1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christian Meyer |
---|
13 | co-programmer: Patrick Boenzli |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #include "command_node.h" |
---|
18 | #include "keynames.h" |
---|
19 | #include "ini_parser.h" |
---|
20 | #include "world_entity.h" |
---|
21 | #include "game_loader.h" |
---|
22 | #include "world.h" |
---|
23 | |
---|
24 | #include <stdio.h> |
---|
25 | #include <string.h> |
---|
26 | #include <stdlib.h> |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | /** |
---|
31 | \brief constructs a CommandNode to handle remote input |
---|
32 | \param ID: unique denumerator to identify the node in the network |
---|
33 | */ |
---|
34 | CommandNode::CommandNode (int ID) |
---|
35 | { |
---|
36 | this->bound = new tList<WorldEntity>(); |
---|
37 | this->aliases = NULL; |
---|
38 | this->netID = ID; |
---|
39 | this->bLocalInput = false; |
---|
40 | this->bEnabled = true; |
---|
41 | this->world = NULL; |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | \brief constructs a CommandNode to handle local input |
---|
46 | \param filename: The path and name of the file to load the key bindings from |
---|
47 | */ |
---|
48 | CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE) |
---|
49 | { |
---|
50 | this->aliases = NULL; |
---|
51 | this->bLocalInput = true; |
---|
52 | this->netID = 0; |
---|
53 | this->bound = new tList<WorldEntity>(); |
---|
54 | this->bEnabled = true; |
---|
55 | this->world = NULL; |
---|
56 | this->loadBindings (filename); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | \brief removes the CommandNode from memory |
---|
61 | */ |
---|
62 | CommandNode::~CommandNode () |
---|
63 | { |
---|
64 | if( aliases != NULL) free (aliases); |
---|
65 | if( bound != NULL) delete bound; /* \todo should this delete bound? dangerous FIX */ |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | \brief this resets the command node |
---|
71 | |
---|
72 | deleting all data contained in the command node to fill it up again |
---|
73 | |
---|
74 | \todo coppling to different game-entities |
---|
75 | \todo reset/destroy has to be redesigned |
---|
76 | */ |
---|
77 | |
---|
78 | void CommandNode::reset() |
---|
79 | { |
---|
80 | this->bound->destroy(); |
---|
81 | //this->bound = NULL; /* \todo this produces a NULLpointer error.. FIX */ |
---|
82 | this->bEnabled = false; |
---|
83 | this->world = NULL; |
---|
84 | } |
---|
85 | |
---|
86 | void CommandNode::enable(bool bEnabled) |
---|
87 | { |
---|
88 | this->bEnabled = bEnabled; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | \brief adds Node to a GameWorld |
---|
94 | |
---|
95 | this is usefull, if you want to catch events in a world class. usualy |
---|
96 | this is done automaticaly via GameLoader. Reset it via |
---|
97 | CommandNode::reset() |
---|
98 | |
---|
99 | */ |
---|
100 | void CommandNode::addToWorld(World* world) |
---|
101 | { |
---|
102 | this->world = world; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /** |
---|
107 | \brief loads new key bindings from a file |
---|
108 | \param filename: The path and name of the file to load the bindings from |
---|
109 | */ |
---|
110 | void CommandNode::loadBindings (char* filename) |
---|
111 | { |
---|
112 | FILE* stream; |
---|
113 | |
---|
114 | printf("Loading key bindings from %s\n", filename); |
---|
115 | |
---|
116 | if( filename == NULL) filename = DEFAULT_KEYBIND_FILE; |
---|
117 | |
---|
118 | // remove old bindings if present |
---|
119 | if( aliases != NULL) |
---|
120 | { |
---|
121 | free (aliases); |
---|
122 | aliases = NULL; |
---|
123 | } |
---|
124 | |
---|
125 | // create parser |
---|
126 | IniParser parser (filename); |
---|
127 | if( parser.getSection ("Bindings") == -1) |
---|
128 | { |
---|
129 | printf("Could not find key bindings in %s\n", filename); |
---|
130 | return; |
---|
131 | } |
---|
132 | // allocate empty lookup table |
---|
133 | aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
134 | |
---|
135 | char namebuf[256]; |
---|
136 | char valuebuf[256]; |
---|
137 | memset (namebuf, 0, 256); |
---|
138 | memset (valuebuf, 0, 256); |
---|
139 | int* index; |
---|
140 | |
---|
141 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
142 | { |
---|
143 | index = nameToIndex (namebuf); |
---|
144 | switch( index[0]) |
---|
145 | { |
---|
146 | case 0: |
---|
147 | printf("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), valuebuf); |
---|
148 | strcpy (aliases->keys[index[1]], valuebuf); |
---|
149 | break; |
---|
150 | case 1: |
---|
151 | printf("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), valuebuf); |
---|
152 | strcpy (aliases->buttons[index[1]], valuebuf); |
---|
153 | break; |
---|
154 | default: |
---|
155 | break; |
---|
156 | } |
---|
157 | memset (namebuf, 0, 256); |
---|
158 | memset (valuebuf, 0, 256); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | \brief binds a WorldEntity to the CommandNode |
---|
164 | \param entity: Pointer to the entity to bind |
---|
165 | */ |
---|
166 | void CommandNode::bind (WorldEntity* entity) |
---|
167 | { |
---|
168 | bound->add (entity); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | \brief removes an entity from the list of the CommandNode |
---|
173 | \param entity: Pointer to the entity to relese |
---|
174 | */ |
---|
175 | void CommandNode::unbind (WorldEntity* entity) |
---|
176 | { |
---|
177 | bound->remove (entity); |
---|
178 | } |
---|
179 | |
---|
180 | int* CommandNode::nameToIndex (char* name) |
---|
181 | { |
---|
182 | coord[0] = -1; |
---|
183 | coord[1] = -1; |
---|
184 | int c; |
---|
185 | if( (c = keynameToSDLK (name)) != -1) |
---|
186 | { |
---|
187 | coord[1] = c; |
---|
188 | coord[0] = 0; |
---|
189 | } |
---|
190 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
191 | { |
---|
192 | coord[1] = c; |
---|
193 | coord[0] = 1; |
---|
194 | } |
---|
195 | return coord; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | \brief tells the CommandNode to run through all pending events and relay them accordingly |
---|
200 | */ |
---|
201 | void CommandNode::process () |
---|
202 | { |
---|
203 | if( this->bEnabled) |
---|
204 | { |
---|
205 | if( bLocalInput) processLocal (); |
---|
206 | else processNetwork (); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | void CommandNode::processLocal () |
---|
211 | { |
---|
212 | SDL_Event event; |
---|
213 | Command cmd; |
---|
214 | while( SDL_PollEvent (&event)) |
---|
215 | { |
---|
216 | printf("CommandNode::processLocal() =========================got Event\n"); |
---|
217 | memset (cmd.cmd, 0, CMD_LENGHT); |
---|
218 | switch( event.type) |
---|
219 | { |
---|
220 | case SDL_KEYDOWN: |
---|
221 | strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
222 | cmd.bUp = false; |
---|
223 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
224 | break; |
---|
225 | case SDL_KEYUP: |
---|
226 | strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
227 | cmd.bUp = true; |
---|
228 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
229 | break; |
---|
230 | case SDL_MOUSEMOTION: |
---|
231 | strcpy( cmd.cmd, "cursor"); |
---|
232 | cmd.x = event.motion.x; |
---|
233 | cmd.y = event.motion.y; |
---|
234 | cmd.xrel = event.motion.xrel; |
---|
235 | cmd.yrel = event.motion.yrel; |
---|
236 | break; |
---|
237 | case SDL_MOUSEBUTTONUP: |
---|
238 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
239 | cmd.bUp = true; |
---|
240 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
241 | break; |
---|
242 | case SDL_MOUSEBUTTONDOWN: |
---|
243 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
244 | cmd.bUp = false; |
---|
245 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
246 | break; |
---|
247 | case SDL_JOYAXISMOTION: |
---|
248 | case SDL_JOYBALLMOTION: |
---|
249 | case SDL_JOYHATMOTION: |
---|
250 | case SDL_JOYBUTTONDOWN: |
---|
251 | case SDL_JOYBUTTONUP: |
---|
252 | break; |
---|
253 | default: |
---|
254 | Orxonox *orx = Orxonox::getInstance(); |
---|
255 | orx->eventHandler(&event); |
---|
256 | break; |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | void CommandNode::processNetwork () |
---|
263 | { |
---|
264 | |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | void CommandNode::relay (Command* cmd) |
---|
269 | { |
---|
270 | Orxonox *orx = Orxonox::getInstance(); |
---|
271 | if( orx->systemCommand (cmd)) return; |
---|
272 | |
---|
273 | GameLoader* gl = GameLoader::getInstance(); |
---|
274 | if( gl->worldCommand(cmd)) return; |
---|
275 | |
---|
276 | if( bLocalInput) sendOverNetwork (cmd); |
---|
277 | |
---|
278 | if( this->world->command(cmd)) return; |
---|
279 | |
---|
280 | WorldEntity* entity = bound->enumerate(); |
---|
281 | while( entity != NULL) |
---|
282 | { |
---|
283 | entity->command (cmd); /*no absorbtion of command! strange*/ |
---|
284 | entity = bound->nextElement(); |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | /** |
---|
290 | \brief sets the network identifier of the CommandNode |
---|
291 | \param ID: the new ID to use |
---|
292 | */ |
---|
293 | void CommandNode::setNetID (int ID) |
---|
294 | { |
---|
295 | netID = ID; |
---|
296 | } |
---|
297 | |
---|
298 | void CommandNode::sendOverNetwork (Command* cmd) |
---|
299 | { |
---|
300 | } |
---|