From 6341bc7e4fe89dafb66c1433a8b9bcae746782c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Tue, 20 Jun 2023 17:23:44 +0200 Subject: [PATCH] =?UTF-8?q?!OSDN:#47827:S=C5=82awomir=20Lach=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement basic lua api for counters. diff --git a/common/scriptcore/Makefile.am b/common/scriptcore/Makefile.am index 344dc71dfb..853af9cf31 100644 --- a/common/scriptcore/Makefile.am +++ b/common/scriptcore/Makefile.am @@ -18,6 +18,8 @@ libscriptcore_la_SOURCES = \ api_common_intl.h \ api_common_utilities.c \ api_common_utilities.h \ + api_game_counters.c \ + api_game_counters.h \ api_game_effects.c \ api_game_effects.h \ api_game_find.c \ diff --git a/common/scriptcore/api_game_counters.c b/common/scriptcore/api_game_counters.c new file mode 100644 index 0000000000..f9e52839ec --- /dev/null +++ b/common/scriptcore/api_game_counters.c @@ -0,0 +1,46 @@ +/***************************************************************************** + Freeciv - Copyright (C) 2023 - 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 +#endif + +/* common */ +#include "city.h" +#include "counters.h" +#include "name_translation.h" + +/* common/scriptcore */ +#include "luascript.h" + +#include "api_game_counters.h" + +/**********************************************************************//** + Returns rule name of the counter. +**************************************************************************/ +const char *api_counter_rule_name(lua_State *L, Counter *c) +{ + LUASCRIPT_CHECK_ARG_NIL(L, c, 2, Counter, NULL); + + return counter_rule_name(c); +} + +/**********************************************************************//** + Returns translation name of the counter. +**************************************************************************/ +const char *api_counter_name_translation(lua_State *L, Counter *c) +{ + LUASCRIPT_CHECK_ARG_NIL(L, c, 2, Counter, NULL); + + return counter_name_translation(c); +} diff --git a/common/scriptcore/api_game_counters.h b/common/scriptcore/api_game_counters.h new file mode 100644 index 0000000000..8dceb5ca13 --- /dev/null +++ b/common/scriptcore/api_game_counters.h @@ -0,0 +1,24 @@ +/***************************************************************************** + Freeciv - Copyright (C) 2023 - 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_GAME_COUNTERS_H +#define FC__API_GAME_COUNTERS_H + +/* common/scriptcore */ +#include "luascript_types.h" + +struct lua_State; + +const char *api_counter_rule_name(lua_State *L, Counter *c); +const char *api_counter_name_translation(lua_State *L, Counter *c); +#endif diff --git a/common/scriptcore/api_game_find.c b/common/scriptcore/api_game_find.c index b0ef7aa2ec..d24295c54d 100644 --- a/common/scriptcore/api_game_find.c +++ b/common/scriptcore/api_game_find.c @@ -16,6 +16,7 @@ #endif /* common */ +#include "counters.h" #include "idex.h" #include "map.h" #include "movement.h" @@ -369,6 +370,27 @@ Action *api_find_action_type_by_name(lua_State *L, const char *name) return action_by_rule_name(name); } +/**********************************************************************//** + Returns the counter specified by id. +**************************************************************************/ +Counter *api_find_counter(lua_State *L, int counter_id) +{ + LUASCRIPT_CHECK_STATE(L, NULL); + + return counter_by_id(counter_id); +} + +/**********************************************************************//** + Returns the counter specified by name. +**************************************************************************/ +Counter *api_find_counter_by_name(lua_State *L, const char *name) +{ + LUASCRIPT_CHECK_STATE(L, NULL); + LUASCRIPT_CHECK_ARG_NIL(L, name, 2, string, NULL); + + return counter_by_rule_name(name); +} + /**********************************************************************//** Return a dummy pointer. **************************************************************************/ diff --git a/common/scriptcore/api_game_find.h b/common/scriptcore/api_game_find.h index 8594adcde6..921586de5b 100644 --- a/common/scriptcore/api_game_find.h +++ b/common/scriptcore/api_game_find.h @@ -30,6 +30,9 @@ Player *api_find_player(lua_State *L, int player_id); City *api_find_city(lua_State *L, Player *pplayer, int city_id); +Counter *api_find_counter_by_name(lua_State *L, const char *name); +Counter *api_find_counter(lua_State *L, int counter_id); + Unit *api_find_unit(lua_State *L, Player *pplayer, int unit_id); Unit *api_find_transport_unit(lua_State *L, Player *pplayer, Unit_Type *ptype, Tile *ptile); diff --git a/common/scriptcore/luascript_types.h b/common/scriptcore/luascript_types.h index a87aecfbee..ede832e145 100644 --- a/common/scriptcore/luascript_types.h +++ b/common/scriptcore/luascript_types.h @@ -23,6 +23,7 @@ extern "C" { /* common */ #include "achievements.h" #include "actions.h" +#include "counters.h" #include "city.h" #include "connection.h" #include "events.h" @@ -44,6 +45,7 @@ extern "C" { * tolua_common_z.pkg. */ typedef struct player Player; typedef struct player_ai Player_ai; +typedef struct counter Counter; typedef struct city City; typedef struct unit Unit; typedef struct tile Tile; diff --git a/common/scriptcore/tolua_game.pkg b/common/scriptcore/tolua_game.pkg index 588e9abcba..cfd651c0bb 100644 --- a/common/scriptcore/tolua_game.pkg +++ b/common/scriptcore/tolua_game.pkg @@ -26,6 +26,7 @@ $#endif /* common/scriptcore */ $#include "api_common_utilities.h" +$#include "api_game_counters.h" $#include "api_game_effects.h" $#include "api_game_find.h" $#include "api_game_methods.h" @@ -47,6 +48,10 @@ struct City { const int id; }; +struct Counter +{ +}; + struct Connection { const int id; }; @@ -164,6 +169,11 @@ module game { @ ruleset_name (lua_State *L); } +module Counter { + const char *api_counter_rule_name @ rule_name (lua_State *L,Counter *c); + const char *api_counter_name_translation @ name_translation (lua_State *L,Counter *c); +} + /* Module Player. */ module Player { module properties { @@ -528,6 +538,10 @@ module City_List_Link { /* Module find. */ module find { + Counter *api_find_counter_by_name + @ counter (lua_State *L, const char *name); + Counter *api_find_counter + @ counter (lua_State *L, int counter_id); Player *api_find_player_by_name @ player (lua_State *L, const char *name); Player *api_find_player diff --git a/meson.build b/meson.build index 1ab57b554b..04a4faed5a 100644 --- a/meson.build +++ b/meson.build @@ -1022,6 +1022,7 @@ common_lib = library('freeciv', 'common/networking/packets_json.c', 'common/scriptcore/api_common_intl.c', 'common/scriptcore/api_common_utilities.c', + 'common/scriptcore/api_game_counters.c', 'common/scriptcore/api_game_effects.c', 'common/scriptcore/api_game_find.c', 'common/scriptcore/api_game_methods.c', -- 2.41.0