Simutrans-Squirrel-API  r11919
api_factory.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 #include "api_obj_desc_base.h"
12 #include "../api_class.h"
13 #include "../api_function.h"
14 #include "../../dataobj/scenario.h"
15 #include "../../simfab.h"
16 #include "../../descriptor/factory_desc.h"
17 
18 using namespace script_api;
19 
20 SQInteger exp_factory_constructor(HSQUIRRELVM vm)
21 {
22  sint16 x = param<sint16>::get(vm, 2);
23  sint16 y = param<sint16>::get(vm, 3);
24  // set coordinates
25  set_slot(vm, "x", x, 1);
26  set_slot(vm, "y", y, 1);
27  // transform coordinates
28  koord pos(x,y);
29  coordinate_transform_t::koord_sq2w(pos);
30  fabrik_t *fab = fabrik_t::get_fab(pos);
31  if (!fab) {
32  return sq_raise_error(vm, "No factory found at (%s)", pos.get_str());
33  }
34  const factory_desc_t *desc = fab->get_desc();
35  // create input/output tables
36  for (int io=0; io<2; io++) {
37  sq_pushstring(vm, io==0 ? "input" : "output", -1);
38  sq_newtable(vm);
39  const array_tpl<ware_production_t> &prodslot = io==0 ? fab->get_input() :fab->get_output();
40  for(uint32 p=0; p < prodslot.get_count(); p++) {
41  // create slots 'good name' <- {x,y,name} //'factory_production'
42  sq_pushstring(vm, prodslot[p].get_typ()->get_name(), -1);
43  // create instance of factory_production_x
44  if(!SQ_SUCCEEDED(push_instance(vm, "factory_production_x",
45  x, y, prodslot[p].get_typ()->get_name(), p + (io > 0 ? fab->get_input().get_count() : 0))))
46  {
47  // create empty table
48  sq_newtable(vm);
49  }
50  sint64 factor = io == 0 ? desc->get_supplier(p)->get_consumption() : desc->get_product(p)->get_factor();
51  // set max value - see fabrik_t::info_prod
52  set_slot(vm, "max_storage", convert_goods( (sint64)prodslot[p].max * factor), -1);
53  // production/consumption scaling
54  set_slot(vm, "scaling", factor, -1);
55  // put class into table
56  sq_newslot(vm, -3, false);
57  }
58  sq_set(vm, 1);
59  }
60  return 0; // dummy return type
61 }
62 
63 
64 vector_tpl<sint64> const& get_factory_stat(fabrik_t *fab, sint32 INDEX)
65 {
66  static vector_tpl<sint64> v;
67  v.clear();
68  if (fab && 0<=INDEX && INDEX<MAX_FAB_STAT) {
69  for(uint16 i = 0; i < MAX_MONTH; i++) {
70  v.append(fab->get_stat_converted(i, INDEX));
71  }
72  }
73  return v;
74 }
75 
76 
77 vector_tpl<sint64> const& get_factory_production_stat(const ware_production_t *prod_slot, sint32 INDEX)
78 {
79  static vector_tpl<sint64> v;
80  v.clear();
81  if (prod_slot && 0<=INDEX && INDEX<MAX_FAB_GOODS_STAT) {
82  for(uint16 i = 0; i < MAX_MONTH; i++) {
83  v.append(prod_slot->get_stat_converted(i, INDEX));
84  }
85  }
86  return v;
87 }
88 
89 
90 uint32 get_production_factor(const factory_product_desc_t *desc)
91 {
92  return desc ? ( (1<< (DEFAULT_PRODUCTION_FACTOR_BITS-1)) + (uint32)desc->get_factor() * 100) >> DEFAULT_PRODUCTION_FACTOR_BITS : 0;
93 }
94 
95 
96 uint32 get_consumption_factor(const factory_supplier_desc_t *desc)
97 {
98  return desc ? ( (1<< (DEFAULT_PRODUCTION_FACTOR_BITS-1)) + (uint32)desc->get_consumption() * 100) >> DEFAULT_PRODUCTION_FACTOR_BITS : 0;
99 }
100 
101 
102 vector_tpl<grund_t*> const& factory_get_tile_list(fabrik_t *fab)
103 {
104  static vector_tpl<koord> list;
105  fab->get_tile_list(list);
106 
107  static vector_tpl<grund_t*> tile_list;
108  tile_list.clear();
109  for(koord k : list) {
110  tile_list.append(welt->lookup_kartenboden(k));
111  }
112  return tile_list;
113 }
114 
115 vector_tpl<grund_t*> const& factory_get_fields_list(fabrik_t *fab)
116 {
117  static vector_tpl<grund_t*> list;
118  fab->get_fields_list(list);
119  return list;
120 }
121 
122 vector_tpl<halthandle_t> const& square_get_halt_list(planquadrat_t *plan); // api_tiles.cc
123 
124 vector_tpl<halthandle_t> const& factory_get_halt_list(fabrik_t *fab)
125 {
126  planquadrat_t *plan = welt->access(fab->get_pos().get_2d());
127  return square_get_halt_list(plan);
128 }
129 
130  leitung_t* factory_get_transformer( fabrik_t* fab )
131  {
132  return fab->get_transformers().empty() ? NULL :fab->get_transformers().front();
133  }
134 
135 call_tool_init factory_set_name(fabrik_t *fab, const char* name)
136 {
137  return command_rename(welt->get_public_player(), 'f', fab->get_pos(), name);
138 }
139 
140 
141 const char* fabrik_get_raw_name(fabrik_t *fab)
142 {
143  return fab->get_desc()->get_name();
144 }
145 
146 
147 SQInteger ware_production_get_production(HSQUIRRELVM vm)
148 {
149  fabrik_t* fab = param<fabrik_t*>::get(vm, 1);
150  sint64 prod = 0;
151  if (fab) {
152  sint64 scaling = 0;
153  if (SQ_SUCCEEDED(get_slot(vm, "scaling", scaling, 1))) {
154  // see fabrik_t::step
155  prod = (scaling * welt->scale_with_month_length(fab->get_base_production()) * DEFAULT_PRODUCTION_FACTOR ) >> (8+8);
156  }
157  }
158  return param<sint64>::push(vm, prod);
159 }
160 
161 SQInteger world_get_next_factory(HSQUIRRELVM vm)
162 {
163  return generic_get_next(vm, welt->get_fab_list().get_count());
164 }
165 
166 
167 SQInteger world_get_factory_by_index(HSQUIRRELVM vm)
168 {
169  uint32 index = param<uint32>::get(vm, -1);
170  fabrik_t *fab = welt->get_fab(index);
171  return param<fabrik_t*>::push(vm, fab);
172 }
173 
174 SQInteger world_get_factory_count(HSQUIRRELVM vm)
175 {
176  return param<uint32>::push(vm, welt->get_fab_list().get_count());
177 }
178 
179 
180 void export_factory(HSQUIRRELVM vm)
181 {
193  begin_class(vm, "factory_list_x", 0);
197  register_function(vm, world_get_next_factory, "_nexti", 2, "x o|i");
201  register_function(vm, world_get_factory_by_index, "_get", 2, "xi");
206  register_function(vm, world_get_factory_count, "get_count", 1, "x");
207 
208  end_class(vm);
209 
210 
215  begin_class(vm, "factory_x", "coord,extend_get,ingame_object");
216 
223  register_function(vm, exp_factory_constructor, "constructor", 3, "xii");
224 
225 #ifdef SQAPI_DOC // document members
226 
243  table<factory_production_x> input;
252  table<factory_production_x> output;
253 #endif
254 
257  export_is_valid<fabrik_t*>(vm); //register_function("is_valid")
258 
263  register_method(vm, &fabrik_t::get_consumer, "get_consumers");
264 
269  register_method(vm, &fabrik_t::get_suppliers, "get_suppliers");
270 
275  register_method(vm, &fabrik_t::get_name, "get_name");
276 
281  register_method(vm, &fabrik_get_raw_name, "get_raw_name", true);
282 
287  register_method(vm, &factory_set_name, "set_name", true);
288 
293  register_method_fv(vm, &get_factory_stat, "get_production", freevariable<sint32>(FAB_PRODUCTION), true);
294 
299  register_method_fv(vm, &get_factory_stat, "get_power", freevariable<sint32>(FAB_POWER), true);
300 
305  register_method_fv(vm, &get_factory_stat, "get_boost_electric", freevariable<sint32>(FAB_BOOST_ELECTRIC), true);
306 
311  register_method_fv(vm, &get_factory_stat, "get_boost_pax", freevariable<sint32>(FAB_BOOST_PAX), true);
312 
317  register_method_fv(vm, &get_factory_stat, "get_boost_mail", freevariable<sint32>(FAB_BOOST_MAIL), true);
318 
323  register_method_fv(vm, &get_factory_stat, "get_pax_generated", freevariable<sint32>(FAB_PAX_GENERATED), true);
324 
329  register_method_fv(vm, &get_factory_stat, "get_pax_departed", freevariable<sint32>(FAB_PAX_DEPARTED), true);
330 
335  register_method_fv(vm, &get_factory_stat, "get_pax_arrived", freevariable<sint32>(FAB_PAX_ARRIVED), true);
336 
341  register_method_fv(vm, &get_factory_stat, "get_mail_generated", freevariable<sint32>(FAB_MAIL_GENERATED), true);
342 
347  register_method_fv(vm, &get_factory_stat, "get_mail_departed", freevariable<sint32>(FAB_MAIL_DEPARTED), true);
348 
353  register_method_fv(vm, &get_factory_stat, "get_mail_arrived", freevariable<sint32>(FAB_MAIL_ARRIVED), true);
354 
359  register_method(vm, &factory_get_tile_list, "get_tile_list", true);
360 
365  register_method(vm, &factory_get_fields_list, "get_fields_list", true);
366 
371  register_method(vm, &factory_get_halt_list, "get_halt_list", true);
372 
377  register_method(vm, &fabrik_t::is_transformer_connected, "is_transformer_connected");
382  register_method(vm, factory_get_transformer, "get_transformer", true);
386  register_method(vm, &fabrik_t::get_field_count, "get_field_count");
390  register_method(vm, &fabrik_t::get_min_field_count, "get_min_field_count");
394  register_method(vm, &fabrik_t::get_desc, "get_desc");
395  // pop class
396  end_class(vm);
397 
402  begin_class(vm, "factory_production_x", "extend_get");
403 
404 #ifdef SQAPI_DOC // document members
405 
408  integer max_storage;
409 #endif
410 
414  register_method_fv(vm, &get_factory_production_stat, "get_storage", freevariable<sint32>(FAB_GOODS_STORAGE), true);
415 
420  register_method_fv(vm, &get_factory_production_stat, "get_received", freevariable<sint32>(FAB_GOODS_RECEIVED), true);
421 
426  register_method_fv(vm, &get_factory_production_stat, "get_consumed", freevariable<sint32>(FAB_GOODS_CONSUMED), true);
427 
432  register_method_fv(vm, &get_factory_production_stat, "get_in_transit",freevariable<sint32>(FAB_GOODS_TRANSIT), true);
433 
438  register_method_fv(vm, &get_factory_production_stat, "get_delivered", freevariable<sint32>(FAB_GOODS_DELIVERED), true);
439 
444  register_method_fv(vm, &get_factory_production_stat, "get_produced", freevariable<sint32>(FAB_GOODS_PRODUCED), true);
445 
451  register_function(vm, &ware_production_get_production, "get_base_production", 1, "x");
452 
458  register_function(vm, &ware_production_get_production, "get_base_consumption", 1, "x");
459 
465  register_method(vm, &get_consumption_factor, "get_consumption_factor", 1, "x");
466 
472  register_method(vm, &get_production_factor, "get_production_factor", 1, "x");
473 
474 
475  // pop class
476  end_class(vm);
477 }