Complete.Org: Mailing Lists: Archives: freeciv-dev: May 1999:
[Freeciv-Dev] patch: caravan improvements
Home

[Freeciv-Dev] patch: caravan improvements

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] patch: caravan improvements
From: David Pfitzner <dwp@xxxxxxxxxxxxxx>
Date: Wed, 12 May 1999 22:04:17 +1000 (EST)

Based on some previous discussion here, this patch fixes/changes 
some things with caravans:

- Caravans with insufficient move cannot establish trade routes
or help build wonders (that is, when adjacent to city, and 
moves_left=0, or not enough to make move and rand() fails).

- A caravan entering a friendly city via goto (as well as 
manually) will popup a caravan dialog if the caravan can 
establish trade or help build wonder.  This occurs even if 
the goto happens at end-of-turn, which I'm not sure is ideal, 
but probably better than the present situation.  But a caravan
on "goto" which is just passing through the city on the
way to its destination does not cause a popup.  Also, (as 
a side-effect!) a caravan entering a city by boat or airlift 
also generates a popup!  (Minor bug if more than one caravan
in the boat though, since client expects only one caravan
popup at a time so has only one set of data...)

- A caravan entering an enemy city automatically builds a
traderoute with no dialog, if a traderoute is possible.  Else
and message why.  This applies whether moving by goto or manually.

Code mechanics:

Current situation:
  Player tells client to move caravan into a city;
  Client checks to see if can build wonder/trade;
  If not, tell server to move, if so, popup caravan dialog;
  Based on dialog, either tell server to move, or build trade
  or help build wonder.

This patch:
  Player tells client to move caravan into a city;
  Client tells server to move caravan into city;
  Server checks: if enemy city, and if can build trade route,
  and if can do the move, then establish traderoute.
  Otherwise try to do the move as normal, or say why not
  for an enemy city.
  Client receives information that a caravan has moved into
  a city: client checks to see if can build wonder/trade;
  if so, then popup dialog.  

Compatibility: for new clients with old servers, client must
know that _it_ has to initiate trade route stuff for enemy
cities, instead of the server doing it automatically.  Patch
uses non-required capability tag "caravan1". 
diff -u -r --exclude-from exclude freeciv-cvs/client/gui-gtk/dialogs.c 
freeciv-mod/client/gui-gtk/dialogs.c
--- freeciv-cvs/client/gui-gtk/dialogs.c        Mon Apr 26 17:16:16 1999
+++ freeciv-mod/client/gui-gtk/dialogs.c        Wed May 12 21:09:10 1999
@@ -1005,7 +1005,8 @@
   struct unit *punit;
   struct city *pcity;
   
-  if((punit=find_unit_by_id(caravan_unit_id)) && 
+  /* Now don't want to move at all in this case --dwp */
+  if(0 && (punit=find_unit_by_id(caravan_unit_id)) && 
      (pcity=find_city_by_id(caravan_city_id))) {
     struct unit req_unit;
 
diff -u -r --exclude-from exclude freeciv-cvs/client/gui-gtk/mapctrl.c 
freeciv-mod/client/gui-gtk/mapctrl.c
--- freeciv-cvs/client/gui-gtk/mapctrl.c        Fri Apr 16 22:56:25 1999
+++ freeciv-mod/client/gui-gtk/mapctrl.c        Wed May 12 21:11:17 1999
@@ -33,6 +33,7 @@
 #include <menu.h>
 #include <colors.h>
 #include <log.h>
+#include <capability.h>
 
 extern int map_view_x0, map_view_y0;
 extern int map_canvas_store_twidth, map_canvas_store_theight;
@@ -166,36 +167,45 @@
   dest_x=map_adjust_x(punit->x+dx);
   dest_y=punit->y+dy;   /* not adjusting on purpose*/
 
+  /* Now only do caravan stuff here for old servers and a caravan
+     entering an enemy city.  New servers do trade routes to enemy
+     cities automatically, and for friendly cities we wait for the
+     unit to enter the city.  --dwp
+  */
   if(unit_flag(punit->type, F_CARAVAN)) {
     struct city *pcity, *phomecity;
 
-    if((pcity=map_get_city(dest_x, dest_y)) &&
-       (phomecity=find_city_by_id(punit->homecity))) {
-      if(can_establish_trade_route(phomecity, pcity) ||
-        unit_can_help_build_wonder(punit, pcity)) {
+    if((pcity=map_get_city(dest_x, dest_y))
+       && pcity->owner != game.player_idx
+       && !has_capability("caravan1", aconnection.capability)
+       && (phomecity=find_city_by_id(punit->homecity))) {
+      if (can_establish_trade_route(phomecity, pcity)) {
        popup_caravan_dialog(punit, phomecity, pcity);
        return;
-      } else if (game.player_idx != pcity->owner) {  
-               append_output_window("Game: You cannot establish a trade route 
here.");
-               for (i=0;i<4;i++)
-                   if (phomecity->trade[i]==pcity->id) {
-                      sprintf(buf, "      A traderoute already exists between 
%s and %s!",
-                                   phomecity->name, pcity->name);
-                      append_output_window(buf);
-                      return;
-                   }
-               if (city_num_trade_routes(phomecity)==4) {
-                  sprintf(buf, "      The city of %s has already 4 trade 
routes!",phomecity->name);
-                  append_output_window(buf);
-                  return;
-               } 
-               if (city_num_trade_routes(pcity)==4) {
-                  sprintf(buf, "      The city of %s has already 4 trade 
routes!",pcity->name);
-                  append_output_window(buf);
-                  return;
-               }
-               return;
-            }
+      } else {
+       append_output_window("Game: You cannot establish a trade route here.");
+       for (i=0;i<4;i++) {
+         if (phomecity->trade[i]==pcity->id) {
+           sprintf(buf, "      A traderoute already exists between %s and %s!",
+                   phomecity->name, pcity->name);
+           append_output_window(buf);
+           return;
+         }
+       }
+       if (city_num_trade_routes(phomecity)==4) {
+         sprintf(buf, "      The city of %s already has 4 trade routes!",
+                 phomecity->name);
+         append_output_window(buf);
+         return;
+       } 
+       if (city_num_trade_routes(pcity)==4) {
+         sprintf(buf, "      The city of %s already has 4 trade routes!",
+                 pcity->name);
+         append_output_window(buf);
+         return;
+       }
+       return;
+      }
     }
   }
   else if(unit_flag(punit->type, F_DIPLOMAT) &&
diff -u -r --exclude-from exclude freeciv-cvs/client/gui-xaw/dialogs.c 
freeciv-mod/client/gui-xaw/dialogs.c
--- freeciv-cvs/client/gui-xaw/dialogs.c        Mon Apr 26 17:16:16 1999
+++ freeciv-mod/client/gui-xaw/dialogs.c        Wed May 12 21:09:10 1999
@@ -1050,8 +1050,9 @@
 {
   struct unit *punit;
   struct city *pcity;
-  
-  if((punit=find_unit_by_id(caravan_unit_id)) && 
+
+  /* Now don't want to move at all in this case --dwp */
+  if(0 && (punit=find_unit_by_id(caravan_unit_id)) && 
      (pcity=find_city_by_id(caravan_city_id))) {
     struct unit req_unit;
 
diff -u -r --exclude-from exclude freeciv-cvs/client/gui-xaw/mapctrl.c 
freeciv-mod/client/gui-xaw/mapctrl.c
--- freeciv-cvs/client/gui-xaw/mapctrl.c        Mon Apr 26 17:07:18 1999
+++ freeciv-mod/client/gui-xaw/mapctrl.c        Wed May 12 21:11:28 1999
@@ -45,6 +45,7 @@
 #include <graphics.h>
 #include <colors.h>
 #include <log.h>
+#include <capability.h>
 
 extern Display *display;
 
@@ -180,37 +181,46 @@
 
   dest_x=map_adjust_x(punit->x+dx);
   dest_y=punit->y+dy;   /* not adjusting on purpose*/
-
+ 
+  /* Now only do caravan stuff here for old servers and a caravan
+     entering an enemy city.  New servers do trade routes to enemy
+     cities automatically, and for friendly cities we wait for the
+     unit to enter the city.  --dwp
+  */
   if(unit_flag(punit->type, F_CARAVAN)) {
     struct city *pcity, *phomecity;
 
-    if((pcity=map_get_city(dest_x, dest_y)) &&
-       (phomecity=find_city_by_id(punit->homecity))) {
-      if(can_establish_trade_route(phomecity, pcity) ||
-        unit_can_help_build_wonder(punit, pcity)) {
+    if((pcity=map_get_city(dest_x, dest_y))
+       && pcity->owner != game.player_idx
+       && !has_capability("caravan1", aconnection.capability)
+       && (phomecity=find_city_by_id(punit->homecity))) {
+      if (can_establish_trade_route(phomecity, pcity)) {
        popup_caravan_dialog(punit, phomecity, pcity);
        return;
-      } else if (game.player_idx != pcity->owner) {  
-                append_output_window("Game: You cannot establish a trade route 
here.");
-                for (i=0;i<4;i++)
-                    if (phomecity->trade[i]==pcity->id) {
-                       sprintf(buf, "      A traderoute already exists between 
%s and %s!",
-                                    phomecity->name, pcity->name);
-                       append_output_window(buf);
-                       return;
-                    }
-                if (city_num_trade_routes(phomecity)==4) {
-                   sprintf(buf, "      The city of %s has already 4 trade 
routes!",phomecity->name);
-                   append_output_window(buf);
-                   return;
-                } 
-                if (city_num_trade_routes(pcity)==4) {
-                   sprintf(buf, "      The city of %s has already 4 trade 
routes!",pcity->name);
-                   append_output_window(buf);
-                   return;
-                }
-                return;
-             }
+      } else {
+       append_output_window("Game: You cannot establish a trade route here.");
+       for (i=0;i<4;i++) {
+         if (phomecity->trade[i]==pcity->id) {
+           sprintf(buf, "      A traderoute already exists between %s and %s!",
+                   phomecity->name, pcity->name);
+           append_output_window(buf);
+           return;
+         }
+       }
+       if (city_num_trade_routes(phomecity)==4) {
+         sprintf(buf, "      The city of %s already has 4 trade routes!",
+                 phomecity->name);
+         append_output_window(buf);
+         return;
+       } 
+       if (city_num_trade_routes(pcity)==4) {
+         sprintf(buf, "      The city of %s already has 4 trade routes!",
+                 pcity->name);
+         append_output_window(buf);
+         return;
+       }
+       return;
+      }
     }
   }
   else if(unit_flag(punit->type, F_DIPLOMAT) &&
diff -u -r --exclude-from exclude freeciv-cvs/client/packhand.c 
freeciv-mod/client/packhand.c
--- freeciv-cvs/client/packhand.c       Mon May  3 22:39:28 1999
+++ freeciv-mod/client/packhand.c       Wed May 12 21:09:11 1999
@@ -450,6 +450,19 @@
          repaint_city=1;
        else
          refresh_city_dialog(pcity);
+       
+       if(unit_flag(punit->type, F_CARAVAN)
+          && punit->owner==game.player_idx
+          && (punit->activity!=ACTIVITY_GOTO ||
+              same_pos(punit->goto_dest_x, punit->goto_dest_y,
+                       pcity->x, pcity->y))
+          && (unit_can_help_build_wonder_here(punit)
+              || unit_can_est_traderoute_here(punit))) {
+         struct city *phomecity = find_city_by_id(punit->homecity);
+         if (phomecity) {
+           popup_caravan_dialog(punit, phomecity, pcity);
+         }
+       }
       }
       
       repaint_unit=0;
diff -u -r --exclude-from exclude freeciv-cvs/common/shared.h 
freeciv-mod/common/shared.h
--- freeciv-cvs/common/shared.h Mon Apr 26 17:16:17 1999
+++ freeciv-mod/common/shared.h Wed May 12 21:13:15 1999
@@ -73,7 +73,11 @@
  */
 
 /* The default string is really simple */
-#define CAPABILITY "+1.8"
+#define CAPABILITY "+1.8 caravan1"
+/* caravan1 means to server automatically established a traderoute
+   when a caravan type unit moves into an enemy city.  For older
+   servers the client has to explicitly ask for a trade route.
+*/
 
 #define CITY_NAMES_FONT "10x20"
 #define BROADCAST_EVENT -2
diff -u -r --exclude-from exclude freeciv-cvs/server/unithand.c 
freeciv-mod/server/unithand.c
--- freeciv-cvs/server/unithand.c       Wed May 12 21:33:14 1999
+++ freeciv-mod/server/unithand.c       Wed May 12 21:37:54 1999
@@ -768,6 +768,7 @@
   int unit_id, transport_units = 1;
   struct unit *pdefender, *ferryboat, *bodyguard, *passenger;
   struct unit_list cargolist;
+  struct city *pcity;
 
   if (same_pos(punit->x, punit->y, dest_x, dest_y)) return 0;
 /* this occurs often during lag, and to the AI due to some quirks -- Syela */
@@ -775,6 +776,18 @@
   unit_id=punit->id;
   if (do_airline(punit, dest_x, dest_y))
     return 1;
+
+  if (unit_flag(punit->type, F_CARAVAN)
+      && (pcity=map_get_city(dest_x, dest_y))
+      && (pcity->owner != punit->owner)
+      && punit->homecity) {
+    struct packet_unit_request req;
+    req.unit_id = punit->id;
+    req.city_id = pcity->id;
+    req.name[0] = '\0';
+    return handle_unit_establish_trade(pplayer, &req);
+  }
+  
   pdefender=get_defender(pplayer, punit, dest_x, dest_y);
 
   if(pdefender && pdefender->owner!=punit->owner) {
@@ -836,7 +849,6 @@
        unit_types[punit->type].name);
   } else if(can_unit_move_to_tile(punit, dest_x, dest_y) && 
try_move_unit(punit, dest_x, dest_y)) {
     int src_x, src_y;
-    struct city *pcity;
     
     if((pcity=map_get_city(dest_x, dest_y))) {
       if (pcity->owner!=punit->owner &&  
@@ -947,6 +959,7 @@
   }
   return 0;
 }
+
 /**************************************************************************
 ...
 **************************************************************************/
@@ -954,73 +967,115 @@
                                   struct packet_unit_request *req)
 {
   struct unit *punit;
-  
-  if((punit=unit_list_find(&pplayer->units, req->unit_id))) {
-    struct city *pcity_dest;
-    
-    pcity_dest=find_city_by_id(req->city_id);
+  struct city *pcity_dest;
     
-    if(unit_flag(punit->type, F_CARAVAN) && pcity_dest &&
-       unit_can_help_build_wonder(punit, pcity_dest)) {
-      pcity_dest->shield_stock+=50;
-      if (build_points_left(pcity_dest) < 0) {
-       pcity_dest->shield_stock = 
improvement_value(pcity_dest->currently_building);
-      }
+  punit = unit_list_find(&pplayer->units, req->unit_id);
+  if (!punit || !unit_flag(punit->type, F_CARAVAN))
+    return;
 
-      notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
-                 "Game: Your %s helps build the %s in %s. (%d remaining)", 
-                      unit_name(punit->type),
-                      
get_improvement_type(pcity_dest->currently_building)->name,
-                      pcity_dest->name, 
-                      build_points_left(pcity_dest)
-                      );
-
-      wipe_unit(0, punit);
-      send_player_info(pplayer, pplayer);
-      send_city_info(pplayer, pcity_dest, 0);
-    }
+  pcity_dest = find_city_by_id(req->city_id);
+  if (!pcity_dest || !unit_can_help_build_wonder(punit, pcity_dest))
+    return;
+
+  if (!is_tiles_adjacent(punit->x, punit->y, pcity_dest->x, pcity_dest->y))
+    return;
+
+  if (!(same_pos(punit->x, punit->y, pcity_dest->x, pcity_dest->y)
+       || try_move_unit(punit, pcity_dest->x, pcity_dest->y)))
+    return;
+
+  /* we're there! */
+
+  pcity_dest->shield_stock+=50;
+  if (build_points_left(pcity_dest) < 0) {
+    pcity_dest->shield_stock = 
improvement_value(pcity_dest->currently_building);
   }
+
+  connection_do_buffer(pplayer->conn);
+  notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+                  "Game: Your %s helps build the %s in %s. (%d remaining)", 
+                  unit_name(punit->type),
+                  get_improvement_type(pcity_dest->currently_building)->name,
+                  pcity_dest->name, 
+                  build_points_left(pcity_dest));
+
+  wipe_unit(0, punit);
+  send_player_info(pplayer, pplayer);
+  send_city_info(pplayer, pcity_dest, 0);
+  connection_do_unbuffer(pplayer->conn);
 }
 
 /**************************************************************************
 ...
 **************************************************************************/
-void handle_unit_establish_trade(struct player *pplayer, 
+int handle_unit_establish_trade(struct player *pplayer, 
                                 struct packet_unit_request *req)
 {
   struct unit *punit;
+  struct city *pcity_homecity, *pcity_dest;
+  int revenue;
   
-  if((punit=unit_list_find(&pplayer->units, req->unit_id))) {
-    
-    struct city *pcity_homecity, *pcity_dest;
-    
-    pcity_homecity=city_list_find_id(&pplayer->cities, punit->homecity);
-    pcity_dest=find_city_by_id(req->city_id);
-    
-    if(unit_flag(punit->type, F_CARAVAN) && pcity_homecity && pcity_dest && 
-       is_tiles_adjacent(punit->x, punit->y, pcity_dest->x, pcity_dest->y)) {
-          if (can_establish_trade_route(pcity_homecity, pcity_dest)) {
-             int revenue;
-
-             revenue=establish_trade_route(pcity_homecity, pcity_dest);
-             connection_do_buffer(pplayer->conn);
-             notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
-                      "Game: Your %s has arrived in %s, and revenues amount to 
%d in gold.", 
-                      unit_name(punit->type), pcity_dest->name, revenue);
-             wipe_unit(0, punit);
-             pplayer->economic.gold+=revenue;
-             send_player_info(pplayer, pplayer);
-             city_refresh(pcity_homecity);
-             city_refresh(pcity_dest);
-             send_city_info(pplayer, pcity_homecity, 0);
-             send_city_info(city_owner(pcity_dest), pcity_dest, 0);
-             connection_do_unbuffer(pplayer->conn);
-          } else 
-             notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
-                    "Game: Sorry. Your %s cannot establish a trade route 
here!", unit_name(punit->type));
+  punit = unit_list_find(&pplayer->units, req->unit_id);
+  if (!punit || !unit_flag(punit->type, F_CARAVAN))
+    return 0;
+    
+  pcity_homecity=city_list_find_id(&pplayer->cities, punit->homecity);
+  pcity_dest=find_city_by_id(req->city_id);
+  if(!pcity_homecity || !pcity_dest)
+    return 0;
+    
+  if (!is_tiles_adjacent(punit->x, punit->y, pcity_dest->x, pcity_dest->y))
+    return 0;
+
+  if (!(same_pos(punit->x, punit->y, pcity_dest->x, pcity_dest->y)
+       || try_move_unit(punit, pcity_dest->x, pcity_dest->y)))
+    return 0;
+
+  if (!can_establish_trade_route(pcity_homecity, pcity_dest)) {
+    int i;
+    notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+                    "Game: Sorry. Your %s cannot establish a trade route 
here!",
+                    unit_name(punit->type));
+    for (i=0;i<4;i++) {
+      if (pcity_homecity->trade[i]==pcity_dest->id) {
+       notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+                     "      A traderoute already exists between %s and %s!",
+                     pcity_homecity->name, pcity_dest->name);
+       return 0;
+      }
     }
+    if (city_num_trade_routes(pcity_homecity)==4) {
+      notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+                      "      The city of %s already has 4 trade routes!",
+                      pcity_homecity->name);
+      return 0;
+    } 
+    if (city_num_trade_routes(pcity_dest)==4) {
+      notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+                      "      The city of %s already has 4 trade routes!",
+                      pcity_dest->name);
+      return 0;
+    }
+    return 0;
   }
+  
+  revenue = establish_trade_route(pcity_homecity, pcity_dest);
+  connection_do_buffer(pplayer->conn);
+  notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+                  "Game: Your %s has arrived in %s,"
+                  " and revenues amount to %d in gold.", 
+                  unit_name(punit->type), pcity_dest->name, revenue);
+  wipe_unit(0, punit);
+  pplayer->economic.gold+=revenue;
+  send_player_info(pplayer, pplayer);
+  city_refresh(pcity_homecity);
+  city_refresh(pcity_dest);
+  send_city_info(pplayer, pcity_homecity, 0);
+  send_city_info(city_owner(pcity_dest), pcity_dest, 0);
+  connection_do_unbuffer(pplayer->conn);
+  return 1;
 }
+
 /**************************************************************************
 ...
 **************************************************************************/
diff -u -r --exclude-from exclude freeciv-cvs/server/unithand.h 
freeciv-mod/server/unithand.h
--- freeciv-cvs/server/unithand.h       Thu Nov  5 18:17:47 1998
+++ freeciv-mod/server/unithand.h       Wed May 12 21:09:11 1999
@@ -40,8 +40,8 @@
                              int dest_x, int dest_y);
 void handle_unit_help_build_wonder(struct player *pplayer, 
                                   struct packet_unit_request *req);
-void handle_unit_establish_trade(struct player *pplayer, 
-                                struct packet_unit_request *req);
+int handle_unit_establish_trade(struct player *pplayer, 
+                               struct packet_unit_request *req);
 void handle_unit_enter_city(struct player *pplayer, struct city *pcity);
 void handle_unit_auto_request(struct player *pplayer, 
                              struct packet_unit_request *req);

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