[Freeciv-Dev] (PR#13024) wanted: new script API types
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://bugs.freeciv.org/Ticket/Display.html?id=13024 >
The attached patch:
Adds these new API types (types you can pass as signal arguments):
API_TYPE_IMPR_TYPE,
API_TYPE_NATION_TYPE,
API_TYPE_UNIT_TYPE,
API_TYPE_TECH_TYPE,
API_TYPE_TERRAIN
Requested by Jason Dorje Short.
Index: server/scripting/script.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/scripting/script.c,v
retrieving revision 1.4
diff -u -u -r1.4 script.c
--- server/scripting/script.c 10 May 2005 01:48:56 -0000 1.4
+++ server/scripting/script.c 10 May 2005 16:08:53 -0000
@@ -60,16 +60,11 @@
}
/**************************************************************************
- Invoke the 'callback_name' Lua function.
+ Push callback arguments into the Lua stack.
**************************************************************************/
-bool script_callback_invoke(const char *callback_name,
- int nargs, va_list args)
+static void script_callback_push_args(int nargs, va_list args)
{
- int i, nres;
- bool res;
-
- /* The function name */
- lua_getglobal(state, callback_name);
+ int i;
for (i = 0; i < nargs; i++) {
int type;
@@ -101,40 +96,38 @@
tolua_pushstring(state, arg);
}
break;
- case API_TYPE_PLAYER:
- {
- Player *arg;
-
- arg = va_arg(args, Player*);
- tolua_pushusertype(state, (void*)arg, "Player");
- }
- break;
- case API_TYPE_CITY:
+ default:
{
- City *arg;
+ const char *name;
+ void *arg;
- arg = va_arg(args, City*);
- tolua_pushusertype(state, (void*)arg, "City");
- }
- break;
- case API_TYPE_UNIT:
- {
- Unit *arg;
+ name = get_api_type_name(type);
+ if (!name) {
+ assert(0);
+ return;
+ }
- arg = va_arg(args, Unit*);
- tolua_pushusertype(state, (void*)arg, "Unit");
- }
- break;
- case API_TYPE_TILE:
- {
- Tile *arg;
-
- arg = va_arg(args, Tile*);
- tolua_pushusertype(state, (void*)arg, "Tile");
+ arg = va_arg(args, void*);
+ tolua_pushusertype(state, arg, name);
}
break;
}
}
+}
+
+/**************************************************************************
+ Invoke the 'callback_name' Lua function.
+**************************************************************************/
+bool script_callback_invoke(const char *callback_name,
+ int nargs, va_list args)
+{
+ int nres;
+ bool res;
+
+ /* The function name */
+ lua_getglobal(state, callback_name);
+
+ script_callback_push_args(nargs, args);
/* Call the function with nargs arguments, return 1 results */
if (lua_pcall(state, nargs, 1, 0) != 0) {
Index: server/scripting/script_signal.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/scripting/script_signal.c,v
retrieving revision 1.4
diff -u -u -r1.4 script_signal.c
--- server/scripting/script_signal.c 10 May 2005 13:24:03 -0000 1.4
+++ server/scripting/script_signal.c 10 May 2005 16:08:53 -0000
@@ -42,6 +42,8 @@
#include <config.h>
#endif
+#include <assert.h>
+
#include "hash.h"
#include "log.h"
#include "registry.h"
@@ -78,16 +80,52 @@
};
/**************************************************************************
- Signal and callback state datastructure.
+ Signal datastructure.
**************************************************************************/
static struct hash_table *signals;
+/**************************************************************************
+ Api type names.
+**************************************************************************/
+static const char *api_type_names[] = {
+ NULL, NULL, NULL,
+
+ "Player", "City", "Unit", "Tile",
+
+ "Impr_Type", "Nation_Type", "Unit_Type", "Tech_Type", "Terrain"
+};
+
+/**************************************************************************
+ Api type names.
+**************************************************************************/
+const char *get_api_type_name(enum api_types id)
+{
+ if (id >= 0 && id < API_TYPE_LAST) {
+ return api_type_names[id];
+ } else {
+ return NULL;
+ }
+}
/**************************************************************************
- Prototypes.
+ Declare any new signal types you need here.
**************************************************************************/
-static void signals_create(void);
+static void signals_create(void)
+{
+ script_signal_create("turn_started", 2, API_TYPE_INT, API_TYPE_INT);
+ script_signal_create("unit_moved",
+ 3, API_TYPE_UNIT, API_TYPE_TILE, API_TYPE_TILE);
+ /* Includes all newly-built cities. */
+ script_signal_create("city_built", 1, API_TYPE_CITY);
+
+ script_signal_create("city_growth", 2, API_TYPE_CITY, API_TYPE_INT);
+
+ /* Only includes units built in cities, for now. */
+ script_signal_create("unit_built", 2, API_TYPE_UNIT, API_TYPE_CITY);
+
+ script_signal_create("hut_enter", 1, API_TYPE_UNIT);
+}
/**************************************************************************
Connects a callback function to a certain signal (internal).
@@ -280,6 +318,8 @@
if (!signals) {
signals = hash_new(hash_fval_string, hash_fcmp_string);
+ assert(ARRAY_SIZE(api_type_names) == API_TYPE_LAST);
+
signals_create();
}
}
@@ -302,23 +342,3 @@
signals = NULL;
}
}
-
-/**************************************************************************
- Declare any new signal types you need here.
-**************************************************************************/
-static void signals_create(void)
-{
- script_signal_create("turn_started", 2, API_TYPE_INT, API_TYPE_INT);
- script_signal_create("unit_moved",
- 3, API_TYPE_UNIT, API_TYPE_TILE, API_TYPE_TILE);
-
- /* Includes all newly-built cities. */
- script_signal_create("city_built", 1, API_TYPE_CITY);
-
- script_signal_create("city_growth", 2, API_TYPE_CITY, API_TYPE_INT);
-
- /* Only includes units built in cities, for now. */
- script_signal_create("unit_built", 2, API_TYPE_UNIT, API_TYPE_CITY);
-
- script_signal_create("hut_enter", 1, API_TYPE_UNIT);
-}
Index: server/scripting/script_signal.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/scripting/script_signal.h,v
retrieving revision 1.2
diff -u -u -r1.2 script_signal.h
--- server/scripting/script_signal.h 9 May 2005 21:24:48 -0000 1.2
+++ server/scripting/script_signal.h 10 May 2005 16:08:53 -0000
@@ -24,11 +24,21 @@
API_TYPE_PLAYER,
API_TYPE_CITY,
API_TYPE_UNIT,
- API_TYPE_TILE
+ API_TYPE_TILE,
+
+ API_TYPE_IMPR_TYPE,
+ API_TYPE_NATION_TYPE,
+ API_TYPE_UNIT_TYPE,
+ API_TYPE_TECH_TYPE,
+ API_TYPE_TERRAIN,
+
+ API_TYPE_LAST
};
struct section_file;
+const char *get_api_type_name(enum api_types id);
+
void script_signal_emit_valist(const char *signal_name,
int nargs, va_list args);
void script_signal_emit(const char *signal_name, int nargs, ...);
|
|