Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3213 was 3213, checked in by patrick, 19 years ago

orxonox/trunk: enhanced the CommandNode - cleaning up garbage doing the wash etc

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