[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; |
---|
[3279] | 185 | cmd.NumPressed+=1; |
---|
[3172] | 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; |
---|
[3279] | 192 | cmd.NumPressed-=1; |
---|
[3155] | 193 | |
---|
[3172] | 194 | |
---|
[2636] | 195 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
| 196 | break; |
---|
| 197 | case SDL_MOUSEMOTION: |
---|
| 198 | strcpy( cmd.cmd, "cursor"); |
---|
| 199 | cmd.x = event.motion.x; |
---|
| 200 | cmd.y = event.motion.y; |
---|
| 201 | cmd.xrel = event.motion.xrel; |
---|
| 202 | cmd.yrel = event.motion.yrel; |
---|
| 203 | break; |
---|
| 204 | case SDL_MOUSEBUTTONUP: |
---|
| 205 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
| 206 | cmd.bUp = true; |
---|
| 207 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
| 208 | break; |
---|
| 209 | case SDL_MOUSEBUTTONDOWN: |
---|
| 210 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
| 211 | cmd.bUp = false; |
---|
| 212 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
| 213 | break; |
---|
| 214 | case SDL_JOYAXISMOTION: |
---|
| 215 | case SDL_JOYBALLMOTION: |
---|
| 216 | case SDL_JOYHATMOTION: |
---|
| 217 | case SDL_JOYBUTTONDOWN: |
---|
| 218 | case SDL_JOYBUTTONUP: |
---|
| 219 | break; |
---|
| 220 | default: |
---|
| 221 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 222 | orx->event_handler (&event); |
---|
| 223 | |
---|
| 224 | break; |
---|
[2066] | 225 | } |
---|
[2636] | 226 | } |
---|
[2066] | 227 | } |
---|
| 228 | |
---|
[2860] | 229 | |
---|
[2066] | 230 | void CommandNode::process_network () |
---|
| 231 | { |
---|
| 232 | |
---|
| 233 | } |
---|
| 234 | |
---|
[2860] | 235 | |
---|
[2066] | 236 | void CommandNode::relay (Command* cmd) |
---|
| 237 | { |
---|
[2636] | 238 | |
---|
| 239 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 240 | if( orx->system_command (cmd)) return; |
---|
| 241 | GameLoader* gl = GameLoader::getInstance(); |
---|
| 242 | if(gl->worldCommand(cmd)) return; |
---|
| 243 | |
---|
| 244 | if( bLocalInput) send_over_network (cmd); |
---|
| 245 | |
---|
[2860] | 246 | WorldEntity* entity = bound->enumerate(); |
---|
| 247 | while( entity != NULL) |
---|
[2636] | 248 | { |
---|
[2860] | 249 | entity->command (cmd); |
---|
| 250 | entity = bound->nextElement(); |
---|
[2636] | 251 | } |
---|
[2066] | 252 | } |
---|
[2096] | 253 | |
---|
[2860] | 254 | |
---|
[2141] | 255 | /** |
---|
[2636] | 256 | \brief sets the network identifier of the CommandNode |
---|
| 257 | \param ID: the new ID to use |
---|
[2141] | 258 | */ |
---|
[2096] | 259 | void CommandNode::set_netID (int ID) |
---|
| 260 | { |
---|
[2636] | 261 | netID = ID; |
---|
[2096] | 262 | } |
---|
| 263 | |
---|
| 264 | void CommandNode::send_over_network (Command* cmd) |
---|
| 265 | { |
---|
| 266 | } |
---|