Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/command_node.cc @ 3816

Last change on this file since 3816 was 3654, checked in by patrick, 20 years ago

orxonox/trunk: altered the list function to make it more performant the world now uses iterators everywhere.

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