Complete.Org: Mailing Lists: Archives: freeciv-dev: February 1999:
[Freeciv-Dev] new kludge cleanup patch
Home

[Freeciv-Dev] new kludge cleanup patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] new kludge cleanup patch
From: David Pfitzner <dwp@xxxxxxxxxxxxxx>
Date: Wed, 3 Feb 1999 19:10:45 +1100

The attached patch fixes some kludges in common pointed out 
by Per Mathisen, but in a way acceptable to me :-}

Though its not entirely clean (using global function pointers!)
so if anyone wants to write a module in common which does an 
ident to (unit,city) pointers lookup table, go right aheaad :-)

find_city_by_id() and dealloc_id() are now implemented in 
common, with function pointer hooks find_city_by_id_func
and dealloc_id_func, for the server to plug in 
server_find_city_by_id() and server_dealloc_id().

I've eliminated the is_server variable altogether.

It isn't tested much...

Regards,
-- David
diff -u -r --exclude-from exclude freeciv-cvs/client/civclient.c 
freeciv-mod/client/civclient.c
--- freeciv-cvs/client/civclient.c      Sat Jan 30 14:36:04 1999
+++ freeciv-mod/client/civclient.c      Wed Feb  3 18:30:59 1999
@@ -43,7 +43,6 @@
 char server_host[512];
 char name[512];
 int server_port;
-int is_server = 0;
 
 enum client_states client_state=CLIENT_BOOT_STATE;
 int use_solid_color_behind_units;
@@ -345,4 +344,3 @@
   return client_state;
 }
 
-void dealloc_id(int id) { }/* kludge */
diff -u -r --exclude-from exclude freeciv-cvs/client/climisc.c 
freeciv-mod/client/climisc.c
--- freeciv-cvs/client/climisc.c        Thu Jan 28 22:03:32 1999
+++ freeciv-mod/client/climisc.c        Wed Feb  3 18:23:11 1999
@@ -150,11 +150,6 @@
   }
 }
 
-struct city *find_city_by_id(int id)
-{ /* no idea where this belongs either */
-  return(game_find_city_by_id(id));
-}
-
 #define NCONT 256              /* should really be (UCHAR_MAX+1)? */
 static char used_continent_val[NCONT];   /* boolean */
 
diff -u -r --exclude-from exclude freeciv-cvs/common/city.c 
freeciv-mod/common/city.c
--- freeciv-cvs/common/city.c   Sat Jan 30 14:29:11 1999
+++ freeciv-mod/common/city.c   Wed Feb  3 18:39:33 1999
@@ -19,6 +19,9 @@
 #include <tech.h>
 #include <shared.h>
 
+/* function pointer: set to non-NULL by server to use alternate method */
+struct city *(*find_city_by_id_func)(int) = NULL;
+
 static int improvement_upkeep_asmiths(struct city *pcity, int i, int asmiths);
 
 /****************************************************************
@@ -1371,3 +1374,16 @@
   return mystrcasecmp( (*(struct city**)p1)->name,
                       (*(struct city**)p2)->name );
 }
+
+/**************************************************************************
+Use alternate method if set, else default.
+**************************************************************************/
+struct city *find_city_by_id(int id)
+{
+  if(find_city_by_id_func != NULL) {
+    return (*find_city_by_id_func)(id);
+  } else {
+    return game_find_city_by_id(id);
+  }
+}
+
diff -u -r --exclude-from exclude freeciv-cvs/common/city.h 
freeciv-mod/common/city.h
--- freeciv-cvs/common/city.h   Thu Jan 28 22:12:50 1999
+++ freeciv-mod/common/city.h   Wed Feb  3 18:45:34 1999
@@ -220,8 +220,6 @@
 
 /* list functions */
 struct city *find_city_by_id(int id);
-/* this funct is no longer in this module, but leaving it proto'd
-here saves more headaches than you can imagine -- Syela */
 void city_list_init(struct city_list *This);
 struct city *city_list_get(struct city_list *This, int index);
 struct city *city_list_find_id(struct city_list *This, int id);
@@ -234,5 +232,8 @@
 void city_list_insert_back(struct city_list *This, struct city *pcity);
 
 int city_name_compare(const void *p1, const void *p2);
+
+/* function pointer for server to set: */
+extern struct city *(*find_city_by_id_func)(int);
 
 #endif
diff -u -r --exclude-from exclude freeciv-cvs/common/game.c 
freeciv-mod/common/game.c
--- freeciv-cvs/common/game.c   Sat Jan 30 14:29:11 1999
+++ freeciv-mod/common/game.c   Wed Feb  3 18:43:06 1999
@@ -25,8 +25,6 @@
 #define DEBUG 0
 #endif
 
-void dealloc_id(int id);
-extern int is_server;
 struct civ_game game;
 
 /*
@@ -220,8 +218,7 @@
     unit_list_unlink(&game.players[punit->owner].units, punit);
     
     free(punit);
-    if(is_server)
-      dealloc_id(unit_id);
+    dealloc_id(unit_id);       /* only has effect in the server */
   }
 }
 
diff -u -r --exclude-from exclude freeciv-cvs/common/player.c 
freeciv-mod/common/player.c
--- freeciv-cvs/common/player.c Thu Jan 28 22:12:50 1999
+++ freeciv-mod/common/player.c Wed Feb  3 18:43:26 1999
@@ -21,8 +21,6 @@
 #include <shared.h>
 #include <tech.h>
 
-extern int is_server;
-
 struct player_race races[]= { /* additional goals added by Syela */
   {"Roman", "Romans",           1, 2, 2,
      {100,100,100,100,100,100,100},
@@ -340,8 +338,8 @@
 struct city *player_find_city_by_id(struct player *pplayer, int city_id)
 {
   struct city *pcity;
-  
-  if(is_server) {
+
+  if(find_city_by_id_func!=NULL) {
     pcity = find_city_by_id(city_id);
     if(pcity && (pcity->owner==pplayer->player_no)) {
       return pcity;
diff -u -r --exclude-from exclude freeciv-cvs/common/shared.c 
freeciv-mod/common/shared.c
--- freeciv-cvs/common/shared.c Wed Jan 20 21:30:55 1999
+++ freeciv-mod/common/shared.c Wed Feb  3 18:55:09 1999
@@ -24,7 +24,10 @@
 
 /* Random Number Generator variables */
 RANDOM_TYPE RandomState[56];
-int iRandJ, iRandK, iRandX; 
+int iRandJ, iRandK, iRandX;
+
+/* function pointer: set to non-NULL by server to use alternate method */
+void (*dealloc_id_func)(int) = NULL;
 
 /**************************************************************************
 ... 
@@ -371,4 +374,14 @@
   };
   sprintf(realfile,"%s/%s",datadir,filename);
   return(realfile);
+}
+
+/**************************************************************************
+Use alternate method if set, else nothing.
+**************************************************************************/
+void dealloc_id(int id)
+{
+  if(dealloc_id_func != NULL) {
+    return (*dealloc_id_func)(id);
+  }
 }
diff -u -r --exclude-from exclude freeciv-cvs/common/shared.h 
freeciv-mod/common/shared.h
--- freeciv-cvs/common/shared.h Sat Jan 30 14:29:11 1999
+++ freeciv-mod/common/shared.h Wed Feb  3 18:45:40 1999
@@ -110,4 +110,9 @@
 
 char *datafilename(char *filename);    /* used to be in client/climisc */
 
+void dealloc_id(int id);
+
+/* function pointer for server to set: */
+extern void (*dealloc_id_func)(int);
+
 #endif
diff -u -r --exclude-from exclude freeciv-cvs/server/cityhand.c 
freeciv-mod/server/cityhand.c
--- freeciv-cvs/server/cityhand.c       Sat Jan 30 14:33:47 1999
+++ freeciv-mod/server/cityhand.c       Wed Feb  3 18:25:07 1999
@@ -74,7 +74,7 @@
   citycache[id]=NULL;
 }
 
-struct city *find_city_by_id(int id)
+struct city *server_find_city_by_id(int id)
 {
   /* This is sometimes called with id=unit.ai.charge, which is either
    * a unit id or a city id; if its a unit id then that id won't be used
diff -u -r --exclude-from exclude freeciv-cvs/server/cityhand.h 
freeciv-mod/server/cityhand.h
--- freeciv-cvs/server/cityhand.h       Tue Jul 21 00:34:11 1998
+++ freeciv-mod/server/cityhand.h       Wed Feb  3 18:35:34 1999
@@ -21,6 +21,9 @@
 void add_city_to_cache(struct city *pcity);
 void remove_city_from_cache(int id);
 
+/* function to plug into common/ */
+struct city *server_find_city_by_id(int id);
+
 void create_city(struct player *pplayer, int x, int y, char *name);
 void remove_city(struct city *pcity);
 void send_city_info(struct player *dest, struct city *pcity, int dosend);
diff -u -r --exclude-from exclude freeciv-cvs/server/civserver.c 
freeciv-mod/server/civserver.c
--- freeciv-cvs/server/civserver.c      Sun Jan 31 13:32:41 1999
+++ freeciv-mod/server/civserver.c      Wed Feb  3 18:44:01 1999
@@ -94,7 +94,6 @@
 /* use get_next_id_number()                     */
 unsigned short global_id_counter=100;
 unsigned char used_ids[8192]={0};
-int is_server = 1;
 
 int is_new_game=1;
 
@@ -128,6 +127,10 @@
 
   strcpy(metaserver_info_line, DEFAULT_META_SERVER_INFO_STRING);
 
+  /* plug-in functions for common/ */
+  find_city_by_id_func = &server_find_city_by_id;
+  dealloc_id_func = &server_dealloc_id;
+
   /* no  we don't use GNU's getopt or even the "standard" getopt */
   /* yes we do have reasons ;)                                   */
   i=1;
@@ -988,7 +991,7 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void dealloc_id(int id)
+void server_dealloc_id(int id)
 {
   used_ids[id/8]&= 0xff ^ (1<<(id%8));
 }
diff -u -r --exclude-from exclude freeciv-cvs/server/civserver.h 
freeciv-mod/server/civserver.h
--- freeciv-cvs/server/civserver.h      Tue Dec 22 20:32:41 1998
+++ freeciv-mod/server/civserver.h      Wed Feb  3 18:35:26 1999
@@ -32,7 +32,7 @@
 void start_game(void);
 void send_select_race(struct player *pplayer);
 void save_game(void);
-void dealloc_id(int id);
+void server_dealloc_id(int id);
 int is_id_allocated(int id);
 void alloc_id(int id);
 int get_next_id_number(void);

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