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