Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/heightMap/src/command_node.cc @ 4639

Last change on this file since 4639 was 4122, checked in by bensch, 20 years ago

orxonox/branches/heightMap: merged the Trunk back into branches/heightMap:
merged with Command
svn merge -r 3918:HEAD trunk branches/heightMap
conflicts resolved in favor of the Trunk

File size: 7.4 KB
RevLine 
[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
[3591]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COMMAND_NODE
[2066]17
18#include "command_node.h"
19#include "keynames.h"
20#include "ini_parser.h"
[2100]21#include "world_entity.h"
[2636]22#include "game_loader.h"
[3216]23#include "world.h"
[3608]24#include "list.h"
25#include "orxonox.h"
[2066]26
27#include <stdio.h>
[2166]28#include <string.h>
[2105]29#include <stdlib.h>
[2066]30
31using namespace std;
32
[2141]33/**
[2636]34   \brief constructs a CommandNode to handle remote input
35   \param ID: unique denumerator to identify the node in the network
[2141]36*/
[2066]37CommandNode::CommandNode (int ID)
38{
[3214]39  this->bound = new tList<WorldEntity>();
[3213]40  this->aliases = NULL;
41  this->netID = ID;
42  this->bLocalInput = false;
43  this->bEnabled = true;
[3216]44  this->world = NULL;
[2066]45}
46
[2141]47/**
[2636]48   \brief constructs a CommandNode to handle local input
49   \param filename: The path and name of the file to load the key bindings from
[2141]50*/
[2066]51CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
52{
[3213]53  this->aliases = NULL;
54  this->bLocalInput = true;
55  this->netID = 0;
[3214]56  this->bound = new tList<WorldEntity>();
[3213]57  this->bEnabled = true;
[3216]58  this->world = NULL;
[4122]59  this->loadBindings(filename);
[2066]60}
61
[2141]62/**
[2636]63   \brief removes the CommandNode from memory
[2141]64*/
[2066]65CommandNode::~CommandNode ()
66{
[2636]67  if( aliases != NULL) free (aliases);
[3213]68  if( bound != NULL) delete bound; /* \todo should this delete bound? dangerous FIX */
[2066]69}
70
[2816]71
[3194]72/**
73  \brief this resets the command node
74
75   deleting all data contained in the command node to fill it up again
76
77  \todo coppling to different game-entities
78  \todo reset/destroy has to be redesigned
79*/
80
[2816]81void CommandNode::reset()
82{
[3194]83  this->bound->destroy();
[3213]84  //this->bound = NULL; /* \todo this produces a NULLpointer error.. FIX */
85  this->bEnabled = false;
[3216]86  this->world = NULL;
[2816]87}
88
[3213]89void CommandNode::enable(bool bEnabled)
90{
91  this->bEnabled = bEnabled;
92}
93
[3216]94
[2141]95/**
[3216]96  \brief adds Node to a GameWorld
97
98   this is usefull, if you want to catch events in a world class. usualy
99   this is done automaticaly via GameLoader. Reset it via
100   CommandNode::reset()
101
102*/
103void CommandNode::addToWorld(World* world)
104{
105  this->world = world;
106}
107
108
109/**
[2636]110   \brief loads new key bindings from a file
111   \param filename: The path and name of the file to load the bindings from
[2141]112*/
[4122]113void CommandNode::loadBindings (char* filename = DEFAULT_KEYBIND_FILE)
[2066]114{
[2636]115  FILE* stream;
116 
[3591]117  PRINTF(4)("Loading key bindings from %s\n", filename);
[2636]118 
119  // remove old bindings if present
120  if( aliases != NULL)
121    {
122      free (aliases);
123      aliases = NULL;
124    }
125 
126  // create parser
127  IniParser parser (filename);
[4122]128  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
[2636]129    {
[4122]130      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", filename);
[2636]131      return;
132    }
133  // allocate empty lookup table
134  aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
135 
136  char namebuf[256];
137  char valuebuf[256];
138  memset (namebuf, 0, 256);
139  memset (valuebuf, 0, 256);
140  int* index;
141 
[3236]142  while( parser.nextVar (namebuf, valuebuf) != -1)
[2636]143    {
[4122]144      index = nameToIndex (valuebuf);
[2636]145      switch( index[0])
[2066]146        {
[2636]147        case 0:
[4122]148          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
149          strcpy (aliases->keys[index[1]], namebuf);
[2636]150          break;
151        case 1:
[4122]152          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
153          strcpy (aliases->buttons[index[1]], namebuf);
[2636]154          break;
155        default:
156          break;
[2066]157        }
[2636]158      memset (namebuf, 0, 256);
159      memset (valuebuf, 0, 256);
160    }
[4122]161
162
163  // PARSE MISC SECTION
164  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
165    {
166      PRINTF(1)("Could not find key bindings in %s\n", filename);
167      return;
168    }
169
170  while( parser.nextVar (namebuf, valuebuf) != -1)
171    {
172      index = nameToIndex (valuebuf);
173      switch( index[0])
174        {
175        case 0:
176          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
177          strcpy (aliases->keys[index[1]], namebuf);
178          break;
179        case 1:
180          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
181          strcpy (aliases->buttons[index[1]], namebuf);
182          break;
183        default:
184          break;
185        }
186      memset (namebuf, 0, 256);
187      memset (valuebuf, 0, 256);
188    }
189
[2066]190}
191
[2141]192/**
[2636]193   \brief binds a WorldEntity to the CommandNode
194   \param entity: Pointer to the entity to bind
[2141]195*/
[2066]196void CommandNode::bind (WorldEntity* entity)
197{
[3225]198  bound->add (entity);
[2066]199}
200
[2141]201/**
[2636]202   \brief removes an entity from the list of the CommandNode
203   \param entity: Pointer to the entity to relese
[2141]204*/
[2066]205void CommandNode::unbind (WorldEntity* entity)
206{
[3225]207  bound->remove (entity);
[2066]208}
209
[3225]210int* CommandNode::nameToIndex (char* name)
[2066]211{
[2636]212  coord[0] = -1;
213  coord[1] = -1;
214  int c;
[3225]215  if( (c = keynameToSDLK (name)) != -1)
[2636]216    {
217      coord[1] = c;
218      coord[0] = 0;
219    }
[3225]220  if( (c = buttonnameToSDLB (name)) != -1)
[2636]221    {
222      coord[1] = c;
223      coord[0] = 1;
224    }
225  return coord;
[2066]226}
227
[2141]228/**
[2636]229   \brief tells the CommandNode to run through all pending events and relay them accordingly
[2141]230*/
[2066]231void CommandNode::process ()
232{
[3213]233  if( this->bEnabled) 
234    {
[3225]235      if( bLocalInput) processLocal ();
236      else processNetwork ();
[3213]237    }
[2066]238}
239
[3236]240void CommandNode::processLocal ()
[2066]241{
[2636]242  SDL_Event event;
243  Command cmd;
244  while( SDL_PollEvent (&event))
245    {
[3591]246      PRINTF(3)("CommandNode::processLocal() =========================got Event\n");
[2636]247      memset (cmd.cmd, 0, CMD_LENGHT); 
248      switch( event.type)
[2066]249        {
[2636]250        case SDL_KEYDOWN:
251          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
252          cmd.bUp = false;
[3225]253          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]254          break;
255        case SDL_KEYUP:
256          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
257          cmd.bUp = true;
[3225]258          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]259          break;
260        case SDL_MOUSEMOTION:
261          strcpy( cmd.cmd, "cursor");
262          cmd.x = event.motion.x;
263          cmd.y = event.motion.y;
264          cmd.xrel = event.motion.xrel;
265          cmd.yrel = event.motion.yrel;
266          break;
267        case SDL_MOUSEBUTTONUP:
268          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
269          cmd.bUp = true;
[3225]270          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]271          break;
272        case SDL_MOUSEBUTTONDOWN:
273          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
274          cmd.bUp = false;
[3225]275          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]276          break;
277        case SDL_JOYAXISMOTION:
278        case SDL_JOYBALLMOTION:
279        case SDL_JOYHATMOTION:
280        case SDL_JOYBUTTONDOWN:
281        case SDL_JOYBUTTONUP:
282          break;
283        default:
284          Orxonox *orx = Orxonox::getInstance();
[3225]285          orx->eventHandler(&event);
[2636]286          break;
[2066]287        }
[2636]288    }
[2066]289}
290
[2816]291
[3225]292void CommandNode::processNetwork ()
[2066]293{
294
295}
296
[2816]297
[2066]298void CommandNode::relay (Command* cmd)
299{
[2636]300  Orxonox *orx = Orxonox::getInstance();
[3225]301  if( orx->systemCommand (cmd)) return;
[3216]302
[2636]303  GameLoader* gl = GameLoader::getInstance();
[3216]304  if( gl->worldCommand(cmd)) return;
305
[3225]306  if( bLocalInput) sendOverNetwork (cmd);
[2636]307 
[3216]308  if( this->world->command(cmd)) return;
309
[3654]310  tIterator<WorldEntity>* iterator = bound->getIterator();
311  WorldEntity* entity = iterator->nextElement();
[3216]312  while( entity != NULL)
[2636]313    {
[3586]314      entity->command (cmd); /*no absorbtion of command! strange*/
[3654]315      entity = iterator->nextElement();
[2636]316    }
[3654]317  delete iterator;
[2066]318}
[2096]319
[2816]320
[2141]321/**
[2636]322   \brief sets the network identifier of the CommandNode
323   \param ID: the new ID to use
[2141]324*/
[3225]325void CommandNode::setNetID (int ID)
[2096]326{
[2636]327  netID = ID;
[2096]328}
329
[3225]330void CommandNode::sendOverNetwork (Command* cmd)
[2096]331{
332}
Note: See TracBrowser for help on using the repository browser.