Skip to content

Commit

Permalink
sqapi ADD methods is_valid to check whether in-game object is still p…
Browse files Browse the repository at this point in the history
…resent

git-svn-id: svn://tron.homeunix.org/simutrans/simutrans/trunk@8358 8aca7d54-2c30-db11-9de9-000461428c89
  • Loading branch information
Dwachs committed Dec 30, 2017
1 parent 53335ab commit fffc8ac
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 19 deletions.
6 changes: 5 additions & 1 deletion script/api/api_city.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void export_city(HSQUIRRELVM vm)
/**
* Class to access cities.
*/
begin_class(vm, "city_x", "extend_get,coord");
begin_class(vm, "city_x", "extend_get,coord,ingame_object");

/**
* Constructor.
Expand All @@ -111,6 +111,10 @@ void export_city(HSQUIRRELVM vm)
// actually defined in simutrans/script/script_base.nut
// register_function(..., "constructor", ...);

/**
* @returns if object is still valid.
*/
export_is_valid<stadt_t*>(vm); //register_function("is_valid")
/**
* Return name of city
* @returns name
Expand Down
6 changes: 5 additions & 1 deletion script/api/api_convoy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ void export_convoy(HSQUIRRELVM vm)
* Class to access a convoy.
* Player vehicles are convoys, which themselves consist of individual vehicles (trucks, trailers, ...).
*/
begin_class(vm, "convoy_x", "extend_get");
begin_class(vm, "convoy_x", "extend_get,ingame_object");
/**
* @returns if object is still valid.
*/
export_is_valid<convoi_t*>(vm); //register_function("is_valid")
/**
* Does convoy needs electrified ways?
* @returns true if this is the case
Expand Down
6 changes: 5 additions & 1 deletion script/api/api_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void export_factory(HSQUIRRELVM vm)
* Class to access information about factories.
* Identified by coordinate.
*/
begin_class(vm, "factory_x", "extend_get,coord");
begin_class(vm, "factory_x", "extend_get,coord,ingame_object");

/**
* Constructor.
Expand Down Expand Up @@ -221,6 +221,10 @@ void export_factory(HSQUIRRELVM vm)
*/
table<factory_production_x> output;
#endif
/**
* @returns if object is still valid.
*/
export_is_valid<fabrik_t*>(vm); //register_function("is_valid")

/**
* Get list of consumers of this factory.
Expand Down
6 changes: 5 additions & 1 deletion script/api/api_halt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ void export_halt(HSQUIRRELVM vm)
/**
* Class to access halts / stations / bus stops.
*/
begin_class(vm, "halt_x", "extend_get");
begin_class(vm, "halt_x", "extend_get,ingame_object");

/**
* @returns if object is still valid.
*/
export_is_valid<const haltestelle_t*>(vm); //register_function("is_valid")
/**
* Station name.
* @returns name
Expand Down
7 changes: 3 additions & 4 deletions script/api/api_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,12 @@ void export_line(HSQUIRRELVM vm)
/**
* Class to access lines.
*/
begin_class(vm, "line_x", "extend_get");
begin_class(vm, "line_x", "extend_get,ingame_object");

/**
* Is line a valid object:
* @returns if object is still valid.
*/
register_method(vm, line_is_valid, "is_valid", true);

export_is_valid<simline_t*>(vm); //register_function("is_valid")
/**
* Line name.
* @returns name
Expand Down
6 changes: 5 additions & 1 deletion script/api/api_map_objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ void export_map_objects(HSQUIRRELVM vm)
* Class to access objects on the map
* These classes cannot modify anything.
*/
begin_class(vm, "map_object_x", "extend_get,coord3d");
begin_class(vm, "map_object_x", "extend_get,coord3d,ingame_object");
uint8 objtype = bind_code<obj_t>::objtype;
sq_settypetag(vm, -1, obj_t_tag + objtype);
/**
Expand All @@ -359,6 +359,10 @@ void export_map_objects(HSQUIRRELVM vm)
* @typemask void(integer,integer,integer,map_objects)
*/
register_function(vm, exp_obj_pos_constructor, "constructor", 5, "xiiii");
/**
* @returns if object is still valid.
*/
export_is_valid<obj_t*>(vm); //register_function("is_valid")
/**
* @returns owner of the object.
*/
Expand Down
4 changes: 4 additions & 0 deletions script/api/api_obj_desc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ void export_goods_desc(HSQUIRRELVM vm)
* @return true if this==other
*/
register_method(vm, &are_equal, "is_equal", true);
/**
* @returns if object is still valid.
*/
export_is_valid<const obj_named_desc_t*>(vm); //register_function("is_valid")
end_class(vm);

/**
Expand Down
13 changes: 6 additions & 7 deletions script/api/api_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ vector_tpl<sint64> const& get_player_stat(player_t *player, sint32 INDEX, sint32
}


bool player_active(player_t *player)
{
return player != NULL;
}


// export of finance_t only here
namespace script_api {
declare_specialized_param(finance_t*, param<player_t*>::typemask(), param<player_t*>::squirrel_type());
Expand Down Expand Up @@ -129,7 +123,7 @@ void export_player(HSQUIRRELVM vm, bool scenario)
* Class to access player statistics.
* Here, a player refers to one transport company, not to an individual playing simutrans.
*/
begin_class(vm, "player_x", "extend_get");
begin_class(vm, "player_x", "extend_get,ingame_object");

/**
* Constructor.
Expand All @@ -139,6 +133,11 @@ void export_player(HSQUIRRELVM vm, bool scenario)
// actually defined in simutrans/script/script_base.nut
// register_function(..., "constructor", ...);

/**
* @returns if object is still valid.
*/
export_is_valid<player_t*>(vm); //register_function("is_valid")

if (!scenario) {
/**
* @returns player associated with the AI.
Expand Down
17 changes: 17 additions & 0 deletions script/api/api_simple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,21 @@ void export_simple(HSQUIRRELVM vm)
*/
STATIC register_method(vm, &slope_to_ribi, "to_dir", false, true);
end_class(vm);

#ifdef SQAPI_DOC
/**
* Classes to access in-game objects.
* Creating an instance of such classes will not create an object in the game.
* Instead, these classes contain data to access the in-game object.
* If the in-game object disappears, then any access to it via these classes will fail.
* Use the method @r ingame_object::is_valid to check whether the in-game object is still alive.
*/
class ingame_object
{
/**
* @returns true if in-game object can still be accessed.
*/
bool is_valid();
}
#endif
}
14 changes: 11 additions & 3 deletions script/api/api_tiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void export_tiles(HSQUIRRELVM vm)
* }
* @endcode
*/
begin_class(vm, "tile_x", "extend_get,coord3d");
begin_class(vm, "tile_x", "extend_get,coord3d,ingame_object");

/**
* Constructor. Returns tile at particular 3d coordinate.
Expand All @@ -118,7 +118,10 @@ void export_tiles(HSQUIRRELVM vm)
// actually defined in simutrans/script/script_base.nut
// register_function(..., "constructor", ...);


/**
* @returns if object is still valid.
*/
export_is_valid<grund_t*>(vm); //register_function("is_valid")
/**
* Search for a given object type on the tile.
* @return some instance or null if not found
Expand Down Expand Up @@ -269,7 +272,7 @@ void export_tiles(HSQUIRRELVM vm)
/**
* Class to map squares, which holds all the tiles on one particular coordinate.
*/
begin_class(vm, "square_x", "extend_get,coord");
begin_class(vm, "square_x", "extend_get,coord,ingame_object");

/**
* Constructor. Returns map square at particular 2d coordinate.
Expand All @@ -280,6 +283,11 @@ void export_tiles(HSQUIRRELVM vm)
*/
// actually defined in simutrans/script/script_base.nut
// register_function(..., "constructor", ...);

/**
* @returns if object is still valid.
*/
export_is_valid<planquadrat_t*>(vm); //register_function("is_valid")
/**
* Access some halt at this square.
* @deprecated Use square_x::get_player_halt or tile_x::get_halt instead!
Expand Down
1 change: 1 addition & 0 deletions script/api/changelog.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @section api-trunk Current trunk
*
* - Added methods is_valid to check whether in-game object is still present, see @ref ingame_object
* - Added tile_x::can_remove_all_objects
* - Added @ref coord_to_dir, @ref coord.to_dir, @ref dir.to_coord
* - Added @ref simple_heap_x, @ref way_planner_x, @ref bridge_planner_x
Expand Down
9 changes: 9 additions & 0 deletions script/api/squirrel_types_ai.awk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# needs to be copied to trunk/script/api
BEGIN {
export_types["city_list_x::_get"] = "city_x(integer)"
export_types["city_x::is_valid"] = "bool()"
export_types["city_x::get_name"] = "string()"
export_types["city_x::set_name"] = "string(string)"
export_types["city_x::get_citizens"] = "array<integer>()"
Expand All @@ -26,6 +27,7 @@ BEGIN {
export_types["city_x::get_pos_se"] = "coord()"
export_types["city_x::change_size"] = "string(integer)"
export_types["city_x::set_citygrowth_enabled"] = "string(bool)"
export_types["convoy_x::is_valid"] = "bool()"
export_types["convoy_x::needs_electrification"] = "bool()"
export_types["convoy_x::get_name"] = "string()"
export_types["convoy_x::set_name"] = "string(string)"
Expand Down Expand Up @@ -56,6 +58,7 @@ BEGIN {
export_types["convoy_x::destroy"] = "string(player_x)"
export_types["convoy_x::calc_max_speed"] = "integer(integer, integer, integer)"
export_types["convoy_x::speed_to_tiles_per_month"] = "integer(integer)"
export_types["factory_x::is_valid"] = "bool()"
export_types["factory_x::get_consumers"] = "array<coord>()"
export_types["factory_x::get_suppliers"] = "array<coord>()"
export_types["factory_x::get_name"] = "string()"
Expand Down Expand Up @@ -83,6 +86,7 @@ BEGIN {
export_types["factory_production_x::get_production_factor"] = "integer()"
export_types["obj_desc_x::get_name"] = "string()"
export_types["obj_desc_x::is_equal"] = "bool(obj_desc_x)"
export_types["obj_desc_x::is_valid"] = "bool()"
export_types["obj_desc_time_x::get_intro_date"] = "time_x()"
export_types["obj_desc_time_x::get_retire_date"] = "time_x()"
export_types["obj_desc_time_x::is_future"] = "bool(time_x)"
Expand Down Expand Up @@ -142,6 +146,7 @@ BEGIN {
export_types["sign_desc_x::is_longblock_signal"] = "bool()"
export_types["sign_desc_x::is_end_choose_signal"] = "bool()"
export_types["sign_desc_x::get_available_signs"] = "array<sign_desc_x>(way_types)"
export_types["halt_x::is_valid"] = "bool()"
export_types["halt_x::get_name"] = "string()"
export_types["halt_x::set_name"] = "string(string)"
export_types["halt_x::get_owner"] = "player_x()"
Expand Down Expand Up @@ -178,6 +183,7 @@ BEGIN {
export_types["line_x::get_waytype"] = "way_types()"
export_types["line_x::change_schedule"] = "string(player_x, schedule_x)"
export_types["line_x::destroy"] = "string(player_x)"
export_types["map_object_x::is_valid"] = "bool()"
export_types["map_object_x::get_owner"] = "player_x()"
export_types["map_object_x::get_name"] = "string()"
export_types["map_object_x::get_waytype"] = "way_types()"
Expand Down Expand Up @@ -213,6 +219,7 @@ BEGIN {
export_types["label_x::set_text"] = "string(string)"
export_types["label_x::get_text"] = "string()"
export_types["sign_x::get_desc"] = "sign_desc_x()"
export_types["player_x::is_valid"] = "bool()"
export_types["player_x::get_headquarter_level"] = "integer()"
export_types["player_x::get_headquarter_pos"] = "coord()"
export_types["player_x::get_name"] = "string()"
Expand Down Expand Up @@ -270,6 +277,7 @@ BEGIN {
export_types["::coord_to_string"] = "string(coord)"
export_types["::coord3d_to_string"] = "string(coord3d)"
export_types["::get_month_name"] = "string(integer)"
export_types["tile_x::is_valid"] = "bool()"
export_types["tile_x::find_object"] = "map_object_x(map_objects)"
export_types["tile_x::remove_object"] = "string(player_x, map_objects)"
export_types["tile_x::get_halt"] = "halt_x()"
Expand All @@ -285,6 +293,7 @@ BEGIN {
export_types["tile_x::has_two_ways"] = "bool()"
export_types["tile_x::get_neighbour"] = "tile_x(way_types, dir)"
export_types["tile_x::can_remove_all_objects"] = "string(player_x)"
export_types["square_x::is_valid"] = "bool()"
export_types["square_x::get_halt"] = "halt_x()"
export_types["square_x::get_player_halt"] = "halt_x(player_x)"
export_types["square_x::get_tile_at_height"] = "tile_x(integer)"
Expand Down
9 changes: 9 additions & 0 deletions script/api/squirrel_types_scenario.awk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# needs to be copied to trunk/script/api
BEGIN {
export_types["city_list_x::_get"] = "city_x(integer)"
export_types["city_x::is_valid"] = "bool()"
export_types["city_x::get_name"] = "string()"
export_types["city_x::set_name"] = "string(string)"
export_types["city_x::get_citizens"] = "array<integer>()"
Expand All @@ -26,6 +27,7 @@ BEGIN {
export_types["city_x::get_pos_se"] = "coord()"
export_types["city_x::change_size"] = "string(integer)"
export_types["city_x::set_citygrowth_enabled"] = "string(bool)"
export_types["convoy_x::is_valid"] = "bool()"
export_types["convoy_x::needs_electrification"] = "bool()"
export_types["convoy_x::get_name"] = "string()"
export_types["convoy_x::set_name"] = "string(string)"
Expand Down Expand Up @@ -56,6 +58,7 @@ BEGIN {
export_types["convoy_x::destroy"] = "string(player_x)"
export_types["convoy_x::calc_max_speed"] = "integer(integer, integer, integer)"
export_types["convoy_x::speed_to_tiles_per_month"] = "integer(integer)"
export_types["factory_x::is_valid"] = "bool()"
export_types["factory_x::get_consumers"] = "array<coord>()"
export_types["factory_x::get_suppliers"] = "array<coord>()"
export_types["factory_x::get_name"] = "string()"
Expand Down Expand Up @@ -83,6 +86,7 @@ BEGIN {
export_types["factory_production_x::get_production_factor"] = "integer()"
export_types["obj_desc_x::get_name"] = "string()"
export_types["obj_desc_x::is_equal"] = "bool(obj_desc_x)"
export_types["obj_desc_x::is_valid"] = "bool()"
export_types["obj_desc_time_x::get_intro_date"] = "time_x()"
export_types["obj_desc_time_x::get_retire_date"] = "time_x()"
export_types["obj_desc_time_x::is_future"] = "bool(time_x)"
Expand Down Expand Up @@ -146,6 +150,7 @@ BEGIN {
export_types["::open_info_win_at"] = "bool(string)"
export_types["gui::add_message_at"] = "void(string, coord)"
export_types["gui::add_message"] = "string(player_x, string)"
export_types["halt_x::is_valid"] = "bool()"
export_types["halt_x::get_name"] = "string()"
export_types["halt_x::set_name"] = "string(string)"
export_types["halt_x::get_owner"] = "player_x()"
Expand Down Expand Up @@ -182,6 +187,7 @@ BEGIN {
export_types["line_x::get_waytype"] = "way_types()"
export_types["line_x::change_schedule"] = "string(player_x, schedule_x)"
export_types["line_x::destroy"] = "string(player_x)"
export_types["map_object_x::is_valid"] = "bool()"
export_types["map_object_x::get_owner"] = "player_x()"
export_types["map_object_x::get_name"] = "string()"
export_types["map_object_x::get_waytype"] = "way_types()"
Expand Down Expand Up @@ -217,6 +223,7 @@ BEGIN {
export_types["label_x::set_text"] = "string(string)"
export_types["label_x::get_text"] = "string()"
export_types["sign_x::get_desc"] = "sign_desc_x()"
export_types["player_x::is_valid"] = "bool()"
export_types["player_x::get_headquarter_level"] = "integer()"
export_types["player_x::get_headquarter_pos"] = "coord()"
export_types["player_x::get_name"] = "string()"
Expand Down Expand Up @@ -286,6 +293,7 @@ BEGIN {
export_types["::coord_to_string"] = "string(coord)"
export_types["::coord3d_to_string"] = "string(coord3d)"
export_types["::get_month_name"] = "string(integer)"
export_types["tile_x::is_valid"] = "bool()"
export_types["tile_x::find_object"] = "map_object_x(map_objects)"
export_types["tile_x::remove_object"] = "string(player_x, map_objects)"
export_types["tile_x::get_halt"] = "halt_x()"
Expand All @@ -301,6 +309,7 @@ BEGIN {
export_types["tile_x::has_two_ways"] = "bool()"
export_types["tile_x::get_neighbour"] = "tile_x(way_types, dir)"
export_types["tile_x::can_remove_all_objects"] = "string(player_x)"
export_types["square_x::is_valid"] = "bool()"
export_types["square_x::get_halt"] = "halt_x()"
export_types["square_x::get_player_halt"] = "halt_x(player_x)"
export_types["square_x::get_tile_at_height"] = "tile_x(integer)"
Expand Down
15 changes: 15 additions & 0 deletions script/api_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,5 +752,20 @@ namespace script_api {
};


/**
* Exports function to check whether pointer to in-game object is not null
*/
template<class P> SQInteger is_ptr_valid(HSQUIRRELVM vm)
{
P ptr = param<P>::get(vm, 1);
sq_pushbool(vm, ptr != NULL);
return 1;
}
template<class P> void export_is_valid(HSQUIRRELVM vm)
{
register_function(vm, is_ptr_valid<P>, "is_valid", 1, param<P>::typemask());
log_squirrel_type(param<P>::squirrel_type(), "is_valid", "bool()");
}

}; // end of namespace
#endif

0 comments on commit fffc8ac

Please sign in to comment.