Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2006:
[Freeciv-Dev] (PR#17187) Move hut code into lua script
Home

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

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#17187) Move hut code into lua script
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Sat, 20 May 2006 05:36:50 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch moves the hut related code into lua script, so that it can be 
modified by modpack authors.

To accomplish this, I added the following new API functionality:

   barbarian_hut(Unit *punit)
   Unit:is_on_possible_city_tile()

the former is a wrapper around the barbarian code for huts, which I moved 
into barbarian.c; the latter a wrapper around city_can_be_built_here().

It does not depend on any saved state.

   - Per
Index: server/scripting/api_actions.h
===================================================================
--- server/scripting/api_actions.h      (revision 11947)
+++ server/scripting/api_actions.h      (working copy)
@@ -16,6 +16,7 @@
 
 #include "api_types.h"
 
+bool api_actions_barbarian_hut(Unit *punit);
 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 11947)
+++ server/scripting/api_methods.h      (working copy)
@@ -16,6 +16,8 @@
 
 #include "api_types.h"
 
+bool api_methods_tile_city_can_be_built_here(Unit *punit);
+
 int api_methods_player_num_cities(Player *pplayer);
 int api_methods_player_num_units(Player *pplayer);
 
Index: server/scripting/api.pkg
===================================================================
--- server/scripting/api.pkg    (revision 11947)
+++ server/scripting/api.pkg    (working copy)
@@ -124,8 +124,15 @@
 bool api_methods_building_type_is_improvement
        @ methods_building_type_is_improvement (Building_Type *pbuilding);
 
+bool api_methods_tile_city_can_be_built_here
+        @ methods_tile_city_can_be_built_here (Unit *punit);
 
 $[
+-- Unit methods.
+function Unit:is_on_possible_city_tile()
+  return methods_tile_city_can_be_built_here(self)
+end
+
 -- Player methods.
 function Player:is_human()
   return not self.ai.control
@@ -413,4 +420,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_actions_barbarian_hut @ barbarian_hut (Unit *punit);
Index: server/scripting/api_actions.c
===================================================================
--- server/scripting/api_actions.c      (revision 11984)
+++ server/scripting/api_actions.c      (working copy)
@@ -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 from hut.
+**************************************************************************/
+bool api_actions_barbarian_hut(Unit *punit)
+{
+  return hut_get_barbarians(punit);
+}
+
+/**************************************************************************
   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 11947)
+++ server/scripting/api_methods.c      (working copy)
@@ -21,6 +21,14 @@
 #include "script.h"
 
 /**************************************************************************
+  Is a unit on a tile that can support a city?
+**************************************************************************/
+bool api_methods_tile_city_can_be_built_here(Unit *punit)
+{
+  return city_can_be_built_here(punit->tile, punit);
+}
+
+/**************************************************************************
   Return the number of cities pplayer has.
 **************************************************************************/
 int api_methods_player_num_cities(Player *pplayer)
Index: server/barbarian.c
===================================================================
--- server/barbarian.c  (revision 11947)
+++ server/barbarian.c  (working copy)
@@ -57,14 +57,38 @@
 #define BARBARIAN_INITIAL_VISION_RADIUS 3
 #define BARBARIAN_INITIAL_VISION_RADIUS_SQ 9
 
-/*
- IDEAS:
- 1. Unrest factors configurable via rulesets (distance and gov factor)
- 2. Separate nations for Sea Raiders and Land Barbarians
+/**************************************************************************
+  Get barbarians from hut, unless close to a city.
+  Unit may die: returns 1 if unit is alive after, or 0 if it was killed.
+**************************************************************************/
+bool hut_get_barbarians(struct unit *punit)
+{
+  struct player *pplayer = unit_owner(punit);
+  bool ok = TRUE;
 
- - are these good ideas ??????
-*/
+  if (city_exists_within_city_radius(punit->tile, TRUE)
+      || unit_flag(punit, F_GAMELOSS)) {
+    notify_player(pplayer, punit->tile, E_HUT_BARB_CITY_NEAR,
+                     _("An abandoned village is here."));
+  } else {
+    /* save coords and type in case unit dies */
+    struct tile *unit_tile = punit->tile;
+    struct unit_type *type = punit->type;
 
+    ok = unleash_barbarians(unit_tile);
+
+    if (ok) {
+      notify_player(pplayer, unit_tile, E_HUT_BARB,
+                       _("You have unleashed a horde of barbarians!"));
+    } else {
+      notify_player(pplayer, unit_tile, E_HUT_BARB_KILLED,
+                       _("Your %s has been killed by barbarians!"),
+                       unit_name(type));
+    }
+  }
+  return ok;
+}
+
 /**************************************************************************
   Is player a land barbarian?
 **************************************************************************/
Index: server/barbarian.h
===================================================================
--- server/barbarian.h  (revision 11947)
+++ server/barbarian.h  (working copy)
@@ -31,5 +31,6 @@
 bool unleash_barbarians(struct tile *ptile);
 void summon_barbarians(void);
 bool is_land_barbarian(struct player *pplayer);
+bool hut_get_barbarians(struct unit *punit);
 
 #endif  /* FC__BARBARIAN_H */
Index: server/unittools.c
===================================================================
--- server/unittools.c  (revision 11947)
+++ server/unittools.c  (working copy)
@@ -2122,109 +2122,6 @@
 }
 
 /**************************************************************************
-  Get gold from entering a hut.
-**************************************************************************/
-static void hut_get_gold(struct unit *punit, int cred)
-{
-  struct player *pplayer = unit_owner(punit);
-  notify_player(pplayer, punit->tile, E_HUT_GOLD,
-                  _("You found %d gold."), cred);
-  pplayer->economic.gold += cred;
-}
-
-/**************************************************************************
-  Get a tech from entering a hut.
-**************************************************************************/
-static void hut_get_tech(struct unit *punit)
-{
-  struct player *pplayer = unit_owner(punit);
-  Tech_type_id new_tech;
-  const char* tech_name;
-  
-  new_tech = give_random_free_tech(pplayer);
-  
-  tech_name = get_tech_name(pplayer, new_tech);
-  notify_player(pplayer, punit->tile, E_HUT_TECH,
-                  _("You found %s in ancient scrolls of wisdom."),
-                  tech_name);
-  script_signal_emit("tech_researched", 3,
-                    API_TYPE_TECH_TYPE, &advances[new_tech],
-                    API_TYPE_PLAYER, pplayer,
-                    API_TYPE_STRING, "hut");
-  notify_embassies(pplayer, NULL, NULL, E_TECH_GAIN,
-                  _("The %s have acquired %s"
-                    " from ancient scrolls of wisdom."),
-                  get_nation_name_plural(pplayer->nation), tech_name);
-}
-
-/**************************************************************************
-  Get a mercenary unit from entering a hut.
-**************************************************************************/
-static void hut_get_mercenaries(struct unit *punit)
-{
-  struct player *pplayer = unit_owner(punit);
-  
-  notify_player(pplayer, punit->tile, E_HUT_MERC,
-                  _("A band of friendly mercenaries joins your cause."));
-  (void) create_unit(pplayer, punit->tile,
-                    find_a_unit_type(L_HUT, L_HUT_TECH), FALSE,
-                    punit->homecity, -1);
-}
-
-/**************************************************************************
-  Get barbarians from hut, unless close to a city.
-  Unit may die: returns 1 if unit is alive after, or 0 if it was killed.
-**************************************************************************/
-static bool hut_get_barbarians(struct unit *punit)
-{
-  struct player *pplayer = unit_owner(punit);
-  bool ok = TRUE;
-
-  if (city_exists_within_city_radius(punit->tile, TRUE)
-      || unit_flag(punit, F_GAMELOSS)) {
-    notify_player(pplayer, punit->tile, E_HUT_BARB_CITY_NEAR,
-                    _("An abandoned village is here."));
-  } else {
-    /* save coords and type in case unit dies */
-    struct tile *unit_tile = punit->tile;
-    struct unit_type *type = punit->type;
-
-    ok = unleash_barbarians(unit_tile);
-
-    if (ok) {
-      notify_player(pplayer, unit_tile, E_HUT_BARB,
-                      _("You have unleashed a horde of barbarians!"));
-    } else {
-      notify_player(pplayer, unit_tile, E_HUT_BARB_KILLED,
-                      _("Your %s has been killed by barbarians!"),
-                      unit_name(type));
-    }
-  }
-  return ok;
-}
-
-/**************************************************************************
-  Get new city from hut, or settlers (nomads) if terrain is poor.
-**************************************************************************/
-static void hut_get_city(struct unit *punit)
-{
-  struct player *pplayer = unit_owner(punit);
-
-  if (city_can_be_built_here(punit->tile, punit)) {
-    notify_player(pplayer, punit->tile, E_HUT_CITY,
-                    _("You found a friendly city."));
-    create_city(pplayer, punit->tile,
-               city_name_suggestion(pplayer, punit->tile));
-  } else {
-    notify_player(pplayer, punit->tile, E_HUT_SETTLER,
-                    _("Friendly nomads are impressed by you,"
-                      " and join you."));
-    (void) create_unit(pplayer, punit->tile, get_role_unit(F_CITIES,0),
-               0, punit->homecity, -1);
-  }
-}
-
-/**************************************************************************
   Return 1 if unit is alive, and 0 if it was killed
 **************************************************************************/
 static bool unit_enter_hut(struct unit *punit)
@@ -2255,34 +2152,6 @@
 
   script_signal_emit("hut_enter", 1, API_TYPE_UNIT, punit);
 
-  switch (hut_chance) {
-  case 0:
-    hut_get_gold(punit, 25);
-    break;
-  case 1: case 2: case 3:
-    hut_get_gold(punit, 50);
-    break;
-  case 4:
-    hut_get_gold(punit, 100);
-    break;
-  case 5: case 6: case 7:
-    hut_get_tech(punit);
-    break;
-  case 8: case 9:
-    if (num_role_units(L_HUT) != 0) {
-      hut_get_mercenaries(punit);
-    } else {
-      hut_get_gold(punit, 25);
-    }
-    break;
-  case 10:
-    ok = hut_get_barbarians(punit);
-    break;
-  case 11:
-    hut_get_city(punit);
-    break;
-  }
-
   send_player_info(pplayer, pplayer);       /* eg, gold */
   return ok;
 }
Index: data/default/script.lua
===================================================================
--- data/default/script.lua     (revision 11947)
+++ data/default/script.lua     (working copy)
@@ -1,8 +1,81 @@
--- Callbacks
+function hut_get_gold(unit, gold)
+  local owner = unit.owner
+ 
+  notify.event(owner, unit.tile, E.HUT_GOLD, _('You found %d gold.'), gold)
+  change_gold(owner, gold)
+end
+
+function hut_get_tech(unit)
+  local owner = unit.owner
+  local tech = give_technology(owner, nil)
+
+  if tech then
+    notify.event(owner, unit.tile, E.HUT_TECH,
+                 _('You found %s in ancient scrolls of wisdom.'),
+                 tech.name)
+    notify.embassies(owner, unit.tile, E.HUT_TECH,
+                     _('The %s have acquired %s from ancient scrolls of 
wisdom.'),
+                     owner.nation.name_plural, tech.name)
+    return true
+  else
+    return false
+  end
+end
+ 
+function hut_get_mercenaries(unit)
+  local type = find.unit_type('Legion')
+
+  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:homecity(), -1)
+    return true
+  else
+    return false
+  end
+end
+ 
+function hut_get_city(unit)
+  local owner = unit.owner
+  local settlers = find.unit_type('Settlers')
+
+  if unit:is_on_possible_city_tile() then
+    create_city(owner, unit.tile, nil)
+    notify.event(owner, unit.tile, E.HUT_CITY,
+                 _('You found a friendly city.'))
+  else
+    create_unit(owner, unit.tile, settlers, 0, unit:homecity(), -1)
+    notify.event(owner, unit.tile, E.HUT_SETTLER,
+                 _('Friendly nomads are impressed by you, and join you.'))
+  end
+end
+
 function hut_enter_callback(unit)
-  -- to be implemented
+  local chance = random(0, 11)
+
+  if chance == 0 then
+    hut_get_gold(unit, 25)
+  elseif chance == 1 or chance == 2 or chance == 3 then
+    hut_get_gold(unit, 50)
+  elseif chance == 4 then
+    hut_get_gold(unit, 100)
+  elseif chance == 5 or chance == 6 or chance == 7 then
+    hut_get_tech(unit)
+  elseif chance == 8 or chance == 9 then
+    if not hut_get_mercenaries(unit) then
+      hut_get_gold(unit, 25)
+    end
+  elseif chance == 10 then
+    -- TODO: more of this function should be moved in here
+    barbarian_hut(unit)
+  elseif chance == 11 then
+    hut_get_city(unit)
+  end
+
+  -- continue processing.
+  return false
 end
 
--- Main code
 signal.connect("hut_enter", "hut_enter_callback")
-

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#17187) Move hut code into lua script, Per I. Mathisen <=