From d77db96a7c0397f214fd753a7f7e17eb5a081dc6 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 10 Jan 2022 19:01:19 +0200 Subject: [PATCH 45/45] Lua: Add finder methods for Achievements and Disasters Reported by ihnatus and nef See osdn #43594 Signed-off-by: Marko Lindqvist --- common/disaster.c | 17 +++++++++++++ common/disaster.h | 3 ++- common/scriptcore/api_game_find.c | 42 +++++++++++++++++++++++++++++++ common/scriptcore/api_game_find.h | 6 +++++ common/scriptcore/tolua_game.pkg | 8 ++++++ 5 files changed, 75 insertions(+), 1 deletion(-) diff --git a/common/disaster.c b/common/disaster.c index 74fbc50d9b..e2e1722e4d 100644 --- a/common/disaster.c +++ b/common/disaster.c @@ -107,6 +107,23 @@ const char *disaster_rule_name(struct disaster_type *pdis) return rule_name_get(&pdis->name); } +/************************************************************************//** + Return disaster matching rule name or NULL if there is no disaster + with such a name. +****************************************************************************/ +struct disaster_type *disaster_by_rule_name(const char *name) +{ + const char *qs = Qn_(name); + + disaster_type_iterate(pdis) { + if (!fc_strcasecmp(disaster_rule_name(pdis), qs)) { + return pdis; + } + } disaster_type_iterate_end; + + return NULL; +} + /************************************************************************//** Check if disaster provides effect ****************************************************************************/ diff --git a/common/disaster.h b/common/disaster.h index b449c73232..69b9c32621 100644 --- a/common/disaster.h +++ b/common/disaster.h @@ -1,4 +1,4 @@ -/********************************************************************** +/*********************************************************************** Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold 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 @@ -69,6 +69,7 @@ struct disaster_type *disaster_by_number(Disaster_type_id id); const char *disaster_name_translation(struct disaster_type *pdis); const char *disaster_rule_name(struct disaster_type *pdis); +struct disaster_type *disaster_by_rule_name(const char *name); bool disaster_has_effect(const struct disaster_type *pdis, enum disaster_effect_id effect); diff --git a/common/scriptcore/api_game_find.c b/common/scriptcore/api_game_find.c index 7bfbdf046a..970440a868 100644 --- a/common/scriptcore/api_game_find.c +++ b/common/scriptcore/api_game_find.c @@ -286,6 +286,48 @@ Terrain *api_find_terrain_by_name(lua_State *L, const char *name_orig) return terrain_by_rule_name(name_orig); } +/**********************************************************************//** + Return the achievement with the given achievement_id index. +**************************************************************************/ +Achievement *api_find_achievement(lua_State *L, int achievement_id) +{ + LUASCRIPT_CHECK_STATE(L, NULL); + + return achievement_by_number(achievement_id); +} + +/**********************************************************************//** + Return the achievement with the given name_orig. +**************************************************************************/ +Achievement *api_find_achievement_by_name(lua_State *L, const char *name_orig) +{ + LUASCRIPT_CHECK_STATE(L, NULL); + LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL); + + return achievement_by_rule_name(name_orig); +} + +/**********************************************************************//** + Return the disaster with the given disaster_id index. +**************************************************************************/ +Disaster *api_find_disaster(lua_State *L, int disaster_id) +{ + LUASCRIPT_CHECK_STATE(L, NULL); + + return disaster_by_number(disaster_id); +} + +/**********************************************************************//** + Return the disaster with the given name_orig. +**************************************************************************/ +Disaster *api_find_disaster_by_name(lua_State *L, const char *name_orig) +{ + LUASCRIPT_CHECK_STATE(L, NULL); + LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL); + + return disaster_by_rule_name(name_orig); +} + /**********************************************************************//** Return the direction with the given id **************************************************************************/ diff --git a/common/scriptcore/api_game_find.h b/common/scriptcore/api_game_find.h index 3080846fbe..312bd1ad06 100644 --- a/common/scriptcore/api_game_find.h +++ b/common/scriptcore/api_game_find.h @@ -56,6 +56,12 @@ Tech_Type *api_find_tech_type_by_name(lua_State *L, const char *name_orig); Terrain *api_find_terrain(lua_State *L, int terrain_id); Terrain *api_find_terrain_by_name(lua_State *L, const char *name_orig); +Achievement *api_find_achievement(lua_State *L, int achievement_id); +Achievement *api_find_achievement_by_name(lua_State *L, const char *name_orig); + +Disaster *api_find_disaster(lua_State *L, int disaster_id); +Disaster *api_find_disaster_by_name(lua_State *L, const char *name_orig); + const Direction *api_find_direction (lua_State *L, int id); Action *api_find_action_type_by_id(lua_State *L, int id); diff --git a/common/scriptcore/tolua_game.pkg b/common/scriptcore/tolua_game.pkg index 9aa7bfd90f..216b47984d 100644 --- a/common/scriptcore/tolua_game.pkg +++ b/common/scriptcore/tolua_game.pkg @@ -521,6 +521,14 @@ module find { @ terrain (lua_State *L, const char *name_orig); Terrain *api_find_terrain @ terrain (lua_State *L, int terrain_id); + Achievement *api_find_achievement_by_name + @ achievement (lua_State *L, const char *name_orig); + Achievement *api_find_achievement + @ achievement (lua_State *L, int achievement_id); + Disaster *api_find_disaster_by_name + @ achievement (lua_State *L, const char *name_orig); + Disaster *api_find_disaster + @ disaster (lua_State *L, int disaster_id); Direction *api_utilities_str2dir @ direction (lua_State *L, const char *str); Direction *api_find_direction -- 2.34.1