<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40355 >
Attached patch adds functionality to find role unit from lua-script. This is used to properly spawn "Hut" and "HutTech" units from huts. - ML
diff -Nurd -X.diff_ignore freeciv/data/default/script.lua freeciv/data/default/script.lua --- freeciv/data/default/script.lua 2008-06-24 23:11:16.000000000 +0300 +++ freeciv/data/default/script.lua 2008-07-02 22:22:53.000000000 +0300 @@ -39,12 +39,14 @@ -- Get a mercenary unit from entering a hut. function hut_get_mercenaries(unit) - -- FIXME: Should be able to find other types of mercenaries - local type = find.unit_type('Legion') + local owner = unit.owner + local type = find.role_unit_type('HutTech', owner) + + if not type then + type = find.role_unit_type('Hut', nil) + end if type then - local owner = unit.owner - notify.event(owner, unit.tile, E.HUT_MERC, _("A band of friendly mercenaries joins your cause.")) create_unit(owner, unit.tile, type, 0, unit:get_homecity(), -1) @@ -57,7 +59,7 @@ -- Get new city from hut, or settlers (nomads) if terrain is poor. function hut_get_city(unit) local owner = unit.owner - local settlers = find.unit_type('Settlers') + local settlers = find.role_unit_type('Settlers', owner) if unit:is_on_possible_city_tile() then create_city(owner, unit.tile, "") diff -Nurd -X.diff_ignore freeciv/server/scripting/api_find.c freeciv/server/scripting/api_find.c --- freeciv/server/scripting/api_find.c 2008-03-09 20:11:24.000000000 +0200 +++ freeciv/server/scripting/api_find.c 2008-07-02 22:04:55.000000000 +0300 @@ -52,6 +52,24 @@ } } +/************************************************************************** + Return a unit type for given role. +**************************************************************************/ +Unit_Type *api_find_role_unit_type(const char *role_name, Player *pplayer) +{ + enum unit_role_id role = find_unit_role_by_rule_name(role_name); + + if (role == L_LAST) { + return NULL; + } + + if (pplayer) { + return best_role_unit_for_player(pplayer, role); + } else { + return get_role_unit(role, 0); + } +} + /************************************************************************** Return the tile at the given native coordinates. **************************************************************************/ diff -Nurd -X.diff_ignore freeciv/server/scripting/api_find.h freeciv/server/scripting/api_find.h --- freeciv/server/scripting/api_find.h 2008-03-09 20:11:24.000000000 +0200 +++ freeciv/server/scripting/api_find.h 2008-07-02 22:04:21.000000000 +0300 @@ -30,6 +30,7 @@ Building_Type *api_find_building_type_by_name(const char *name_orig); Unit_Type *api_find_unit_type(int unit_type_id); Unit_Type *api_find_unit_type_by_name(const char *name_orig); +Unit_Type *api_find_role_unit_type(const char *role_name, Player *pplayer); Tech_Type *api_find_tech_type(int tech_type_id); Tech_Type *api_find_tech_type_by_name(const char *name_orig); Terrain *api_find_terrain(int terrain_id); diff -Nurd -X.diff_ignore freeciv/server/scripting/api.pkg freeciv/server/scripting/api.pkg --- freeciv/server/scripting/api.pkg 2008-06-24 23:11:12.000000000 +0300 +++ freeciv/server/scripting/api.pkg 2008-07-02 22:13:09.000000000 +0300 @@ -301,6 +301,8 @@ @ unit_type (const char *name_orig); Unit_Type *api_find_unit_type @ unit_type (int unit_type_id); + Unit_Type *api_find_role_unit_type + @ role_unit_type (const char *role_name, Player *pplayer); Tech_Type *api_find_tech_type_by_name @ tech_type (const char *name_orig); Tech_Type *api_find_tech_type
_______________________________________________ Freeciv-dev mailing list Freeciv-dev@gna.org https://mail.gna.org/listinfo/freeciv-dev