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