Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unit_wrapper.cpp
Go to the documentation of this file.
1 #include "python_class.h"
2 
3 #include <boost/version.hpp>
4 #if BOOST_VERSION != 102800
5 #include <boost/python.hpp>
6 typedef boost::python::dict BoostPythonDictionary;
7 #else
8 #include <boost/python/objects.hpp>
9 typedef boost::python::dictionary BoostPythonDictionary;
10 #endif
11 #include "cmd/container.h"
12 #include <string>
13 #include "init.h"
14 #include "gfx/vec.h"
15 #include "cmd/unit_generic.h"
16 #include "universe_util.h"
17 #include "cmd/unit_util.h"
18 #include "faction_generic.h"
19 #include "cmd/ai/fire.h"
20 extern void StarSystemExports();
21 
22 //makes to_python for both vector and qvector turn them into tuples :-)
23 using namespace UnitUtil;
24 using namespace UniverseUtil;
25 using Orders::FireAt;
26 
27 #include "unit_wrapper_class.h"
28 extern void DefineOddUnitFunctions( boost::python::class_builder< UnitWrapper > &Class );
29 extern void ExportUnit( boost::python::class_builder< UnitWrapper > &Class );
30 #include "unit_from_to_python.h"
31 #if _MSC_VER <= 1200
32 #else
34 #endif
35 
36 namespace UniverseUtil
37 {
39 {
40  return UniverseUtil::getPlayer();
41 }
42 void setOwner( int obj, UnitWrapper un )
43 {
44  setOwnerII( obj, un.GetUnit() );
45 }
46 }
49 
50 //#undef EXPORT_UTIL
51 //#undef voidEXPORT_UTIL
52 #define EXPORT_UTIL( name, aff ) PYTHON_DEFINE_GLOBAL( VS, &UniverseUtil::name, #name );
53 #define voidEXPORT_UTIL( name ) EXPORT_UTIL( name, 0 )
54 //#undef EXPORT_FACTION
55 //#undef voidEXPORT_FACTION
56 #define EXPORT_FACTION( name, aff ) PYTHON_DEFINE_GLOBAL( VS, &FactionUtil::name, #name );
57 #define voidEXPORT_FACTION( name ) EXPORT_FACTION( name, 0 )
75 #if BOOST_VERSION != 102800
77 #else
78 #include "star_system_exports.h"
79 #endif
80 #undef EXPORT_UTIL
81 #undef voidEXPORT_UTIL
82 #undef EXPORT_FACTION
83 #undef voidEXPORT_FACTION
84 PYTHON_BASE_BEGIN_CLASS( VS, Cargo, "Cargo" )
85 #if BOOST_VERSION != 102800
87 #else
88 Class.def( boost::python::constructor< std::string, std::string, float, int, float, float > () );
89 #endif
90 PYTHON_DEFINE_METHOD( Class, &Cargo::SetPrice, "SetPrice" );
91 PYTHON_DEFINE_METHOD( Class, &Cargo::GetPrice, "GetPrice" );
92 PYTHON_DEFINE_METHOD( Class, &Cargo::SetMass, "SetMass" );
93 PYTHON_DEFINE_METHOD( Class, &Cargo::GetMass, "GetMass" );
94 PYTHON_DEFINE_METHOD( Class, &Cargo::SetVolume, "SetVolume" );
95 PYTHON_DEFINE_METHOD( Class, &Cargo::GetVolume, "GetVolume" );
96 PYTHON_DEFINE_METHOD( Class, &Cargo::SetQuantity, "SetQuantity" );
97 PYTHON_DEFINE_METHOD( Class, &Cargo::GetQuantity, "GetQuantity" );
98 PYTHON_DEFINE_METHOD( Class, &Cargo::SetContent, "SetContent" );
99 PYTHON_DEFINE_METHOD( Class, &Cargo::GetContentPython, "GetContent" );
100 PYTHON_DEFINE_METHOD( Class, &Cargo::SetCategory, "SetCategory" );
101 PYTHON_DEFINE_METHOD( Class, &Cargo::GetCategoryPython, "GetCategory" );
102 PYTHON_DEFINE_METHOD( Class, &Cargo::SetMissionFlag, "SetMissionFlag" );
103 PYTHON_DEFINE_METHOD( Class, &Cargo::GetMissionFlag, "GetMissionFlag" );
104 PYTHON_DEFINE_METHOD( Class, &Cargo::GetDescriptionPython, "GetDescription" );
105 PYTHON_DEFINE_METHOD( Class, &Cargo::SetFunctionality, "SetFunctionality" );
106 PYTHON_DEFINE_METHOD( Class, &Cargo::GetFunctionality, "GetFunctionality" );
107 PYTHON_DEFINE_METHOD( Class, &Cargo::SetMaxFunctionality, "SetMaxFunctionality" );
108 PYTHON_DEFINE_METHOD( Class, &Cargo::GetMaxFunctionality, "GetMaxFunctionality" );
109 
110 PYTHON_END_CLASS( VS, Cargo )
112 PYTHON_DEFINE_METHOD( Class, &UnitWrapper::getSubUnits, "getSubUnits" );
113 ExportUnit( Class );
114 //WARNING: Macro City 2 ahead. Please skip this section, also if you don't like macros.
115 
116 //End of Macro City 2
117 //#if BOOST_VERSION != 102800
118 PYTHON_DEFINE_METHOD( Class, &UnitWrapper::equal, "__eq__" );
119 PYTHON_DEFINE_METHOD( Class, &UnitWrapper::notequal, "__ne__" );
120 //#else
121 //Class.def (boost::python::operators< (boost::python::op_eq | boost::python::op_ne) >(), boost::python::right_operand<UnitWrapper>());
122 //#endif
123 DefineOddUnitFunctions( Class );
124 PYTHON_END_CLASS( VS, UnitWrapper )
125 
126 PYTHON_BEGIN_CLASS( VS, UniverseUtil::PythonUnitIter, "un_iter" )
127 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::current, "current" );
128 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::isDone, "isDone" );
129 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::notDone, "notDone" );
130 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advance, "advance" );
131 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceN, "advanceN" );
132 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceSignificant, "advanceSignificant" );
133 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceNSignificant, "advanceNSignificant" );
134 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceInsignificant, "advanceInsignificant" );
135 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceNInsignificant, "advanceNInsignificant" );
136 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advancePlanet, "advancePlanet" );
137 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceNPlanet, "advanceNPlanet" );
138 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceJumppoint, "advanceJumppoint" );
139 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::advanceNJumppoint, "advanceNJumppoint" );
140 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::next, "next" );
141 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::remove, "remove" );
142 PYTHON_DEFINE_METHOD( Class, &UniverseUtil::PythonUnitIter::preinsert, "preinsert" );
143 PYTHON_END_CLASS( VS, UniverseUtil::PythonUnitIter )
145 PYTHON_BEGIN_INHERIT_CLASS( VS, PythonAIFireAt, FireAt, "PythonAI" )
146 PYTHON_DEFINE_METHOD_DEFAULT( Class, &FireAt::Execute, "Execute", PythonAI< FireAt >::default_Execute );
147 PYTHON_DEFINE_METHOD_DEFAULT( Class, &FireAt::ChooseTarget, "ChooseTarget", PythonAI< FireAt >::default_ChooseTarget );
148 PYTHON_DEFINE_METHOD_DEFAULT( Class, &FireAt::SetParent, "init", PythonAI< FireAt >::default_SetParent );
149 PYTHON_DEFINE_METHOD( Class, &FireAt::GetParent, "GetParent" );
150 PYTHON_DEFINE_METHOD( Class, &FireAt::AddReplaceLastOrder, "AddReplaceLastOrder" );
151 PYTHON_DEFINE_METHOD( Class, &FireAt::ExecuteLastScriptFor, "ExecuteLastScriptFor" );
152 PYTHON_DEFINE_METHOD( Class, &FireAt::FaceTarget, "FaceTarget" );
153 PYTHON_DEFINE_METHOD( Class, &FireAt::FaceTargetITTS, "FaceTargetITTS" );
154 PYTHON_DEFINE_METHOD( Class, &FireAt::MatchLinearVelocity, "MatchLinearVelocity" );
155 PYTHON_DEFINE_METHOD( Class, &FireAt::MatchAngularVelocity, "MatchAngularVelocity" );
156 PYTHON_DEFINE_METHOD( Class, &FireAt::ChangeHeading, "ChangeHeading" );
157 PYTHON_DEFINE_METHOD( Class, &FireAt::ChangeLocalDirection, "ChangeLocalDirection" );
158 PYTHON_DEFINE_METHOD( Class, &FireAt::MoveTo, "MoveTo" );
159 PYTHON_DEFINE_METHOD( Class, &FireAt::MatchVelocity, "MatchVelocity" );
160 PYTHON_DEFINE_METHOD( Class, &FireAt::Cloak, "Cloak" );
161 PYTHON_DEFINE_METHOD( Class, &FireAt::FormUp, "FormUp" );
162 PYTHON_DEFINE_METHOD( Class, &FireAt::FormUpToOwner, "FormUpToOwner" );
163 PYTHON_DEFINE_METHOD( Class, &FireAt::FaceDirection, "FaceDirection" );
164 PYTHON_DEFINE_METHOD( Class, &FireAt::XMLScript, "XMLScript" );
165 PYTHON_DEFINE_METHOD( Class, &FireAt::LastPythonScript, "LastPythonScript" );
166 PYTHON_END_CLASS( VS, FireAt )
167 
168 PYTHON_END_MODULE( VS )
169 
170 void InitVS()
171 {
172  Python::reseterrors();
173  PYTHON_INIT_MODULE( VS );
174  Python::reseterrors();
175 }
176 
177 static std::string ParseSizeFlags( int size )
178 {
179  static const std::pair< int, std::string >masks[] = {
180  std::pair< int, std::string > ( weapon_info::LIGHT, " LIGHT" ),
181  std::pair< int, std::string > ( weapon_info::MEDIUM, " MEDIUM" ),
182  std::pair< int, std::string > ( weapon_info::HEAVY, " HEAVY" ),
183  std::pair< int, std::string > ( weapon_info::CAPSHIPLIGHT, " CAPSHIPLIGHT" ),
184  std::pair< int, std::string > ( weapon_info::CAPSHIPHEAVY, " CAPSHIPHEAVY" ),
185  std::pair< int, std::string > ( weapon_info::SPECIAL, " SPECIAL" ),
186  std::pair< int, std::string > ( weapon_info::LIGHTMISSILE, " LIGHTMISSILE" ),
187  std::pair< int, std::string > ( weapon_info::MEDIUMMISSILE, " MEDIUMMISSILE" ),
188  std::pair< int, std::string > ( weapon_info::HEAVYMISSILE, " HEAVYMISSILE" ),
189  std::pair< int, std::string > ( weapon_info::CAPSHIPLIGHTMISSILE, " CAPSHIPLIGHTMISSILE" ),
190  std::pair< int, std::string > ( weapon_info::CAPSHIPHEAVYMISSILE, " CAPSHIPHEAVYMISSILE" ),
191  std::pair< int, std::string > ( weapon_info::SPECIALMISSILE, " SPECIALMISSILE" ),
192  std::pair< int, std::string > ( weapon_info::AUTOTRACKING, " AUTOTRACKING" )
193  };
194  std::string rv;
195  for (unsigned int i = 0; i < sizeof (masks)/sizeof (*masks); ++i)
196  if (size&masks[i].first)
197  rv += masks[i].second;
198  if ( !rv.empty() )
199  return rv.substr( 1 );
200 
201  else
202  return std::string( "NOWEAP" );
203 }
204 
205 #define PARSE_CASE( Class, Enum ) \
206 case Class::Enum: \
207  return std::string( #Enum )
208 #define PARSE_CASE_DEFAULT \
209 default: \
210  return std::string( "UNDEFINED" )
211 
212 static std::string ParseMountStatus( Mount::STATUS status )
213 {
214  switch (status)
215  {
216  PARSE_CASE( Mount, ACTIVE );
217  PARSE_CASE( Mount, INACTIVE );
218  PARSE_CASE( Mount, DESTROYED );
219  PARSE_CASE( Mount, UNCHOSEN );
221  }
222 }
223 
224 static std::string ParseWeaponType( weapon_info::WEAPON_TYPE type )
225 {
226  switch (type)
227  {
233  }
234 }
235 
237 {
239  if (wi) {
240  rv["type"] = ParseWeaponType( wi->type );
241  rv["speed"] = wi->Speed;
242  rv["range"] = wi->Range;
243  rv["damage"] = wi->Damage;
244  rv["phaseDamage"] = wi->PhaseDamage;
245  rv["stability"] = wi->Stability;
246  rv["longRange"] = wi->Longrange;
247  rv["lockTime"] = wi->LockTime;
248  rv["energyRate"] = wi->EnergyRate;
249  rv["refire"] = wi->Refire();
250  rv["volume"] = wi->volume;
251  rv["name"] = wi->weapon_name;
252  }
253  return rv;
254 }
255 
257 {
259  if ( (index >= 0) && ( (unsigned) index < unit->mounts.size() ) ) {
260  Mount &mnt = unit->mounts[index];
261 
262  Vector pos = mnt.GetMountLocation();
263  Vector scale = Vector( mnt.xyscale, mnt.xyscale, mnt.zscale );
264  Quaternion orientation = mnt.GetMountOrientation();
265 
266  rv["position"] = VS_BOOST_MAKE_TUPLE( pos.i, pos.j, pos.k );
267  rv["orientation"] = VS_BOOST_MAKE_TUPLE_4( orientation.v.i, orientation.v.j, orientation.v.k, orientation.s );
268  rv["scale"] = VS_BOOST_MAKE_TUPLE( scale.i, scale.j, scale.k );
269 
270  rv["empty"] = mnt.IsEmpty();
271  rv["volume"] = mnt.volume;
272  rv["ammo"] = mnt.ammo;
273  rv["size"] = mnt.size;
274  rv["size_flags"] = ParseSizeFlags( mnt.size );
275  rv["bank"] = mnt.bank;
276  rv["functionality"] = mnt.functionality;
277  rv["maxfunctionality"] = mnt.maxfunctionality;
278 
279  rv["status"] = ParseMountStatus( mnt.status );
280  if ( !mnt.IsEmpty() )
281  rv["weapon_info"] = GatherWeaponInfo( mnt.type );
282  } else {
283  rv["empty"] = true;
284  rv["status"] = std::string( "UNDEFINED" );
285  }
286  return rv;
287 }
288