From e73883b0d29b73de4a5b829ac74575f0baee1af2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Fri, 14 Oct 2022 17:18:22 +0200
Subject: [PATCH] =?UTF-8?q?!OSDN=20#TICKET:=2045429=20:=20S=C5=82awomir=20?=
 =?UTF-8?q?Lach=20<slawek@lach.art.pl>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Send city counters (at turn end) for player, who own this city.
Also add support for this packet to client-side. Client routines will
only update information.

diff --git a/client/packhand.c b/client/packhand.c
index e6f8069e87..1385ad89f3 100644
--- a/client/packhand.c
+++ b/client/packhand.c
@@ -5478,3 +5478,23 @@ void handle_ruleset_counter(const struct packet_ruleset_counter *packet)
 
   attach_city_counter(curr);
 }
+
+void handle_city_update_counter(int city, int counter, int value)
+{
+  int counter_count = counters_get_city_counters_count();
+  struct city *pcity = game_city_by_number(city);
+  
+  if (NULL == pcity) {
+  
+    return;
+  }
+  
+  if (counter_count <= counter) {
+  
+    return;
+  }
+  
+  pcity->counter_values[counter] = value;
+  
+}
+
diff --git a/common/networking/packets.def b/common/networking/packets.def
index d14136c100..14ae80db7f 100644
--- a/common/networking/packets.def
+++ b/common/networking/packets.def
@@ -3,7 +3,7 @@
 Max used id:
 ============
 
-Max id: 513
+Max id: 514
 
 Packets are not ordered by their id, but by their category. New packet
 with higher id may get added to existing category, and not to the end of file.
@@ -267,8 +267,8 @@ type CAPITAL            = uint8(enum capital_type)
 type WONDER_VISIB       = uint8(enum wonder_visib_type)
 type TRANSP_DEF_TYPE    = uint8(enum transp_def_type)
 type AI_LEVEL           = uint8(enum ai_level)
-type COUNTER_TARGET       = uint8(enum counter_target)
-type COUNTER_BEHAVIOUR       = uint8(enum counter_behaviour)
+type COUNTER_BEHAVIOUR  = uint8(enum counter_behaviour)
+type COUNTER_TARGET     = uint8(enum counter_target)
 
 # typedefs for bit vectors
 type BV_ACTIONS         = bitvector(bv_actions)
@@ -303,6 +303,7 @@ type ACTION_ID          = uint8(action_id)
 type ACTION_TGT         = SINT32
 type ACTION_SUB_TGT     = SINT16
 type BASE               = sint8(Base_type_id)
+type COUNTER            = UINT32
 type ROAD               = sint8(Road_type_id)
 type CITY               = UINT16
 # city id, with space for special values
@@ -772,6 +773,12 @@ PACKET_CITY_INFO = 31; sc, lsend, is-game-info, force, cancel(PACKET_CITY_SHORT_
   UNIT_ORDER rally_point_orders[MAX_LEN_ROUTE:rally_point_length];
 end
 
+PACKET_CITY_UPDATE_COUNTER = 514; sc, lsend
+  CITY city;
+  COUNTER counter;
+  UINT32 value;
+end
+
 PACKET_CITY_SHORT_INFO = 32; sc, lsend, is-game-info, cancel(PACKET_CITY_INFO), cancel(PACKET_WEB_CITY_INFO_ADDITION)
   CITY id; key
   TILE tile;
@@ -1959,6 +1966,7 @@ PACKET_RULESET_DESCRIPTION_PART = 247; sc, lsend
   STRING text[MAX_LEN_CONTENT];
 end
 
+
 /*********************************************************
  Below are the packets that control single-player mode.  
 *********************************************************/
diff --git a/server/srv_main.c b/server/srv_main.c
index 1123b3dd4c..86ca8f74a4 100644
--- a/server/srv_main.c
+++ b/server/srv_main.c
@@ -1534,6 +1534,8 @@ static void end_turn(void)
   players_iterate(pplayer) {
     city_list_iterate(pplayer->cities, pcity) {
       city_counters_iterate(pcount) {
+        int old_val = pcity->counter_values[pcount->index];
+        
         if (pcount->type == CB_CITY_OWNED_TURNS) {
           pcity->counter_values[pcount->index]++;
         }
@@ -1545,6 +1547,19 @@ static void end_turn(void)
             pcity->counter_values[pcount->index] = 0;
           }
         }
+        
+        if (pcity->counter_values[pcount->index] != old_val) {
+          struct packet_city_update_counter packet;
+          
+          packet.city = pcity->id;
+          packet.counter = counter_index(pcount);
+          packet.value = pcity->counter_values[pcount->index];
+          
+          if (is_human(pplayer) && NULL != pplayer->current_conn) {
+          
+            send_packet_city_update_counter(pplayer->current_conn, &packet);
+          }
+        }
       } city_counters_iterate_end;
     } city_list_iterate_end;
   } players_iterate_end;
-- 
2.38.0

