From 7fff45d8dd7f8fd2ad316d0f1322b1fa71d72fc3 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 3 May 2022 01:00:30 +0300 Subject: [PATCH 50/50] Move treaties list from server/ to common/ See osdn #44501 Signed-off-by: Marko Lindqvist --- common/diptreaty.c | 79 +++++++++++++++++++++++++++++++++++++ common/diptreaty.h | 20 ++++++++++ common/game.c | 3 ++ server/diplhand.c | 67 ++----------------------------- server/diplhand.h | 18 +-------- server/savegame/savegame2.c | 3 +- server/savegame/savegame3.c | 54 ++++++++++++++----------- server/srv_main.c | 2 - 8 files changed, 139 insertions(+), 107 deletions(-) diff --git a/common/diptreaty.c b/common/diptreaty.c index 650ff84d50..c864e5d5f0 100644 --- a/common/diptreaty.c +++ b/common/diptreaty.c @@ -27,6 +27,8 @@ static struct clause_info clause_infos[CLAUSE_COUNT]; +static struct treaty_list *treaties = NULL; + /**********************************************************************//** Returns TRUE iff pplayer could do diplomacy in the game at all. **************************************************************************/ @@ -297,3 +299,80 @@ bool clause_enabled(enum clause_type type, struct player *from, return TRUE; } + +/**********************************************************************//** + Initialize treaties module +**************************************************************************/ +void treaties_init(void) +{ + treaties = treaty_list_new(); +} + +/**********************************************************************//** + Free all the resources allocated by treaties. +**************************************************************************/ +void treaties_free(void) +{ + free_treaties(); + + treaty_list_destroy(treaties); + treaties = NULL; +} + +/**********************************************************************//** + Free all the treaties currently in treaty list. +**************************************************************************/ +void free_treaties(void) +{ + /* Free memory allocated for treaties */ + treaty_list_iterate(treaties, pt) { + clear_treaty(pt); + free(pt); + } treaty_list_iterate_end; + + treaty_list_clear(treaties); +} + +/**********************************************************************//** + Find currently active treaty between two players. +**************************************************************************/ +struct Treaty *find_treaty(struct player *plr0, struct player *plr1) +{ + treaty_list_iterate(treaties, ptreaty) { + if ((ptreaty->plr0 == plr0 && ptreaty->plr1 == plr1) + || (ptreaty->plr0 == plr1 && ptreaty->plr1 == plr0)) { + return ptreaty; + } + } treaty_list_iterate_end; + + return NULL; +} + +/**********************************************************************//** + Add treaty to the global list. +**************************************************************************/ +void treaty_add(struct Treaty *ptreaty) +{ + treaty_list_prepend(treaties, ptreaty); +} + +/**********************************************************************//** + Remove treaty from the global list. +**************************************************************************/ +void treaty_remove(struct Treaty *ptreaty) +{ + treaty_list_remove(treaties, ptreaty); + + clear_treaty(ptreaty); + free(ptreaty); +} + +/**********************************************************************//** + Call callback for each treaty +**************************************************************************/ +void treaties_iterate(treaty_cb cb, void *data) +{ + treaty_list_iterate(treaties, ptr) { + cb(ptr, data); + } treaty_list_iterate_end; +} diff --git a/common/diptreaty.h b/common/diptreaty.h index 9dead4d8f5..540e8c4195 100644 --- a/common/diptreaty.h +++ b/common/diptreaty.h @@ -101,6 +101,26 @@ struct clause_info *clause_info_get(enum clause_type type); bool clause_enabled(enum clause_type type, struct player *from, struct player *to); +#define SPECLIST_TAG treaty +#define SPECLIST_TYPE struct Treaty +#include "speclist.h" + +#define treaty_list_iterate(list, p) \ + TYPED_LIST_ITERATE(struct Treaty, list, p) +#define treaty_list_iterate_end LIST_ITERATE_END + +void treaties_init(void); +void treaties_free(void); +void free_treaties(void); + +struct Treaty *find_treaty(struct player *plr0, struct player *plr1); + +void treaty_add(struct Treaty *ptreaty); +void treaty_remove(struct Treaty *ptreaty); + +typedef void (*treaty_cb)(struct Treaty *, void *data); +void treaties_iterate(treaty_cb cb, void *data); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/common/game.c b/common/game.c index b86d662011..017595744d 100644 --- a/common/game.c +++ b/common/game.c @@ -33,6 +33,7 @@ #include "city.h" #include "connection.h" #include "counters.h" +#include "diptreaty.h" #include "disaster.h" #include "extras.h" #include "government.h" @@ -455,6 +456,7 @@ void game_init(bool keep_ruleset_value) cm_init(); researches_init(); universal_found_functions_init(); + treaties_init(); } /**********************************************************************//** @@ -476,6 +478,7 @@ void game_map_init(void) **************************************************************************/ void game_free(void) { + treaties_free(); player_slots_free(); main_map_free(); free_city_map_index(); diff --git a/server/diplhand.c b/server/diplhand.c index 16f98892f5..62e57a60d7 100644 --- a/server/diplhand.c +++ b/server/diplhand.c @@ -55,7 +55,6 @@ #include "diplhand.h" -static struct treaty_list *treaties = NULL; /* FIXME: Should this be put in a ruleset somewhere? */ #define TURNS_LEFT 16 @@ -82,54 +81,6 @@ static void call_treaty_accepted(struct player *pplayer, struct player *aplayer, } } -/**********************************************************************//** - Initialize diplhand module -**************************************************************************/ -void diplhand_init(void) -{ - treaties = treaty_list_new(); -} - -/**********************************************************************//** - Free all the resources allocated by diplhand. -**************************************************************************/ -void diplhand_free(void) -{ - free_treaties(); - - treaty_list_destroy(treaties); - treaties = NULL; -} - -/**********************************************************************//** - Free all the treaties currently in treaty list. -**************************************************************************/ -void free_treaties(void) -{ - /* Free memory allocated for treaties */ - treaty_list_iterate(treaties, pt) { - clear_treaty(pt); - free(pt); - } treaty_list_iterate_end; - - treaty_list_clear(treaties); -} - -/**********************************************************************//** - Find currently active treaty between two players. -**************************************************************************/ -struct Treaty *find_treaty(struct player *plr0, struct player *plr1) -{ - treaty_list_iterate(treaties, ptreaty) { - if ((ptreaty->plr0 == plr0 && ptreaty->plr1 == plr1) - || (ptreaty->plr0 == plr1 && ptreaty->plr1 == plr0)) { - return ptreaty; - } - } treaty_list_iterate_end; - - return NULL; -} - /**********************************************************************//** Return the closest of the two diplstate types. **************************************************************************/ @@ -697,9 +648,7 @@ void handle_diplomacy_accept_treaty_req(struct player *pplayer, } cleanup: - treaty_list_remove(treaties, ptreaty); - clear_treaty(ptreaty); - free(ptreaty); + treaty_remove(ptreaty); send_player_all_c(pplayer, NULL); send_player_all_c(pother, NULL); } @@ -831,9 +780,7 @@ static void really_diplomacy_cancel_meeting(struct player *pplayer, notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server, _("Meeting with %s canceled."), player_name(pother)); - treaty_list_remove(treaties, ptreaty); - clear_treaty(ptreaty); - free(ptreaty); + treaty_remove(ptreaty); } } @@ -880,7 +827,7 @@ void handle_diplomacy_init_meeting_req(struct player *pplayer, ptreaty = fc_malloc(sizeof(*ptreaty)); init_treaty(ptreaty, pplayer, pother); - treaty_list_prepend(treaties, ptreaty); + treaty_add(ptreaty); dlsend_packet_diplomacy_init_meeting(pplayer->connections, player_number(pother), @@ -965,11 +912,3 @@ void reject_all_treaties(struct player *pplayer) FALSE); } players_iterate_end; } - -/**********************************************************************//** - Get treaty list -**************************************************************************/ -struct treaty_list *get_all_treaties(void) -{ - return treaties; -} diff --git a/server/diplhand.h b/server/diplhand.h index 38ed9e4ac0..dc9f6057ae 100644 --- a/server/diplhand.h +++ b/server/diplhand.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 @@ -21,26 +21,10 @@ struct Treaty; struct packet_diplomacy_info; struct connection; -#define SPECLIST_TAG treaty -#define SPECLIST_TYPE struct Treaty -#include "speclist.h" - -#define treaty_list_iterate(list, p) \ - TYPED_LIST_ITERATE(struct Treaty, list, p) -#define treaty_list_iterate_end LIST_ITERATE_END - void establish_embassy(struct player *pplayer, struct player *aplayer); -void diplhand_init(void); -void diplhand_free(void); -void free_treaties(void); - -struct Treaty *find_treaty(struct player *plr0, struct player *plr1); - void send_diplomatic_meetings(struct connection *dest); void cancel_all_meetings(struct player *pplayer); void reject_all_treaties(struct player *pplayer); -struct treaty_list *get_all_treaties(void); - #endif /* FC__DIPLHAND_H */ diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index 6199d6d662..14c85ac066 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -4968,7 +4968,6 @@ static void sg_load_treaties(struct loaddata *loading) { int tidx; const char *plr0; - struct treaty_list *treaties = get_all_treaties(); /* Check status and return if not OK (sg_success != TRUE). */ sg_check_ret(); @@ -4992,7 +4991,7 @@ static void sg_load_treaties(struct loaddata *loading) struct Treaty *ptreaty = fc_malloc(sizeof(*ptreaty)); init_treaty(ptreaty, p0, p1); - treaty_list_prepend(treaties, ptreaty); + treaty_add(ptreaty); for (cidx = 0; (ct = secfile_lookup_str_default(loading->file, NULL, "treaty%d.clause%d.type", diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 41446d24a5..63e86f2b54 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -7349,7 +7349,6 @@ static void sg_load_treaties(struct loaddata *loading) { int tidx; const char *plr0; - struct treaty_list *treaties = get_all_treaties(); /* Check status and return if not OK (sg_success != TRUE). */ sg_check_ret(); @@ -7373,8 +7372,8 @@ static void sg_load_treaties(struct loaddata *loading) struct Treaty *ptreaty = fc_malloc(sizeof(*ptreaty)); init_treaty(ptreaty, p0, p1); - treaty_list_prepend(treaties, ptreaty); - + treaty_add(ptreaty); + for (cidx = 0; (ct = secfile_lookup_str_default(loading->file, NULL, "treaty%d.clause%d.type", tidx, cidx)) != NULL ; @@ -7421,35 +7420,46 @@ static void sg_load_treaties(struct loaddata *loading) } } +typedef struct { + int tidx; + struct section_file *file; +} treaty_cb_data; + /************************************************************************//** Save '[treaty_xxx]'. ****************************************************************************/ -static void sg_save_treaties(struct savedata *saving) +static void treaty_save(struct Treaty *ptr, void *data_in) { - struct treaty_list *treaties = get_all_treaties(); - int tidx = 0; + char tpath[512]; + int cidx = 0; + treaty_cb_data *data = (treaty_cb_data *)data_in; - treaty_list_iterate(treaties, ptr) { - char tpath[512]; - int cidx = 0; + fc_snprintf(tpath, sizeof(tpath), "treaty%d", data->tidx++); - fc_snprintf(tpath, sizeof(tpath), "treaty%d", tidx++); + secfile_insert_str(data->file, player_name(ptr->plr0), "%s.plr0", tpath); + secfile_insert_str(data->file, player_name(ptr->plr1), "%s.plr1", tpath); + secfile_insert_bool(data->file, ptr->accept0, "%s.accept0", tpath); + secfile_insert_bool(data->file, ptr->accept1, "%s.accept1", tpath); - secfile_insert_str(saving->file, player_name(ptr->plr0), "%s.plr0", tpath); - secfile_insert_str(saving->file, player_name(ptr->plr1), "%s.plr1", tpath); - secfile_insert_bool(saving->file, ptr->accept0, "%s.accept0", tpath); - secfile_insert_bool(saving->file, ptr->accept1, "%s.accept1", tpath); + clause_list_iterate(ptr->clauses, pclaus) { + char cpath[512]; - clause_list_iterate(ptr->clauses, pclaus) { - char cpath[512]; + fc_snprintf(cpath, sizeof(cpath), "%s.clause%d", tpath, cidx++); - fc_snprintf(cpath, sizeof(cpath), "%s.clause%d", tpath, cidx++); + secfile_insert_str(data->file, clause_type_name(pclaus->type), "%s.type", cpath); + secfile_insert_str(data->file, player_name(pclaus->from), "%s.from", cpath); + secfile_insert_int(data->file, pclaus->value, "%s.value", cpath); + } clause_list_iterate_end; +} + +/************************************************************************//** + Save all treaties. +****************************************************************************/ +static void sg_save_treaties(struct savedata *saving) +{ + treaty_cb_data data = { .tidx = 0, .file = saving->file }; - secfile_insert_str(saving->file, clause_type_name(pclaus->type), "%s.type", cpath); - secfile_insert_str(saving->file, player_name(pclaus->from), "%s.from", cpath); - secfile_insert_int(saving->file, pclaus->value, "%s.value", cpath); - } clause_list_iterate_end; - } treaty_list_iterate_end; + treaties_iterate(treaty_save, &data); } /* ======================================================================= diff --git a/server/srv_main.c b/server/srv_main.c index 31e4e49abe..afa600ff93 100644 --- a/server/srv_main.c +++ b/server/srv_main.c @@ -1831,7 +1831,6 @@ void server_quit(void) set_server_state(S_S_OVER); mapimg_free(); server_game_free(); - diplhand_free(); voting_free(); adv_settlers_free(); ai_timer_free(); @@ -2992,7 +2991,6 @@ static void srv_prepare(void) stdinhand_init(); edithand_init(); voting_init(); - diplhand_init(); voting_init(); ai_timer_init(); -- 2.35.1