<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40509 >

As requested in 17880, the attached patch implements editable
object properties for the property editor corresponding to the
city food and shield stocks.


-----------------------------------------------------------------------
でも、古い家にお化けと一緒に住んでいることが好きです!
diff --git a/client/gui-gtk-2.0/editprop.c b/client/gui-gtk-2.0/editprop.c
index 67d6012..d741071 100644
--- a/client/gui-gtk-2.0/editprop.c
+++ b/client/gui-gtk-2.0/editprop.c
@@ -15,6 +15,8 @@
 #include <config.h>
 #endif
 
+#include <limits.h> /* USHRT_MAX */
+
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 
@@ -248,6 +250,8 @@ enum object_property_ids {
   OPID_CITY_XY,
   OPID_CITY_SIZE,
   OPID_CITY_BUILDINGS,
+  OPID_CITY_FOOD_STOCK,
+  OPID_CITY_SHIELD_STOCK,
 
   OPID_PLAYER_NAME,
   OPID_PLAYER_NATION,
@@ -1227,6 +1231,12 @@ static struct propval *objbind_get_value_from_object(struct objbind *ob,
     case OPID_CITY_BUILDINGS:
       pv->data.v_built = pcity->built;
       break;
+    case OPID_CITY_FOOD_STOCK:
+      pv->data.v_int = pcity->food_stock;
+      break;
+    case OPID_CITY_SHIELD_STOCK:
+      pv->data.v_int = pcity->shield_stock;
+      break;
     default:
       freelog(LOG_ERROR, "Unhandled request for value of property %d "
               "(%s) from object of type \"%s\" in "
@@ -1352,6 +1362,11 @@ static bool objbind_get_allowed_value_span(struct objbind *ob,
     }
 
   } else if (objtype == OBJTYPE_CITY) {
+    struct city *pcity = objbind_get_object(ob);
+
+    if (!pcity) {
+      return FALSE;
+    }
 
     switch (propid) {
     case OPID_CITY_SIZE:
@@ -1360,6 +1375,18 @@ static bool objbind_get_allowed_value_span(struct objbind *ob,
       step = 1;
       big_step = 5;
       break;
+    case OPID_CITY_FOOD_STOCK:
+      min = 0;
+      max = city_granary_size(pcity->size);
+      step = 1;
+      big_step = 5;
+      break;
+    case OPID_CITY_SHIELD_STOCK:
+      min = 0;
+      max = USHRT_MAX; /* Limited to uint16 by city info packet. */
+      step = 1;
+      big_step = 10;
+      break;
     default:
       freelog(LOG_ERROR, "Unhandled request for value range of "
               "property %d (%s) from object of type \"%s\" in "
@@ -1652,6 +1679,8 @@ static void objbind_pack_current_values(struct objbind *ob,
     for (i = 0; i < B_LAST; i++) {
       packet->built[i] = pcity->built[i].turn;
     }
+    packet->food_stock = pcity->food_stock;
+    packet->shield_stock = pcity->shield_stock;
     /* TODO: Set more packet fields. */
 
   } else if (objtype == OBJTYPE_PLAYER) {
@@ -1768,6 +1797,12 @@ static void objbind_pack_modified_value(struct objbind *ob,
     case OPID_CITY_SIZE:
       packet->size = pv->data.v_int;
       break;
+    case OPID_CITY_FOOD_STOCK:
+      packet->food_stock = pv->data.v_int;
+      break;
+    case OPID_CITY_SHIELD_STOCK:
+      packet->shield_stock = pv->data.v_int;
+      break;
     case OPID_CITY_BUILDINGS:
       for (i = 0; i < B_LAST; i++) {
         packet->built[i] = pv->data.v_built[i].turn;
@@ -2140,6 +2175,7 @@ static void objprop_setup_widget(struct objprop *op)
     break;
 
   case OPID_CITY_SIZE:
+  case OPID_CITY_SHIELD_STOCK:
   case OPID_GAME_YEAR:
     spin = gtk_spin_button_new_with_range(0.0, 100.0, 1.0);
     g_signal_connect(spin, "value-changed",
@@ -2149,6 +2185,7 @@ static void objprop_setup_widget(struct objprop *op)
     break;
 
   case OPID_UNIT_MOVES_LEFT:
+  case OPID_CITY_FOOD_STOCK:
     hbox2 = gtk_hbox_new(FALSE, 4);
     gtk_box_pack_start(GTK_BOX(hbox), hbox2, TRUE, TRUE, 0);
     spin = gtk_spin_button_new_with_range(0.0, 100.0, 1.0);
@@ -2303,6 +2340,7 @@ static void objprop_refresh_widget(struct objprop *op,
     break;
 
   case OPID_CITY_SIZE:
+  case OPID_CITY_SHIELD_STOCK:
   case OPID_GAME_YEAR:
     spin = objprop_get_child_widget(op, "spin");
     if (pv) {
@@ -2322,6 +2360,7 @@ static void objprop_refresh_widget(struct objprop *op,
     break;
 
   case OPID_UNIT_MOVES_LEFT:
+  case OPID_CITY_FOOD_STOCK:
     spin = objprop_get_child_widget(op, "spin");
     label = objprop_get_child_widget(op, "max-value-label");
     if (pv) {
@@ -3097,6 +3136,10 @@ static void property_page_setup_objprops(struct property_page *pp)
             OPF_IN_LISTVIEW | OPF_HAS_WIDGET | OPF_EDITABLE, VALTYPE_INT);
     ADDPROP(OPID_CITY_BUILDINGS, _("Buildings"), OPF_IN_LISTVIEW
             | OPF_HAS_WIDGET | OPF_EDITABLE, VALTYPE_BUILT_ARRAY);
+    ADDPROP(OPID_CITY_FOOD_STOCK, _("Food Stock"),
+            OPF_IN_LISTVIEW | OPF_HAS_WIDGET | OPF_EDITABLE, VALTYPE_INT);
+    ADDPROP(OPID_CITY_SHIELD_STOCK, _("Shield Stock"),
+            OPF_IN_LISTVIEW | OPF_HAS_WIDGET | OPF_EDITABLE, VALTYPE_INT);
     break;
 
   case OBJTYPE_PLAYER:
diff --git a/common/packets.def b/common/packets.def
index a0e0f45..49a7433 100644
--- a/common/packets.def
+++ b/common/packets.def
@@ -1542,6 +1542,7 @@ PACKET_EDIT_CITY=167;cs,handle-per-conn,handle-via-packet
   UINT8 specialists_size;
   UINT8 specialists[SP_MAX:specialists_size];
   UINT16 trade[NUM_TRADEROUTES];
+  UINT16 food_stock, shield_stock;
   BOOL airlift;
   BOOL debug;
   BOOL did_buy;
diff --git a/server/edithand.c b/server/edithand.c
index f87772e..e120d8b 100644
--- a/server/edithand.c
+++ b/server/edithand.c
@@ -16,6 +16,7 @@
 #endif
 
 #include <assert.h>
+#include <limits.h> /* USHRT_MAX */
 
 #include "events.h"
 #include "fcintl.h"
@@ -709,6 +710,33 @@ void handle_edit_city(struct connection *pc,
     }
   } improvement_iterate_end;
  
+  /* Handle food stock change. */
+  if (packet->food_stock != pcity->food_stock) {
+    int max = city_granary_size(pcity->size);
+    if (!(0 <= packet->food_stock && packet->food_stock <= max)) {
+      notify_conn(pc->self, ptile, E_BAD_COMMAND,
+                  _("Invalid city food stock amount %d for city %s "
+                    "(allowed range is %d to %d)."),
+                  packet->food_stock, pcity->name, 0, max);
+    } else {
+      pcity->food_stock = packet->food_stock;
+      changed = TRUE;
+    }
+  }
+
+  /* Handle shield stock change. */
+  if (packet->shield_stock != pcity->shield_stock) {
+    int max = USHRT_MAX; /* Limited to uint16 by city info packet. */
+    if (!(0 <= packet->shield_stock && packet->shield_stock <= max)) {
+      notify_conn(pc->self, ptile, E_BAD_COMMAND,
+                  _("Invalid city shield stock amount %d for city %s "
+                    "(allowed range is %d to %d)."),
+                  packet->shield_stock, pcity->name, 0, max);
+    } else {
+      pcity->shield_stock = packet->shield_stock;
+      changed = TRUE;
+    }
+  }
 
   /* TODO: Handle more property edits. */
 
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to