Complete.Org: Mailing Lists: Archives: freeciv-dev: June 2003:
[Freeciv-Dev] Re: (PR#4420) New traderoute rules
Home

[Freeciv-Dev] Re: (PR#4420) New traderoute rules

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#4420) New traderoute rules
From: "Rafa³ Bursig" <bursig@xxxxxxxxx>
Date: Wed, 18 Jun 2003 09:19:54 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Hi all

This is secound version of this code. It implement new functionality in 
client side ( all clients ).
I add new check : can_enter_marketplace( pcity1, pcity2 ) which check 
city distance ( > 8 ) and remove this check from 
can_estabilish_trade_route(...) function. Now secound check tell us 
about trade route ability and not about gold and sicence bonus ability.

Doing those checks with this patch we must first call 
can_enter_marketplace( ... ) and if this return TRUE we can check trade 
route ability (can_estabilish_trade_route(...)), if secoud check fail 
we still can get gold/science bonus.

Rafal
diff -u -r fc_new/ai/aiunit.c fc/ai/aiunit.c
--- fc_new/ai/aiunit.c  Sat Jun 14 00:21:21 2003
+++ fc/ai/aiunit.c      Wed Jun 18 16:01:19 2003
@@ -2484,7 +2484,8 @@
        players_iterate(aplayer) {
          if (pplayers_at_war(pplayer, aplayer)) continue;
          city_list_iterate(pplayer->cities,pdest) {
-           if (pcity && can_establish_trade_route(pcity, pdest)
+           if (pcity && can_enter_marketplace(pcity, pdest)
+              && can_establish_trade_route(pcity, pdest)
                && map_get_continent(pcity->x, pcity->y) 
                                 == map_get_continent(pdest->x, pdest->y)) {
              tradeval=trade_between_cities(pcity, pdest);
diff -u -r fc_new/client/gui-gtk/dialogs.c fc/client/gui-gtk/dialogs.c
--- fc_new/client/gui-gtk/dialogs.c     Fri Apr 11 13:17:09 2003
+++ fc/client/gui-gtk/dialogs.c Wed Jun 18 16:25:05 2003
@@ -1210,6 +1210,7 @@
                          struct city *phomecity, struct city *pdestcity)
 {
   char buf[128];
+  bool can_establish, can_enter_marketplace;
   
   my_snprintf(buf, sizeof(buf),
              _("Your caravan from %s reaches the city of %s.\nWhat now?"),
@@ -1218,15 +1219,20 @@
   caravan_city_id=pdestcity->id; /* callbacks need these */
   caravan_unit_id=punit->id;
   
+  can_enter_marketplace = can_enter_marketplace(phomecity, pdestcity);
+  can_establish = can_enter_marketplace &&
+                 can_establish_trade_route(phomecity, pdestcity);
+  
   caravan_dialog = popup_message_dialog(top_vbox,
                                        _("Your Caravan Has Arrived"), buf,
                                        caravan_close_callback, NULL,
-                          _("Establish 
_Traderoute"),caravan_establish_trade_callback, 0,
+                          (can_establish ? _("Establish _Traderoute") :
+                          _("Enter 
Marketplace")),caravan_establish_trade_callback, 0,
                           _("Help build 
_Wonder"),caravan_help_build_wonder_callback, 0,
                           _("_Keep moving"),NULL, 0,
                           0);
   
-  if (!can_establish_trade_route(phomecity, pdestcity)) {
+  if (!can_enter_marketplace) {
     message_dialog_button_set_sensitive(caravan_dialog, "button0", FALSE);
   }
   
diff -u -r fc_new/client/gui-gtk-2.0/dialogs.c fc/client/gui-gtk-2.0/dialogs.c
--- fc_new/client/gui-gtk-2.0/dialogs.c Sun Jun  1 09:52:15 2003
+++ fc/client/gui-gtk-2.0/dialogs.c     Wed Jun 18 16:27:45 2003
@@ -1080,6 +1080,7 @@
                          struct city *phomecity, struct city *pdestcity)
 {
   char buf[128];
+  bool can_establish, can_enter_marketplace;
   
   my_snprintf(buf, sizeof(buf),
              _("Your caravan from %s reaches the city of %s.\nWhat now?"),
@@ -1088,10 +1089,15 @@
   caravan_city_id=pdestcity->id; /* callbacks need these */
   caravan_unit_id=punit->id;
   
+  can_enter_marketplace = can_enter_marketplace(phomecity, pdestcity);
+  can_establish = can_enter_marketplace &&
+                 can_establish_trade_route(phomecity, pdestcity);
+  
   caravan_dialog = popup_message_dialog(GTK_WINDOW(toplevel),
     _("Your Caravan Has Arrived"), 
     buf,
-    _("Establish _Traderoute"),caravan_establish_trade_callback, NULL,
+    (can_establish ? _("Establish _Traderoute") :
+    _("Enter Marketplace")),caravan_establish_trade_callback, NULL,
     _("Help build _Wonder"),caravan_help_build_wonder_callback, NULL,
     _("_Keep moving"), NULL, NULL,
     NULL);
@@ -1099,7 +1105,7 @@
   g_signal_connect(caravan_dialog, "destroy",
                   G_CALLBACK(caravan_destroy_callback), NULL);
   
-  if (!can_establish_trade_route(phomecity, pdestcity)) {
+  if (!can_enter_marketplace) {
     message_dialog_button_set_sensitive(caravan_dialog, 0, FALSE);
   }
   
diff -u -r fc_new/client/gui-mui/dialogs.c fc/client/gui-mui/dialogs.c
--- fc_new/client/gui-mui/dialogs.c     Fri Apr 11 13:17:11 2003
+++ fc/client/gui-mui/dialogs.c Wed Jun 18 16:11:45 2003
@@ -1035,15 +1035,19 @@
   my_snprintf(buf, sizeof(buf),_("Your caravan from %s reaches the city of 
%s.\nWhat now?"),
           phomecity->name, pdestcity->name);
 
-  if(can_establish_trade_route(phomecity, pdestcity))
+  if(can_enter_marketplace(phomecity, pdestcity))
   {
     struct caravan_data *cd = malloc_struct(struct caravan_data);
     if(cd)
     {
       cd->caravan_city_id = pdestcity->id;
       cd->caravan_unit_id = punit->id;
-
-      msg_dlg[i].label = _("_Establish traderoute");
+      if(can_establish_trade_route(phomecity, pdestcity))
+      {
+        msg_dlg[i].label = _("_Establish traderoute");
+      } else {
+       msg_dlg[i].label = _("_Enter Marketplace");
+      }
       msg_dlg[i].function = (APTR)caravan_establish;
       msg_dlg[i].data = (APTR)cd;
       i++;
diff -u -r fc_new/client/gui-sdl/dialogs.c fc/client/gui-sdl/dialogs.c
--- fc_new/client/gui-sdl/dialogs.c     Mon Jun  9 01:06:06 2003
+++ fc/client/gui-sdl/dialogs.c Wed Jun 18 16:10:25 2003
@@ -1503,16 +1503,21 @@
   pCaravan_Dlg->pEndWidgetList = pWindow;
     
   /* ---------- */
-  if (can_establish_trade_route(pHomecity, pDestcity))
+  if (can_enter_marketplace(pHomecity, pDestcity))
   {
-    create_active_iconlabel(pBuf, pWindow->dst, pStr,
+    if (can_establish_trade_route(pHomecity, pDestcity)) {
+      create_active_iconlabel(pBuf, pWindow->dst, pStr,
            _("Establish Traderoute"), caravan_establish_trade_callback);
+    } else {
+      create_active_iconlabel(pBuf, pWindow->dst, pStr,
+           _("Enter Marketplace"), caravan_establish_trade_callback);
+    }      
     pBuf->data.cont = pCont;
-    set_wstate(pBuf , FC_WS_NORMAL);
+    set_wstate(pBuf, FC_WS_NORMAL);
   
-    add_to_gui_list(ID_LABEL , pBuf);
+    add_to_gui_list(ID_LABEL, pBuf);
     
-    w = MAX(w , pBuf->size.w);
+    w = MAX(w, pBuf->size.w);
     h += pBuf->size.h;
   }
   
diff -u -r fc_new/client/gui-win32/dialogs.c fc/client/gui-win32/dialogs.c
--- fc_new/client/gui-win32/dialogs.c   Fri Apr 11 13:17:13 2003
+++ fc/client/gui-win32/dialogs.c       Wed Jun 18 16:25:05 2003
@@ -1036,7 +1036,8 @@
                           struct city *phomecity, struct city *pdestcity)
 {
   char buf[128];
- 
+  bool can_establish, can_enter_marketplace;
+  
   my_snprintf(buf, sizeof(buf),
               _("Your caravan from %s reaches the city of %s.\nWhat now?"),
               phomecity->name, pdestcity->name);
@@ -1044,15 +1045,20 @@
   caravan_city_id=pdestcity->id; /* callbacks need these */
   caravan_unit_id=punit->id;
  
+  can_enter_marketplace = can_enter_marketplace(phomecity, pdestcity);
+  can_establish = can_enter_marketplace &&
+                 can_establish_trade_route(phomecity, pdestcity);
+  
   caravan_dialog=popup_message_dialog(NULL,
                            /*"caravandialog"*/_("Your Caravan Has Arrived"),
                            buf,
-                           _("Establish 
_Traderoute"),caravan_establish_trade_callback, 0,
+                           (can_establish ? _("Establish _Traderoute") :
+                          _("Enter 
Marketplace")),caravan_establish_trade_callback, 0,
                            _("Help build 
_Wonder"),caravan_help_build_wonder_callback, 0,
                            _("_Keep moving"),caravan_keep_moving_callback, 0,
                            0);
  
-  if(!can_establish_trade_route(phomecity, pdestcity))
+  if(!can_enter_marketplace)
     {
       message_dialog_button_set_sensitive(caravan_dialog,0,FALSE);
     }
diff -u -r fc_new/client/gui-xaw/dialogs.c fc/client/gui-xaw/dialogs.c
--- fc_new/client/gui-xaw/dialogs.c     Fri Apr 11 13:17:14 2003
+++ fc/client/gui-xaw/dialogs.c Wed Jun 18 16:23:07 2003
@@ -1290,7 +1290,7 @@
                           caravan_keep_moving_callback, 0, 0,
                           NULL);
   
-  if(!can_establish_trade_route(phomecity, pdestcity))
+  if(!can_enter_marketplace(phomecity, pdestcity))
     XtSetSensitive(XtNameToWidget(caravan_dialog, "*button0"), FALSE);
   
   if(!unit_can_help_build_wonder(punit, pdestcity))
diff -u -r fc_new/common/city.c fc/common/city.c
--- fc_new/common/city.c        Fri Jun 13 18:44:42 2003
+++ fc/common/city.c    Wed Jun 18 15:57:48 2003
@@ -843,13 +843,24 @@
 /**************************************************************************
 ...
 **************************************************************************/
-bool can_establish_trade_route(struct city *pc1, struct city *pc2)
+bool can_enter_marketplace(struct city *pc1, struct city *pc2)
 {
-  int i, free1 = 0, free2 = 0;
+  return (pc1 && pc2 && (pc1 != pc2) &&
+        (pc1->owner != pc2->owner || 
+        map_distance(pc1->x, pc1->y, pc2->x, pc2->y) > 8));
+}
+
 
-  if (!pc1 || !pc2 || pc1 == pc2
-      || (pc1->owner == pc2->owner
-         && map_distance(pc1->x, pc1->y, pc2->x, pc2->y) <= 8)) {
+/**************************************************************************
+  Function don't check min cities distance then should be call with
+  connection of can_enter_marketplace(...) function.
+**************************************************************************/
+bool can_establish_trade_route(struct city *pc1, struct city *pc2)
+{
+  int i, trade = -1;
+  bool can_establish = TRUE;
+  
+  if (!pc1 || !pc2 || pc1 == pc2) {
     return FALSE;
   }
 
@@ -858,17 +869,44 @@
       /* cities already have a traderoute */
       return FALSE;
     }
-
-    if (pc1->trade[i] == 0) {
-      free1++;
+  }
+    
+  if(city_num_trade_routes(pc1) == NUM_TRADEROUTES) {
+    int slot = 0, value = pc1->trade_value[0];
+    trade = trade_between_cities(pc1, pc2);
+    /* find min */
+    for (i = 1; i < NUM_TRADEROUTES; i++) {
+      if(value > pc1->trade_value[i]) {
+       slot = i;
+       value = pc1->trade_value[i];
+      }
     }
-    if (pc2->trade[i] == 0) {
-      free2++;
+    /* can we replace traderoute */
+    if(value >= trade) {
+      can_establish = FALSE;
     }
   }
+  
+  if(can_establish && city_num_trade_routes(pc2) == NUM_TRADEROUTES) {
+    int slot = 0, value = pc2->trade_value[0];
+    
+    if(trade == -1) {
+      trade = trade_between_cities(pc1, pc2);
+    }
+    /* find min */
+    for (i = 1; i < NUM_TRADEROUTES; i++) {
+      if(value > pc2->trade_value[i]) {
+       slot = i;
+       value = pc2->trade_value[i];
+      }
+    }
+    /* can we replace traderoute */
+    if(value >= trade) {
+      can_establish = FALSE;
+    }
+  }  
 
-  /* both cities need a free slot */
-  return (free1 > 0 && free2 > 0);
+  return can_establish;
 }
 
 /**************************************************************************
diff -u -r fc_new/common/city.h fc/common/city.h
--- fc_new/common/city.h        Tue May 13 16:23:03 2003
+++ fc/common/city.h    Wed Jun 18 15:34:02 2003
@@ -396,6 +396,7 @@
 bool city_can_be_built_here(int x, int y);
 
 /* trade functions */
+bool can_enter_marketplace(struct city *pc1, struct city *pc2);
 bool can_establish_trade_route(struct city *pc1, struct city *pc2);
 int trade_between_cities(struct city *pc1, struct city *pc2);
 int city_num_trade_routes(struct city *pcity);
diff -u -r fc_new/common/unit.c fc/common/unit.c
--- fc_new/common/unit.c        Fri Jun 13 18:44:42 2003
+++ fc/common/unit.c    Wed Jun 18 16:06:52 2003
@@ -243,7 +243,7 @@
   pdestcity = map_get_city(punit->x, punit->y);
   if (!pdestcity) return FALSE;
   phomecity = find_city_by_id(punit->homecity);
-  return phomecity && can_establish_trade_route(phomecity, pdestcity);
+  return phomecity && can_enter_marketplace(phomecity, pdestcity);
 }
 
 /**************************************************************************
diff -u -r fc_new/server/citytools.c fc/server/citytools.c
--- fc_new/server/citytools.c   Sat Jun 14 00:21:03 2003
+++ fc/server/citytools.c       Mon Jun 16 19:14:01 2003
@@ -1667,29 +1667,45 @@
 }
 
 /**************************************************************************
-Establish a trade route, notice that there has to be space for them, 
-So use can_establish_Trade_route first.
-returns the revenue aswell.
+...
 **************************************************************************/
-int establish_trade_route(struct city *pc1, struct city *pc2)
+static void remove_smallest_trade_route(struct city *pcity)
 {
-  int i, tb;
-
-  for (i = 0; i < NUM_TRADEROUTES; i++) {
-    if (pc1->trade[i] == 0) {
-      pc1->trade[i]=pc2->id;
-      break;
+  int i, slot, value;
+  struct city *pdest;
+    
+  assert(pcity);
+  
+  slot = 0;
+  value = pcity->trade_value[0];
+  for (i = 1; i < NUM_TRADEROUTES; i++) {
+    if(value > pcity->trade_value[0]) {
+      slot = i;
+      value = pcity->trade_value[i];
     }
   }
-  for (i = 0; i < NUM_TRADEROUTES; i++) {
-    if (pc2->trade[i] == 0) {
-      pc2->trade[i]=pc1->id;
-      break;
+    
+  if((pdest = find_city_by_id(pcity->trade[slot]))) {
+    for (i = 1; i < NUM_TRADEROUTES; i++) {
+      if (pdest->trade[i] == pcity->id) {
+        pdest->trade[i] = 0;
+        break;
+      }
     }
   }
+  
+  pcity->trade[slot] = 0;
+}
+
+/**************************************************************************
+  Returns the revenue trade route bonus.
+**************************************************************************/
+int get_establish_trade_route_bonus(struct city *pc1, struct city *pc2)
+{
+  int i, tb;
 
   tb=(map_distance(pc1->x, pc1->y, pc2->x, pc2->y)+10);
-/* should this be real_map_distance?  Leaving for now -- Syela */
+  /* should this be real_map_distance?  Leaving for now -- Syela */
   tb=(tb*(pc1->trade_prod+pc2->trade_prod))/24;
   /* 
    * fudge factor to more closely approximate Civ2 behavior (Civ2 is
@@ -1706,10 +1722,49 @@
     tb = (tb * 2) / 3;
   }
   /* was: A_RAILROAD, A_FLIGHT */
+  
+  /*
+   * this value will be reduced when both cities have established
+   * traderoute togeder -> tb = (tb + 2) / 3;
+   */
+  
   return tb;
 }
 
 /**************************************************************************
+Establish a trade route, notice that there has to be space for them, 
+So you must use can_establish_trade_route first.
+**************************************************************************/
+void establish_trade_route(struct city *pc1, struct city *pc2)
+{
+  int i;
+
+  if(city_num_trade_routes(pc1) == NUM_TRADEROUTES) {
+    remove_smallest_trade_route(pc1);
+  }
+  
+  if(city_num_trade_routes(pc2) == NUM_TRADEROUTES) {
+    remove_smallest_trade_route(pc2);
+  }
+  
+  for (i = 0; i < NUM_TRADEROUTES; i++) {
+    if (pc1->trade[i] == 0) {
+      pc1->trade[i]=pc2->id;
+      break;
+    }
+  }
+  
+  for (i = 0; i < NUM_TRADEROUTES; i++) {
+    if (pc2->trade[i] == 0) {
+      pc2->trade[i]=pc1->id;
+      break;
+    }
+  }
+  
+  return;
+}
+
+/**************************************************************************
 ...
 **************************************************************************/
 void do_sell_building(struct player *pplayer, struct city *pcity, int id)
diff -u -r fc_new/server/citytools.h fc/server/citytools.h
--- fc_new/server/citytools.h   Fri Apr 11 13:17:36 2003
+++ fc/server/citytools.h       Mon Jun 16 19:14:01 2003
@@ -73,8 +73,9 @@
                 const char *name);
 void remove_city(struct city *pcity);
 
-int establish_trade_route(struct city *pc1, struct city *pc2);
-
+void establish_trade_route(struct city *pc1, struct city *pc2);
+int get_establish_trade_route_bonus(struct city *pc1, struct city *pc2);
+  
 void do_sell_building(struct player *pplayer, struct city *pcity, int id);
 void building_lost(struct city *pcity, int id);
 void change_build_target(struct player *pplayer, struct city *pcity, 
diff -u -r fc_new/server/unithand.c fc/server/unithand.c
--- fc_new/server/unithand.c    Fri Jun 13 22:08:58 2003
+++ fc/server/unithand.c        Tue Jun 17 01:05:36 2003
@@ -1155,58 +1155,153 @@
                                 struct packet_unit_request *req)
 {
   struct unit *punit = player_find_unit_by_id(pplayer, req->unit_id);
+  struct city *pcity_out_of_home = NULL, *pcity_out_of_dest = NULL;
   struct city *pcity_homecity, *pcity_dest = find_city_by_id(req->city_id);
   int revenue;
+  bool home_full = FALSE, dest_full = FALSE, can_establish = TRUE;
   
-  if (!punit || !unit_flag(punit, F_TRADE_ROUTE) || !pcity_dest) {
+  if (!punit || !pcity_dest || !unit_flag(punit, F_TRADE_ROUTE)) {
     return FALSE;
   }
 
   pcity_homecity = player_find_city_by_id(pplayer, punit->homecity);
-  if (!pcity_homecity) {
+  if (!pcity_homecity || pcity_homecity == pcity_dest) {
     return FALSE;
   }
     
   if (!is_tiles_adjacent(punit->x, punit->y, pcity_dest->x, pcity_dest->y)
-      && !same_pos(punit->x, punit->y, pcity_dest->x, pcity_dest->y))
+      && !same_pos(punit->x, punit->y, pcity_dest->x, pcity_dest->y)) {
     return FALSE;
+  }
 
   if (!(same_pos(punit->x, punit->y, pcity_dest->x, pcity_dest->y)
-       || try_move_unit(punit, pcity_dest->x, pcity_dest->y)))
+       || try_move_unit(punit, pcity_dest->x, pcity_dest->y))) {
+    return FALSE;
+  }
+ 
+  /* cities too close */
+  if (pcity_homecity->owner == pcity_dest->owner
+         && map_distance(pcity_homecity->x, pcity_homecity->y,
+                                 pcity_dest->x, pcity_dest->y) <= 8) {
     return FALSE;
+  }
 
-  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 < NUM_TRADEROUTES; 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 FALSE;
-      }
+  for (i = 0; i < NUM_TRADEROUTES; i++) {
+    if (pcity_homecity->trade[i] == pcity_dest->id ||
+       pcity_dest->trade[i] == pcity_homecity->id) {
+      /* cities already have a traderoute */
+      can_establish = FALSE;
+      break;
     }
-    if (city_num_trade_routes(pcity_homecity) == NUM_TRADEROUTES) {
-      notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+  }
+  
+  revenue = get_establish_trade_route_bonus(pcity_homecity, pcity_dest);
+  
+  if(can_establish) {
+    home_full = (city_num_trade_routes(pcity_homecity) == NUM_TRADEROUTES);
+    dest_full = (city_num_trade_routes(pcity_dest) == NUM_TRADEROUTES);
+  }
+  
+  if (home_full || dest_full) {
+    int i, trade = trade_between_cities(pcity_homecity, pcity_dest);
+    
+    if (home_full) {
+      int slot = 0, value = pcity_homecity->trade_value[0];
+      
+      /* find min */
+      for (i = 1; i < NUM_TRADEROUTES; i++) {
+       if(value > pcity_homecity->trade_value[i]) {
+         slot = i;
+         value = pcity_homecity->trade_value[i];
+       }
+      }
+      
+      if((value < trade) {
+       pcity_out_of_home = find_city_by_id(pcity_homecity->trade[slot]);
+      } 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));
+        notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
                       _("      The city of %s already has %d "
-                        "trade routes!"), pcity_homecity->name,
+                        "better trade routes!"), pcity_homecity->name,
                       NUM_TRADEROUTES);
-      return FALSE;
+        can_establish = FALSE;
+      }
     }
-    if (city_num_trade_routes(pcity_dest) == NUM_TRADEROUTES) {
-      notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
+    
+    if (dest_full && can_establish) {
+      int slot = 0, value = pcity_dest->trade_value[0];
+      
+      /* find min */
+      for (i = 1; i < NUM_TRADEROUTES; i++) {
+       if(value > pcity_dest->trade_value[i]) {
+         slot = i;
+         value = pcity_dest->trade_value[i];
+       }
+      }
+      
+      if((value < trade) &&
+       (pcity_out_of_dest = find_city_by_id(pcity_dest->trade[slot]))) {
+       remove_trade_route(pcity_dest, pcity_out_of_dest);
+       notify_player_ex(city_owner(pcity_out_of_dest),
+                         pcity_lost_trade->x, pcity_lost_trade->y, E_NOEVENT,
+                    _("Game: Sorry, Your city %s cannot hold"
+                      " a trade route from %s!"), pcity_out_of_dest->name,
+                         pcity_dest->name);
+         
+       if(pcity_out_of_home) {
+         remove_trade_route(pcity_homecity, pcity_out_of_home);
+         notify_player_ex(city_owner(pcity_out_of_home),
+                         pcity_lost_trade->x, pcity_lost_trade->y, E_NOEVENT,
+                    _("Game: Sorry, Your city %s cannot hold"
+                      " a trade route from %s!"), pcity_out_of_home->name,
+                         pcity_homecity->name);
+       }
+       
+      } 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));
+        notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
                       _("      The city of %s already has %d "
-                        "trade routes!"), pcity_dest->name,
+                        "better trade routes!"), pcity_dest->name,
                       NUM_TRADEROUTES);
-      return FALSE;
+       can_establish = FALSE;
+      }  
+    } else {
+      if(can_establish && pcity_out_of_home) {
+         remove_trade_route(pcity_homecity, pcity_out_of_home);
+         notify_player_ex(city_owner(pcity_out_of_home),
+                         pcity_lost_trade->x, pcity_lost_trade->y, E_NOEVENT,
+                    _("Game: Sorry, Your city %s cannot hold"
+                      " a trade route from %s!"), pcity_out_of_home->name,
+                         pcity_homecity->name);
+      }
     }
-    return FALSE;
+    
+  }
+  
+  if(can_establish) {
+   /*  establish traderoute */
+    for (i = 0; i < NUM_TRADEROUTES; i++) {
+      if (pcity_homecity->trade[i] == 0) {
+        pcity_homecity->trade[i]=pcity_dest->id;
+        break;
+      }
+    }
+  
+    for (i = 0; i < NUM_TRADEROUTES; i++) {
+      if (pcity_dest->trade[i] == 0) {
+        pcity_dest->trade[i]=pcity_homecity->id;
+        break;
+      }
+    }
+  } else {
+    /* enter marketplace */
+    revenue = (revenue + 2) / 3;
   }
   
-  revenue = establish_trade_route(pcity_homecity, pcity_dest);
   conn_list_do_buffer(&pplayer->connections);
   notify_player_ex(pplayer, pcity_dest->x, pcity_dest->y, E_NOEVENT,
                   _("Game: Your %s from %s has arrived in %s,"
@@ -1216,21 +1311,70 @@
   wipe_unit(punit);
   pplayer->economic.gold+=revenue;
   update_tech(pplayer, revenue);
-  send_player_info(pplayer, pplayer);
-  city_refresh(pcity_homecity);
-  city_refresh(pcity_dest);
-
-  /* Notify the owners of the city. */
-  send_city_info(pplayer, pcity_homecity);
-  send_city_info(city_owner(pcity_dest), pcity_dest);
-
-  /* 
-   * Notify the players about the other city so that they get the
-   * tile_trade value.
-   */
-  send_city_info(city_owner(pcity_dest), pcity_homecity);
-  send_city_info(pplayer, pcity_dest);
+  
+  if(can_establish) {
+    city_refresh(pcity_homecity);
+    city_refresh(pcity_dest);
+    if(pcity_out_of_home) {
+      city_refresh(pcity_out_of_home);
+    }
+    if(pcity_out_of_dest) {
+      city_refresh(pcity_out_of_dest);
+    }
+  
+    /* Notify the owners of the city. */
+    send_city_info(pplayer, pcity_homecity);
+    send_city_info(city_owner(pcity_dest), pcity_dest);
 
+    if(pcity_out_of_home) {
+      send_city_info(city_owner(pcity_out_of_home), pcity_out_of_home);
+    }
+    if(pcity_out_of_dest) {
+      send_city_info(city_owner(pcity_out_of_dest), pcity_out_of_dest);
+    }
+  
+    /* 
+     * Notify the players about the other city so that they get the
+     * tile_trade value.
+     */
+    if(pplayer != city_owner(pcity_dest)) {
+      send_city_info(city_owner(pcity_dest), pcity_homecity);
+      send_city_info(pplayer, pcity_dest);
+    }
+    
+    if(pcity_out_of_home) {
+      if(city_owner(pcity_dest) != city_owner(pcity_out_of_home)) {
+        send_city_info(city_owner(pcity_dest), pcity_out_of_home);
+       send_city_info(city_owner(pcity_out_of_home), pcity_dest);
+      }
+      if(pplayer != city_owner(pcity_out_of_home)) {
+        send_city_info(pplayer, pcity_out_of_home);
+       send_city_info(city_owner(pcity_out_of_home), pcity_homecity);
+      }
+      if(pcity_out_of_dest && city_owner(pcity_out_of_home) !=
+                                       city_owner(pcity_out_of_dest)) {
+       send_city_info(city_owner(pcity_out_of_home), pcity_out_of_dest);
+      }
+    }
+    
+    if(pcity_out_of_dest) {
+      if(city_owner(pcity_dest) != city_owner(pcity_out_of_dest)) {
+        send_city_info(city_owner(pcity_dest), pcity_out_of_dest);
+       send_city_info(city_owner(pcity_out_of_dest), pcity_dest);
+      }
+      if(pplayer != city_owner(pcity_out_of_dest)) {
+        send_city_info(pplayer, pcity_out_of_dest);
+       send_city_info(city_owner(pcity_out_of_dest), pcity_homecity);
+      }
+      if(pcity_out_of_home && city_owner(pcity_out_of_home) !=
+                                       city_owner(pcity_out_of_dest)) {
+       send_city_info(city_owner(pcity_out_of_dest), pcity_out_of_home);
+      }
+    }
+    
+    send_player_info(pplayer, pplayer);
+    
+  }
   conn_list_do_unbuffer(&pplayer->connections);
   return TRUE;
 }

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