[Freeciv-Dev] (PR#11347) put unit upkeep values into an array
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=11347 >
The unit structure has fields upkeep, upkeep_food, and upkeep_gold.
This patch puts them into an array indexed by output type. This
simplifies the code in a few places. In most places I just did a
straight substitution even when a partial rewrite was possible.
-jason
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.162
diff -u -r1.162 mapview_common.c
--- client/mapview_common.c 30 Nov 2004 01:49:03 -0000 1.162
+++ client/mapview_common.c 5 Dec 2004 09:59:00 -0000
@@ -956,12 +956,12 @@
struct canvas *pcanvas,
int canvas_x, int canvas_y)
{
- int upkeep_food = CLIP(0, punit->upkeep_food, 2);
- int upkeep_gold = CLIP(0, punit->upkeep_gold, 2);
+ int upkeep_food = CLIP(0, punit->upkeep[O_FOOD], 2);
+ int upkeep_gold = CLIP(0, punit->upkeep[O_GOLD], 2);
int unhappy = CLIP(0, punit->unhappiness, 2);
/* draw overlay pixmaps */
- if (punit->upkeep > 0) {
+ if (punit->upkeep[O_SHIELD] > 0) {
canvas_put_sprite_full(pcanvas, canvas_x, canvas_y,
sprites.upkeep.shield);
}
Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.435
diff -u -r1.435 packhand.c
--- client/packhand.c 5 Dec 2004 09:21:27 -0000 1.435
+++ client/packhand.c 5 Dec 2004 09:59:01 -0000
@@ -93,9 +93,9 @@
punit->activity = packet->activity;
punit->activity_count = packet->activity_count;
punit->unhappiness = packet->unhappiness;
- punit->upkeep = packet->upkeep;
- punit->upkeep_food = packet->upkeep_food;
- punit->upkeep_gold = packet->upkeep_gold;
+ output_type_iterate(o) {
+ punit->upkeep[o] = packet->upkeep[o];
+ } output_type_iterate_end;
punit->ai.control = packet->ai;
punit->fuel = packet->fuel;
if (is_normal_map_pos(packet->goto_dest_x, packet->goto_dest_y)) {
@@ -1175,18 +1175,12 @@
punit->unhappiness = packet_unit->unhappiness;
repaint_city = TRUE;
}
- if (punit->upkeep != packet_unit->upkeep) {
- punit->upkeep = packet_unit->upkeep;
- repaint_city = TRUE;
- }
- if (punit->upkeep_food != packet_unit->upkeep_food) {
- punit->upkeep_food = packet_unit->upkeep_food;
- repaint_city = TRUE;
- }
- if (punit->upkeep_gold != packet_unit->upkeep_gold) {
- punit->upkeep_gold = packet_unit->upkeep_gold;
- repaint_city = TRUE;
- }
+ output_type_iterate(o) {
+ if (punit->upkeep[o] != packet_unit->upkeep[o]) {
+ punit->upkeep[o] = packet_unit->upkeep[o];
+ repaint_city = TRUE;
+ }
+ } output_type_iterate_end;
if (repaint_city) {
if((pcity=find_city_by_id(punit->homecity))) {
refresh_city_dialog(pcity);
Index: client/repodlgs_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/repodlgs_common.c,v
retrieving revision 1.17
diff -u -r1.17 repodlgs_common.c
--- client/repodlgs_common.c 30 Nov 2004 08:37:02 -0000 1.17
+++ client/repodlgs_common.c 5 Dec 2004 09:59:01 -0000
@@ -113,7 +113,7 @@
if (punit->type == utype) {
count++;
- partial_cost += punit->upkeep_gold;
+ partial_cost += punit->upkeep[O_GOLD];
}
} unit_list_iterate_end;
Index: client/gui-gtk/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/repodlgs.c,v
retrieving revision 1.86
diff -u -r1.86 repodlgs.c
--- client/gui-gtk/repodlgs.c 23 Nov 2004 17:58:55 -0000 1.86
+++ client/gui-gtk/repodlgs.c 5 Dec 2004 09:59:02 -0000
@@ -1020,9 +1020,9 @@
unit_list_iterate(game.player_ptr->units, punit) {
(unitarray[punit->type].active_count)++;
if (punit->homecity) {
- unitarray[punit->type].upkeep_shield += punit->upkeep;
- unitarray[punit->type].upkeep_food += punit->upkeep_food;
- unitarray[punit->type].upkeep_gold += punit->upkeep_gold;
+ unitarray[punit->type].upkeep_shield += punit->upkeep[O_SHIELD];
+ unitarray[punit->type].upkeep_food += punit->upkeep[O_FOOD];
+ unitarray[punit->type].upkeep_gold += punit->upkeep[O_GOLD];
}
}
unit_list_iterate_end;
Index: client/gui-gtk-2.0/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/repodlgs.c,v
retrieving revision 1.71
diff -u -r1.71 repodlgs.c
--- client/gui-gtk-2.0/repodlgs.c 25 Nov 2004 06:57:17 -0000 1.71
+++ client/gui-gtk-2.0/repodlgs.c 5 Dec 2004 09:59:02 -0000
@@ -1142,9 +1142,7 @@
{
struct repoinfo {
int active_count;
- int upkeep_shield;
- int upkeep_food;
- int upkeep_gold;
+ int upkeep[O_COUNT];
int building_count;
};
@@ -1164,12 +1162,11 @@
unit_list_iterate(game.player_ptr->units, punit) {
(unitarray[punit->type].active_count)++;
if (punit->homecity) {
- unitarray[punit->type].upkeep_shield += punit->upkeep;
- unitarray[punit->type].upkeep_food += punit->upkeep_food;
- unitarray[punit->type].upkeep_gold += punit->upkeep_gold;
+ output_type_iterate(o) {
+ unitarray[punit->type].upkeep[o] += punit->upkeep[o];
+ } output_type_iterate_end;
}
- }
- unit_list_iterate_end;
+ } unit_list_iterate_end;
city_list_iterate(game.player_ptr->cities,pcity) {
if (pcity->is_building_unit) {
(unitarray[pcity->currently_building].building_count)++;
@@ -1181,7 +1178,8 @@
memset(&unittotals, '\0', sizeof(unittotals));
unit_type_iterate(i) {
- if ((unitarray[i].active_count > 0) || (unitarray[i].building_count >
0)) {
+ if (unitarray[i].active_count > 0
+ || unitarray[i].building_count > 0) {
can = (can_upgrade_unittype(game.player_ptr, i) != -1);
gtk_list_store_append(activeunits_store, &it);
@@ -1189,9 +1187,9 @@
1, can,
2, unitarray[i].building_count,
3, unitarray[i].active_count,
- 4, unitarray[i].upkeep_shield,
- 5, unitarray[i].upkeep_food,
- 6, unitarray[i].upkeep_gold,
+ 4, unitarray[i].upkeep[O_SHIELD],
+ 5, unitarray[i].upkeep[O_FOOD],
+ 6, unitarray[i].upkeep[O_GOLD],
7, TRUE, -1);
g_value_init(&value, G_TYPE_STRING);
g_value_set_static_string(&value, unit_name(i));
@@ -1201,9 +1199,9 @@
activeunits_type[k]=(unitarray[i].active_count > 0) ? i : U_LAST;
k++;
unittotals.active_count += unitarray[i].active_count;
- unittotals.upkeep_shield += unitarray[i].upkeep_shield;
- unittotals.upkeep_food += unitarray[i].upkeep_food;
- unittotals.upkeep_gold += unitarray[i].upkeep_gold;
+ output_type_iterate(o) {
+ unittotals.upkeep[o] += unitarray[i].upkeep[o];
+ } output_type_iterate_end;
unittotals.building_count += unitarray[i].building_count;
}
} unit_type_iterate_end;
@@ -1213,9 +1211,9 @@
1, FALSE,
2, unittotals.building_count,
3, unittotals.active_count,
- 4, unittotals.upkeep_shield,
- 5, unittotals.upkeep_food,
- 6, unittotals.upkeep_gold,
+ 4, unittotals.upkeep[O_SHIELD],
+ 5, unittotals.upkeep[O_FOOD],
+ 6, unittotals.upkeep[O_GOLD],
7, FALSE, -1);
g_value_init(&value, G_TYPE_STRING);
g_value_set_static_string(&value, _("Totals:"));
Index: client/gui-sdl/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/repodlgs.c,v
retrieving revision 1.39
diff -u -r1.39 repodlgs.c
--- client/gui-sdl/repodlgs.c 20 Nov 2004 21:27:18 -0000 1.39
+++ client/gui-sdl/repodlgs.c 5 Dec 2004 09:59:02 -0000
@@ -94,12 +94,12 @@
(entries[pUnit->type].active_count)++;
(total->active_count)++;
if (pUnit->homecity) {
- entries[pUnit->type].upkeep_shield += pUnit->upkeep;
- total->upkeep_shield += pUnit->upkeep;
- entries[pUnit->type].upkeep_food += pUnit->upkeep_food;
- total->upkeep_food += pUnit->upkeep_food;
- entries[pUnit->type].upkeep_gold += pUnit->upkeep_gold;
- total->upkeep_gold += pUnit->upkeep_gold;
+ entries[pUnit->type].upkeep_shield += pUnit->upkeep[O_SHIELD];
+ total->upkeep_shield += pUnit->upkeep[O_SHIELD];
+ entries[pUnit->type].upkeep_food += pUnit->upkeep[O_FOOD];
+ total->upkeep_food += pUnit->upkeep[O_FOOD];
+ entries[pUnit->type].upkeep_gold += pUnit->upkeep[O_GOLD];
+ total->upkeep_gold += pUnit->upkeep[O_GOLD];
}
} unit_list_iterate_end;
Index: client/gui-win32/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/repodlgs.c,v
retrieving revision 1.43
diff -u -r1.43 repodlgs.c
--- client/gui-win32/repodlgs.c 25 Nov 2004 06:57:17 -0000 1.43
+++ client/gui-win32/repodlgs.c 5 Dec 2004 09:59:03 -0000
@@ -591,8 +591,9 @@
unit_list_iterate(game.player_ptr->units, punit) {
(unitarray[punit->type].active_count)++;
if (punit->homecity) {
- unitarray[punit->type].upkeep_shield += punit->upkeep;
- unitarray[punit->type].upkeep_food += punit->upkeep_food;
+ unitarray[punit->type].upkeep_shield += punit->upkeep[O_SHIELD];
+ unitarray[punit->type].upkeep_food += punit->upkeep[O_FOOD];
+ /* TODO: gold upkeep */
}
}
Index: client/gui-xaw/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/repodlgs.c,v
retrieving revision 1.64
diff -u -r1.64 repodlgs.c
--- client/gui-xaw/repodlgs.c 17 Nov 2004 19:21:14 -0000 1.64
+++ client/gui-xaw/repodlgs.c 5 Dec 2004 09:59:03 -0000
@@ -1065,8 +1065,9 @@
unit_list_iterate(game.player_ptr->units, punit) {
(unitarray[punit->type].active_count)++;
if (punit->homecity) {
- unitarray[punit->type].upkeep_shield += punit->upkeep;
- unitarray[punit->type].upkeep_food += punit->upkeep_food;
+ unitarray[punit->type].upkeep_shield += punit->upkeep[O_SHIELD];
+ unitarray[punit->type].upkeep_food += punit->upkeep[O_FOOD];
+ /* TODO: gold upkeep */
}
}
unit_list_iterate_end;
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.270
diff -u -r1.270 city.c
--- common/city.c 5 Dec 2004 09:01:00 -0000 1.270
+++ common/city.c 5 Dec 2004 09:59:03 -0000
@@ -1094,7 +1094,9 @@
} built_impr_iterate_end;
unit_list_iterate(pcity->units_supported, punit) {
- cost += punit->upkeep_gold;
+ /* FIXME: unlike food and shields, gold upkeep isn't subtracted off of
+ * the surplus directly when calculating unit support. */
+ cost += punit->upkeep[O_GOLD];
} unit_list_iterate_end;
return tax_total - cost;
@@ -2085,15 +2087,13 @@
/* Save old values so we can decide if the unit info should be resent */
int old_unhappiness = this_unit->unhappiness;
- int old_upkeep = this_unit->upkeep;
- int old_upkeep_food = this_unit->upkeep_food;
- int old_upkeep_gold = this_unit->upkeep_gold;
+ int old_upkeep = this_unit->upkeep[O_SHIELD];
+ int old_upkeep_food = this_unit->upkeep[O_FOOD];
+ int old_upkeep_gold = this_unit->upkeep[O_GOLD];
/* set current upkeep on unit to zero */
this_unit->unhappiness = 0;
- this_unit->upkeep = 0;
- this_unit->upkeep_food = 0;
- this_unit->upkeep_gold = 0;
+ memset(this_unit->upkeep, 0, O_COUNT * sizeof(*this_unit->upkeep));
/* This is how I think it should work (dwp)
* Base happy cost (unhappiness) assumes unit is being aggressive;
@@ -2125,30 +2125,30 @@
adjust_city_free_cost(&free_shield, &shield_cost);
if (shield_cost > 0) {
pcity->surplus[O_SHIELD] -= shield_cost;
- this_unit->upkeep = shield_cost;
+ this_unit->upkeep[O_SHIELD] = shield_cost;
}
}
if (food_cost > 0) {
adjust_city_free_cost(&free_food, &food_cost);
if (food_cost > 0) {
pcity->surplus[O_FOOD] -= food_cost;
- this_unit->upkeep_food = food_cost;
+ this_unit->upkeep[O_FOOD] = food_cost;
}
}
if (gold_cost > 0) {
adjust_city_free_cost(&free_gold, &gold_cost);
if (gold_cost > 0) {
- /* FIXME: This is not implemented -- SKi */
- this_unit->upkeep_gold = gold_cost;
+ /* FIXME: gold upkeep is subtracted off of the tax_total later. */
+ this_unit->upkeep[O_GOLD] = gold_cost;
}
}
/* Send unit info if anything has changed */
if (send_unit_info
&& (this_unit->unhappiness != old_unhappiness
- || this_unit->upkeep != old_upkeep
- || this_unit->upkeep_food != old_upkeep_food
- || this_unit->upkeep_gold != old_upkeep_gold)) {
+ || this_unit->upkeep[O_SHIELD] != old_upkeep
+ || this_unit->upkeep[O_FOOD] != old_upkeep_food
+ || this_unit->upkeep[O_GOLD] != old_upkeep_gold)) {
send_unit_info(unit_owner(this_unit), this_unit);
}
}
Index: common/packets.def
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.def,v
retrieving revision 1.67
diff -u -r1.67 packets.def
--- common/packets.def 5 Dec 2004 09:21:27 -0000 1.67
+++ common/packets.def 5 Dec 2004 09:59:03 -0000
@@ -620,7 +620,7 @@
UNIT_TYPE type;
UNIT transported_by; /* Only valid if transported is set. */
UINT8 movesleft, hp, fuel, activity_count;
- UINT8 unhappiness, upkeep, upkeep_food, upkeep_gold, occupy;
+ UINT8 unhappiness, upkeep[O_MAX], occupy;
COORD goto_dest_x,goto_dest_y;
ACTIVITY activity;
SPECIAL activity_target;
Index: common/unit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.c,v
retrieving revision 1.221
diff -u -r1.221 unit.c
--- common/unit.c 25 Nov 2004 06:37:30 -0000 1.221
+++ common/unit.c 5 Dec 2004 09:59:04 -0000
@@ -1710,9 +1710,7 @@
}
punit->goto_tile = NULL;
punit->veteran = veteran_level;
- punit->upkeep = 0;
- punit->upkeep_food = 0;
- punit->upkeep_gold = 0;
+ memset(punit->upkeep, 0, O_COUNT * sizeof(*punit->upkeep));
punit->unhappiness = 0;
/* A unit new and fresh ... */
punit->foul = FALSE;
Index: common/unit.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.h,v
retrieving revision 1.129
diff -u -r1.129 unit.h
--- common/unit.h 20 Oct 2004 18:20:53 -0000 1.129
+++ common/unit.h 5 Dec 2004 09:59:04 -0000
@@ -130,9 +130,7 @@
int hp;
int veteran;
int unhappiness;
- int upkeep;
- int upkeep_food;
- int upkeep_gold;
+ int upkeep[O_MAX];
int fuel;
int bribe_cost;
struct unit_ai ai;
Index: server/unittools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.c,v
retrieving revision 1.311
diff -u -r1.311 unittools.c
--- server/unittools.c 4 Dec 2004 00:29:38 -0000 1.311
+++ server/unittools.c 5 Dec 2004 09:59:05 -0000
@@ -260,7 +260,7 @@
unit_list_iterate_safe(pcity->units_supported, punit) {
- if (pplayer->economic.gold + potential_gold < punit->upkeep_gold) {
+ if (pplayer->economic.gold + potential_gold < punit->upkeep[O_GOLD]) {
/* We cannot upkeep this unit any longer and selling off city
* improvements will not help so we will have to disband */
assert(pplayer->economic.gold + potential_gold >= 0);
@@ -273,7 +273,7 @@
/* Gold can get negative here as city improvements will be sold
* afterwards to balance our budget. FIXME: Should units with gold
* upkeep give gold when they are disbanded? */
- pplayer->economic.gold -= punit->upkeep_gold;
+ pplayer->economic.gold -= punit->upkeep[O_GOLD];
}
} unit_list_iterate_safe_end;
}
@@ -1783,9 +1783,9 @@
packet->activity = punit->activity;
packet->activity_count = punit->activity_count;
packet->unhappiness = punit->unhappiness;
- packet->upkeep = punit->upkeep;
- packet->upkeep_food = punit->upkeep_food;
- packet->upkeep_gold = punit->upkeep_gold;
+ output_type_iterate(o) {
+ packet->upkeep[o] = punit->upkeep[o];
+ } output_type_iterate_end;
packet->ai = punit->ai.control;
packet->fuel = punit->fuel;
if (punit->goto_tile) {
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#11347) put unit upkeep values into an array,
Jason Short <=
|
|