[Freeciv-Dev] (PR#2566) PATCH: clean up handle_upgrade_unittype_request
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
[jdorje - Thu Dec 19 07:54:40 2002]:
> gcc -DHAVE_CONFIG_H -I. -I. -I.. -I./../common -I./../ai -I../intl
> -g -Wall -Werror -Wpointer-arith -Wcast-align -Wmissing-prototypes
> -Wmissing-declarations -O3 -c `test -f 'unithand.c' || echo
> './'`unithand.c
> cc1: warnings being treated as errors
> unithand.c: In function `handle_upgrade_unittype_request':
> unithand.c:163: warning: passing arg 1 of `can_upgrade_unittype'
> discards qualifiers from pointer target type
> unithand.c:169: warning: passing arg 1 of `unit_upgrade_price'
> discards qualifiers from pointer target type
> unithand.c:174: warning: passing arg 1 of `conn_list_do_buffer'
> discards qualifiers from pointer target type
> unithand.c:175: warning: passing arg 2 of `genlist_iterator_init'
> discards qualifiers from pointer target type
> unithand.c:182: warning: assignment of read-only member `gold'
> unithand.c:188: warning: passing arg 1 of `conn_list_do_unbuffer'
> discards qualifiers from pointer target type
> unithand.c:196: warning: passing arg 1 of `send_player_info'
> discards qualifiers from pointer target type
> unithand.c:196: warning: passing arg 2 of `send_player_info'
> discards qualifiers from pointer target type
>
> So until/unless the underlying functions are also changed it is no
> good to add lots of 'const' qualifiers here.
OK. Here is a patch with that 'const' qualifier removed.
diff -rXi -U2 freeciv/server/srv_main.c
freeciv-cleanup_handle_upgrade_unittype_request/server/srv_main.c
--- freeciv/server/srv_main.c 2002-12-18 19:17:18.000000000 +0100
+++ freeciv-cleanup_handle_upgrade_unittype_request/server/srv_main.c
2002-12-18 19:23:39.000000000 +0100
@@ -851,5 +851,5 @@
case PACKET_UNITTYPE_UPGRADE:
- handle_upgrade_unittype_request(pplayer, (struct packet_unittype_info
*)packet);
+ handle_upgrade_unittype_request(pplayer, ((struct packet_unittype_info
*)packet)->type);
break;
diff -rXi -U2 freeciv/server/unithand.c
freeciv-cleanup_handle_upgrade_unittype_request/server/unithand.c
--- freeciv/server/unithand.c 2002-12-19 09:59:53.000000000 +0100
+++ freeciv-cleanup_handle_upgrade_unittype_request/server/unithand.c
2002-12-19 10:38:50.000000000 +0100
@@ -157,38 +157,43 @@
Upgrade all units of a given type.
**************************************************************************/
-void handle_upgrade_unittype_request(struct player *pplayer,
- struct packet_unittype_info *packet)
+void handle_upgrade_unittype_request
+(struct player * const pplayer, const Unit_Type_id from_unittype)
{
- int cost;
- int to_unit;
- int upgraded = 0;
- if ((to_unit =can_upgrade_unittype(pplayer, packet->type)) == -1) {
- notify_player(pplayer, _("Game: Illegal packet, can't upgrade %s (yet)."),
- unit_types[packet->type].name);
- return;
- }
- cost = unit_upgrade_price(pplayer, packet->type, to_unit);
- conn_list_do_buffer(&pplayer->connections);
- unit_list_iterate(pplayer->units, punit) {
- struct city *pcity;
- if (cost > pplayer->economic.gold)
- break;
- pcity = map_get_city(punit->x, punit->y);
- if (punit->type == packet->type && pcity && pcity->owner ==
pplayer->player_no) {
- pplayer->economic.gold -= cost;
-
- upgrade_unit(punit, to_unit);
- upgraded++;
- }
- }
- unit_list_iterate_end;
- conn_list_do_unbuffer(&pplayer->connections);
- if (upgraded > 0) {
- notify_player(pplayer, _("Game: %d %s upgraded to %s for %d gold."),
- upgraded, unit_types[packet->type].name,
- unit_types[to_unit].name, cost * upgraded);
- send_player_info(pplayer, pplayer);
+ const Unit_Type_id to_unittype =
+ can_upgrade_unittype(pplayer, from_unittype);
+ if (to_unittype == -1) {
+ notify_player(pplayer, _("Game: Illegal packet, can't upgrade %s (yet)."),
+ unit_types[from_unittype].name);
} else {
- notify_player(pplayer, _("Game: No units could be upgraded."));
+ const int cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
+ int number_of_upgraded_units = 0;
+
+ if (pplayer->economic.gold >= cost) {
+ const int player_no = pplayer->player_no;
+ conn_list_do_buffer(&pplayer->connections);
+ unit_list_iterate(pplayer->units, punit) {
+ if (punit->type == from_unittype) {
+ const struct city * const pcity = map_get_city(punit->x, punit->y);
+ if (pcity && pcity->owner == player_no) {
+ upgrade_unit(punit, to_unittype);
+ ++number_of_upgraded_units;
+ if ((pplayer->economic.gold -= cost) < cost) {
+ break;
+ }
+ }
+ }
+ } unit_list_iterate_end;
+ conn_list_do_unbuffer(&pplayer->connections);
+ }
+
+ if (number_of_upgraded_units > 0) {
+ notify_player(pplayer, _("Game: %d %s upgraded to %s for %d gold."),
+ number_of_upgraded_units, unit_types[from_unittype].name,
+ unit_types[to_unittype].name,
+ cost * number_of_upgraded_units);
+ send_player_info(pplayer, pplayer);
+ } else {
+ notify_player(pplayer, _("Game: No units could be upgraded."));
+ }
}
}
diff -rXi -U2 freeciv/server/unithand.h
freeciv-cleanup_handle_upgrade_unittype_request/server/unithand.h
--- freeciv/server/unithand.h 2002-12-18 19:17:18.000000000 +0100
+++ freeciv-cleanup_handle_upgrade_unittype_request/server/unithand.h
2002-12-19 10:38:23.000000000 +0100
@@ -23,6 +23,6 @@
void handle_unit_connect(struct player *pplayer,
struct packet_unit_connect *req);
-void handle_upgrade_unittype_request(struct player *pplayer,
- struct packet_unittype_info *packet);
+void handle_upgrade_unittype_request
+(struct player * const pplayer, const Unit_Type_id from_unittype);
void handle_unit_upgrade_request(struct player *pplayer,
struct packet_unit_request *packet);
[Freeciv-Dev] (PR#2566) PATCH: clean up handle_upgrade_unittype_request, Guest via RT, 2002/12/19
[Freeciv-Dev] (PR#2566) PATCH: clean up handle_upgrade_unittype_request, Guest via RT, 2002/12/20
[Freeciv-Dev] (PR#2566) PATCH: clean up handle_upgrade_unittype_request, Guest via RT, 2002/12/20
|
|