vegastrike  0.5.1.r1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ship_commands.cpp
Go to the documentation of this file.
1 #include "cmd/unit_generic.h"
2 #include "config_xml.h"
3 #include "xml_support.h"
4 #include "vs_globals.h"
5 #include "universe_util.h"
6 #include "gldrv/winsys.h"
7 
8 static inline float fmin( float a, float b )
9 {
10  return (a < b) ? a : b;
11 }
12 static inline float fmax( float a, float b )
13 {
14  return (a > b) ? a : b;
15 }
16 
18 {
19  Functor< ShipCommands > *csetkps;
27  Functor< ShipCommands > *cpymenu;
28  bool broll;
29  bool bleft;
30  bool bright;
31  bool bup;
32  bool bdown;
33 public:
34  virtual ~ShipCommands()
35  {
36  CommandInterpretor->remCommand( cpymenu );
37  CommandInterpretor->remCommand( csetkps );
38  }
40  {
41  //create some functors, register them with the command interp {{{
42  cpymenu = new Functor< ShipCommands > ( this, &ShipCommands::pymenu );
43  CommandInterpretor->addCommand( cpymenu, "pymenu" );
44  csetkps = new Functor< ShipCommands > ( this, &ShipCommands::setkps );
45  CommandInterpretor->addCommand( csetkps, "setspeed" );
46  //}}}
47  //set some local bools false {{{
48  broll = false;
49  bleft = false;
50  bright = false;
51  bup = false;
52  bdown = false;
53  //}}}
54  //a test menu {{{
55  {
56  static char const python_test[20] = "python test";
57  static char const test_string[40] = "This is a test of the menusystem";
58  menu *m = new menu( python_test, test_string, "\r\n" );
60  {
61  mItem *mi = new mItem;
62  mi->autoreprint = true;
63  mi->Name.append( "1" ); //argument to access menu
64  mi->display.append( "Python One Line input" ); //menu's display name
65  mi->func2call.append( "python" );
66  mi->inputbit = true; //set single-line input mode
67  mi->selectstring.append( "Type a single line of Python" ); //call function "Display" with this string
69  }
70  {
71  mItem *mi = new mItem;
72  mi->autoreprint = true; //auto-re-print the
73  //menu after this menuitem is finished
74  mi->Name.append( "2" ); //argument to access menu
75  mi->display.append( "(Python Multi-Line input)" ); //menu's display name
76  mi->func2call.append( "python" ); //call this function when this menuitem is called and input is all recieved, user input is appened with a space, along with the action string if there is one. (string generated: "func2call action userinput")
77  mi->inputbit2 = true; //set single-line input mode
78  mi->selectstring.append( "Type multiple lines of python input. Use <ENTER> on a line ALONE to finish" ); //Call function "Display" with this string
80  }
81  }
82  }
83  void pymenu();
84  void left( bool *isKeyDown );
85  void right( bool *isKeyDown );
86  void up( bool *isKeyDown );
87  void down( bool *isKeyDown );
88  void roll( bool *isKeyDown );
89  void setkps( const char *in );
90 };
91 //these _would_ work if the physics routines polled the ship_commands object
92 //for these bools..
94 {
95  std::string response( CommandInterpretor->setMenu( "python test" ) );
96  CommandInterpretor->conoutf( response );
97 }
98 void ShipCommands::left( bool *isKeyDown )
99 {
100  bleft = isKeyDown;
101 }
102 void ShipCommands::right( bool *isKeyDown )
103 {
104  bright = isKeyDown;
105 }
106 void ShipCommands::up( bool *isKeyDown )
107 {
108  bup = isKeyDown;
109 }
110 void ShipCommands::down( bool *isKeyDown )
111 {
112  bdown = isKeyDown;
113 }
114 void ShipCommands::roll( bool *isKeyDown )
115 {
116  broll = isKeyDown;
117 }
118 
119 static ShipCommands *ship_commands = NULL;
120 
121 void ShipCommands::setkps( const char *in )
122 {
123  if (in == NULL) throw "What speed?";
124  float kps = XMLSupport::parse_float( std::string( in ) );
125  Unit *player = UniverseUtil::getPlayer();
126  if (player) {
127  static float game_speed = XMLSupport::parse_float( vs_config->getVariable( "physics", "game_speed", "1" ) );
128  static bool display_in_meters = XMLSupport::parse_bool( vs_config->getVariable( "physics", "display_in_meters", "true" ) );
129  static bool lie = XMLSupport::parse_bool( vs_config->getVariable( "physics", "game_speed_lying", "true" ) );
130  if (lie)
131  kps *= game_speed;
132 
133  else
134  kps /= display_in_meters ? 1.0f : 3.6f;
135  player->GetComputerData().set_speed = fmin( player->GetComputerData().max_speed(), kps );
136  }
137 }
138 
140 {
141  if (ship_commands) delete ship_commands;
142  ship_commands = new ShipCommands;
143 }
144 
146 {
147  if (ship_commands) delete ship_commands;
148  ship_commands = NULL;
149 }
150