Author: mir3x
Date: Thu Nov 10 09:03:39 2016
New Revision: 34473

URL: http://svn.gna.org/viewcvs/freeciv?rev=34473&view=rev
Log:
Qt client - added autocaravan

See patch #7965

Modified:
    branches/S2_6/client/gui-qt/citydlg.cpp
    branches/S2_6/client/gui-qt/dialogs.cpp
    branches/S2_6/client/gui-qt/mapview.cpp
    branches/S2_6/client/gui-qt/menu.cpp
    branches/S2_6/client/gui-qt/menu.h
    branches/S2_6/client/gui-qt/shortcuts.cpp

Modified: branches/S2_6/client/gui-qt/citydlg.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/citydlg.cpp?rev=34473&r1=34472&r2=34473&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/citydlg.cpp     (original)
+++ branches/S2_6/client/gui-qt/citydlg.cpp     Thu Nov 10 09:03:39 2016
@@ -286,6 +286,9 @@
     impr_pixmap->map_pixmap.fill(Qt::transparent);
     pixmap_copy(&impr_pixmap->map_pixmap, sprite->pm, 0 , 0, 0, 0,
                 sprite->pm->width(), sprite->pm->height());
+  } else {
+    impr_pixmap = qtg_canvas_create(10, 10);
+    impr_pixmap->map_pixmap.fill(Qt::red);
   }
 
   setFixedWidth(impr_pixmap->map_pixmap.width() + 4);
@@ -324,13 +327,16 @@
     qtg_canvas_free(impr_pixmap);
   }
 
-  if (impr) {
-    sprite = get_building_sprite(tileset , impr);
+  sprite = get_building_sprite(tileset , impr);
+  if (impr && sprite) {
     impr_pixmap = qtg_canvas_create(sprite->pm->width(),
                                     sprite->pm->height());
     impr_pixmap->map_pixmap.fill(QColor(palette().color(QPalette::Highlight)));
     pixmap_copy(&impr_pixmap->map_pixmap, sprite->pm, 0 , 0, 0, 0,
                 sprite->pm->width(), sprite->pm->height());
+  } else {
+    impr_pixmap = qtg_canvas_create(10, 10);
+    impr_pixmap->map_pixmap.fill(QColor(palette().color(QPalette::Highlight)));
   }
 
   init_pix();
@@ -347,13 +353,16 @@
     qtg_canvas_free(impr_pixmap);
   }
 
-  if (impr) {
-    sprite = get_building_sprite(tileset , impr);
+  sprite = get_building_sprite(tileset , impr);
+  if (impr && sprite) {
     impr_pixmap = qtg_canvas_create(sprite->pm->width(),
                                     sprite->pm->height());
     impr_pixmap->map_pixmap.fill(Qt::transparent);
     pixmap_copy(&impr_pixmap->map_pixmap, sprite->pm, 0 , 0, 0, 0,
                 sprite->pm->width(), sprite->pm->height());
+  } else {
+    impr_pixmap = qtg_canvas_create(10, 10);
+    impr_pixmap->map_pixmap.fill(Qt::red);
   }
 
   init_pix();

Modified: branches/S2_6/client/gui-qt/dialogs.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/dialogs.cpp?rev=34473&r1=34472&r2=34473&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/dialogs.cpp     (original)
+++ branches/S2_6/client/gui-qt/dialogs.cpp     Thu Nov 10 09:03:39 2016
@@ -1422,6 +1422,7 @@
                             const struct act_prob *act_probs)
 {
   struct astring title = ASTRING_INIT, text = ASTRING_INIT;
+  qtiles caras;
   QVariant qv1, qv2;
   pfcn_void func;
   struct city *actor_homecity;
@@ -1431,6 +1432,21 @@
   unit_act = qdef_act::action()->vs_unit_get();
   city_act = qdef_act::action()->vs_city_get();
 
+  foreach (caras, gui()->trade_gen.lines) {
+    if (caras.autocaravan == actor_unit) {
+      int i;
+      if (nullptr != game_unit_by_number(actor_unit->id)
+          && nullptr != game_city_by_number(target_city->id)) {
+        request_do_action(ACTION_TRADE_ROUTE, actor_unit->id,
+                          target_city->id, 0);
+        client_unit_init_act_prob_cache(actor_unit);
+        diplomat_queue_handle_primary(actor_unit->id);
+        i = gui()->trade_gen.lines.indexOf(caras);
+        gui()->trade_gen.lines.takeAt(i);
+        return;
+      }
+    }
+  }
   if (target_city
       && try_default_city_action(actor_unit->id, target_city->id)
       && action_prob_possible(act_probs[static_cast<gen_action>(unit_act)])) {

Modified: branches/S2_6/client/gui-qt/mapview.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/mapview.cpp?rev=34473&r1=34472&r2=34473&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/mapview.cpp     (original)
+++ branches/S2_6/client/gui-qt/mapview.cpp     Thu Nov 10 09:03:39 2016
@@ -78,11 +78,9 @@
   int dx, dy;
   float w, h;
   float x1, y1, x2, y2;
-  bool ret;
   qtiles qgilles;
   struct city *pcity;
   struct color *pcolor;
-  trade_city *tc;
   QPen pen;
 
   if (!gui_options.draw_city_trade_routes || !can_client_control()
@@ -104,6 +102,10 @@
         && have_cities_trade_route(tile_city(qgilles.t1),
                                    tile_city(qgilles.t2))) {
         continue;
+    }
+
+    if (qgilles.autocaravan != nullptr) {
+      pcolor = get_color(tileset, COLOR_MAPVIEW_TRADE_ROUTES_SOME_BUILT);
     }
 
     pen.setColor(pcolor->qcolor);
@@ -133,53 +135,6 @@
                           *get_attention_crosshair_sprite(tileset)->pm);
     }
   }
-  /* Find units which might be going to establish trade */
-  pcolor = get_color(tileset, COLOR_MAPVIEW_TRADE_ROUTES_SOME_BUILT);
-  unit_list_iterate(client_player()->units, punit) {
-    struct tile *stile;
-    struct tile *ttile;
-    if (unit_can_do_action(punit, ACTION_TRADE_ROUTE)) {
-      if (!unit_has_orders(punit)) {
-        continue;
-      }
-      pcity = game_city_by_number(punit->homecity);
-      stile = pcity->tile;
-      ttile = punit->goto_tile;
-      if (tile_city(ttile) == nullptr) {
-        continue;
-      }
-      ret = true;
-      foreach (tc, gui()->trade_gen.cities) {
-        if ((tc->city == pcity && 
tc->new_tr_cities.contains(tile_city(ttile)))) {
-          ret = false;
-        }
-      }
-      if (ret == true) {
-        continue;
-      }
-      base_map_distance_vector(&dx, &dy, TILE_XY(stile), TILE_XY(ttile));
-      map_to_gui_vector(tileset, 1.0, &w, &h, dx, dy);
-      tile_to_canvas_pos(&x1, &y1, stile);
-      tile_to_canvas_pos(&x2, &y2, ttile);
-
-      pen.setColor(pcolor->qcolor);
-      pen.setStyle(Qt::DashLine);
-      pen.setDashOffset(4);
-      pen.setWidth(1);
-      painter->setPen(pen);
-      if (x2 - x1 == w && y2 - y1 == h) {
-        painter->drawLine(x1 + tileset_tile_width(tileset) / 2,
-                          y1 + tileset_tile_height(tileset) / 2,
-                          x1 + tileset_tile_width(tileset) / 2 + w,
-                          y1 + tileset_tile_height(tileset) / 2 + h);
-        continue;
-      }
-      painter->drawLine(x2 + tileset_tile_width(tileset) / 2,
-                        y2 + tileset_tile_height(tileset) / 2,
-                        x2 + tileset_tile_width(tileset) / 2 - w,
-                        y2 + tileset_tile_height(tileset) / 2 - h);
-    }
-  } unit_list_iterate_end;
 }
 
 /**************************************************************************

Modified: branches/S2_6/client/gui-qt/menu.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/menu.cpp?rev=34473&r1=34472&r2=34473&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/menu.cpp        (original)
+++ branches/S2_6/client/gui-qt/menu.cpp        Thu Nov 10 09:03:39 2016
@@ -60,12 +60,14 @@
 
 extern QApplication *qapp;
 
+static bool tradecity_rand(const trade_city *t1, const trade_city *t2);
 /**************************************************************************
   New turn callback
 **************************************************************************/
 void qt_start_turn()
 {
   gui()->rallies.run();
+  real_menus_update();
 }
 
 /**************************************************************************
@@ -281,35 +283,49 @@
 {
   trade_city *tc;
   trade_city *ttc;
-
-  lines.clear();
-  foreach (tc, cities) {
-    tc->pos_cities.clear();
-    tc->new_tr_cities.clear();
-    tc->curr_tr_cities.clear();
-  }
-  foreach (tc, cities) {
-    tc->trade_num = city_num_trade_routes(tc->city);
-    tc->poss_trade_num = 0;
-    tc->pos_cities.clear();
-    tc->new_tr_cities.clear();
-    tc->curr_tr_cities.clear();
-    tc->done = false;
-    foreach (ttc, cities) {
-      if (have_cities_trade_route(tc->city, ttc->city) == false
-          && can_establish_trade_route(tc->city, ttc->city)) {
-        tc->poss_trade_num++;
-        tc->pos_cities.append(ttc->city);
+  int i;
+  bool tdone;
+
+  for (i = 0; i < 100; i++) {
+    tdone = true;
+    qSort(cities.begin(), cities.end(), tradecity_rand);
+    lines.clear();
+    foreach (tc, cities) {
+      tc->pos_cities.clear();
+      tc->new_tr_cities.clear();
+      tc->curr_tr_cities.clear();
+    }
+    foreach (tc, cities) {
+      tc->trade_num = city_num_trade_routes(tc->city);
+      tc->poss_trade_num = 0;
+      tc->pos_cities.clear();
+      tc->new_tr_cities.clear();
+      tc->curr_tr_cities.clear();
+      tc->done = false;
+      foreach (ttc, cities) {
+        if (have_cities_trade_route(tc->city, ttc->city) == false
+            && can_establish_trade_route(tc->city, ttc->city)) {
+          tc->poss_trade_num++;
+          tc->pos_cities.append(ttc->city);
+        }
+        tc->over_max = tc->trade_num + tc->poss_trade_num
+                       - max_trade_routes(tc->city);
       }
-      tc->over_max = tc->trade_num + tc->poss_trade_num
-                     - max_trade_routes(tc->city);
-    }
-  }
-
-  find_certain_routes();
-  discard();
-  find_certain_routes();
-
+    }
+
+    find_certain_routes();
+    discard();
+    find_certain_routes();
+
+    foreach (tc, cities) {
+      if (!tc->done) {
+        tdone = false;
+      }
+    }
+    if (tdone) {
+      break;
+    }
+  }
   foreach (tc, cities) {
     if (!tc->done) {
       char text[1024];
@@ -397,7 +413,7 @@
 /**************************************************************************
   Drops one trade route for given city if possible
 **************************************************************************/
-bool trade_generator::discard_one(trade_city* tc)
+bool trade_generator::discard_one(trade_city *tc)
 {
   int best = 0;
   int current_candidate = 0;
@@ -419,6 +435,7 @@
   discard_trade(tc, ttc);
   return true;
 }
+
 
 /**************************************************************************
   Drops all trade routes for given city
@@ -437,6 +454,14 @@
     }
   }
   return false;
+}
+
+/**************************************************************************
+  Helper function ato randomize list
+**************************************************************************/
+bool tradecity_rand(const trade_city *t1, const trade_city *t2)
+{
+  return (qrand() % 2);
 }
 
 /**************************************************************************
@@ -472,6 +497,7 @@
         check_if_done(tc, ttc);
         gilles.t1 = tc->city->tile;
         gilles.t2 = ttc->city->tile;
+        gilles.autocaravan = nullptr;
         lines.append(gilles);
       }
     }
@@ -1143,6 +1169,10 @@
   connect(act, SIGNAL(triggered()), this, SLOT(slot_trade_city()));
   act = multiplayer_menu->addAction(_("Clear Trade Planning"));
   connect(act, SIGNAL(triggered()), this, SLOT(slot_clear_trade()));
+  act = multiplayer_menu->addAction(_("Automatic caravan"));
+  menu_list.insertMulti(AUTOTRADEROUTE, act);
+  connect(act, SIGNAL(triggered()), this, SLOT(slot_autocaravan()));
+  act->setShortcut(QKeySequence(tr("ctrl+j")));
   act = multiplayer_menu->addAction(_("Set/Unset rally point"));
   act->setShortcut(QKeySequence(tr("shift+s")));
   connect(act, SIGNAL(triggered()), this, SLOT(slot_rally()));
@@ -1906,6 +1936,12 @@
         }
         break;
 
+      case AUTOTRADEROUTE:
+        if (units_can_do_action(punits, ACTION_TRADE_ROUTE, TRUE)) {
+          i.value()->setEnabled(true);
+        }
+        break;
+
       case ORDER_TRADEROUTE:
         i.value()->setText(action_id_name_translation(ACTION_TRADE_ROUTE));
         if (can_units_do(punits, unit_can_est_trade_route_here)) {
@@ -2278,6 +2314,46 @@
   gui()->trade_gen.clear_trade_planing();
 }
 
+/***************************************************************************
+  Sends automatic caravan
+***************************************************************************/
+void mr_menu::slot_autocaravan()
+{
+  qtiles gilles;
+  struct unit *punit;
+  struct city *homecity;
+  struct tile *home_tile;
+  struct tile *dest_tile;
+  bool sent = false;
+
+  punit = head_of_units_in_focus();
+  homecity = game_city_by_number(punit->homecity);
+  home_tile = homecity->tile;
+  foreach(gilles, gui()->trade_gen.lines) {
+    if ((gilles.t1 == home_tile || gilles.t2 == home_tile)
+         && gilles.autocaravan == nullptr) {
+      /* send caravan */
+      if (gilles.t1 == home_tile) {
+        dest_tile = gilles.t2;
+      } else {
+        dest_tile = gilles.t1;
+      }
+      if (send_goto_tile(punit, dest_tile)) {
+        int i;
+        i = gui()->trade_gen.lines.indexOf(gilles);
+        gilles = gui()->trade_gen.lines.takeAt(i);
+        gilles.autocaravan = punit;
+        gui()->trade_gen.lines.append(gilles);
+        sent = true;
+        break;
+      }
+    }
+  }
+
+  if (!sent) {
+    send_chat(_("Didn't find any trade route to establish"));
+  }
+}
 
 /**************************************************************************
   Slot for setting quick airlift

Modified: branches/S2_6/client/gui-qt/menu.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/menu.h?rev=34473&r1=34472&r2=34473&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/menu.h  (original)
+++ branches/S2_6/client/gui-qt/menu.h  Thu Nov 10 09:03:39 2016
@@ -67,6 +67,7 @@
   GOTO_CITY,
   AIRLIFT,
   BUILD_WONDER,
+  AUTOTRADEROUTE,
   ORDER_TRADEROUTE,
   ORDER_DIPLOMAT_DLG,
   NUKE,
@@ -165,12 +166,19 @@
 };
 
 /**************************************************************************
-  Struct of 2 tiles, used for drawing trade routes
+  Struct of 2 tiles, used for drawing trade routes.
+  Also assigned caravan if it was sent
 ***************************************************************************/
 struct qtiles
 {
   struct tile *t1;
   struct tile *t2;
+  struct unit *autocaravan;
+
+  bool operator==(const qtiles& a) const
+  {
+    return (t1 == a.t1 && t2 == a.t2 && autocaravan == a.autocaravan);
+  }
 };
 
 /**************************************************************************
@@ -366,6 +374,7 @@
   void slot_trade_city();
   void slot_calculate();
   void slot_clear_trade();
+  void slot_autocaravan();
   void slot_rally();
   void slot_quickairlift_set();
   void slot_quickairlift();

Modified: branches/S2_6/client/gui-qt/shortcuts.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/shortcuts.cpp?rev=34473&r1=34472&r2=34473&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/shortcuts.cpp   (original)
+++ branches/S2_6/client/gui-qt/shortcuts.cpp   Thu Nov 10 09:03:39 2016
@@ -101,7 +101,7 @@
     _("Go to/Airlift to City...") },
   {SC_AUTOEXPLORE, Qt::Key_X, Qt::AllButtons, Qt::NoModifier,
     _("Auto Explore") },
-  {SC_PATROL, Qt::Key_P, Qt::AllButtons, Qt::NoModifier,
+  {SC_PATROL, Qt::Key_P, Qt::AllButtons, Qt::ControlModifier,
     _("Patrol") },
   {SC_UNSENTRY_TILE, Qt::Key_D, Qt::AllButtons,
     Qt::ShiftModifier | Qt::ControlModifier, _("Unsentry All On Tile") },


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to