Author: cazfi Date: Tue May 2 08:08:41 2017 New Revision: 35362 URL: http://svn.gna.org/viewcvs/freeciv?rev=35362&view=rev Log: Add idex hashes to world object
See hrm Feature #656546 Modified: trunk/client/packhand.c trunk/common/game.c trunk/common/idex.c trunk/common/idex.h trunk/common/player.c trunk/common/scriptcore/api_game_find.c trunk/common/world_object.h trunk/server/citytools.c trunk/server/savegame2.c trunk/server/savegame3.c trunk/server/unittools.c Modified: trunk/client/packhand.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/client/packhand.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/client/packhand.c (original) +++ trunk/client/packhand.c Tue May 2 08:08:41 2017 @@ -139,7 +139,7 @@ { if (NULL != invisible.cities) { city_list_iterate(invisible.cities, pcity) { - idex_unregister_city(pcity); + idex_unregister_city(&wld, pcity); destroy_city_virtual(pcity); } city_list_iterate_end; @@ -634,7 +634,7 @@ city_is_new = TRUE; pcity = create_city_virtual(powner, pcenter, packet->name); pcity->id = packet->id; - idex_register_city(pcity); + idex_register_city(&wld, pcity); update_descriptions = TRUE; } else if (pcity->id != packet->id) { log_error("handle_city_info() city id %d != id %d.", @@ -1103,7 +1103,7 @@ pcity = create_city_virtual(powner, pcenter, packet->name); pcity->id = packet->id; city_map_radius_sq_set(pcity, radius_sq); - idex_register_city(pcity); + idex_register_city(&wld, pcity); } else if (pcity->id != packet->id) { log_error("handle_city_short_info() city id %d != id %d.", pcity->id, packet->id); @@ -1800,7 +1800,7 @@ } else { /*** Create new unit ***/ punit = packet_unit; - idex_register_unit(punit); + idex_register_unit(&wld, punit); unit_list_prepend(unit_owner(punit)->units, punit); unit_list_prepend(unit_tile(punit)->units, punit); @@ -2885,7 +2885,7 @@ pwork = create_city_virtual(invisible.placeholder, NULL, named); pwork->id = packet->worked; - idex_register_city(pwork); + idex_register_city(&wld, pwork); city_list_prepend(invisible.cities, pwork); Modified: trunk/common/game.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/common/game.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/common/game.c (original) +++ trunk/common/game.c Tue May 2 08:08:41 2017 @@ -122,7 +122,7 @@ **************************************************************************/ struct city *game_city_by_number(int id) { - return idex_lookup_city(id); + return idex_lookup_city(&wld, id); } @@ -132,7 +132,7 @@ **************************************************************************/ struct unit *game_unit_by_number(int id) { - return idex_lookup_unit(id); + return idex_lookup_unit(&wld, id); } /************************************************************************** @@ -181,7 +181,7 @@ unit_list_remove(unit_tile(punit)->units, punit); unit_list_remove(unit_owner(punit)->units, punit); - idex_unregister_unit(punit); + idex_unregister_unit(&wld, punit); if (game.callbacks.unit_deallocate) { (game.callbacks.unit_deallocate)(punit->id); @@ -220,7 +220,7 @@ } city_tile_iterate_end; } - idex_unregister_city(pcity); + idex_unregister_city(&wld, pcity); destroy_city_virtual(pcity); } @@ -439,7 +439,7 @@ map_init(&wld.map, is_server()); team_slots_init(); game_ruleset_init(); - idex_init(); + idex_init(&wld); cm_init(); researches_init(); universal_found_functions_init(); @@ -467,7 +467,7 @@ player_slots_free(); map_free(&(wld.map)); free_city_map_index(); - idex_free(); + idex_free(&wld); team_slots_free(); game_ruleset_free(); researches_free(); @@ -491,10 +491,10 @@ map_free(&(wld.map)); free_city_map_index(); - idex_free(); + idex_free(&wld); map_init(&wld.map, FALSE); - idex_init(); + idex_init(&wld); researches_init(); } } Modified: trunk/common/idex.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/common/idex.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/common/idex.c (original) +++ trunk/common/idex.c Tue May 2 08:08:41 2017 @@ -38,57 +38,36 @@ #include "idex.h" - -/* struct city_hash. */ -#define SPECHASH_TAG city -#define SPECHASH_INT_KEY_TYPE -#define SPECHASH_IDATA_TYPE struct city * -#include "spechash.h" - -/* struct unit_hash. */ -#define SPECHASH_TAG unit -#define SPECHASH_INT_KEY_TYPE -#define SPECHASH_IDATA_TYPE struct unit * -#include "spechash.h" - - -/* "Global" data: */ -static struct city_hash *idex_city_hash = NULL; -static struct unit_hash *idex_unit_hash = NULL; - /************************************************************************** Initialize. Should call this at the start before use. ***************************************************************************/ -void idex_init(void) +void idex_init(struct world *iworld) { - fc_assert_ret(idex_city_hash == NULL); - fc_assert_ret(idex_unit_hash == NULL); - - idex_city_hash = city_hash_new(); - idex_unit_hash = unit_hash_new(); + iworld->cities = city_hash_new(); + iworld->units = unit_hash_new(); } /************************************************************************** Free the hashs. ***************************************************************************/ -void idex_free(void) +void idex_free(struct world *iworld) { - city_hash_destroy(idex_city_hash); - idex_city_hash = NULL; + city_hash_destroy(iworld->cities); + iworld->cities = NULL; - unit_hash_destroy(idex_unit_hash); - idex_unit_hash = NULL; + unit_hash_destroy(iworld->units); + iworld->units = NULL; } /************************************************************************** Register a city into idex, with current pcity->id. Call this when pcity created. ***************************************************************************/ -void idex_register_city(struct city *pcity) +void idex_register_city(struct world *iworld, struct city *pcity) { struct city *old; - city_hash_replace_full(idex_city_hash, pcity->id, pcity, NULL, &old); + city_hash_replace_full(iworld->cities, pcity->id, pcity, NULL, &old); fc_assert_ret_msg(NULL == old, "IDEX: city collision: new %d %p %s, old %d %p %s", pcity->id, (void *) pcity, city_name_get(pcity), @@ -99,11 +78,11 @@ Register a unit into idex, with current punit->id. Call this when punit created. ***************************************************************************/ -void idex_register_unit(struct unit *punit) +void idex_register_unit(struct world *iworld, struct unit *punit) { struct unit *old; - unit_hash_replace_full(idex_unit_hash, punit->id, punit, NULL, &old); + unit_hash_replace_full(iworld->units, punit->id, punit, NULL, &old); fc_assert_ret_msg(NULL == old, "IDEX: unit collision: new %d %p %s, old %d %p %s", punit->id, (void *) punit, unit_rule_name(punit), @@ -114,11 +93,11 @@ Remove a city from idex, with current pcity->id. Call this when pcity deleted. ***************************************************************************/ -void idex_unregister_city(struct city *pcity) +void idex_unregister_city(struct world *iworld, struct city *pcity) { struct city *old; - city_hash_remove_full(idex_city_hash, pcity->id, NULL, &old); + city_hash_remove_full(iworld->cities, pcity->id, NULL, &old); fc_assert_ret_msg(NULL != old, "IDEX: city unreg missing: %d %p %s", pcity->id, (void *) pcity, city_name_get(pcity)); @@ -132,11 +111,11 @@ Remove a unit from idex, with current punit->id. Call this when punit deleted. ***************************************************************************/ -void idex_unregister_unit(struct unit *punit) +void idex_unregister_unit(struct world *iworld, struct unit *punit) { struct unit *old; - unit_hash_remove_full(idex_unit_hash, punit->id, NULL, &old); + unit_hash_remove_full(iworld->units, punit->id, NULL, &old); fc_assert_ret_msg(NULL != old, "IDEX: unit unreg missing: %d %p %s", punit->id, (void *) punit, unit_rule_name(punit)); @@ -150,11 +129,12 @@ Lookup city with given id. Returns NULL if the city is not registered (which is not an error). ***************************************************************************/ -struct city *idex_lookup_city(int id) +struct city *idex_lookup_city(struct world *iworld, int id) { struct city *pcity; - city_hash_lookup(idex_city_hash, id, &pcity); + city_hash_lookup(iworld->cities, id, &pcity); + return pcity; } @@ -162,10 +142,11 @@ Lookup unit with given id. Returns NULL if the unit is not registered (which is not an error). ***************************************************************************/ -struct unit *idex_lookup_unit(int id) +struct unit *idex_lookup_unit(struct world *iworld, int id) { struct unit *punit; - unit_hash_lookup(idex_unit_hash, id, &punit); + unit_hash_lookup(iworld->units, id, &punit); + return punit; } Modified: trunk/common/idex.h URL: http://svn.gna.org/viewcvs/freeciv/trunk/common/idex.h?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/common/idex.h (original) +++ trunk/common/idex.h Tue May 2 08:08:41 2017 @@ -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 @@ -22,19 +22,21 @@ id values to unit and city pointers. ***************************************************************************/ +/* common */ #include "fc_types.h" +#include "world_object.h" -void idex_init(void); -void idex_free(void); +void idex_init(struct world *iworld); +void idex_free(struct world *iworld); -void idex_register_city(struct city *pcity); -void idex_register_unit(struct unit *punit); +void idex_register_city(struct world *iworld, struct city *pcity); +void idex_register_unit(struct world *iworld, struct unit *punit); -void idex_unregister_city(struct city *pcity); -void idex_unregister_unit(struct unit *punit); +void idex_unregister_city(struct world *iworld, struct city *pcity); +void idex_unregister_unit(struct world *iworld, struct unit *punit); -struct city *idex_lookup_city(int id); -struct unit *idex_lookup_unit(int id); +struct city *idex_lookup_city(struct world *iworld, int id); +struct unit *idex_lookup_unit(struct world *iworld, int id); #ifdef __cplusplus } Modified: trunk/common/player.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/common/player.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/common/player.c (original) +++ trunk/common/player.c Tue May 2 08:08:41 2017 @@ -1122,7 +1122,7 @@ struct city *player_city_by_number(const struct player *pplayer, int city_id) { /* We call idex directly. Should use game_city_by_number() instead? */ - struct city *pcity = idex_lookup_city(city_id); + struct city *pcity = idex_lookup_city(&wld, city_id); if (!pcity) { return NULL; @@ -1148,7 +1148,7 @@ struct unit *player_unit_by_number(const struct player *pplayer, int unit_id) { /* We call idex directly. Should use game_unit_by_number() instead? */ - struct unit *punit = idex_lookup_unit(unit_id); + struct unit *punit = idex_lookup_unit(&wld, unit_id); if (!punit) { return NULL; Modified: trunk/common/scriptcore/api_game_find.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/common/scriptcore/api_game_find.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/common/scriptcore/api_game_find.c (original) +++ trunk/common/scriptcore/api_game_find.c Tue May 2 08:08:41 2017 @@ -45,7 +45,7 @@ if (pplayer) { return player_city_by_number(pplayer, city_id); } else { - return idex_lookup_city(city_id); + return idex_lookup_city(&wld, city_id); } } @@ -59,7 +59,7 @@ if (pplayer) { return player_unit_by_number(pplayer, unit_id); } else { - return idex_lookup_unit(unit_id); + return idex_lookup_unit(&wld, unit_id); } } Modified: trunk/common/world_object.h URL: http://svn.gna.org/viewcvs/freeciv/trunk/common/world_object.h?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/common/world_object.h (original) +++ trunk/common/world_object.h Tue May 2 08:08:41 2017 @@ -20,9 +20,24 @@ /* common */ #include "map_types.h" + +/* struct city_hash. */ +#define SPECHASH_TAG city +#define SPECHASH_INT_KEY_TYPE +#define SPECHASH_IDATA_TYPE struct city * +#include "spechash.h" + +/* struct unit_hash. */ +#define SPECHASH_TAG unit +#define SPECHASH_INT_KEY_TYPE +#define SPECHASH_IDATA_TYPE struct unit * +#include "spechash.h" + struct world { struct civ_map map; + struct city_hash *cities; + struct unit_hash *units; }; #ifdef __cplusplus Modified: trunk/server/citytools.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/server/citytools.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/server/citytools.c (original) +++ trunk/server/citytools.c Tue May 2 08:08:41 2017 @@ -1483,7 +1483,7 @@ pcity->id = identity_number(); fc_allocate_mutex(&game.server.mutexes.city_list); - idex_register_city(pcity); + idex_register_city(&wld, pcity); fc_release_mutex(&game.server.mutexes.city_list); if (city_list_size(pplayer->cities) == 0) { Modified: trunk/server/savegame2.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/server/savegame2.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/server/savegame2.c (original) +++ trunk/server/savegame2.c Tue May 2 08:08:41 2017 @@ -3257,7 +3257,7 @@ } identity_number_reserve(pcity->id); - idex_register_city(pcity); + idex_register_city(&wld, pcity); /* Load the information about the nationality of citizens. This is done * here because the city sanity check called by citizens_update() requires @@ -3673,7 +3673,7 @@ } identity_number_reserve(punit->id); - idex_register_unit(punit); + idex_register_unit(&wld, punit); if ((pcity = game_city_by_number(punit->homecity))) { unit_list_prepend(pcity->units_supported, punit); Modified: trunk/server/savegame3.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/server/savegame3.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/server/savegame3.c (original) +++ trunk/server/savegame3.c Tue May 2 08:08:41 2017 @@ -4437,7 +4437,7 @@ } identity_number_reserve(pcity->id); - idex_register_city(pcity); + idex_register_city(&wld, pcity); /* Load the information about the nationality of citizens. This is done * here because the city sanity check called by citizens_update() requires @@ -5074,7 +5074,7 @@ } identity_number_reserve(punit->id); - idex_register_unit(punit); + idex_register_unit(&wld, punit); if ((pcity = game_city_by_number(punit->homecity))) { unit_list_prepend(pcity->units_supported, punit); Modified: trunk/server/unittools.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/server/unittools.c?rev=35362&r1=35361&r2=35362&view=diff ============================================================================== --- trunk/server/unittools.c (original) +++ trunk/server/unittools.c Tue May 2 08:08:41 2017 @@ -1586,7 +1586,7 @@ /* Register unit */ punit->id = identity_number(); - idex_register_unit(punit); + idex_register_unit(&wld, punit); fc_assert_ret_val(ptile != NULL, NULL); unit_tile_set(punit, ptile); _______________________________________________ Freeciv-commits mailing list Freeciv-commits@gna.org https://mail.gna.org/listinfo/freeciv-commits