Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2003:
[Freeciv-Dev] (PR#4717) new function city_change_specialist
Home

[Freeciv-Dev] (PR#4717) new function city_change_specialist

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#4717) new function city_change_specialist
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 29 Jul 2003 09:05:25 -0700
Reply-to: rt@xxxxxxxxxxxxxx

This patch adds a function city_change_specialist that is a wrapper for 
a send_packet_city_request(..., PACKET_CITY_CHANGE_SPECIALIST).  It cuts 
out lots of code and avoids sending uninitalized data over the network.

Another function city_rotate_specialist is also introduced.  This is 
used by GTK and GTK-2.0 clients to easily rotate specialists.

Similar functions can be/have been written for other city requests.

jason

Index: client/citydlg_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/citydlg_common.c,v
retrieving revision 1.18
diff -u -r1.18 citydlg_common.c
--- client/citydlg_common.c     2003/07/29 15:16:31     1.18
+++ client/citydlg_common.c     2003/07/29 15:55:27
@@ -322,6 +322,40 @@
 }
 
 /**************************************************************************
+  Rotate the given specialist citizen to the next type of citizen.
+**************************************************************************/
+void city_rotate_specialist(struct city *pcity, int citizen_index)
+{
+  enum citizen_type citizens[MAX_CITY_SIZE];
+  enum citizen_type from, to;
+
+  if (citizen_index < 0 || citizen_index >= pcity->size) {
+    return;
+  }
+
+  get_city_citizen_types(pcity, 4, citizens);
+
+  switch (citizens[citizen_index]) {
+  case CITIZEN_ELVIS:
+    from = SP_ELVIS;
+    to = SP_SCIENTIST;
+    break;
+  case CITIZEN_SCIENTIST:
+    from = SP_SCIENTIST;
+    to = SP_TAXMAN;
+    break;
+  case CITIZEN_TAXMAN:
+    from = SP_TAXMAN;
+    to = SP_ELVIS;
+    break;
+  default:
+    return;
+  }
+
+  city_change_specialist(pcity, from, to);
+}
+    
+/**************************************************************************
   Activate all units on the given map tile.
 **************************************************************************/
 void activate_all_units(int map_x, int map_y)
@@ -376,4 +410,25 @@
   packet.specialist_from = packet.specialist_to = -1;
 
   return send_packet_city_request(&aconnection, &packet, PACKET_CITY_SELL);
+}
+
+/**************************************************************************
+  Change a specialist in the given city.  Return the request ID.
+**************************************************************************/
+int city_change_specialist(struct city *pcity, enum specialist_type from,
+                          enum specialist_type to)
+{
+  struct packet_city_request packet;
+
+  packet.city_id = pcity->id;
+  packet.specialist_from = from;
+  packet.specialist_to = to;
+
+  /* Fill out unused fields. */
+  packet.build_id = -1;
+  packet.is_build_id_unit_id = FALSE;
+  packet.worker_x = packet.worker_y = -1;
+
+  return send_packet_city_request(&aconnection, &packet,
+                                 PACKET_CITY_CHANGE_SPECIALIST);
 }
Index: client/citydlg_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/citydlg_common.h,v
retrieving revision 1.11
diff -u -r1.11 citydlg_common.h
--- client/citydlg_common.h     2003/07/29 15:16:31     1.11
+++ client/citydlg_common.h     2003/07/29 15:55:27
@@ -16,6 +16,7 @@
 
 #include <stddef.h>            /* size_t */
 
+#include "city.h"              /* enum specialist_type */
 #include "improvement.h"       /* Impr_Type_id */
 #include "shared.h"            /* bool type */
 
@@ -48,10 +49,13 @@
 
 void get_city_citizen_types(struct city *pcity, int index,
                            enum citizen_type *citizens);
+void city_rotate_specialist(struct city *pcity, int citizen_index);
 
 void activate_all_units(int map_x, int map_y);
 
 int city_change_production(struct city *pcity, bool is_unit, int build_id);
 int city_sell_improvement(struct city *pcity, Impr_Type_id sell_id);
+int city_change_specialist(struct city *pcity, enum specialist_type from,
+                          enum specialist_type to);
 
 #endif /* FC__CITYDLG_COMMON_H */
Index: client/agents/cma_core.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/agents/cma_core.c,v
retrieving revision 1.45
diff -u -r1.45 cma_core.c
--- client/agents/cma_core.c    2003/05/05 12:11:12     1.45
+++ client/agents/cma_core.c    2003/07/29 15:55:27
@@ -392,10 +392,7 @@
 
   /* Change surplus scientists to entertainers */
   for (i = 0; i < pcity->ppl_scientist - result->scientists; i++) {
-    packet.specialist_from = SP_SCIENTIST;
-    packet.specialist_to = SP_ELVIS;
-    last_request_id = send_packet_city_request(&aconnection, &packet,
-                                              PACKET_CITY_CHANGE_SPECIALIST);
+    last_request_id = city_change_specialist(pcity, SP_SCIENTIST, SP_ELVIS);
     if (first_request_id == 0) {
       first_request_id = last_request_id;
     }
@@ -403,10 +400,7 @@
 
   /* Change surplus taxmen to entertainers */
   for (i = 0; i < pcity->ppl_taxman - result->taxmen; i++) {
-    packet.specialist_from = SP_TAXMAN;
-    packet.specialist_to = SP_ELVIS;
-    last_request_id = send_packet_city_request(&aconnection, &packet,
-                                              PACKET_CITY_CHANGE_SPECIALIST);
+    last_request_id = city_change_specialist(pcity, SP_TAXMAN, SP_ELVIS);
     if (first_request_id == 0) {
       first_request_id = last_request_id;
     }
@@ -427,10 +421,7 @@
 
   /* Set scientists. */
   for (i = 0; i < result->scientists - pcity->ppl_scientist; i++) {
-    packet.specialist_from = SP_ELVIS;
-    packet.specialist_to = SP_SCIENTIST;
-    last_request_id = send_packet_city_request(&aconnection, &packet,
-                                              PACKET_CITY_CHANGE_SPECIALIST);
+    last_request_id = city_change_specialist(pcity, SP_ELVIS, SP_SCIENTIST);
     if (first_request_id == 0) {
       first_request_id = last_request_id;
     }
@@ -438,10 +429,7 @@
 
   /* Set taxmen. */
   for (i = 0; i < result->taxmen - pcity->ppl_taxman; i++) {
-    packet.specialist_from = SP_ELVIS;
-    packet.specialist_to = SP_TAXMAN;
-    last_request_id = send_packet_city_request(&aconnection, &packet,
-                                              PACKET_CITY_CHANGE_SPECIALIST);
+    last_request_id = city_change_specialist(pcity, SP_ELVIS, SP_TAXMAN);
     if (first_request_id == 0) {
       first_request_id = last_request_id;
     }
Index: client/gui-gtk/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/citydlg.c,v
retrieving revision 1.165
diff -u -r1.165 citydlg.c
--- client/gui-gtk/citydlg.c    2003/07/29 15:16:31     1.165
+++ client/gui-gtk/citydlg.c    2003/07/29 15:55:28
@@ -2749,51 +2749,16 @@
 static void citizens_callback(GtkWidget * w, GdkEventButton * ev,
                              gpointer data)
 {
-  struct city_dialog *pdialog = (struct city_dialog *) data;
+  struct city_dialog *pdialog = data;
   struct city *pcity = pdialog->pcity;
-  struct packet_city_request packet;
   int citnum;
-  enum specialist_type type;
-  enum citizen_type citizens[MAX_CITY_SIZE];
 
   if (ev->x > (pcity->size - 1) * pdialog->cwidth + SMALL_TILE_WIDTH)
     return;                    /* no citizen that far to the right */
 
   citnum = MIN(pcity->size - 1, ev->x / pdialog->cwidth);
 
-  get_city_citizen_types(pcity, 4, citizens);
-
-  switch (citizens[citnum]) {
-  case CITIZEN_ELVIS:
-    type = SP_ELVIS;
-    break;
-  case CITIZEN_SCIENTIST:
-    type = SP_SCIENTIST;
-    break;
-  case CITIZEN_TAXMAN:
-    type = SP_TAXMAN;
-    break;
-  default:
-    return;
-  }
-
-  packet.city_id = pdialog->pcity->id;
-  packet.specialist_from = type;
-
-  switch (type) {
-  case SP_ELVIS:
-    packet.specialist_to = SP_SCIENTIST;
-    break;
-  case SP_SCIENTIST:
-    packet.specialist_to = SP_TAXMAN;
-    break;
-  case SP_TAXMAN:
-    packet.specialist_to = SP_ELVIS;
-    break;
-  }
-
-  send_packet_city_request(&aconnection, &packet,
-                          PACKET_CITY_CHANGE_SPECIALIST);
+  city_rotate_specialist(pcity, citnum);
 }
 
 /**************************************************************************
Index: client/gui-gtk-2.0/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/citydlg.c,v
retrieving revision 1.57
diff -u -r1.57 citydlg.c
--- client/gui-gtk-2.0/citydlg.c        2003/07/29 15:16:31     1.57
+++ client/gui-gtk-2.0/citydlg.c        2003/07/29 15:55:28
@@ -2282,51 +2282,17 @@
 static gboolean citizens_callback(GtkWidget * w, GdkEventButton * ev,
                              gpointer data)
 {
-  struct city_dialog *pdialog = (struct city_dialog *) data;
+  struct city_dialog *pdialog = data;
   struct city *pcity = pdialog->pcity;
-  struct packet_city_request packet;
   int citnum;
-  enum specialist_type type;
-  enum citizen_type citizens[MAX_CITY_SIZE];
 
   if (ev->x > (pcity->size - 1) * pdialog->cwidth + SMALL_TILE_WIDTH)
     return FALSE;              /* no citizen that far to the right */
 
   citnum = MIN(pcity->size - 1, ev->x / pdialog->cwidth);
 
-  get_city_citizen_types(pcity, 4, citizens);
+  city_rotate_specialist(pcity, citnum);
 
-  switch (citizens[citnum]) {
-  case CITIZEN_ELVIS:
-    type = SP_ELVIS;
-    break;
-  case CITIZEN_SCIENTIST:
-    type = SP_SCIENTIST;
-    break;
-  case CITIZEN_TAXMAN:
-    type = SP_TAXMAN;
-    break;
-  default:
-    return FALSE;
-  }
-
-  packet.city_id = pdialog->pcity->id;
-  packet.specialist_from = type;
-
-  switch (type) {
-  case SP_ELVIS:
-    packet.specialist_to = SP_SCIENTIST;
-    break;
-  case SP_SCIENTIST:
-    packet.specialist_to = SP_TAXMAN;
-    break;
-  case SP_TAXMAN:
-    packet.specialist_to = SP_ELVIS;
-    break;
-  }
-
-  send_packet_city_request(&aconnection, &packet,
-                          PACKET_CITY_CHANGE_SPECIALIST);
   return TRUE;
 }
 
Index: client/gui-mui/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/citydlg.c,v
retrieving revision 1.68
diff -u -r1.68 citydlg.c
--- client/gui-mui/citydlg.c    2003/07/29 15:16:32     1.68
+++ client/gui-mui/citydlg.c    2003/07/29 15:55:28
@@ -274,19 +274,6 @@
 /****************************************************************
  ...
 *****************************************************************/
-static void request_city_change_specialist(struct city *pcity, int from, int 
to)
-{
-  struct packet_city_request packet;
-
-  packet.city_id = pcity->id;
-  packet.specialist_from = from;
-  packet.specialist_to = to;
-
-  send_packet_city_request(&aconnection, &packet, 
PACKET_CITY_CHANGE_SPECIALIST);
-}
-/****************************************************************
- ...
-*****************************************************************/
 static void request_city_toggle_worker(struct city *pcity, int xtile, int 
ytile)
 {
   if(is_valid_city_coords(xtile, ytile))
@@ -1072,25 +1059,27 @@
 static void city_citizen(struct city_citizen_msg *msg)
 {
   struct city_dialog *pdialog = msg->pdialog;
-  struct city *pcity = pdialog->pcity;
+  enum specialist_type from = msg->type, to;
 
-  switch (msg->type)  {
+  switch (from)  {
   case CITIZEN_ELVIS:
-    request_city_change_specialist(pcity, SP_ELVIS, SP_SCIENTIST);
+    to = SP_SCIENTIST;
     break;
 
   case CITIZEN_SCIENTIST:
-    request_city_change_specialist(pcity, SP_SCIENTIST, SP_TAXMAN);
+    to = SP_TAXMAN;
     break;
 
   case CITIZEN_TAXMAN:
-    request_city_change_specialist(pcity, SP_TAXMAN, SP_ELVIS);
+    to = SP_ELVIST;
     break;
 
   default:
     assert(FALSE);
-    break;
+    return;
   }
+
+  city_change_specialist(pdialog->pcity, from, to);
 }
 
 /**************************************************************************
Index: client/gui-sdl/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/citydlg.c,v
retrieving revision 1.25
diff -u -r1.25 citydlg.c
--- client/gui-sdl/citydlg.c    2003/07/29 15:16:32     1.25
+++ client/gui-sdl/citydlg.c    2003/07/29 15:55:28
@@ -240,24 +240,21 @@
 
 SEND:
   {
-    struct packet_city_request packet;
-    packet.city_id = pCityDlg->pCity->id;
-    packet.specialist_from = type;
+    enum specialist_type to;
 
     switch (type) {
     case SP_ELVIS:
-      packet.specialist_to = SP_TAXMAN;
+      to = SP_TAXMAN;
       break;
     case SP_TAXMAN:
-      packet.specialist_to = SP_SCIENTIST;
+      to = SP_SCIENTIST;
       break;
     case SP_SCIENTIST:
-      packet.specialist_to = SP_ELVIS;
+      to = SP_ELVIST;
       break;
     }
 
-    send_packet_city_request(&aconnection, &packet,
-                            PACKET_CITY_CHANGE_SPECIALIST);
+    city_change_specialist(pCityDlg->pCity, type, to);
   }
   return -1;
 }
Index: client/gui-win32/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/citydlg.c,v
retrieving revision 1.54
diff -u -r1.54 citydlg.c
--- client/gui-win32/citydlg.c  2003/07/29 15:16:32     1.54
+++ client/gui-win32/citydlg.c  2003/07/29 15:55:28
@@ -1685,39 +1685,33 @@
 /**************************************************************************
 ...
 **************************************************************************/
-
-
 static void city_dlg_click_citizens(struct city_dialog *pdialog, int n)
 {
-  struct packet_city_request packet;
-  if (pdialog->citizen_type[n]<0)
+  enum specialist_type from, to;
+
+  if (pdialog->citizen_type[n] < 0
+      || pdialog->citizen_type[n] > 2) {
     return;
-  if (pdialog->citizen_type[n]>2)
+  }
+
+  switch (pdialog->citizen_type[n]) {
+  case 0: /* elvis */
+    from = SP_ELVIS;
+    to = SP_SCIENTIST;
+    break;
+  case 1: /* scientist */
+    from = SP_SCIENTIST;
+    to = SP_TAXMAN;
+    break;
+  case 2: /* taxman */
+    from = SP_TAXMAN;
+    to = SP_ELVIS;
+    break;
+  default:
     return;
-  packet.city_id=pdialog->pcity->id;
-  switch (pdialog->citizen_type[n])
-    {
-    case 0: /* elvis */
-      packet.specialist_from=SP_ELVIS;
-      packet.specialist_to=SP_SCIENTIST;
-      send_packet_city_request(&aconnection, &packet,
-                              PACKET_CITY_CHANGE_SPECIALIST);   
-      break;
-    case 1: /* scientist */
-      packet.specialist_from=SP_SCIENTIST;
-      packet.specialist_to=SP_TAXMAN;   
-      send_packet_city_request(&aconnection, &packet,
-                              PACKET_CITY_CHANGE_SPECIALIST);  
-      break;
-    case 2: /* taxman */
-      packet.specialist_from=SP_TAXMAN;
-      packet.specialist_to=SP_ELVIS;   
-      send_packet_city_request(&aconnection, &packet,
-                              PACKET_CITY_CHANGE_SPECIALIST);   
-      break;
-    default:
-      break;
-    }
+  }
+
+  city_change_specialist(pdialog->pcity, from, to);
 }
 
 /**************************************************************************
Index: client/gui-xaw/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/citydlg.c,v
retrieving revision 1.97
diff -u -r1.97 citydlg.c
--- client/gui-xaw/citydlg.c    2003/07/29 15:16:32     1.97
+++ client/gui-xaw/citydlg.c    2003/07/29 15:55:28
@@ -1908,17 +1908,9 @@
 *****************************************************************/
 void elvis_callback(Widget w, XtPointer client_data, XtPointer call_data)
 {
-  struct city_dialog *pdialog;
-  struct packet_city_request packet;
-  
-  pdialog=(struct city_dialog *)client_data;
+  struct city_dialog *pdialog = client_data;
 
-  packet.city_id=pdialog->pcity->id;
-  packet.specialist_from=SP_ELVIS;
-  packet.specialist_to=SP_SCIENTIST;
-  
-  send_packet_city_request(&aconnection, &packet, 
-                          PACKET_CITY_CHANGE_SPECIALIST);
+  city_change_specialist(pdialog->pcity, SP_ELVIS, SP_SCIENTIST);
 }
 
 /****************************************************************
@@ -1926,17 +1918,9 @@
 *****************************************************************/
 void scientist_callback(Widget w, XtPointer client_data, XtPointer call_data)
 {
-  struct city_dialog *pdialog;
-  struct packet_city_request packet;
-  
-  pdialog=(struct city_dialog *)client_data;
+  struct city_dialog *pdialog = client_data;
 
-  packet.city_id=pdialog->pcity->id;
-  packet.specialist_from=SP_SCIENTIST;
-  packet.specialist_to=SP_TAXMAN;
-  
-  send_packet_city_request(&aconnection, &packet, 
-                          PACKET_CITY_CHANGE_SPECIALIST);
+  city_change_specialist(pdialog->pcity, SP_SCIENTIST, SP_TAXMAN);
 }
 
 /****************************************************************
@@ -1944,17 +1928,9 @@
 *****************************************************************/
 void taxman_callback(Widget w, XtPointer client_data, XtPointer call_data)
 {
-  struct city_dialog *pdialog;
-  struct packet_city_request packet;
-  
-  pdialog=(struct city_dialog *)client_data;
+  struct city_dialog *pdialog = client_data;
 
-  packet.city_id=pdialog->pcity->id;
-  packet.specialist_from=SP_TAXMAN;
-  packet.specialist_to=SP_ELVIS;
-  
-  send_packet_city_request(&aconnection, &packet, 
-                          PACKET_CITY_CHANGE_SPECIALIST);
+  city_change_specialist(pdialog->pcity, SP_TAXMAN, SP_ELVIS);
 }
 
 /****************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#4717) new function city_change_specialist, Jason Short <=