[Freeciv-Dev] (PR#14983) rehoming fixes
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://bugs.freeciv.org/Ticket/Display.html?id=14983 >
This patch cleans up and fixes rehoming behavior. Previously
can_unit_change_homecity was not called in most places so rehoming was
erronously allowed even if the function would have returned false.
This applies to 2.0 also.
-jason
Index: server/unithand.c
===================================================================
--- server/unithand.c (revision 11385)
+++ server/unithand.c (working copy)
@@ -323,22 +323,11 @@
int city_id)
{
struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
- struct city *new_pcity = player_find_city_by_id(pplayer, city_id);
+ struct city *pcity = player_find_city_by_id(pplayer, city_id);
- if (!punit || !new_pcity || city_id == punit->homecity) {
- return;
+ if (punit && pcity && can_unit_change_homecity_to(punit, pcity)) {
+ real_unit_change_homecity(punit, pcity);
}
- if (!same_pos(punit->tile, new_pcity->tile)) {
- freelog(LOG_ERROR,
- "%s's %s (%d) is at (%d,%d), but tried to make "
- "%s at (%d,%d) its homecity.",
- pplayer->name,
- unit_name(punit->type), unit_id, punit->tile->x,
- punit->tile->y, new_pcity->name, new_pcity->tile->x,
- new_pcity->tile->y);
- return;
- }
- real_unit_change_homecity(punit, new_pcity);
}
/**************************************************************************
Index: common/unit.c
===================================================================
--- common/unit.c (revision 11385)
+++ common/unit.c (working copy)
@@ -423,22 +423,34 @@
}
/**************************************************************************
- Return TRUE iff the unit can change homecity at its current location.
+ Return TRUE iff the unit can change homecity to the given city.
**************************************************************************/
-bool can_unit_change_homecity(const struct unit *punit)
+bool can_unit_change_homecity_to(const struct unit *punit,
+ const struct city *pcity)
{
/* Requirements to change homecity:
*
* 1. Homeless cities can't change homecity (this is a feature since
* being homeless is a big benefit).
* 2. The unit must be inside the city it is rehoming to.
- * 3. Of course you can only have your own cities as homecity. */
- return (punit->homecity != -1
+ * 3. Of course you can only have your own cities as homecity.
+ * 4. You can't rehome to the current homecity. */
+ return (punit && pcity
+ && punit->homecity > 0
&& punit->tile->city
- && punit->tile->city->owner == punit->owner);
+ && punit->tile->city->owner == punit->owner
+ && punit->homecity != punit->tile->city->id);
}
/**************************************************************************
+ Return TRUE iff the unit can change homecity at its current location.
+**************************************************************************/
+bool can_unit_change_homecity(const struct unit *punit)
+{
+ return can_unit_change_homecity_to(punit, punit->tile->city);
+}
+
+/**************************************************************************
Returns the speed of a unit doing an activity. This depends on the
veteran level and the base move_rate of the unit (regardless of HP or
effects). Usually this is just used for settlers but the value is also
Index: common/unit.h
===================================================================
--- common/unit.h (revision 11385)
+++ common/unit.h (working copy)
@@ -254,6 +254,8 @@
bool can_unit_unload(const struct unit *punit, const struct unit *ptrans);
bool can_unit_paradrop(const struct unit *punit);
bool can_unit_bombard(const struct unit *punit);
+bool can_unit_change_homecity_to(const struct unit *punit,
+ const struct city *pcity);
bool can_unit_change_homecity(const struct unit *punit);
const char *get_activity_text(enum unit_activity activity);
bool can_unit_continue_current_activity(struct unit *punit);
Index: client/gui-gtk-2.0/citydlg.c
===================================================================
--- client/gui-gtk-2.0/citydlg.c (revision 11385)
+++ client/gui-gtk-2.0/citydlg.c (working copy)
@@ -2100,11 +2100,8 @@
G_CALLBACK(unit_homecity_callback),
GINT_TO_POINTER(punit->id));
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+ gtk_widget_set_sensitive(item, can_unit_change_homecity_to(punit, pcity));
- if (punit->homecity == pcity->id) {
- gtk_widget_set_sensitive(item, FALSE);
- }
-
item = gtk_menu_item_new_with_mnemonic(_("U_pgrade unit"));
g_signal_connect(item, "activate",
G_CALLBACK(unit_upgrade_callback),
Index: server/unithand.c
===================================================================
--- server/unithand.c (revision 11385)
+++ server/unithand.c (working copy)
@@ -290,19 +290,10 @@
struct city *old_pcity, *new_pcity =
player_find_city_by_id(pplayer, city_id);
- if (!punit || !new_pcity || city_id == punit->homecity) {
+ if (!punit || !new_pcity
+ || !can_unit_change_homecity_to(punit, new_pcity)) {
return;
}
- if (!same_pos(punit->tile, new_pcity->tile)) {
- freelog(LOG_ERROR,
- "%s's %s (%d) is at (%d,%d), but tried to make "
- "%s at (%d,%d) its homecity.",
- pplayer->name,
- unit_name(punit->type), unit_id, punit->tile->x,
- punit->tile->y, new_pcity->name, new_pcity->tile->x,
- new_pcity->tile->y);
- return;
- }
old_pcity = player_find_city_by_id(pplayer, punit->homecity);
Index: common/unit.c
===================================================================
--- common/unit.c (revision 11385)
+++ common/unit.c (working copy)
@@ -503,18 +503,29 @@
/**************************************************************************
...
**************************************************************************/
-bool can_unit_change_homecity(struct unit *punit)
+bool can_unit_change_homecity_to(struct unit *punit, struct city *pcity)
{
/* Requirements to change homecity:
*
* 1. Homeless cities can't change homecity (this is a feature since
* being homeless is a big benefit).
* 2. The unit must be inside the city it is rehoming to.
- * 3. Of course you can only have your own cities as homecity. */
- return (punit->homecity != -1
+ * 3. Of course you can only have your own cities as homecity.
+ * 4. You can't rehome to the current homecity. */
+ return (punit && pcity
+ && punit->homecity > 0
&& punit->tile->city
- && punit->tile->city->owner == punit->owner);
+ && punit->tile->city->owner == punit->owner
+ && punit->homecity != punit->tile->city->id);
}
+
+/**************************************************************************
+ Return TRUE iff the unit can change homecity at its current location.
+**************************************************************************/
+bool can_unit_change_homecity(struct unit *punit)
+{
+ return can_unit_change_homecity_to(punit, punit->tile->city);
+}
/**************************************************************************
Returns the speed of a unit doing an activity. This depends on the
Index: common/unit.h
===================================================================
--- common/unit.h (revision 11385)
+++ common/unit.h (working copy)
@@ -235,6 +235,7 @@
bool can_unit_unload(struct unit *punit, struct unit *ptrans);
bool can_unit_paradrop(struct unit *punit);
bool can_unit_bombard(struct unit *punit);
+bool can_unit_change_homecity_to(struct unit *punit, struct city *pcity);
bool can_unit_change_homecity(struct unit *punit);
const char *get_activity_text(enum unit_activity activity);
bool can_unit_continue_current_activity(struct unit *punit);
Index: client/gui-gtk-2.0/citydlg.c
===================================================================
--- client/gui-gtk-2.0/citydlg.c (revision 11385)
+++ client/gui-gtk-2.0/citydlg.c (working copy)
@@ -2001,11 +2001,8 @@
G_CALLBACK(unit_homecity_callback),
GINT_TO_POINTER(punit->id));
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+ gtk_widget_set_sensitive(item, can_unit_change_homecity_to(punit, pcity));
- if (punit->homecity == pcity->id) {
- gtk_widget_set_sensitive(item, FALSE);
- }
-
item = gtk_menu_item_new_with_mnemonic(_("U_pgrade unit"));
g_signal_connect(item, "activate",
G_CALLBACK(unit_upgrade_callback),
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#14983) rehoming fixes,
Jason Short <=
|
|