From 22e4acee687583975e337e638fe6c86e77dae42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Mon, 24 May 2021 14:16:42 +0200 Subject: [PATCH] - Initial commit for counters --- common/counters.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++- common/counters.h | 9 ++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/common/counters.c b/common/counters.c index 20d46caee1..88cb3dd766 100644 --- a/common/counters.c +++ b/common/counters.c @@ -1,4 +1,4 @@ -/**************************************************************************** +/**************************************************************************** Freeciv - Copyright (C) 2004 - The Freeciv Team 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 @@ -15,6 +15,9 @@ #include #endif +/* utility */ +#include "fcintl.h" + #include "counters.h" @@ -51,3 +54,75 @@ void counters_init(void) void counters_free(void) { } + +/************************************************************************//** + Return counter by given id +****************************************************************************/ +struct counter *counter_by_id(int id) +{ + fc_assert_ret_val(id < MAX_COUNTERS, NULL); + + return &counters[id]; +} + +/************************************************************************//** + Return id of a given counter +****************************************************************************/ +int counter_id(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, -1); + return pcount->id; +} + +/************************************************************************//** + Search for counter by rule name + (return matched counter if found or NULL) +****************************************************************************/ +struct counter *counter_by_rule_name(const char *name) +{ + int i; + fc_assert_ret_val(NULL != name, NULL); + fc_assert_ret_val('\0' != name[0], NULL); + + for (i = 0; i < MAX_COUNTERS; i++) { + if (0 == fc_strcasecmp(name, counters[i].rule_name)) { + return &counters[i]; + } + } + + return NULL; +} + +/************************************************************************//** + Return rule name of a given counter +****************************************************************************/ +const char *counter_rule_name(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, NULL); + return pcount->rule_name; +} + +/************************************************************************//** + Return index in global counter's array +****************************************************************************/ +int counter_index(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, -1); + return pcount->index; +} + +/************************************************************************//** + Return counter by given index +****************************************************************************/ +struct counter *counter_by_index(int index, enum counter_target target) +{ + switch (target) { + case CTGT_CITY: + return counters_city[index]; + break; + + default: + fc_assert_exit_msg(false, "Bad counter type inside counter_by_index." + "Exiting"); + } +} diff --git a/common/counters.h b/common/counters.h index e4a160751a..6b2f94b8dc 100644 --- a/common/counters.h +++ b/common/counters.h @@ -36,6 +36,15 @@ struct counter void counters_init(void); void counters_free(void); +struct counter *counter_by_id(int id); +int counter_id(struct counter *pcount); + +struct counter *counter_by_rule_name(const char *name); +const char *counter_rule_name(struct counter *pcount); + +int counter_index(struct counter *pcount); +struct counter *counter_by_index(int index, enum counter_target target); + #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.31.1