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