Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2004:
[Freeciv-Dev] (PR#10011) adding worklist entries in the cityrep
Home

[Freeciv-Dev] (PR#10011) adding worklist entries in the cityrep

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#10011) adding worklist entries in the cityrep
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 3 Dec 2004 14:47:18 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10011 >

This patch provides some useful queue functions.  Naturally they are
untested.

-jason

Index: client/citydlg_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/citydlg_common.c,v
retrieving revision 1.51
diff -u -r1.51 citydlg_common.c
--- client/citydlg_common.c     30 Nov 2004 08:37:02 -0000      1.51
+++ client/citydlg_common.c     3 Dec 2004 22:46:41 -0000
@@ -494,13 +494,17 @@
   return dsend_packet_city_worklist(&aconnection, pcity->id, &copy);
 }
 
+
 /**************************************************************************
-  Insert an item into the city's worklist.
+  Insert an item into the city's queue.  This function will send new
+  production requests to the server but will NOT send the new worklist
+  to the server - the caller should call city_set_worklist() if the
+  function returns TRUE.
 
   Note that the queue DOES include the current production.
 **************************************************************************/
-bool city_queue_insert(struct city *pcity, int position,
-                      bool item_is_unit, int item_id)
+static bool base_city_queue_insert(struct city *pcity, int position,
+                                  bool item_is_unit, int item_id)
 {
   if (position == 0) {
     int old_id;
@@ -520,7 +524,6 @@
       return FALSE;
     }
 
-    city_set_worklist(pcity, &pcity->worklist);
     city_change_production(pcity, item_is_unit, item_id);
   } else if (position >= 1
             && position <= worklist_length(&pcity->worklist)) {
@@ -535,7 +538,6 @@
                         position - 1)) {
       return FALSE;
     }
-    city_set_worklist(pcity, &pcity->worklist);
   } else {
     /* Insert at end. */
     if (item_is_unit && !can_eventually_build_unit(pcity, item_id)) {
@@ -547,12 +549,76 @@
     if (!worklist_append(&pcity->worklist, item_id, item_is_unit)) {
       return FALSE;
     }
+  }
+  return TRUE;
+}
+
+/**************************************************************************
+  Insert an item into the city's queue.
+
+  Note that the queue DOES include the current production.
+**************************************************************************/
+bool city_queue_insert(struct city *pcity, int position,
+                      bool item_is_unit, int item_id)
+{
+  if (base_city_queue_insert(pcity, position, item_is_unit, item_id)) {
     city_set_worklist(pcity, &pcity->worklist);
+    return TRUE;
   }
+  return FALSE;
+}
+
+/**************************************************************************
+  Clear the queue (all entries except the first one since that can't be
+  cleared).
+
+  Note that the queue DOES include the current production.
+**************************************************************************/
+bool city_queue_clear(struct city *pcity)
+{
+  int i;
+
+  for (i = 0; i < MAX_LEN_WORKLIST; i++) {
+    pcity->worklist.wlefs[i] = WEF_END;
+    pcity->worklist.wlids[i] = 0;
+  }
+
   return TRUE;
 }
 
 /**************************************************************************
+  Insert the worklist into the city's queue at the given position.
+
+  Note that the queue DOES include the current production.
+**************************************************************************/
+bool city_queue_insert_worklist(struct city *pcity, int position,
+                               struct worklist *worklist)
+{
+  bool success = FALSE;
+
+  if (worklist_length(worklist) == 0) {
+    return TRUE;
+  }
+
+  worklist_iterate(worklist, id, is_unit) {
+    if (base_city_queue_insert(pcity, position, is_unit, id)) {
+      if (position > 0) {
+       /* Move to the next position (unless position == -1 in which case
+        * we're appending. */
+       position++;
+      }
+      success = TRUE;
+    }
+  } worklist_iterate_end;
+
+  if (success) {
+    city_set_worklist(pcity, &pcity->worklist);
+  }
+
+  return success;
+}
+
+/**************************************************************************
   Get the city current production and the worklist, like it should be.
 **************************************************************************/
 void city_get_queue(struct city *pcity, struct worklist *pqueue)
Index: client/citydlg_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/citydlg_common.h,v
retrieving revision 1.28
diff -u -r1.28 citydlg_common.h
--- client/citydlg_common.h     2 Dec 2004 10:06:58 -0000       1.28
+++ client/citydlg_common.h     3 Dec 2004 22:46:41 -0000
@@ -66,6 +66,9 @@
 int city_set_worklist(struct city *pcity, struct worklist *pworklist);
 bool city_queue_insert(struct city *pcity, int position,
                       bool item_is_unit, int item_id);
+bool city_queue_clear(struct city *pcity);
+bool city_queue_insert_worklist(struct city *pcity, int position,
+                               struct worklist *worklist);
 void city_get_queue(struct city *pcity, struct worklist *pqueue);
 void city_set_queue(struct city *pcity, struct worklist *pqueue);
 bool city_can_buy(const struct city *pcity);
Index: common/worklist.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/worklist.h,v
retrieving revision 1.9
diff -u -r1.9 worklist.h
--- common/worklist.h   28 Nov 2003 17:37:21 -0000      1.9
+++ common/worklist.h   3 Dec 2004 22:46:42 -0000
@@ -50,5 +50,19 @@
 bool are_worklists_equal(const struct worklist *wlist1,
                         const struct worklist *wlist2);
 
+/* Iterate over all entries in the worklist.  Note the 'id' parameter
+ * comes before the 'is_unit' one. */
+#define worklist_iterate(worklist, id, is_unit)                                
    \
+{                                                                          \
+  struct worklist *_worklist = (worklist);                                 \
+  int id, _iter, _length = worklist_length(_worklist);                     \
+  bool is_unit;                                                                
    \
+                                                                           \
+  for (_iter = 0; _iter < _length; _iter++) {                              \
+    worklist_peek_ith(_worklist, &id, &is_unit, _iter);
+
+#define worklist_iterate_end                                               \
+  }                                                                        \
+}
 
 #endif /* FC__WORKLIST_H */

[Prev in Thread] Current Thread [Next in Thread]