Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4355 was 4326, checked in by patrick, 20 years ago

orxonox/trunk: working on an alternativ flight mode and a new event handlig system at the same time. crazy…

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