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