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

 Attached patch adds support to iterate though players, cities, and
units to lua scripting.

 I decided not to use iterator objects with all the memory handling
they would require. Instead, script can call
iterate_[players|cities|units]() and that emits script signal for each
item of the type. Two parameters is passed through all iterate APIs:
"type" is meant to be used as identifier of the iteration reason in
case same script wants to iterate same thing in several places.
"param" is meant to be used as generic parameter.


 - ML

diff -Nurd -X.diff_ignore freeciv/server/scripting/api_iteration.c 
freeciv/server/scripting/api_iteration.c
--- freeciv/server/scripting/api_iteration.c    1970-01-01 02:00:00.000000000 
+0200
+++ freeciv/server/scripting/api_iteration.c    2009-01-08 21:56:19.000000000 
+0200
@@ -0,0 +1,80 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 2005 - The Freeciv Project
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+/* common */
+#include "player.h"
+#include "unit.h"
+
+/* scripting */
+#include "api_iteration.h"
+#include "script_signal.h"
+
+
+/**************************************************************************
+  Iterate through all players.
+**************************************************************************/
+void api_iterate_players(const char *type, const char *param)
+{
+  players_iterate(pplayer) {
+    script_signal_emit("player_iterated", 3,
+                       API_TYPE_PLAYER, pplayer,
+                       API_TYPE_STRING, type,
+                       API_TYPE_STRING, param);
+  } players_iterate_end;
+}
+
+/**************************************************************************
+  Iterate through all player cities.
+**************************************************************************/
+void api_iterate_cities(Player *pplayer, const const char *type, const char 
*param)
+{
+  city_list_iterate_safe(pplayer->cities, pcity) {
+    script_signal_emit("city_iterated", 4,
+                       API_TYPE_PLAYER, pplayer,
+                       API_TYPE_CITY, pcity,
+                       API_TYPE_STRING, type,
+                       API_TYPE_STRING, param);
+  } city_list_iterate_safe_end;
+}
+
+/**************************************************************************
+  Iterate through all player units.
+**************************************************************************/
+void api_iterate_units(Player *pplayer, const const char *type, const char 
*param)
+{
+  unit_list_iterate_safe(pplayer->units, punit) {
+    script_signal_emit("unit_iterated", 4,
+                       API_TYPE_PLAYER, pplayer,
+                       API_TYPE_UNIT, punit,
+                       API_TYPE_STRING, type,
+                       API_TYPE_STRING, param);
+  } unit_list_iterate_safe_end;
+}
+
+/**************************************************************************
+  Iterate through all tile units.
+**************************************************************************/
+void api_iterate_tile_units(Tile *ptile, const const char *type, const char 
*param)
+{
+  unit_list_iterate_safe(ptile->units, punit) {
+    script_signal_emit("tile_unit_iterated", 4,
+                       API_TYPE_TILE, ptile,
+                       API_TYPE_UNIT, punit,
+                       API_TYPE_STRING, type,
+                       API_TYPE_STRING, param);
+  } unit_list_iterate_safe_end;
+}
diff -Nurd -X.diff_ignore freeciv/server/scripting/api_iteration.h 
freeciv/server/scripting/api_iteration.h
--- freeciv/server/scripting/api_iteration.h    1970-01-01 02:00:00.000000000 
+0200
+++ freeciv/server/scripting/api_iteration.h    2009-01-08 21:46:05.000000000 
+0200
@@ -0,0 +1,25 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 2005 - The Freeciv Project
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__API_ITERATION_H
+#define FC__API_ITERATION_H
+
+#include "api_types.h"
+
+/* Iteration module. */
+void api_iterate_players(const char *type, const char *param);
+void api_iterate_cities(Player *pplayer, const char *type, const char *param);
+void api_iterate_units(Player *pplayer, const char *type, const char *param);
+void api_iterate_tile_units(Tile *ptile, const char *type, const char *param);
+
+#endif /* FC__API_ITERATION_H */
diff -Nurd -X.diff_ignore freeciv/server/scripting/api.pkg 
freeciv/server/scripting/api.pkg
--- freeciv/server/scripting/api.pkg    2008-10-27 04:13:34.000000000 +0200
+++ freeciv/server/scripting/api.pkg    2009-01-08 21:49:42.000000000 +0200
@@ -19,6 +19,7 @@
 $#include "api_actions.h"
 $#include "api_find.h"
 $#include "api_intl.h"
+$#include "api_iteration.h"
 $#include "api_methods.h"
 $#include "api_notify.h"
 $#include "api_utilities.h"
@@ -101,6 +102,16 @@
 int api_methods_player_num_units
        @ methods_player_num_units (Player *pplayer);
 
+/* Iteration */
+void api_iterate_players
+        @ iterate_players (const char *type, const char *param);
+void api_iterate_cities
+        @ iterate_cities (Player *pplayer, const char *type, const char 
*param);
+void api_iterate_units
+        @ iterate_units (Player *pplayer, const char *type, const char *param);
+void api_iterate_tile_units
+        @ iterate_tile_units (Tile *ptile, const char *type, const char 
*param);
+
 /* Unit */
 bool api_methods_unit_city_can_be_built_here
        @ methods_unit_city_can_be_built_here (Unit *punit);
diff -Nurd -X.diff_ignore freeciv/server/scripting/Makefile.am 
freeciv/server/scripting/Makefile.am
--- freeciv/server/scripting/Makefile.am        2008-10-27 04:13:34.000000000 
+0200
+++ freeciv/server/scripting/Makefile.am        2009-01-08 21:46:23.000000000 
+0200
@@ -17,6 +17,8 @@
                api_gen.h       \
                api_intl.c      \
                api_intl.h      \
+               api_iteration.c \
+               api_iteration.h \
                api_methods.c   \
                api_methods.h   \
                api_notify.c    \
diff -Nurd -X.diff_ignore freeciv/server/scripting/script_signal.c 
freeciv/server/scripting/script_signal.c
--- freeciv/server/scripting/script_signal.c    2008-11-04 16:19:09.000000000 
+0200
+++ freeciv/server/scripting/script_signal.c    2009-01-08 21:53:16.000000000 
+0200
@@ -160,6 +160,15 @@
   script_signal_create("hut_enter", 1, API_TYPE_UNIT);
 
   script_signal_create("unit_lost", 2, API_TYPE_UNIT, API_TYPE_PLAYER);
+
+  script_signal_create("player_iterated", 3, API_TYPE_PLAYER,
+                       API_TYPE_STRING, API_TYPE_STRING);
+  script_signal_create("city_iterated", 4, API_TYPE_PLAYER, API_TYPE_CITY,
+                       API_TYPE_STRING, API_TYPE_STRING);
+  script_signal_create("unit_iterated", 4, API_TYPE_PLAYER, API_TYPE_UNIT,
+                       API_TYPE_STRING, API_TYPE_STRING);
+  script_signal_create("tile_unit_iterated", 4, API_TYPE_TILE, API_TYPE_UNIT,
+                       API_TYPE_STRING, API_TYPE_STRING);
 }
 
 /**************************************************************************
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to