Simutrans-Squirrel-API  r11919
api_line.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 "get_next.h"
11 
12 #include "api_obj_desc_base.h"
13 #include "../api_class.h"
14 #include "../api_function.h"
15 #include "../../simhalt.h"
16 #include "../../simline.h"
17 #include "../../world/simworld.h"
18 #include "../../player/simplay.h"
19 
20 // for manipulation of lines
21 #include "../../tool/simmenu.h"
22 #include "../../dataobj/schedule.h"
23 
24 // template<> schedule_t* script_api::param<schedule_t*>::get(HSQUIRRELVM, SQInteger);
25 
26 using namespace script_api;
27 
28 vector_tpl<sint64> const& get_line_stat(simline_t *line, sint32 INDEX)
29 {
30  static vector_tpl<sint64> v;
31  v.clear();
32  if (line && 0<=INDEX && INDEX<MAX_LINE_COST) {
33  for(uint16 i = 0; i < MAX_MONTHS; i++) {
34  v.append( line->get_stat_converted(i, INDEX) );
35  }
36  }
37  return v;
38 }
39 
40 
41 waytype_t line_way_type(simline_t *line)
42 {
43  if (line) {
44  switch (line->get_linetype()) {
45  case simline_t::truckline: return road_wt;
46  case simline_t::trainline: return track_wt;
47  case simline_t::shipline: return water_wt;
48  case simline_t::monorailline: return monorail_wt;
49  case simline_t::maglevline: return maglev_wt;
50  case simline_t::narrowgaugeline: return narrowgauge_wt;
51  case simline_t::airline: return air_wt;
52  case simline_t::tramline: return tram_wt;
53  default: ;
54  }
55  }
56  return invalid_wt;
57 }
58 
59 call_tool_init line_change_schedule(simline_t* line, player_t *player, schedule_t *sched)
60 {
61  if (sched) {
62  cbuffer_t buf;
63  // make a copy, and perform validation on it
64  schedule_t *copy = sched->copy();
65  copy->make_valid();
66  if (copy->get_count() >= 2) {
67  // build param string (see line_management_gui_t::apply_schedule)
68  buf.printf( "g,%i,", line->get_handle().get_id() );
69  copy->sprintf_schedule( buf );
70  }
71  else {
72  return "Invalid schedule provided: less than two entries remained after removing doubles";
73  }
74  delete copy;
75  return call_tool_init(TOOL_CHANGE_LINE | SIMPLE_TOOL, buf, 0, player);
76  }
77  return "Invalid schedule provided";
78 }
79 
80 call_tool_init line_delete(simline_t* line, player_t *player)
81 {
82  if (line->count_convoys() == 0) {
83  cbuffer_t buf;
84  buf.printf( "d,%i", line->get_handle().get_id() );
85 
86  return call_tool_init(TOOL_CHANGE_LINE | SIMPLE_TOOL, buf, 0, player);
87  }
88  return "Cannot delete lines with associated convoys";
89 }
90 
91 SQInteger line_export_convoy_list(HSQUIRRELVM vm)
92 {
93  linehandle_t line = param<linehandle_t>::get(vm, 1);
94  if (line.is_bound()) {
95  push_instance(vm, "convoy_list_x");
96  set_slot(vm, "line_id", line.get_id());
97  return 1;
98  }
99  sq_raise_error(vm, "Invalid line handle provided");
100  return SQ_ERROR;
101 }
102 
103 call_tool_init line_set_name(simline_t* line, const char* name)
104 {
105  return command_rename(line->get_owner(), 'l', line->get_handle().get_id(), name);
106 }
107 
108 vector_tpl<linehandle_t> const* generic_get_line_list(HSQUIRRELVM vm, SQInteger index)
109 {
110  uint16 id;
111  if (SQ_SUCCEEDED(get_slot(vm, "halt_id", id, index))) {
112  halthandle_t halt;
113  halt.set_id(id);
114  if (halt.is_bound()) {
115  return &halt->registered_lines;
116  }
117  }
118  if (SQ_SUCCEEDED(get_slot(vm, "player_id", id, index)) && id < PLAYER_UNOWNED) {
119  if (player_t *player = welt->get_player(id)) {
120  return &player->simlinemgmt.get_line_list();
121  }
122  }
123  sq_raise_error(vm, "Invalid line list.");
124  return NULL;
125 }
126 
127 SQInteger generic_get_next_line(HSQUIRRELVM vm)
128 {
129  vector_tpl<linehandle_t> const* list = generic_get_line_list(vm, 1);
130  return list ? generic_get_next(vm, list->get_count()) : SQ_ERROR;
131 }
132 
133 SQInteger generic_get_line_by_index(HSQUIRRELVM vm)
134 {
135  vector_tpl<linehandle_t> const* list = generic_get_line_list(vm, 1);
136  sint32 index = param<sint32>::get(vm, 2);
137  linehandle_t line;
138  if (list) {
139  line = (0<=index && (uint32)index < list->get_count()) ? (*list)[index] : linehandle_t();
140  return push_instance(vm, "line_x", line.is_bound() ? line.get_id() : 0);
141  }
142  return SQ_ERROR;
143 }
144 
145 SQInteger generic_get_line_count(HSQUIRRELVM vm)
146 {
147  vector_tpl<linehandle_t> const* list = generic_get_line_list(vm, 1);
148  return param<uint32>::push(vm, list ? list->get_count() : 0);
149 }
150 
151 void export_line(HSQUIRRELVM vm)
152 {
166  begin_class(vm, "line_list_x", 0);
170  register_function(vm, generic_get_next_line, "_nexti", 2, "x o|i");
175  register_function(vm, generic_get_line_by_index, "_get", 2, "xi");
180  register_function(vm, generic_get_line_count, "get_count", 1, "x");
181  end_class(vm);
182 
186  begin_class(vm, "line_x", "extend_get,ingame_object");
187 
191  export_is_valid<simline_t*>(vm); //register_function("is_valid")
196  register_method(vm, &simline_t::get_name, "get_name");
202  register_method(vm, &line_set_name, "set_name", true);
207  register_method(vm, &simline_t::get_owner, "get_owner");
211  register_method(vm, &simline_t::get_schedule, "get_schedule");
216  register_method(vm, &simline_t::get_goods_catg_index, "get_goods_catg_index");
221  register_method_fv(vm, &get_line_stat, "get_capacity", freevariable<sint32>(LINE_CAPACITY), true);
226  register_method_fv(vm, &get_line_stat, "get_transported_goods", freevariable<sint32>(LINE_TRANSPORTED_GOODS), true );
231  register_method_fv(vm, &get_line_stat, "get_convoy_count", freevariable<sint32>(LINE_CONVOIS), true );
236  register_method_fv(vm, &get_line_stat, "get_revenue", freevariable<sint32>(LINE_REVENUE), true );
241  register_method_fv(vm, &get_line_stat, "get_cost", freevariable<sint32>(LINE_OPERATIONS), true );
246  register_method_fv(vm, &get_line_stat, "get_profit", freevariable<sint32>(LINE_PROFIT), true );
251  register_method_fv(vm, &get_line_stat, "get_traveled_distance", freevariable<sint32>(LINE_DISTANCE), true );
256  register_method_fv(vm, &get_line_stat, "get_way_tolls", freevariable<sint32>(LINE_WAYTOLL), true );
261  register_function(vm, &line_export_convoy_list, "get_convoy_list", 1, param<linehandle_t>::typemask());
262 
266  register_method(vm, &line_way_type, "get_waytype", true);
267 
273  register_method(vm, line_change_schedule, "change_schedule", true);
274 
279  register_method(vm, line_delete, "destroy", true);
280 
281  end_class(vm);
282 }