Simutrans-Squirrel-API  r11919
api_tiles.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 "get_next.h"
12 #include "../api_class.h"
13 #include "../api_function.h"
14 #include "../../tool/simmenu.h"
15 #include "../../world/simworld.h"
16 #include "../../ground/wasser.h"
17 #include "../../obj/crossing.h"
18 
19 #include "../../simconvoi.h"
20 #include "../../vehicle/vehicle.h"
21 
22 namespace script_api {
23  declare_enum_param(grund_t::flag_values, uint8, "flags");
24  declare_specialized_param(depot_t*, "t|x|y", "depot_x");
25 };
26 
27 using namespace script_api;
28 
29 
30 SQInteger get_next_object(HSQUIRRELVM vm)
31 {
32  grund_t *gr = param<grund_t*>::get(vm, 1);
33  return generic_get_next(vm, gr ? gr->obj_count() : 0);
34 }
35 
36 
37 SQInteger get_object_index(HSQUIRRELVM vm)
38 {
39  grund_t *gr = param<grund_t*>::get(vm, 1);
40  uint8 index = param<uint8>::get(vm, 2);
41 
42  obj_t *obj = NULL;
43  if (gr && index < gr->obj_count()) {
44  obj = gr->obj_bei(index);
45  }
46  return param<obj_t*>::push(vm, obj);
47 }
48 
49 SQInteger get_object_count(HSQUIRRELVM vm)
50 {
51  grund_t *gr = param<grund_t*>::get(vm, 1);
52  return param<uint8>::push(vm, gr ? gr->obj_count() : 0);
53 }
54 
55 call_tool_work tile_remove_object(grund_t* gr, player_t* player, obj_t::typ type)
56 {
57  cbuffer_t buf;
58  buf.printf("%d", (int)type);
59  return call_tool_work(TOOL_REMOVER | GENERAL_TOOL, (const char*)buf, 0, player, gr->get_pos());
60 }
61 
62 // return way ribis, have to implement a wrapper, to correctly rotate ribi
63 static SQInteger get_way_ribi(HSQUIRRELVM vm)
64 {
65  grund_t *gr = param<grund_t*>::get(vm, 1);
66  waytype_t wt = param<waytype_t>::get(vm, 2);
67  bool masked = param<bool>::get(vm, 3);
68 
69  ribi_t::ribi ribi = gr ? (masked ? gr->get_weg_ribi(wt) : gr->get_weg_ribi_unmasked(wt) ) : 0;
70 
71  return param<my_ribi_t>::push(vm, ribi);
72 }
73 
74 static SQInteger get_canal_ribi(HSQUIRRELVM vm)
75 {
76  grund_t *gr = param<grund_t*>::get(vm, 1);
77  ribi_t::ribi ribi = gr && gr->is_water() ? ((wasser_t*)gr)->get_canal_ribi() : (ribi_t::ribi)0;
78 
79  return param<my_ribi_t>::push(vm, ribi);
80 }
81 
82 
83 // we have to implement a wrapper, to correctly rotate ribi
84 grund_t* get_neighbour(grund_t *gr, waytype_t wt, my_ribi_t ribi)
85 {
86  grund_t *to = NULL;
87  if (gr && ribi_t::is_single(ribi)) {
88  gr->get_neighbour(to, wt, ribi);
89  }
90  return to;
91 }
92 
93 my_slope_t get_slope(grund_t *gr)
94 {
95  return gr->get_grund_hang();
96 }
97 
98 bool is_crossing(grund_t *gr)
99 {
100  return gr->find<crossing_t>();
101 }
102 
103 halthandle_t get_first_halt_on_square(planquadrat_t* plan)
104 {
105  return plan->get_halt(NULL);
106 }
107 
108 vector_tpl<halthandle_t> const& square_get_halt_list(planquadrat_t *plan)
109 {
110  static vector_tpl<halthandle_t> list;
111  list.clear();
112  if (plan) {
113  const halthandle_t* haltlist = plan->get_haltlist();
114  for(uint8 i=0, end = plan->get_haltlist_count(); i < end; i++) {
115  list.append(haltlist[i]);
116  }
117  }
118  return list;
119 }
120 
121 vector_tpl<convoihandle_t> const get_convoy_list(grund_t* gr)
122 {
123  static vector_tpl<convoihandle_t> list;
124  list.clear();
125 
126  for( uint8 n = 0; n<gr->obj_count(); n++) {
127  obj_t* obj = gr->obj_bei( n );
128  if( vehicle_t* veh = dynamic_cast<vehicle_t*>(obj) ) {
129  convoihandle_t cnv;
130  if( veh->get_convoi() ) {
131  list.append_unique( veh->get_convoi()->self );
132  }
133  }
134  }
135  return list;
136 }
137 
138 void export_tiles(HSQUIRRELVM vm)
139 {
151  begin_class(vm, "tile_x", "coord3d,extend_get,ingame_object");
152 
162  // actually defined in simutrans/script/script_base.nut
163  // register_function(..., "constructor", ...);
164 
168  export_is_valid<grund_t*>(vm); //register_function("is_valid")
173  register_method(vm, &grund_t::suche_obj, "find_object");
185  register_method(vm, &tile_remove_object, "remove_object", true);
190  register_method(vm, &grund_t::get_halt, "get_halt");
191 
196  register_method(vm, &grund_t::is_water, "is_water");
197 
202  register_method(vm, &grund_t::ist_bruecke, "is_bridge");
208  register_method(vm, &grund_t::ist_tunnel, "is_tunnel");
209 
214  register_method(vm, &grund_t::ist_natur, "is_empty");
215 
220  register_method(vm, &grund_t::ist_karten_boden, "is_ground");
221 
226  register_method(vm, &is_crossing, "is_crossing", true);
227 
232  register_method(vm, &get_slope, "get_slope", true);
233 
238  register_method(vm, &grund_t::get_text, "get_text");
239 
245  register_method(vm, &grund_t::hat_weg, "has_way");
246 
251  register_method(vm, &grund_t::hat_wege, "has_ways");
252 
257  register_method<bool (grund_t::*)() const>(vm, &grund_t::has_two_ways, "has_two_ways");
263  register_method(vm, &grund_t::get_weg, "get_way");
270  register_function_fv(vm, &get_way_ribi, "get_way_dirs", 2, "xi", freevariable<bool>(false) );
277  register_function_fv(vm, &get_way_ribi, "get_way_dirs_masked", 2, "xi", freevariable<bool>(true) );
284  register_function(vm, &get_canal_ribi, "get_canal_ribi", 1, "x");
291  register_method(vm, &get_neighbour, "get_neighbour", true);
296  register_method(vm, &grund_t::get_depot, "get_depot");
302  register_method(vm, &grund_t::kann_alle_obj_entfernen, "can_remove_all_objects");
303 
310  register_method_fv(vm, &grund_t::get_flag, "is_marked", freevariable<uint8>(grund_t::marked));
313  register_method_fv(vm, &grund_t::clear_flag, "unmark", freevariable<uint8>(grund_t::marked));
315  register_method_fv(vm, &grund_t::set_flag, "mark", freevariable<uint8>(grund_t::marked));
317 
321  register_method(vm, &get_convoy_list, "get_convoys", true);
322 
323 #ifdef SQAPI_DOC // document members
324 
333  tile_object_list_x get_objects();
334 #endif
335 
336  end_class(vm);
337 
343  begin_class(vm, "tile_object_list_x", "coord3d");
347  register_function(vm, get_next_object, "_nexti", 2, "x o|i");
351  register_function(vm, get_object_index, "_get", 2, "x i|s");
356  register_function(vm, get_object_count, "get_count", 1, "x");
357 
358  end_class(vm);
359 
363  begin_class(vm, "square_x", "coord,extend_get,ingame_object");
364 
372  // actually defined in simutrans/script/script_base.nut
373  // register_function(..., "constructor", ...);
374 
378  export_is_valid<planquadrat_t*>(vm); //register_function("is_valid")
384  register_method(vm, &get_first_halt_on_square, "get_halt", true);
385 
391  register_method(vm, &planquadrat_t::get_halt, "get_player_halt");
392 
398  register_method(vm, &planquadrat_t::get_boden_in_hoehe, "get_tile_at_height");
399 
404  register_method(vm, &planquadrat_t::get_kartenboden, "get_ground_tile");
405 
410  register_method(vm, &square_get_halt_list, "get_halt_list", true);
411 
415  register_method(vm, &planquadrat_t::get_climate, "get_climate");
416 
417  end_class(vm);
418 }