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