[Freeciv-Dev] (PR#17187) Move hut code into lua script

2007-07-15 Thread Ulrik Sverdrup

URL: http://bugs.freeciv.org/Ticket/Display.html?id=17187 

2007/7/14, Ulrik Sverdrup [EMAIL PROTECTED]:

 URL: http://bugs.freeciv.org/Ticket/Display.html?id=17187 

 Here are the three patches for this ticket and its two children:
 #39450: Make unit_type etc names available in the scripting api
   api_class_names_v3.diff
 #39451: Add signal emit to scripting api
   techsig_v1.diff
 #17187: Move hut code into lua script
   hut_v3.diff


 Hut changes:
 Ported hut_get_barbarian as noted in previous emails. unleash_barbarians
 is available as an api action, as noted before.

 Introduced hut_get_limited for ai players with handicap H_LIMITEDHUTS;
 we cannot put this in the script file without passing irrelevant
 parameters to the hut_entered signal callback, or opening ai handicaps
 etc to the script api.

 hut_get_limited is a simplified version of the previous behavior; 25
 gold is given as before, but with 1/12 chance the unit is simply wiped
 (normally unleash_barbarians) would be called; but as the wipe is the
 most common outcome this was simplified.

I have here a patch to simplify: unit_enter_hut and move_unit should
return void, not bool. Currently move_unit returns bool, but the
returned value has no point; as stated in the function documentation
checks should be done by the caller, which they also are. The change
to return void has no side effects to current users, previously it did
semantically wrong things, for example;
if a paratrooper landed on a tile, wakes its neighbors and is killed
by auto-attack, the paradrop is reported internally as FALSE, not
successful. However this is ignored in the code and does not show
ofcourse.

The attached patch applies on top of the three previous.

 One hut issue remains: Only Legions are given as mercenaries. This can
 be solved by porting a method to find a unittype for role, or simplified
 now that different rulesets can customize by script.

We should probably port find_a_unit_type to find units by role to the
scripting api.

diff --git a/server/maphand.c b/server/maphand.c
index d065ab7..4979edd 100644
--- a/server/maphand.c
+++ b/server/maphand.c
@@ -1526,7 +1526,7 @@ static void bounce_units_on_terrain_change(struct tile *ptile)
 			   punit-tile, E_UNIT_RELOCATED,
 			   _(Moved your %s due to changing terrain.),
 			   unit_name_translation(punit));
-	  (void) move_unit(punit, ptile2, 0);
+	  move_unit(punit, ptile2, 0);
 	  if (punit-activity == ACTIVITY_SENTRY) {
 	handle_unit_activity_request(punit, ACTIVITY_IDLE);
 	  }
diff --git a/server/unithand.c b/server/unithand.c
index 32ba567..f46fcf6 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -1180,7 +1180,7 @@ bool handle_unit_move_request(struct unit *punit, struct tile *pdesttile,
   if (can_unit_move_to_tile_with_notify(punit, pdesttile, igzoc)) {
 int move_cost = map_move_cost_unit(punit, pdesttile);
 
-(void) move_unit(punit, pdesttile, move_cost);
+move_unit(punit, pdesttile, move_cost);
 
 return TRUE;
   } else {
diff --git a/server/unittools.c b/server/unittools.c
index 48965eb..6f54a10 100644
--- a/server/unittools.c
+++ b/server/unittools.c
@@ -1095,7 +1095,8 @@ bool teleport_unit_to_city(struct unit *punit, struct city *pcity,
 
 if (move_cost == -1)
   move_cost = punit-moves_left;
-return move_unit(punit, dst_tile, move_cost);
+move_unit(punit, dst_tile, move_cost);
+return TRUE;
   }
   return FALSE;
 }
@@ -2138,7 +2139,7 @@ bool do_airline(struct unit *punit, struct city *city2)
 		   _(%s transported succesfully.),
 		   unit_name_translation(punit));
 
-  (void) move_unit(punit, city2-tile, punit-moves_left);
+  move_unit(punit, city2-tile, punit-moves_left);
 
   /* airlift fields have changed. */
   send_city_info(city_owner(city1), city1);
@@ -2237,7 +2238,8 @@ bool do_paradrop(struct unit *punit, struct tile *ptile)
   {
 int move_cost = unit_type(punit)-paratroopers_mr_sub;
 punit-paradropped = TRUE;
-return move_unit(punit, ptile, move_cost);
+move_unit(punit, ptile, move_cost);
+return TRUE;
   }
 }
 
@@ -2273,18 +2275,16 @@ static bool hut_get_limited(struct unit *punit)
 }
 
 /**
-  Return FALSE if unit is known killed, TRUE means no information.
-  This is due to the effects in the script signal callback can not
-  be predicted.
+  Due to the effects in the scripted hut behavior can not be predicted,
+  unit_enter_hut returns nothing.
 **/
-static bool unit_enter_hut(struct unit *punit)
+static void unit_enter_hut(struct unit *punit)
 {
   struct player *pplayer = unit_owner(punit);
-  bool ok = TRUE;
   enum hut_behavior behavior = unit_class(punit)-hut_behavior;
   
   if (behavior == HUT_NOTHING) {
-return ok;
+return;
   }
 
   tile_clear_special(punit-tile, S_HUT);
@@ -2294,18 +2294,19 @@ static bool unit_enter_hut(struct unit *punit)
 

[Freeciv-Dev] (PR#17187) Move hut code into lua script

2007-07-14 Thread Ulrik Sverdrup

URL: http://bugs.freeciv.org/Ticket/Display.html?id=17187 

Here are the three patches for this ticket and its two children:
#39450: Make unit_type etc names available in the scripting api
  api_class_names_v3.diff
#39451: Add signal emit to scripting api
  techsig_v1.diff
#17187: Move hut code into lua script
  hut_v3.diff

Applies to trunk r13103
They are interdependent and should be applied in from top to bottom. I
used git to sort out the interlocking patches, apply to test with
cat ../api_class_names_v3.diff ../techsig_v1.diff ../hut_v3.diff  |
patch -p1
The patches can and should be checked in in succession, none of them
break the build.

Name api changes:
Now we have:
:rule_name()
:name_translation()
and for Nation_Type there is also:
:plural_translation()

Techsig changes:
the api action give_technology now takes a reason string as well; a
script signal with
the given reason is sent; it was decided in #39451 that the signal
events are so fundamental that scripts should not directly emit them.

Hut changes:
Ported hut_get_barbarian as noted in previous emails. unleash_barbarians
is available as an api action, as noted before.

Introduced hut_get_limited for ai players with handicap H_LIMITEDHUTS;
we cannot put this in the script file without passing irrelevant
parameters to the hut_entered signal callback, or opening ai handicaps
etc to the script api.

hut_get_limited is a simplified version of the previous behavior; 25
gold is given as before, but with 1/12 chance the unit is simply wiped
(normally unleash_barbarians) would be called; but as the wipe is the
most common outcome this was simplified.

One hut issue remains: Only Legions are given as mercenaries. This can
be solved by porting a method to find a unittype for role, or simplified
now that different rulesets can customize by script. 

Ready for comments
diff --git a/server/scripting/api.pkg b/server/scripting/api.pkg
index 6f0af93..d8a5986 100644
--- a/server/scripting/api.pkg
+++ b/server/scripting/api.pkg
@@ -94,16 +94,28 @@ struct Terrain {
 
 
 /* Class methods. */
+
+/* Player */
 int api_methods_player_num_cities
 	@ methods_player_num_cities (Player *pplayer);
 int api_methods_player_num_units
 	@ methods_player_num_units (Player *pplayer);
 
-bool api_methods_unit_type_has_flag
-	@ methods_unit_type_has_flag (Unit_Type *punit_type, const char *flag);
-bool api_methods_unit_type_has_role
-	@ methods_unit_type_has_role (Unit_Type *punit_type, const char *role);
-
+/* Government */
+const char *api_methods_government_rule_name
+	@ methods_government_rule_name (Government *pgovernment);
+const char *api_methods_government_name_translation
+	@ methods_government_name_translation (Government *pgovernment);
+
+/* Nation_Type */
+const char *api_methods_nation_type_rule_name
+	@ methods_nation_type_rule_name (Nation_Type *pnation);
+const char *api_methods_nation_type_name_translation
+	@ methods_nation_type_name_translation (Nation_Type *pnation);
+const char *api_methods_nation_type_plural_translation
+	@ methods_nation_type_plural_translation (Nation_Type *pnation);
+
+/* Building_Type */
 bool api_methods_building_type_is_wonder
 	@ methods_building_type_is_wonder (Building_Type *pbuilding);
 bool api_methods_building_type_is_great_wonder
@@ -112,7 +124,32 @@ bool api_methods_building_type_is_small_wonder
 	@ methods_building_type_is_small_wonder (Building_Type *pbuilding);
 bool api_methods_building_type_is_improvement
 	@ methods_building_type_is_improvement (Building_Type *pbuilding);
+const char *api_methods_building_type_rule_name
+	@ methods_building_type_rule_name (Building_Type *pbuilding);
+const char *api_methods_building_type_name_translation
+	@ methods_building_type_name_translation (Building_Type *pbuilding);
 
+/* Unit_Type */
+bool api_methods_unit_type_has_flag
+	@ methods_unit_type_has_flag (Unit_Type *punit_type, const char *flag);
+bool api_methods_unit_type_has_role
+	@ methods_unit_type_has_role (Unit_Type *punit_type, const char *role);
+const char *api_methods_unit_type_rule_name
+	@ methods_unit_type_rule_name (Unit_Type *punit_type);
+const char *api_methods_unit_type_name_translation
+	@ methods_unit_type_name_translation (Unit_Type *punit_type);
+
+/* Tech_Type */
+const char *api_methods_tech_type_rule_name
+	@ methods_tech_type_rule_name (Tech_Type *ptech);
+const char *api_methods_tech_type_name_translation
+	@ methods_tech_type_name_translation (Tech_Type *ptech);
+
+/* Terrain */
+const char *api_methods_terrain_rule_name
+	@ methods_terrain_rule_name (Terrain *pterrain);
+const char *api_methods_terrain_name_translation
+	@ methods_terrain_name_translation (Terrain *pterrain);
 
 $[
 -- Player methods.
@@ -133,6 +170,28 @@ function Unit:homecity()
   return find.city(self.owner, self.homecity_id)
 end
 
+-- Government methods
+function Government:rule_name()
+  return methods_government_rule_name(self)
+end
+
+function Government:name_translation()
+  return methods_government_name_translation(self)

[Freeciv-Dev] (PR#17187) Move hut code into lua script

2007-07-10 Thread Ulrik Sverdrup

URL: http://bugs.freeciv.org/Ticket/Display.html?id=17187 

Updated the patch to the latest revision trunk. 

The patch is almost ready but depends on #39450: Make rule names
available in the scripting api; right now there is no way to tell the
user Your %s was killed by barbarians

Expanded the patch slightly by porting hut_get_barbarians to Lua, using
a new method Tile:city_exists_within_city_radius and a new action
barbarian_uprising(tile)

Added a copyright header to the script.lua file since it now has a
substantial amount of code; I hope that it is correct.
Index: server/scripting/api_actions.h
===
--- server/scripting/api_actions.h	(revision 13086)
+++ server/scripting/api_actions.h	(arbetskopia)
@@ -16,6 +16,7 @@
 
 #include api_types.h
 
+bool api_unleash_barbarians(Tile *ptile);
 Unit *api_actions_create_unit(Player *pplayer, Tile *ptile, Unit_Type *ptype,
 		  	  int veteran_level, City *homecity,
 			  int moves_left);
Index: server/scripting/api_methods.h
===
--- server/scripting/api_methods.h	(revision 13086)
+++ server/scripting/api_methods.h	(arbetskopia)
@@ -16,12 +16,17 @@
 
 #include api_types.h
 
+bool api_methods_unit_city_can_be_built_here(Unit *punit);
+
 int api_methods_player_num_cities(Player *pplayer);
 int api_methods_player_num_units(Player *pplayer);
 
 bool api_methods_unit_type_has_flag(Unit_Type *punit_type, const char *flag);
 bool api_methods_unit_type_has_role(Unit_Type *punit_type, const char *role);
 
+bool api_methods_tile_city_exists_within_city_radius(Tile *ptile, 
+ bool may_be_on_center);
+
 bool api_methods_building_type_is_wonder(Building_Type *pbuilding);
 bool api_methods_building_type_is_great_wonder(Building_Type *pbuilding);
 bool api_methods_building_type_is_small_wonder(Building_Type *pbuilding);
Index: server/scripting/api.pkg
===
--- server/scripting/api.pkg	(revision 13086)
+++ server/scripting/api.pkg	(arbetskopia)
@@ -94,6 +94,7 @@
 
 
 /* Class methods. */
+
 int api_methods_player_num_cities
 	@ methods_player_num_cities (Player *pplayer);
 int api_methods_player_num_units
@@ -103,7 +104,12 @@
 	@ methods_unit_type_has_flag (Unit_Type *punit_type, const char *flag);
 bool api_methods_unit_type_has_role
 	@ methods_unit_type_has_role (Unit_Type *punit_type, const char *role);
+bool api_methods_unit_city_can_be_built_here
+	@ methods_unit_city_can_be_built_here (Unit *punit);
 
+bool api_methods_tile_city_exists_within_city_radius
+	@ methods_tile_city_exists_within_city_radius (Tile *ptile, bool center);
+
 bool api_methods_building_type_is_wonder
 	@ methods_building_type_is_wonder (Building_Type *pbuilding);
 bool api_methods_building_type_is_great_wonder
@@ -133,6 +139,15 @@
   return find.city(self.owner, self.homecity_id)
 end
 
+function Unit:is_on_possible_city_tile()
+  return methods_unit_city_can_be_built_here(self)
+end
+
+-- Tile methods
+function Tile:city_exists_within_city_radius(center)
+  return methods_tile_city_exists_within_city_radius(self, center)
+end
+
 -- Building_Type methods.
 function Building_Type:build_shield_cost()
   return self.build_cost
@@ -402,4 +417,4 @@
 void api_actions_change_gold @ change_gold (Player *pplayer, int amount);
 Tech_Type *api_actions_give_technology @ give_technology (Player *pplayer,
 		Tech_Type *ptech);
-
+bool api_unleash_barbarians @ unleash_barbarians (Tile *ptile);
Index: server/scripting/api_actions.c
===
--- server/scripting/api_actions.c	(revision 13086)
+++ server/scripting/api_actions.c	(arbetskopia)
@@ -15,6 +15,7 @@
 #include config.h
 #endif
 
+#include barbarian.h
 #include plrhand.h
 #include citytools.h
 #include techtools.h
@@ -26,6 +27,14 @@
 
 
 /**
+  Unleash barbarians on a tile, for example from a hut
+**/
+bool api_unleash_barbarians(Tile *ptile)
+{
+  return unleash_barbarians(ptile);
+}
+
+/**
   Create a new unit.
 **/
 Unit *api_actions_create_unit(Player *pplayer, Tile *ptile, Unit_Type *ptype,
Index: server/scripting/api_methods.c
===
--- server/scripting/api_methods.c	(revision 13086)
+++ server/scripting/api_methods.c	(arbetskopia)
@@ -21,6 +21,14 @@
 #include script.h
 
 /**
+  Can punit found a city on its tile?
+**/
+bool