Freeciv21
Develop your civilization from humble roots to a global empire
idex.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
28 // utility
29 #include "log.h"
30 
31 // common
32 #include "city.h"
33 #include "unit.h"
34 
35 #include "idex.h"
36 
40 void idex_init(struct world *iworld)
41 {
42  iworld->cities = new QHash<int, const struct city *>;
43  iworld->units = new QHash<int, const struct unit *>;
44 }
45 
49 void idex_free(struct world *iworld)
50 {
51  delete iworld->cities;
52  delete iworld->units;
53  iworld->cities = nullptr;
54  iworld->units = nullptr;
55 }
56 
61 void idex_register_city(struct world *iworld, struct city *pcity)
62 {
63  const struct city *old;
64 
65  if (iworld->cities->contains(pcity->id)) {
66  old = iworld->cities->value(pcity->id);
67  fc_assert_ret_msg(nullptr == old,
68  "IDEX: city collision: new %d %p %s, old %d %p %s",
69  pcity->id, (void *) pcity, city_name_get(pcity),
70  old->id, (void *) old, city_name_get(old));
71  }
72  iworld->cities->insert(pcity->id, pcity);
73 }
74 
79 void idex_register_unit(struct world *iworld, struct unit *punit)
80 {
81  const struct unit *old;
82 
83  if (iworld->units->contains(punit->id)) {
84  old = iworld->units->value(punit->id);
85  fc_assert_ret_msg(nullptr == old,
86  "IDEX: unit collision: new %d %p %s, old %d %p %s",
87  punit->id, (void *) punit, unit_rule_name(punit),
88  old->id, (void *) old, unit_rule_name(old));
89  }
90  iworld->units->insert(punit->id, punit);
91 }
92 
97 void idex_unregister_city(struct world *iworld, struct city *pcity)
98 {
99  const struct city *old;
100 
101  if (!iworld->units->contains(pcity->id)) {
102  old = pcity;
103  fc_assert_ret_msg(nullptr != old, "IDEX: city unreg missing: %d %p %s",
104  pcity->id, (void *) pcity, city_name_get(pcity));
105  fc_assert_ret_msg(old == pcity,
106  "IDEX: city unreg mismatch: "
107  "unreg %d %p %s, old %d %p %s",
108  pcity->id, (void *) pcity, city_name_get(pcity),
109  old->id, (void *) old, city_name_get(old));
110  }
111  iworld->cities->remove(pcity->id);
112 }
113 
118 void idex_unregister_unit(struct world *iworld, struct unit *punit)
119 {
120  const struct unit *old;
121 
122  if (!iworld->units->contains(punit->id)) {
123  old = punit;
124  fc_assert_ret_msg(nullptr != old, "IDEX: unit unreg missing: %d %p %s",
125  punit->id, (void *) punit, unit_rule_name(punit));
126  fc_assert_ret_msg(old == punit,
127  "IDEX: unit unreg mismatch: "
128  "unreg %d %p %s, old %d %p %s",
129  punit->id, (void *) punit, unit_rule_name(punit),
130  old->id, (void *) old, unit_rule_name(old));
131  }
132  iworld->units->remove(punit->id);
133 }
134 
139 struct city *idex_lookup_city(struct world *iworld, int id)
140 {
141  const struct city *pcity;
142 
143  pcity = iworld->cities->value(id);
144 
145  return const_cast<struct city *>(pcity);
146 }
147 
152 struct unit *idex_lookup_unit(struct world *iworld, int id)
153 {
154  const struct unit *punit;
155 
156  punit = iworld->units->value(id);
157 
158  return const_cast<struct unit *>(punit);
159 }
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
void idex_free(struct world *iworld)
Free the hashs.
Definition: idex.cpp:49
void idex_register_unit(struct world *iworld, struct unit *punit)
Register a unit into idex, with current punit->id.
Definition: idex.cpp:79
struct unit * idex_lookup_unit(struct world *iworld, int id)
Lookup unit with given id.
Definition: idex.cpp:152
void idex_unregister_city(struct world *iworld, struct city *pcity)
Remove a city from idex, with current pcity->id.
Definition: idex.cpp:97
void idex_init(struct world *iworld)
idex = ident index: a lookup table for quick mapping of unit and city id values to unit and city poin...
Definition: idex.cpp:40
struct city * idex_lookup_city(struct world *iworld, int id)
Lookup city with given id.
Definition: idex.cpp:139
void idex_unregister_unit(struct world *iworld, struct unit *punit)
Remove a unit from idex, with current punit->id.
Definition: idex.cpp:118
void idex_register_city(struct world *iworld, struct city *pcity)
Register a city into idex, with current pcity->id.
Definition: idex.cpp:61
#define fc_assert_ret_msg(condition, message,...)
Definition: log.h:129
Definition: city.h:291
int id
Definition: city.h:296
Definition: unit.h:134
int id
Definition: unit.h:141
QHash< int, const struct city * > * cities
Definition: world_object.h:22
QHash< int, const struct unit * > * units
Definition: world_object.h:23
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
Definition: unittype.cpp:1283