Simutrans-Squirrel-API  124.3 (r11590 on 11-Jan-2025)
api_simple.cc
Go to the documentation of this file.
1 /*
2  * This file is part of the Simutrans project under the Artistic License.
3  * (see LICENSE.txt)
4  */
5 
6 #include "api.h"
7 
10 #include "api_simple.h"
11 #include "../api_class.h"
12 #include "../api_function.h"
13 #include "../../dataobj/ribi.h"
14 #include "../../world/simworld.h"
15 #include "../../dataobj/scenario.h"
16 
17 using namespace script_api;
18 
19 
20 #ifdef DOXYGEN
28 class time_x { // begin_class("time_x")
29 public:
30 #ifdef SQAPI_DOC // document members
31  integer raw;
32  integer year;
33  integer month;
34 #endif
35 }; // end_class
36 
41 class time_ticks_x : public time_x { // begin_class("time_ticks_x", "time_x")
42 public:
43 #ifdef SQAPI_DOC // document members
44  integer ticks;
45  integer ticks_per_month;
46  integer next_month_ticks;
47 #endif
48 }; // end_class
49 #endif
50 
51 // pushes table = { raw = , year = , month = }
52 SQInteger param<mytime_t>::push(HSQUIRRELVM vm, mytime_t const& v)
53 {
54  sq_newtableex(vm, 6);
55  create_slot<uint32>(vm, "raw", v.raw);
56  create_slot<uint32>(vm, "year", v.raw/12);
57  create_slot<uint32>(vm, "month", v.raw%12);
58  return 1;
59 }
60 
61 SQInteger param<mytime_ticks_t>::push(HSQUIRRELVM vm, mytime_ticks_t const& v)
62 {
63  param<mytime_t>::push(vm, v);
64  create_slot<uint32>(vm, "ticks", v.ticks);
65  create_slot<uint32>(vm, "ticks_per_month", v.ticks_per_month);
66  create_slot<uint32>(vm, "next_month_ticks", v.next_month_ticks);
67  return 1;
68 }
69 
70 SQInteger param<mytool_data_t>::push(HSQUIRRELVM vm, mytool_data_t const& v)
71 {
72  sq_newtableex(vm, 6);
73  create_slot<koord3d>(vm, "start_pos", v.start_pos);
74  create_slot<bool>(vm, "is_drag_tool", v.is_drag_tool);
75  create_slot<bool>(vm, "is_ctrl", v.is_ctrl);
76  create_slot<bool>(vm, "is_shift", v.is_shift);
77  return 1;
78 }
79 
80 
81 mytime_t param<mytime_t>::get(HSQUIRRELVM vm, SQInteger index)
82 {
83  // 0 has special meaning of 'no-timeline'
84  SQInteger i=1;
85  if (!SQ_SUCCEEDED(sq_getinteger(vm, index, &i))) {
86  get_slot(vm, "raw", i, index);
87  }
88  return (uint16) (i >= 0 ? i : 1);
89 }
90 
91 
92 #define map_ribi_any(f,type) \
93  type export_## f(my_ribi_t r) \
94  { \
95  return ribi_t::f(r); \
96  }
97 
98 // export the ribi functions
99 map_ribi_any(is_single, bool);
100 map_ribi_any(is_twoway, bool);
101 map_ribi_any(is_threeway, bool);
102 map_ribi_any(is_bend, bool);
103 map_ribi_any(is_straight, bool);
104 map_ribi_any(doubles, my_ribi_t);
105 map_ribi_any(backward, my_ribi_t);
106 
107 my_slope_t ribi_to_slope(my_ribi_t ribi)
108 {
109  return slope_type((ribi_t::ribi)ribi);
110 }
111 
112 my_ribi_t slope_to_ribi(my_slope_t slope)
113 {
114  return ribi_type((slope_t::type)slope);
115 }
116 
117 
118 template<int idx> SQInteger coord_to_ribi(HSQUIRRELVM vm)
119 {
120  // get coordinate vector without transformation
121  sint16 x=-1, y=-1;
122  get_slot(vm, "x", x, idx);
123  get_slot(vm, "y", y, idx);
124  koord k(x,y);
125 
126  // and do not transform ribi either
127  uint8 ribi = ribi_type(k);
128  return param<uint8>::push(vm, ribi);
129 }
130 
131 SQInteger ribi_to_coord(HSQUIRRELVM vm)
132 {
133  const uint8 ribi = param<uint8>::get(vm, 2);
134  if ((ribi & ~(uint8)ribi_t::all) != 0) {
135  return sq_raise_error(vm, "Invalid dir %hhu (valid values are 0..15)", ribi);
136  }
137 
138  koord k( (ribi_t::ribi)ribi );
139  return push_instance(vm, "coord", k.x, k.y);
140 }
141 
142 
143 void export_simple(HSQUIRRELVM vm)
144 {
145 
153  begin_class(vm, "coord");
154 #ifdef SQAPI_DOC // document members
156  integer x;
158  integer y;
159  // operators are defined in script_base.nut
160  coord(int x, int y);
161  coord operator + (coord other);
162  coord operator - (coord other);
163  coord operator - ();
164  coord operator * (integer fac);
165  coord operator / (integer fac);
171  string _tostring();
178  string href(string text);
179 #endif
184  register_function(vm, coord_to_ribi<1>, "to_dir", 1, "t|x|y");
185 
186  end_class(vm);
187 
195  begin_class(vm, "coord3d", "coord");
196 #ifdef SQAPI_DOC // document members
198  integer x;
200  integer y;
202  integer z;
203  // operators are defined in script_base.nut
204  coord(int x, int y, int z);
205  coord3d operator + (coord3d other);
206  coord3d operator - (coord other);
207  coord3d operator + (coord3d other);
208  coord3d operator - (coord other);
209  coord3d operator - ();
210  coord3d operator * (integer fac);
211  coord3d operator / (integer fac);
217  string _tostring();
224  string href(string text);
225 #endif
226  end_class(vm);
227 
233  register_function(vm, coord_to_ribi<2>, "coord_to_dir", 2, "x t|x|y");
234 
239  begin_class(vm, "dir");
240 
241 #ifdef SQAPI_DOC // document members
244  static const dir none;
245  static const dir north;
246  static const dir east;
247  static const dir northeast;
248  static const dir south;
249  static const dir northsouth;
250  static const dir southeast;
251  static const dir northsoutheast;
252  static const dir west;
253  static const dir northwest;
254  static const dir eastwest;
255  static const dir northeastwest;
256  static const dir southwest;
257  static const dir northsouthwest;
258  static const dir southeastwest;
259  static const dir all;
261 #endif
266  STATIC register_method(vm, &export_is_single, "is_single", false, true);
271  STATIC register_method(vm, &export_is_twoway, "is_twoway", false, true);
276  STATIC register_method(vm, &export_is_threeway, "is_threeway", false, true);
281  STATIC register_method(vm, &export_is_bend, "is_curve", false, true);
286  STATIC register_method(vm, &export_is_straight, "is_straight", false, true);
291  STATIC register_method(vm, &export_doubles, "double", false, true);
296  STATIC register_method(vm, &export_backward, "backward", false, true);
297 
302  STATIC register_method(vm, &ribi_to_slope, "to_slope", false, true);
307  STATIC register_function(vm, ribi_to_coord, "to_coord", 2, ".i", true);
308 
309  end_class(vm);
310 
315  begin_class(vm, "slope");
316 
317 #ifdef SQAPI_DOC // document members
320  static const slope flat;
321  static const slope north;
322  static const slope west;
323  static const slope east;
324  static const slope south;
325  static const slope northwest;
326  static const slope northeast;
327  static const slope southeast;
328  static const slope southwest;
329  static const slope raised;
330  static const slope all_up_slope = 82;
331  static const slope all_down_slope = 83;
333 #endif
334 
340  STATIC register_method(vm, &slope_to_ribi, "to_dir", false, true);
341  end_class(vm);
342 
343 #ifdef SQAPI_DOC
351  class ingame_object
352  {
356  bool is_valid();
357  }
358 #endif
359 }
void _tostring()