[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 - Wed Dec 18 09:16:19 2002]:
> [Erik Sigra - Fri Dec 13 17:59:34 2002]:
> > This patch cleans up the procedure
> > handle_upgrade_unittype_request. Of course the behaviour is not
> > changed. The improvements are:
>
> I like your code better than the old code. But two things:
>
> Trivial style issue: unit_list_iterate_end should be on the same
> line with }.
>
> One other issue: from_unittype and to_unittype should be
> Unit_Type_id's.
Here is a patch with those things changed. But I have not changed from
"int" to "Unit_Type_id" in "struct packet_unittype_info" (packets.h).
Should I do that?
> can_upgrade_unittype is also a huge misnomer. Do you want to
> provide a (separate) patch to rename it?
I suppose I could, do you expect something like "successor_unittype"?
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-18 19:17:18.000000000 +0100
+++ freeciv-cleanup_handle_upgrade_unittype_request/server/unithand.c
2002-12-18 19:23:39.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
+(const 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-18 19:23:39.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
+(const 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
|
|