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" 21 #include "../../tool/simmenu.h" 22 #include "../../dataobj/schedule.h" 26 using namespace script_api;
28 vector_tpl<sint64>
const& get_line_stat(simline_t *line, sint32 INDEX)
30 static vector_tpl<sint64> v;
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) );
41 waytype_t line_way_type(simline_t *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;
59 call_tool_init line_change_schedule(simline_t* line, player_t *player, schedule_t *sched)
64 schedule_t *copy = sched->copy();
66 if (copy->get_count() >= 2) {
68 buf.printf(
"g,%i,", line->get_handle().get_id() );
69 copy->sprintf_schedule( buf );
72 return "Invalid schedule provided: less than two entries remained after removing doubles";
75 return call_tool_init(TOOL_CHANGE_LINE | SIMPLE_TOOL, buf, 0, player);
77 return "Invalid schedule provided";
80 call_tool_init line_delete(simline_t* line, player_t *player)
82 if (line->count_convoys() == 0) {
84 buf.printf(
"d,%i", line->get_handle().get_id() );
86 return call_tool_init(TOOL_CHANGE_LINE | SIMPLE_TOOL, buf, 0, player);
88 return "Cannot delete lines with associated convoys";
91 SQInteger line_export_convoy_list(HSQUIRRELVM vm)
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());
99 sq_raise_error(vm,
"Invalid line handle provided");
103 call_tool_init line_set_name(simline_t* line,
const char* name)
105 return command_rename(line->get_owner(),
'l', line->get_handle().get_id(), name);
108 vector_tpl<linehandle_t>
const* generic_get_line_list(HSQUIRRELVM vm, SQInteger index)
111 if (SQ_SUCCEEDED(get_slot(vm,
"halt_id",
id, index))) {
114 if (halt.is_bound()) {
115 return &halt->registered_lines;
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();
123 sq_raise_error(vm,
"Invalid line list.");
127 SQInteger generic_get_next_line(HSQUIRRELVM vm)
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;
133 SQInteger generic_get_line_by_index(HSQUIRRELVM vm)
135 vector_tpl<linehandle_t>
const* list = generic_get_line_list(vm, 1);
136 sint32 index = param<sint32>::get(vm, 2);
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);
145 SQInteger generic_get_line_count(HSQUIRRELVM vm)
147 vector_tpl<linehandle_t>
const* list = generic_get_line_list(vm, 1);
148 return param<uint32>::push(vm, list ? list->get_count() : 0);
151 void export_line(HSQUIRRELVM vm)
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");
186 begin_class(vm,
"line_x",
"extend_get,ingame_object");
191 export_is_valid<simline_t*>(vm);
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());
266 register_method(vm, &line_way_type,
"get_waytype",
true);
273 register_method(vm, line_change_schedule,
"change_schedule",
true);
279 register_method(vm, line_delete,
"destroy",
true);