From 718a23463d354eb172d98ab384389b9353d0a280 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 14 Sep 2024 22:34:37 +0300
Subject: [PATCH 31/31] Lua: Add Player:tag_score()

See RM #859

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 server/report.c                            | 132 ++++++++++++++-------
 server/report.h                            |   1 +
 server/scripting/api_server_game_methods.c |  12 ++
 server/scripting/api_server_game_methods.h |   1 +
 server/scripting/tolua_server.pkg          |   3 +
 5 files changed, 104 insertions(+), 45 deletions(-)

diff --git a/server/report.c b/server/report.c
index fd09410f12..ce7ab258af 100644
--- a/server/report.c
+++ b/server/report.c
@@ -147,6 +147,32 @@ static int get_pollution(const struct player *pplayer);
 static int get_mil_service(const struct player *pplayer);
 static int get_culture(const struct player *pplayer);
 
+static int get_pop(const struct player *pplayer);
+static int get_cities(const struct player *pplayer);
+static int get_techs(const struct player *pplayer);
+static int get_munits(const struct player *pplayer);
+static int get_settlers(const struct player *pplayer);
+static int get_wonders(const struct player *pplayer);
+static int get_techout(const struct player *pplayer);
+static int get_literacy2(const struct player *pplayer);
+static int get_spaceship(const struct player *pplayer);
+static int get_gold(const struct player *pplayer);
+static int get_taxrate(const struct player *pplayer);
+static int get_scirate(const struct player *pplayer);
+static int get_luxrate(const struct player *pplayer);
+static int get_riots(const struct player *pplayer);
+static int get_happypop(const struct player *pplayer);
+static int get_contentpop(const struct player *pplayer);
+static int get_unhappypop(const struct player *pplayer);
+static int get_specialists(const struct player *pplayer);
+static int get_gov(const struct player *pplayer);
+static int get_corruption(const struct player *pplayer);
+static int get_total_score(const struct player *pplayer);
+static int get_units_built(const struct player *pplayer);
+static int get_units_killed(const struct player *pplayer);
+static int get_units_lost(const struct player *pplayer);
+static int get_units_used(const struct player *pplayer);
+
 static const char *area_to_text(int value);
 static const char *percent_to_text(int value);
 static const char *production_to_text(int value);
@@ -156,6 +182,51 @@ static const char *mil_service_to_text(int value);
 static const char *pollution_to_text(int value);
 static const char *culture_to_text(int value);
 
+/* Add new tags only at end of this list. Maintaining the order of
+ * old tags is critical. */
+static const struct {
+  char *name;
+  int (*get_value) (const struct player *);
+} score_tags[] = {
+  {"pop",             get_pop},
+  {"bnp",             get_economics},
+  {"mfg",             get_production},
+  {"cities",          get_cities},
+  {"techs",           get_techs},
+  {"munits",          get_munits},
+  {"settlers",        get_settlers},  /* "original" tags end here */
+
+  {"wonders",         get_wonders},
+  {"techout",         get_techout},
+  {"landarea",        get_landarea},
+  {"settledarea",     get_settledarea},
+  {"pollution",       get_pollution},
+  {"literacy",        get_literacy2},
+  {"spaceship",       get_spaceship}, /* new 1.8.2 tags end here */
+
+  {"gold",            get_gold},
+  {"taxrate",         get_taxrate},
+  {"scirate",         get_scirate},
+  {"luxrate",         get_luxrate},
+  {"riots",           get_riots},
+  {"happypop",        get_happypop},
+  {"contentpop",      get_contentpop},
+  {"unhappypop",      get_unhappypop},
+  {"specialists",     get_specialists},
+  {"gov",             get_gov},
+  {"corruption",      get_corruption}, /* new 1.11.5 tags end here */
+
+  {"score",           get_total_score}, /* New 2.1.10 tag end here. */
+
+  {"unitsbuilt",      get_units_built}, /* New tags since 2.3.0. */
+  {"unitskilled",     get_units_killed},
+  {"unitslost",       get_units_lost},
+
+  {"culture",         get_culture},     /* New tag in 2.6.0. */
+
+  {"unitsused",       get_units_used}   /* New tag in 3.2.0. */
+};
+
 #define GOOD_PLAYER(p) ((p)->is_alive && !is_barbarian(p))
 
 /*
@@ -1468,51 +1539,6 @@ void log_civ_score_now(void)
   char id[MAX_LEN_GAME_IDENTIFIER];
   int i = 0;
 
-  /* Add new tags only at end of this list. Maintaining the order of
-   * old tags is critical. */
-  static const struct {
-    char *name;
-    int (*get_value) (const struct player *);
-  } score_tags[] = {
-    {"pop",             get_pop},
-    {"bnp",             get_economics},
-    {"mfg",             get_production},
-    {"cities",          get_cities},
-    {"techs",           get_techs},
-    {"munits",          get_munits},
-    {"settlers",        get_settlers},  /* "original" tags end here */
-
-    {"wonders",         get_wonders},
-    {"techout",         get_techout},
-    {"landarea",        get_landarea},
-    {"settledarea",     get_settledarea},
-    {"pollution",       get_pollution},
-    {"literacy",        get_literacy2},
-    {"spaceship",       get_spaceship}, /* new 1.8.2 tags end here */
-
-    {"gold",            get_gold},
-    {"taxrate",         get_taxrate},
-    {"scirate",         get_scirate},
-    {"luxrate",         get_luxrate},
-    {"riots",           get_riots},
-    {"happypop",        get_happypop},
-    {"contentpop",      get_contentpop},
-    {"unhappypop",      get_unhappypop},
-    {"specialists",     get_specialists},
-    {"gov",             get_gov},
-    {"corruption",      get_corruption}, /* new 1.11.5 tags end here */
-
-    {"score",           get_total_score}, /* New 2.1.10 tag end here. */
-
-    {"unitsbuilt",      get_units_built}, /* New tags since 2.3.0. */
-    {"unitskilled",     get_units_killed},
-    {"unitslost",       get_units_lost},
-
-    {"culture",         get_culture},     /* New tag in 2.6.0. */
-
-    {"unitsused",       get_units_used}   /* New tag in 3.2.0. */
-  };
-
   if (!game.server.scorelog) {
     return;
   }
@@ -1826,3 +1852,19 @@ struct history_report *history_report_get(void)
 {
   return &latest_history_report;
 }
+
+/**********************************************************************//**
+  Return score of the type associated to the tag
+**************************************************************************/
+int get_tag_score(const char *tag, const struct player *pplayer)
+{
+  int i;
+
+  for (i = 0; i < ARRAY_SIZE(score_tags); i++) {
+    if (!fc_strcasecmp(tag, score_tags[i].name)) {
+      return score_tags[i].get_value(pplayer);
+    }
+  }
+
+  return -1;
+}
diff --git a/server/report.h b/server/report.h
index 90ae9e560f..6f7cec85cc 100644
--- a/server/report.h
+++ b/server/report.h
@@ -42,6 +42,7 @@ bool is_valid_demography(const char *demography, int *error);
 void report_demographics(struct connection *pconn);
 void report_achievements(struct connection *pconn);
 void report_final_scores(struct conn_list *dest);
+int get_tag_score(const char *tag, const struct player *pplayer);
 
 struct history_report *history_report_get(void);
 
diff --git a/server/scripting/api_server_game_methods.c b/server/scripting/api_server_game_methods.c
index 895b521687..fe2dbaa59b 100644
--- a/server/scripting/api_server_game_methods.c
+++ b/server/scripting/api_server_game_methods.c
@@ -26,6 +26,7 @@
 
 /* server */
 #include "plrhand.h"
+#include "report.h"
 
 /* server/scripting */
 #include "script_server.h"
@@ -222,3 +223,14 @@ int api_methods_player_free_bulbs(lua_State *L, Player *pplayer)
 
   return presearch->free_bulbs;
 }
+
+/**********************************************************************//**
+  Return score of the type associated to the tag.
+**************************************************************************/
+int api_methods_tag_score(lua_State *L, Player *pplayer, const char *tag)
+{
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_SELF(L, pplayer, -1);
+
+  return get_tag_score(tag, pplayer);
+}
diff --git a/server/scripting/api_server_game_methods.h b/server/scripting/api_server_game_methods.h
index 4a6e3d5148..47c8fba753 100644
--- a/server/scripting/api_server_game_methods.h
+++ b/server/scripting/api_server_game_methods.h
@@ -40,5 +40,6 @@ int api_methods_nation_trait_default(lua_State *L, Nation_Type *pnation,
 int api_methods_player_tech_bulbs(lua_State *L, Player *pplayer,
                                   Tech_Type *tech);
 int api_methods_player_free_bulbs(lua_State *L, Player *pplayer);
+int api_methods_tag_score(lua_State *L, Player *pplayer, const char *tag);
 
 #endif /* FC__API_SERVER_GAME_METHODS_H */
diff --git a/server/scripting/tolua_server.pkg b/server/scripting/tolua_server.pkg
index 5f38e372ba..425cd5edb8 100644
--- a/server/scripting/tolua_server.pkg
+++ b/server/scripting/tolua_server.pkg
@@ -566,6 +566,9 @@ module Player {
     @ lose (lua_State *L, Player *pplayer, Player *looter = NULL);
   int api_methods_player_tech_bulbs
     @ bulbs_saved (lua_State *L, Player *pplayer, Tech_Type *tech);
+
+  int api_methods_tag_score
+    @ tag_score (lua_State *L, Player *pplayer, const char *tag);
 }
 
 $[
-- 
2.45.2

