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