Freeciv21
Develop your civilization from humble roots to a global empire
editor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2023 Freeciv21 and Freeciv contributors. This file
3  is part of Freeciv21. Freeciv21 is free software:
4 |\_/|,,_____,~~` you can redistribute it and/or modify it under the
5 (.".)~~ )`~}} terms of the GNU General Public License as published
6  \o/\ /---~\\ ~}} by the Free Software Foundation, either version 3 of
7  _// _// ~} the License, or (at your option) any later version.
8  You should have received a copy of the GNU General
9  Public License along with Freeciv21. If not, see
10  https://www.gnu.org/licenses/.
11  */
12 
13 #include <QRect>
14 #include <QSet>
15 
16 // utility
17 #include "bitvector.h"
18 #include "fcintl.h"
19 #include "log.h"
20 #include "support.h"
21 
22 // common
23 #include "game.h"
24 #include "map.h"
25 #include "packets.h"
26 
27 // client
28 #include "client_main.h"
29 #include "climap.h"
30 #include "control.h"
31 #include "editor.h"
32 #include "mapctrl_common.h"
33 #include "mapview_g.h"
34 #include "qtg_cxxside.h"
35 #include "tileset/tilespec.h"
37 
42 };
43 
46  ETF_HAS_VALUE = 1 << 0,
47  ETF_HAS_SIZE = 1 << 1,
48  ETF_HAS_COUNT = 1 << 2,
50  ETF_HAS_VALUE_ERASE = 1 << 4
51 };
52 
53 struct edit_buffer {
55  struct tile_list *vtiles;
56  const struct tile *origin;
57 };
58 
59 struct editor_tool {
60  int flags;
62  int size;
63  int count;
65  const char *name;
66  int value;
67  const char *tooltip;
68 };
69 
70 struct editor_state {
73 
74  const struct tile *current_tile;
76 
80  int selrect_x;
81  int selrect_y;
84 
86 
87  QSet<const struct tile *> *selected_tile_table;
89 };
90 
91 static struct editor_state *editor = nullptr;
92 
97 {
98  struct editor_tool *tool = editor->tools + ett;
99 
100  if (ett == ETT_TERRAIN_SPECIAL || ett == ETT_ROAD
101  || ett == ETT_MILITARY_BASE || ett == ETT_TERRAIN_RESOURCE) {
102  struct extra_type *first = nullptr;
103 
104  extra_type_iterate(pextra)
105  {
106  if (ett == ETT_ROAD) {
107  if (is_extra_caused_by(pextra, EC_ROAD)) {
108  first = pextra;
109  break;
110  }
111  } else if (ett == ETT_MILITARY_BASE) {
112  if (is_extra_caused_by(pextra, EC_BASE)) {
113  first = pextra;
114  break;
115  }
116  } else if (ett == ETT_TERRAIN_RESOURCE) {
117  if (is_extra_caused_by(pextra, EC_RESOURCE)) {
118  first = pextra;
119  break;
120  }
121  } else {
122  // Considers extras that are neither bases or roads, specials
123  first = pextra;
124  break;
125  }
126  }
128 
129  if (first != nullptr) {
130  tool->value = extra_index(first);
131  } else {
132  tool->value = 0;
133  }
134  } else {
135  tool->value = 0;
136  }
137 }
138 
142 static void tool_init(enum editor_tool_type ett, const char *name, int flags,
143  const char *tooltip)
144 {
145  struct editor_tool *tool;
146 
147  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
148  return;
149  }
150 
151  tool = editor->tools + ett;
152 
153  if (ett == ETT_COPYPASTE) {
154  tool->mode = ETM_COPY;
155  } else {
156  tool->mode = ETM_PAINT;
157  }
158  tool->name = name;
159  tool->flags = flags;
160  tool->tooltip = tooltip;
161  tool->size = 1;
162  tool->count = 1;
163  tool->applied_player_no = 0;
164 
165  tool_set_init_value(ett);
166 }
167 
172 {
173  int t;
174 
175  for (t = 0; t < NUM_EDITOR_TOOL_TYPES; t++) {
176  tool_set_init_value(static_cast<editor_tool_type>(t));
177  }
178 }
179 
185 {
186  fc_assert(editor == nullptr);
187 
188  editor = new editor_state[1]();
189 
191  _("Change tile terrain.\nShortcut: t\n"
192  "Select terrain type: shift+t or right-click here."));
193  tool_init(ETT_TERRAIN_RESOURCE, _("Terrain Resource"),
195  _("Change tile terrain resources.\nShortcut: r\n"
196  "Select resource type: shift+r or right-click here."));
197  tool_init(ETT_TERRAIN_SPECIAL, _("Terrain Special"),
199  _("Modify tile specials.\nShortcut: s\n"
200  "Select special type: shift+s or right-click here."));
201  tool_init(ETT_ROAD, _("Road"),
203  _("Modify roads on tile.\nShortcut: p\n"
204  "Select road type: shift+p or right-click here."));
205  tool_init(ETT_MILITARY_BASE, _("Military Base"),
207  _("Create a military base.\nShortcut: m\n"
208  "Select base type: shift+m or right-click here."));
209  tool_init(ETT_UNIT, _("Unit"),
212  _("Create unit.\nShortcut: u\nSelect unit "
213  "type: shift+u or right-click here."));
215  _("Create city.\nShortcut: c"));
216  tool_init(ETT_VISION, _("Vision"), ETF_HAS_SIZE,
217  _("Modify player's tile knowledge.\nShortcut: v"));
218  tool_init(ETT_STARTPOS, _("Start Position"), ETF_NO_FLAGS,
219  _("Place a start position which allows any nation to "
220  "start at the tile. To allow only certain nations to "
221  "start there, middle click on the start position on "
222  "the map and use the property editor.\nShortcut: b"));
223 
224  tool_init(ETT_COPYPASTE, _("Copy/Paste"), ETF_HAS_SIZE,
225  _("Copy and paste tiles.\n"
226  "Shortcut for copy mode: shift-c\n"
227  "Shoftcut for paste mode: shift-v"));
229 
230  editor->selected_tile_table = new QSet<const struct tile *>;
231 }
232 
237 {
238  fc_assert_ret(editor != nullptr);
239 
241  editor->selected_tile_table->clear();
242 }
243 
248 {
249  if (editor != nullptr) {
251  delete editor->selected_tile_table;
252  delete[] editor;
253  editor = nullptr;
254  }
255 }
256 
261 {
262  if (editor == nullptr) {
263  return;
264  }
265 
266  if (ett >= NUM_EDITOR_TOOL_TYPES) {
267  return;
268  }
269 
270  editor->tool = ett;
271 }
272 
277 {
278  if (editor == nullptr) {
279  return NUM_EDITOR_TOOL_TYPES;
280  }
281 
282  return editor->tool;
283 }
284 
289  enum editor_tool_mode etm)
290 {
291  if (editor == nullptr || ett >= NUM_EDITOR_TOOL_TYPES
292  || etm >= NUM_EDITOR_TOOL_MODES || !editor_tool_has_mode(ett, etm)) {
293  return;
294  }
295 
296  editor->tools[ett].mode = etm;
297 }
298 
303  enum editor_tool_mode etm)
304 {
305  if (editor == nullptr || ett >= NUM_EDITOR_TOOL_TYPES
306  || etm >= NUM_EDITOR_TOOL_MODES) {
307  return false;
308  }
309 
310  if (etm == ETM_COPY || etm == ETM_PASTE) {
311  return ett == ETT_COPYPASTE;
312  }
313 
314  if (ett == ETT_COPYPASTE) {
315  return etm == ETM_COPY || etm == ETM_PASTE;
316  }
317 
318  return true;
319 }
320 
325 {
326  if (editor == nullptr || ett >= NUM_EDITOR_TOOL_TYPES) {
327  return NUM_EDITOR_TOOL_MODES;
328  }
329  return editor->tools[ett].mode;
330 }
331 
336 
346 {
347  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
348  return false;
349  }
350 
351  switch (ett) {
352  case ETT_MILITARY_BASE:
353  return base_count() > 0;
354  case ETT_ROAD:
355  return road_count() > 0;
357  return game.control.num_resource_types > 0;
358  case ETT_UNIT:
359  return utype_count() > 0;
360  default:
361  break;
362  }
363 
364  return true;
365 }
366 
372 {
373  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
374  return false;
375  }
376  return editor->tools[ett].flags & ETF_HAS_VALUE;
377 }
378 
384 {
385  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES
386  || !editor_tool_has_value(ett)) {
387  return;
388  }
389  editor->tools[ett].value = value;
390 }
391 
396 {
397  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES
398  || !editor_tool_has_value(ett)) {
399  return 0;
400  }
401 
402  return editor->tools[ett].value;
403 }
404 
408 static void editor_start_selection_rectangle(int canvas_x, int canvas_y)
409 {
410  if (!editor) {
411  return;
412  }
413 
415  && editor->selected_tile_table->count() > 0) {
418  }
419 
420  editor->selrect_start_x = canvas_x;
421  editor->selrect_start_y = canvas_y;
422  editor->selrect_width = 0;
423  editor->selrect_height = 0;
424  editor->selrect_active = true;
425 }
426 
432 static inline bool tile_really_has_any_specials(const struct tile *ptile)
433 {
434  if (!ptile) {
435  return false;
436  }
437 
438  extra_type_by_cause_iterate(static_cast<extra_cause>(EC_SPECIAL), pextra)
439  {
440  if (tile_has_extra(ptile, pextra)) {
441  return true;
442  }
443  }
445 
446  return false;
447 }
448 
453 static void editor_grab_applied_player(const struct tile *ptile)
454 {
455  int apno = -1;
456 
457  if (!editor || !ptile) {
458  return;
459  }
460 
461  if (client_has_player()
462  && tile_get_known(ptile, client_player()) == TILE_UNKNOWN) {
463  return;
464  }
465 
466  if (tile_city(ptile) != nullptr) {
467  apno = player_number(city_owner(tile_city(ptile)));
468  } else if (unit_list_size(ptile->units) > 0) {
469  struct unit *punit = unit_list_get(ptile->units, 0);
470 
471  apno = player_number(unit_owner(punit));
472  } else if (tile_owner(ptile) != nullptr) {
473  apno = player_number(tile_owner(ptile));
474  }
475 
476  if (player_by_number(apno) != nullptr) {
478  editgui_refresh();
479  }
480 }
481 
485 static void editor_grab_tool(const struct tile *ptile)
486 {
487  int ett = -1, value = 0;
488  struct extra_type *first_base = nullptr;
489  struct extra_type *first_road = nullptr;
490  struct extra_type *first_resource = nullptr;
491 
492  if (!editor) {
493  return;
494  }
495 
496  if (!ptile) {
497  return;
498  }
499 
500  extra_type_by_cause_iterate(EC_BASE, pextra)
501  {
502  if (tile_has_extra(ptile, pextra)) {
503  first_base = pextra;
504  break;
505  }
506  }
508 
509  extra_type_by_cause_iterate(EC_ROAD, pextra)
510  {
511  if (tile_has_extra(ptile, pextra)) {
512  first_road = pextra;
513  break;
514  }
515  }
517 
518  extra_type_by_cause_iterate(EC_RESOURCE, pextra)
519  {
520  if (tile_has_extra(ptile, pextra)) {
521  first_resource = pextra;
522  break;
523  }
524  }
526 
527  if (client_has_player()
528  && tile_get_known(ptile, client_player()) == TILE_UNKNOWN) {
529  ett = ETT_VISION;
530 
531  } else if (tile_city(ptile)) {
532  ett = ETT_CITY;
533 
534  } else if (unit_list_size(ptile->units) > 0) {
535  int max_score = 0, score;
536  struct unit *grabbed_punit = nullptr;
537 
538  unit_list_iterate(ptile->units, punit)
539  {
540  if (uclass_has_flag(unit_class_get(punit), UCF_UNREACHABLE)) {
541  score = 5;
542  } else if (utype_move_type(unit_type_get(punit)) == UMT_LAND) {
543  score = 4;
544  } else if (utype_move_type(unit_type_get(punit)) == UMT_SEA) {
545  score = 3;
546  } else {
547  score = 2;
548  }
549  if (unit_transported(punit)) {
550  score = 1;
551  }
552 
553  if (score > max_score) {
554  max_score = score;
555  grabbed_punit = punit;
556  }
557  }
559 
560  if (grabbed_punit) {
561  ett = ETT_UNIT;
562  value = utype_number(unit_type_get(grabbed_punit));
563  }
564  } else if (first_base != nullptr) {
565  ett = ETT_MILITARY_BASE;
566  value = extra_index(first_base);
567 
568  } else if (first_road != nullptr) {
569  ett = ETT_ROAD;
570  value = extra_index(first_road);
571 
572  } else if (tile_really_has_any_specials(ptile)) {
573  struct extra_type *specials_array[MAX_EXTRA_TYPES];
574  int count = 0, i;
575  struct extra_type *special = nullptr;
576 
577  extra_type_by_cause_iterate(static_cast<extra_cause>(EC_SPECIAL), s)
578  {
579  specials_array[count++] = s;
580  }
582 
583  // Grab specials in reverse order of enum tile_special_type.
584 
585  for (i = count - 1; i >= 0; i--) {
586  if (tile_has_extra(ptile, specials_array[i])) {
587  special = specials_array[i];
588  break;
589  }
590  }
591 
592  if (special != nullptr) {
593  ett = ETT_TERRAIN_SPECIAL;
594  value = extra_index(special);
595  }
596  } else if (tile_resource(ptile) && first_resource) {
597  ett = ETT_TERRAIN_RESOURCE;
598  value = extra_index(first_resource);
599 
600  } else if (tile_terrain(ptile) != nullptr) {
601  ett = ETT_TERRAIN;
602  value = terrain_number(tile_terrain(ptile));
603  }
604 
605  if (ett < 0) {
606  return;
607  }
608 
609  editor_set_tool(static_cast<editor_tool_type>(ett));
610  if (editor_tool_has_value(static_cast<editor_tool_type>(ett))) {
611  editor_tool_set_value(static_cast<editor_tool_type>(ett), value);
612  }
613  editgui_refresh();
614 }
615 
619 static inline bool can_edit_tile_properties(struct tile *ptile)
620 {
621  return ptile != nullptr;
622 }
623 
629 static void popup_properties(struct tile *ptile)
630 {
631  struct tile_list *tiles;
632 
633  if (!ptile) {
634  return;
635  }
636 
637  tiles = tile_list_new();
638 
639  if (editor_tile_is_selected(ptile)) {
640  for (const auto *sel_tile : qAsConst(*editor->selected_tile_table)) {
641  if (can_edit_tile_properties(const_cast<tile *>(sel_tile))) {
642  tile_list_append(tiles, const_cast<tile *>(sel_tile));
643  }
644  }
645  } else {
646  if (can_edit_tile_properties(ptile)) {
647  tile_list_append(tiles, ptile);
648  }
649  }
650 
652 
653  tile_list_destroy(tiles);
654 }
655 
659 void editor_mouse_button_press(int canvas_x, int canvas_y, int button,
660  int modifiers)
661 {
662  struct tile *ptile;
663 
664  if (editor == nullptr) {
665  return;
666  }
667 
668  ptile = canvas_pos_to_tile(canvas_x, canvas_y);
669  if (ptile == nullptr) {
670  return;
671  }
672 
673  switch (button) {
674  case MOUSE_BUTTON_LEFT:
675  if (modifiers == EKM_SHIFT) {
676  editor_grab_tool(ptile);
677  } else if (modifiers == EKM_CTRL) {
679  } else if (modifiers == EKM_NONE) {
680  editor->tool_active = true;
681  editor_apply_tool(ptile, false);
684  }
685  break;
686 
687  case MOUSE_BUTTON_RIGHT:
688  if (modifiers == (EKM_ALT | EKM_CTRL)) {
689  popup_properties(ptile);
690  break;
691  }
692 
693  if (modifiers == EKM_SHIFT) {
695  } else if (modifiers == EKM_ALT) {
697  } else if (modifiers == EKM_NONE) {
699  } else {
700  break;
701  }
702  editor_start_selection_rectangle(canvas_x, canvas_y);
703  break;
704 
705  case MOUSE_BUTTON_MIDDLE:
706  if (modifiers == EKM_NONE) {
707  popup_properties(ptile);
708  }
709  break;
710 
711  default:
712  break;
713  }
714 }
715 
719 static void editor_end_selection_rectangle(int canvas_x, int canvas_y)
720 {
721  int w, h;
722 
723  if (!editor) {
724  return;
725  }
726 
727  editor->selrect_active = false;
728 
729  if (editor->selrect_width <= 0 || editor->selrect_height <= 0) {
730  struct tile *ptile;
731 
732  ptile = canvas_pos_to_tile(canvas_x, canvas_y);
733  if (ptile && editor->selection_mode == SELECTION_MODE_ADD) {
734  editor_selection_add(ptile);
735  } else if (ptile && editor->selection_mode == SELECTION_MODE_REMOVE) {
737  } else {
738  recenter_button_pressed(canvas_x, canvas_y);
739  return;
740  }
741 
742  if (ptile) {
743  refresh_tile_mapcanvas(ptile, true);
744  }
745 
746  return;
747  }
748 
749  const auto rect = QRect(mapview.gui_x0 + editor->selrect_x,
752  for (auto it = freeciv::gui_rect_iterator(tileset, rect); it.next();) {
753  if (!it.has_tile()) {
754  continue;
755  }
758  editor_selection_add(it.tile());
760  editor_selection_remove(it.tile());
761  }
762  }
763 
766 
768  editor->selrect_width + 2 * w,
769  editor->selrect_height + 2 * h);
770  flush_dirty();
771 }
772 
776 static void editor_draw_selrect()
777 {
778  if (!editor) {
779  return;
780  }
781 
783  && editor->selrect_height > 0) {
786  }
787 }
788 
792 void editor_mouse_button_release(int canvas_x, int canvas_y, int button,
793  int modifiers)
794 {
795  Q_UNUSED(modifiers)
796  switch (button) {
797  case MOUSE_BUTTON_LEFT:
798  editor_set_current_tile(nullptr);
799  editor->tool_active = false;
800  break;
801 
802  case MOUSE_BUTTON_RIGHT:
803  if (editor->selrect_active) {
804  editor_end_selection_rectangle(canvas_x, canvas_y);
805  }
806  break;
807 
808  case MOUSE_BUTTON_MIDDLE:
809  break;
810 
811  default:
812  break;
813  }
814 }
815 
820 static void editor_resize_selection_rectangle(int canvas_x, int canvas_y)
821 {
822  int xl, yt, xr, yb;
823 
824  if (editor->selrect_start_x <= canvas_x) {
825  xl = editor->selrect_start_x;
826  xr = canvas_x;
827  } else {
828  xl = canvas_x;
829  xr = editor->selrect_start_x;
830  }
831 
832  if (editor->selrect_start_y <= canvas_y) {
833  yt = editor->selrect_start_y;
834  yb = canvas_y;
835  } else {
836  yt = canvas_y;
837  yb = editor->selrect_start_y;
838  }
839 
840  // Erase the previously drawn rectangle.
842 
843  if (xl == xr || yt == yb) {
844  editor->selrect_width = 0;
845  editor->selrect_height = 0;
846  return;
847  }
848 
849  editor->selrect_x = xl;
850  editor->selrect_y = yt;
851  editor->selrect_width = xr - xl;
852  editor->selrect_height = yb - yt;
853 
855 }
856 
860 void editor_mouse_move(int canvas_x, int canvas_y, int modifiers)
861 {
862  Q_UNUSED(modifiers)
863  const struct tile *ptile, *old;
864 
865  if (!editor) {
866  return;
867  }
868 
869  old = editor_get_current_tile();
870  ptile = canvas_pos_to_tile(canvas_x, canvas_y);
871 
872  if (!ptile) {
873  return;
874  }
875 
876  if (editor->tool_active && old != nullptr && old != ptile) {
877  editor_apply_tool(ptile, false);
880  }
881 
882  if (editor->selrect_active) {
883  editor_resize_selection_rectangle(canvas_x, canvas_y);
884  }
885 }
886 
893 {
894  send_packet_edit_check_tiles(&client.conn);
895 }
896 
904 void editor_apply_tool(const struct tile *ptile, bool part_of_selection)
905 {
906  enum editor_tool_type ett;
907  enum editor_tool_mode etm;
908  int value, size, count, apno, tile, id;
909  bool erase;
910  struct connection *my_conn = &client.conn;
911 
912  if (editor == nullptr || ptile == nullptr) {
913  return;
914  }
915 
916  ett = editor_get_tool();
917  etm = editor_tool_get_mode(ett);
918  size = editor_tool_get_size(ett);
919  count = editor_tool_get_count(ett);
920  value = editor_tool_get_value(ett);
921  apno = editor_tool_get_applied_player(ett);
922 
923  if (ett != ETT_VISION && !client_is_global_observer()
924  && client_has_player()
925  && tile_get_known(ptile, client_player()) == TILE_UNKNOWN) {
926  return;
927  }
928 
930  && player_by_number(apno) == nullptr) {
931  return;
932  }
933 
934  if (ett == ETT_COPYPASTE) {
935  struct edit_buffer *ebuf;
936  ebuf = editor_get_copy_buffer();
937  if (etm == ETM_COPY) {
938  if (part_of_selection) {
939  edit_buffer_copy(ebuf, ptile);
940  } else {
941  edit_buffer_clear(ebuf);
942  edit_buffer_copy_square(ebuf, ptile, size);
943  editgui_refresh();
944  }
945  } else if (etm == ETM_PAINT || etm == ETM_PASTE) {
946  edit_buffer_paste(ebuf, ptile);
947  }
948  return;
949  }
950 
951  if (part_of_selection && ett != ETT_CITY) {
952  size = 1;
953  }
954 
955  erase = (etm == ETM_ERASE);
956  tile = tile_index(ptile);
957 
958  switch (ett) {
959  case ETT_TERRAIN:
960  dsend_packet_edit_tile_terrain(my_conn, tile, erase ? 0 : value, size);
961  break;
962 
964  case ETT_TERRAIN_SPECIAL:
965  case ETT_ROAD:
966  case ETT_MILITARY_BASE:
967  dsend_packet_edit_tile_extra(my_conn, tile, value, erase, apno, size);
968  break;
969 
970  case ETT_UNIT:
971  if (erase) {
972  dsend_packet_edit_unit_remove(my_conn, apno, tile, value, count);
973  } else {
974  dsend_packet_edit_unit_create(my_conn, apno, tile, value, count, 0);
975  }
976  break;
977 
978  case ETT_CITY:
979  if (erase) {
980  struct city *pcity = tile_city(ptile);
981  if (pcity != nullptr) {
982  id = pcity->id;
983  dsend_packet_edit_city_remove(my_conn, id);
984  }
985  } else {
986  dsend_packet_edit_city_create(my_conn, apno, tile, size, 0);
987  }
988  break;
989 
990  case ETT_VISION:
991  if (client_has_player()) {
992  id = client_player_number();
993  dsend_packet_edit_player_vision(my_conn, id, tile, !erase, size);
994  }
995  break;
996 
997  case ETT_STARTPOS:
998  dsend_packet_edit_startpos(my_conn, tile, erase, 0);
999  break;
1000 
1001  default:
1002  break;
1003  }
1004 }
1005 
1009 void editor_set_current_tile(const struct tile *ptile)
1010 {
1011  if (editor == nullptr) {
1012  return;
1013  }
1014 
1015  editor->current_tile = ptile;
1016 }
1017 
1022 {
1023  if (editor == nullptr) {
1024  return nullptr;
1025  }
1026 
1027  return editor->current_tile;
1028 }
1029 
1035  enum editor_tool_mode etm)
1036 {
1037  if (!editor_tool_has_mode(ett, etm)) {
1038  return;
1039  }
1040  if (editor_tool_get_mode(ett) == etm) {
1042  } else {
1043  editor_tool_set_mode(ett, etm);
1044  }
1045 }
1046 
1051 {
1052  int mode, count;
1053  bool found = false;
1054 
1055  mode = editor_tool_get_mode(ett);
1056  if (!(0 <= mode && mode < NUM_EDITOR_TOOL_MODES)) {
1057  return;
1058  }
1059 
1060  for (count = 0; count < NUM_EDITOR_TOOL_MODES; count++) {
1061  mode = (mode + 1) % NUM_EDITOR_TOOL_MODES;
1062  if (editor_tool_has_mode(ett, static_cast<editor_tool_mode>(mode))) {
1063  found = true;
1064  break;
1065  }
1066  }
1067 
1068  if (found) {
1069  editor_tool_set_mode(ett, static_cast<editor_tool_mode>(mode));
1070  }
1071 }
1072 
1077 {
1078  if (!editor) {
1079  return;
1080  }
1081  editor->selected_tile_table->clear();
1082 }
1083 
1087 void editor_selection_add(const struct tile *ptile)
1088 {
1089  if (!editor || !ptile) {
1090  return;
1091  }
1092  editor->selected_tile_table->insert(ptile);
1093 }
1094 
1098 void editor_selection_remove(const struct tile *ptile)
1099 {
1100  if (!editor || !ptile) {
1101  return;
1102  }
1103  editor->selected_tile_table->remove(ptile);
1104 }
1105 
1109 bool editor_tile_is_selected(const struct tile *ptile)
1110 {
1111  if (!editor || !ptile) {
1112  return false;
1113  }
1114  return editor->selected_tile_table->contains(ptile);
1115 }
1116 
1121 {
1122  enum editor_tool_type ett;
1123 
1124  if (!editor || editor->selected_tile_table->count() <= 0) {
1125  return;
1126  }
1127 
1128  ett = editor_get_tool();
1129  if (editor_tool_get_mode(ett) == ETM_COPY) {
1130  struct edit_buffer *ebuf;
1131  ebuf = editor_get_copy_buffer();
1132  edit_buffer_clear(ebuf);
1134  }
1135 
1137  for (const auto *ptile : qAsConst(*editor->selected_tile_table)) {
1138  editor_apply_tool(ptile, true);
1139  }
1142 
1143  if (editor_tool_get_mode(ett) == ETM_COPY) {
1144  editgui_refresh();
1145  }
1146 }
1147 
1152 {
1153  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1154  return "";
1155  }
1156 
1157  return editor->tools[ett].name;
1158 }
1159 
1164 const char *editor_tool_get_value_name(enum editor_tool_type emt, int value)
1165 {
1166  struct terrain *pterrain;
1167  struct unit_type *putype;
1168  struct extra_type *pextra;
1169 
1170  if (!editor) {
1171  return "";
1172  }
1173 
1174  switch (emt) {
1175  case ETT_TERRAIN:
1176  pterrain = terrain_by_number(value);
1177  return pterrain ? terrain_name_translation(pterrain) : "";
1178  break;
1179  case ETT_TERRAIN_RESOURCE:
1180  case ETT_TERRAIN_SPECIAL:
1181  case ETT_ROAD:
1182  case ETT_MILITARY_BASE:
1183  pextra = extra_by_number(value);
1184  return pextra != nullptr ? extra_name_translation(pextra) : "";
1185  break;
1186  case ETT_UNIT:
1187  putype = utype_by_number(value);
1188  return putype ? utype_name_translation(putype) : "";
1189  break;
1190  default:
1191  break;
1192  }
1193  return "";
1194 }
1195 
1200 {
1201  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1202  return false;
1203  }
1204  return editor->tools[ett].flags & ETF_HAS_SIZE;
1205 }
1206 
1211 {
1212  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1213  return 1;
1214  }
1215  return editor->tools[ett].size;
1216 }
1217 
1222 {
1223  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1224  return;
1225  }
1226  editor->tools[ett].size = MAX(1, size);
1227 }
1228 
1234 {
1235  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1236  return false;
1237  }
1238  return editor->tools[ett].flags & ETF_HAS_COUNT;
1239 }
1240 
1245 {
1246  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1247  return 1;
1248  }
1249  return editor->tools[ett].count;
1250 }
1251 
1255 void editor_tool_set_count(enum editor_tool_type ett, int count)
1256 {
1257  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1258  return;
1259  }
1260  editor->tools[ett].count = MAX(1, count);
1261 }
1262 
1268 {
1269  const struct editor_sprites *sprites;
1270 
1271  if (!tileset || ett >= NUM_EDITOR_TOOL_TYPES) {
1272  return nullptr;
1273  }
1274 
1275  sprites = get_editor_sprites(tileset);
1276  if (!sprites) {
1277  return nullptr;
1278  }
1279 
1280  switch (ett) {
1281  case ETT_COPYPASTE:
1282  return sprites->copypaste;
1283  break;
1284  case ETT_TERRAIN:
1285  return sprites->terrain;
1286  break;
1287  case ETT_TERRAIN_RESOURCE:
1288  return sprites->terrain_resource;
1289  break;
1290  case ETT_TERRAIN_SPECIAL:
1291  return sprites->terrain_special;
1292  break;
1293  case ETT_ROAD:
1294  return sprites->road;
1295  case ETT_MILITARY_BASE:
1296  return sprites->military_base;
1297  case ETT_UNIT:
1298  return sprites->unit;
1299  break;
1300  case ETT_CITY:
1301  return sprites->city;
1302  break;
1303  case ETT_VISION:
1304  return sprites->vision;
1305  break;
1306  case ETT_STARTPOS:
1307  return sprites->startpos;
1308  break;
1309  default:
1310  break;
1311  }
1312 
1313  return nullptr;
1314 }
1315 
1320 {
1321  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES
1322  || !editor->tools[ett].tooltip) {
1323  return "";
1324  }
1325  return editor->tools[ett].tooltip;
1326 }
1327 
1334 {
1335  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1336  return -1;
1337  }
1338  return editor->tools[ett].applied_player_no;
1339 }
1340 
1345 {
1346  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1347  return;
1348  }
1349  editor->tools[ett].applied_player_no = player_no;
1350 }
1351 
1357 {
1358  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1359  return false;
1360  }
1361  return editor->tools[ett].flags & ETF_HAS_APPLIED_PLAYER;
1362 }
1363 
1369 {
1370  if (!editor || ett >= NUM_EDITOR_TOOL_TYPES) {
1371  return false;
1372  }
1373  return editor->tools[ett].flags & ETF_HAS_VALUE_ERASE;
1374 }
1375 
1386 {
1387  struct unit *vunit;
1388  struct player *pplayer;
1389  struct unit_type *putype;
1390  int apno, value;
1391 
1393  putype = utype_by_number(value);
1394 
1395  if (!putype) {
1396  return nullptr;
1397  }
1398 
1400  pplayer = player_by_number(apno);
1401  if (!pplayer) {
1402  return nullptr;
1403  }
1404 
1405  vunit = unit_virtual_create(pplayer, nullptr, putype, 0);
1406 
1407  return vunit;
1408 }
1409 
1414 {
1415  struct edit_buffer *ebuf;
1416 
1417  if (!(0 <= type_flags && type_flags <= EBT_ALL)) {
1418  return nullptr;
1419  }
1420 
1421  ebuf = new edit_buffer[1]();
1422  ebuf->type_flags = type_flags;
1423  ebuf->vtiles = tile_list_new();
1424 
1425  return ebuf;
1426 }
1427 
1431 void edit_buffer_free(struct edit_buffer *ebuf)
1432 {
1433  if (!ebuf) {
1434  return;
1435  }
1436 
1437  if (ebuf->vtiles) {
1438  tile_list_iterate(ebuf->vtiles, vtile) { tile_virtual_destroy(vtile); }
1440  tile_list_destroy(ebuf->vtiles);
1441  ebuf->vtiles = nullptr;
1442  }
1443  delete[] ebuf;
1444 }
1445 
1449 void edit_buffer_clear(struct edit_buffer *ebuf)
1450 {
1451  if (!ebuf || !ebuf->vtiles) {
1452  return;
1453  }
1454 
1455  tile_list_iterate(ebuf->vtiles, vtile) { tile_virtual_destroy(vtile); }
1457  tile_list_clear(ebuf->vtiles);
1458 
1459  edit_buffer_set_origin(ebuf, nullptr);
1460 }
1461 
1467  const struct tile *center, int radius)
1468 {
1469  if (!ebuf || !center || radius < 1) {
1470  return;
1471  }
1472 
1473  edit_buffer_set_origin(ebuf, center);
1474  square_iterate(&(wld.map), center, radius - 1, ptile)
1475  {
1476  edit_buffer_copy(ebuf, ptile);
1477  }
1479 }
1480 
1484 void edit_buffer_copy(struct edit_buffer *ebuf, const struct tile *ptile)
1485 {
1486  struct tile *vtile;
1487  struct unit *vunit;
1488  bool copied = false;
1489 
1490  if (!ebuf || !ptile) {
1491  return;
1492  }
1493 
1494  vtile = tile_virtual_new(nullptr);
1495  vtile->index = tile_index(ptile);
1496 
1497  edit_buffer_type_iterate(ebuf, type)
1498  {
1499  switch (type) {
1500  case EBT_TERRAIN:
1501  if (tile_terrain(ptile)) {
1502  tile_set_terrain(vtile, tile_terrain(ptile));
1503  copied = true;
1504  }
1505  break;
1506  case EBT_RESOURCE:
1507  if (tile_resource(ptile)) {
1508  tile_set_resource(vtile, tile_resource(ptile));
1509  copied = true;
1510  }
1511  break;
1512  case EBT_SPECIAL:
1513  extra_type_by_cause_iterate(static_cast<extra_cause>(EC_SPECIAL),
1514  pextra)
1515  {
1516  if (tile_has_extra(ptile, pextra)) {
1517  tile_add_extra(vtile, pextra);
1518  copied = true;
1519  }
1520  }
1522  break;
1523  case EBT_BASE:
1524  extra_type_iterate(pextra)
1525  {
1526  if (tile_has_extra(ptile, pextra)
1527  && is_extra_caused_by(pextra, EC_BASE)) {
1528  tile_add_extra(vtile, pextra);
1529  copied = true;
1530  }
1531  }
1533  break;
1534  case EBT_ROAD:
1535  extra_type_iterate(pextra)
1536  {
1537  if (tile_has_extra(ptile, pextra)
1538  && is_extra_caused_by(pextra, EC_ROAD)) {
1539  tile_add_extra(vtile, pextra);
1540  copied = true;
1541  }
1542  }
1544  break;
1545  case EBT_UNIT:
1546  unit_list_iterate(ptile->units, punit)
1547  {
1548  if (!punit) {
1549  continue;
1550  }
1551  vunit = unit_virtual_create(unit_owner(punit), nullptr,
1552  unit_type_get(punit), punit->veteran);
1553  vunit->homecity = punit->homecity;
1554  vunit->hp = punit->hp;
1555  unit_list_append(vtile->units, vunit);
1556  copied = true;
1557  }
1559  break;
1560  case EBT_CITY:
1561  if (tile_city(ptile)) {
1562  struct city *pcity, *vcity;
1563  char name[MAX_LEN_NAME];
1564 
1565  pcity = tile_city(ptile);
1566  fc_snprintf(name, sizeof(name), "Copy of %s", city_name_get(pcity));
1567  vcity = create_city_virtual(city_owner(pcity), nullptr, name);
1568  city_size_set(vcity, city_size_get(pcity));
1569  improvement_iterate(pimprove)
1570  {
1571  if (!is_improvement(pimprove)
1572  || !city_has_building(pcity, pimprove)) {
1573  continue;
1574  }
1575  city_add_improvement(vcity, pimprove);
1576  }
1578  tile_set_worked(vtile, vcity);
1579  copied = true;
1580  }
1581  break;
1582  default:
1583  break;
1584  }
1585  }
1587 
1588  if (copied) {
1589  tile_list_append(ebuf->vtiles, vtile);
1590  } else {
1591  tile_virtual_destroy(vtile);
1592  }
1593 }
1594 
1598 static void fill_tile_edit_packet(struct packet_edit_tile *packet,
1599  const struct tile *ptile)
1600 {
1601  const struct extra_type *presource;
1602  const struct terrain *pterrain;
1603 
1604  if (!packet || !ptile) {
1605  return;
1606  }
1607  packet->tile = tile_index(ptile);
1608  packet->extras = *tile_extras(ptile);
1609 
1610  presource = tile_resource(ptile);
1611  packet->resource = presource ? extra_number(presource) : extra_count();
1612 
1613  pterrain = tile_terrain(ptile);
1614  packet->terrain = pterrain ? terrain_number(pterrain) : terrain_count();
1615  if (ptile->extras_owner != nullptr) {
1616  packet->eowner = player_number(ptile->extras_owner);
1617  } else {
1618  packet->eowner = MAP_TILE_OWNER_NULL;
1619  }
1620 
1621  if (ptile->label == nullptr) {
1622  packet->label[0] = '\0';
1623  } else {
1624  sz_strlcpy(packet->label, ptile->label);
1625  }
1626 }
1627 
1632 static void paste_tile(struct edit_buffer *ebuf, const struct tile *vtile,
1633  const struct tile *ptile_dest)
1634 {
1635  struct connection *my_conn = &client.conn;
1636  struct packet_edit_tile tile_packet;
1637  struct city *vcity;
1638  int value, owner, tile;
1639  bool send_edit_tile = false;
1640 
1641  if (!ebuf || !vtile || !ptile_dest) {
1642  return;
1643  }
1644 
1645  tile = tile_index(ptile_dest);
1646 
1647  fill_tile_edit_packet(&tile_packet, ptile_dest);
1648 
1649  edit_buffer_type_iterate(ebuf, type)
1650  {
1651  switch (type) {
1652  case EBT_TERRAIN:
1653  if (!tile_terrain(vtile)) {
1654  continue;
1655  }
1656  value = terrain_number(tile_terrain(vtile));
1657  dsend_packet_edit_tile_terrain(my_conn, tile, value, 1);
1658  break;
1659  case EBT_RESOURCE:
1660  extra_type_by_cause_iterate(EC_RESOURCE, pextra)
1661  {
1662  if (tile_has_extra(vtile, pextra)) {
1663  BV_SET(tile_packet.extras, extra_index(pextra));
1664  send_edit_tile = true;
1665  }
1666  }
1668  break;
1669  case EBT_SPECIAL:
1670  extra_type_by_cause_iterate(static_cast<extra_cause>(EC_SPECIAL),
1671  pextra)
1672  {
1673  if (tile_has_extra(vtile, pextra)) {
1674  BV_SET(tile_packet.extras, extra_index(pextra));
1675  send_edit_tile = true;
1676  }
1677  }
1679  break;
1680  case EBT_BASE:
1681  extra_type_iterate(pextra)
1682  {
1683  if (tile_has_extra(vtile, pextra)
1684  && is_extra_caused_by(pextra, EC_BASE)) {
1685  BV_SET(tile_packet.extras, extra_index(pextra));
1686  send_edit_tile = true;
1687  }
1688  }
1690  break;
1691  case EBT_ROAD:
1692  extra_type_iterate(pextra)
1693  {
1694  if (tile_has_extra(vtile, pextra)
1695  && is_extra_caused_by(pextra, EC_ROAD)) {
1696  BV_SET(tile_packet.extras, extra_index(pextra));
1697  send_edit_tile = true;
1698  }
1699  }
1701  break;
1702  case EBT_UNIT:
1703  unit_list_iterate(vtile->units, vunit)
1704  {
1705  value = utype_number(unit_type_get(vunit));
1706  owner = player_number(unit_owner(vunit));
1707  dsend_packet_edit_unit_create(my_conn, owner, tile, value, 1, 0);
1708  }
1710  break;
1711  case EBT_CITY:
1712  vcity = tile_city(vtile);
1713  if (!vcity) {
1714  continue;
1715  }
1716  owner = player_number(city_owner(vcity));
1717  value = city_size_get(vcity);
1718  dsend_packet_edit_city_create(my_conn, owner, tile, value, 0);
1719  break;
1720  default:
1721  break;
1722  }
1723  }
1725 
1726  if (send_edit_tile) {
1727  send_packet_edit_tile(my_conn, &tile_packet);
1728  }
1729 }
1730 
1734 void edit_buffer_paste(struct edit_buffer *ebuf, const struct tile *dest)
1735 {
1736  struct connection *my_conn = &client.conn;
1737  const struct tile *origin, *ptile;
1738  int dx, dy;
1739 
1740  if (!ebuf || !dest) {
1741  return;
1742  }
1743 
1744  // Calculate vector.
1745  origin = edit_buffer_get_origin(ebuf);
1746  fc_assert_ret(origin != nullptr);
1747  map_distance_vector(&dx, &dy, origin, dest);
1748 
1749  connection_do_buffer(my_conn);
1750  tile_list_iterate(ebuf->vtiles, vtile)
1751  {
1752  int virt_x, virt_y;
1753 
1754  index_to_map_pos(&virt_x, &virt_y, tile_index(vtile));
1755  ptile = map_pos_to_tile(&(wld.map), virt_x + dx, virt_y + dy);
1756  if (!ptile) {
1757  continue;
1758  }
1759  paste_tile(ebuf, vtile, ptile);
1760  }
1762  connection_do_unbuffer(my_conn);
1763 }
1764 
1769 {
1770  if (!editor) {
1771  return nullptr;
1772  }
1773  return editor->copybuf;
1774 }
1775 
1780  enum editor_tool_mode etm)
1781 {
1782  bool value_erase;
1783 
1784  value_erase = editor_tool_has_value_erase(ett);
1785 
1786  switch (etm) {
1787  case ETM_PAINT:
1788  return _("Paint");
1789  break;
1790  case ETM_ERASE:
1791  if (value_erase) {
1792  return _("Erase Value");
1793  } else {
1794  return _("Erase");
1795  }
1796  break;
1797  case ETM_COPY:
1798  return _("Copy");
1799  break;
1800  case ETM_PASTE:
1801  return _("Paste");
1802  break;
1803  default:
1804  qCritical("Unrecognized editor tool mode %d "
1805  "in editor_tool_get_mode_name().",
1806  etm);
1807  break;
1808  }
1809 
1810  return "";
1811 }
1812 
1818 {
1819  switch (etm) {
1820  case ETM_ERASE:
1821  return _("Toggle erase mode.\nShortcut: shift-d");
1822  break;
1823  case ETM_COPY:
1824  return _("Toggle copy mode.\nShortcut: shift-c");
1825  break;
1826  case ETM_PASTE:
1827  return _("Toggle paste mode.\nShortcut: shift-v");
1828  break;
1829  default:
1830  break;
1831  }
1832 
1833  return nullptr;
1834 }
1835 
1840 {
1841  const struct editor_sprites *sprites;
1842 
1843  sprites = get_editor_sprites(tileset);
1844  if (!sprites) {
1845  return nullptr;
1846  }
1847 
1848  switch (etm) {
1849  case ETM_PAINT:
1850  return sprites->brush;
1851  break;
1852  case ETM_ERASE:
1853  return sprites->erase;
1854  break;
1855  case ETM_COPY:
1856  return sprites->copy;
1857  break;
1858  case ETM_PASTE:
1859  return sprites->paste;
1860  break;
1861  default:
1862  break;
1863  }
1864 
1865  return nullptr;
1866 }
1867 
1872 int edit_buffer_get_status_string(const struct edit_buffer *ebuf, char *buf,
1873  int buflen)
1874 {
1875  int ret, total;
1876  const char *fmt;
1877 
1878  if (!buf || buflen < 1) {
1879  return 0;
1880  }
1881 
1882  ret = fc_strlcpy(buf, _("Buffer empty."), buflen);
1883  if (!ebuf || !ebuf->vtiles) {
1884  return ret;
1885  }
1886 
1887  total = tile_list_size(ebuf->vtiles);
1888  if (total > 0) {
1889  fmt = PL_("%d tile copied.", "%d tiles copied.", total);
1890  ret = fc_snprintf(buf, buflen, fmt, total);
1891  }
1892 
1893  return ret;
1894 }
1895 
1901  const struct tile *ptile)
1902 {
1903  if (!ebuf) {
1904  return;
1905  }
1906  ebuf->origin = ptile;
1907 }
1908 
1912 const struct tile *edit_buffer_get_origin(const struct edit_buffer *ebuf)
1913 {
1914  if (!ebuf) {
1915  return nullptr;
1916  }
1917  return ebuf->origin;
1918 }
1919 
1923 bool edit_buffer_has_type(const struct edit_buffer *ebuf, int type)
1924 {
1925  if (!ebuf) {
1926  return false;
1927  }
1928  return ebuf->type_flags & type;
1929 }
1930 
1937 {
1938  int count;
1939  const struct tile *origin, *center;
1940  int dx, dy, cx, cy;
1941  int xsum = 0, ysum = 0;
1942 
1943  if (!editor || !editor->selected_tile_table) {
1944  return nullptr;
1945  }
1946 
1947  count = editor->selected_tile_table->count();
1948  if (count < 1) {
1949  return nullptr;
1950  }
1951 
1952  origin = map_pos_to_tile(&(wld.map), 0, 0);
1953  fc_assert_ret_val(origin, nullptr);
1954  for (const auto *ptile : qAsConst(*editor->selected_tile_table)) {
1955  map_distance_vector(&dx, &dy, origin, ptile);
1956  xsum += dx;
1957  ysum += dy;
1958  }
1959 
1960  cx = xsum / count;
1961  cy = ysum / count;
1962  center = map_pos_to_tile(&(wld.map), cx, cy);
1963 
1964  return center;
1965 }
Base_type_id base_count()
Return the number of base_types.
Definition: base.cpp:152
#define BV_SET(bv, bit)
Definition: bitvector.h:44
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Return TRUE iff the city has this building in it.
Definition: city.cpp:1189
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
void city_size_set(struct city *pcity, citizens size)
Set the city size.
Definition: city.cpp:1126
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Adds an improvement (and its effects) to a city.
Definition: city.cpp:3272
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Create virtual skeleton for a city.
Definition: city.cpp:3346
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
Iterates over all map tiles that intersect with a rectangle in GUI coordinates.
bool next()
Iterates to the next item.
int client_player_number()
Returns number of player attached to client.
bool client_has_player()
Either controlling or observing.
bool client_is_global_observer()
Returns whether client is global observer.
struct player * client_player()
Either controlling or observing.
struct civclient client
bool can_conn_edit(const struct connection *pconn)
Return TRUE iff the connection is currently allowed to edit.
Definition: connection.cpp:472
void connection_do_buffer(struct connection *pc)
Turn on buffering, using a counter so that calls may be nested.
Definition: connection.cpp:278
void connection_do_unbuffer(struct connection *pc)
Turn off buffering if internal counter of number of times buffering was turned on falls to zero,...
Definition: connection.cpp:290
int edit_buffer_get_status_string(const struct edit_buffer *ebuf, char *buf, int buflen)
Fill the supplied buffer with a translated string describing the edit buffer's current state.
Definition: editor.cpp:1872
void editor_selection_remove(const struct tile *ptile)
Remove the given tile from the current selection.
Definition: editor.cpp:1098
const struct tile * editor_get_current_tile()
Get the tile that the user's mouse pointer is currently over.
Definition: editor.cpp:1021
bool edit_buffer_has_type(const struct edit_buffer *ebuf, int type)
Returns TRUE if the edit buffer was created with the given type flag.
Definition: editor.cpp:1923
const char * editor_tool_get_mode_name(enum editor_tool_type ett, enum editor_tool_mode etm)
Returns the translated string name for the given mode.
Definition: editor.cpp:1779
const char * editor_get_mode_tooltip(enum editor_tool_mode etm)
Returns a translated tooltip string assumed to be used for the toggle button for this tool mode in th...
Definition: editor.cpp:1817
int editor_tool_get_size(enum editor_tool_type ett)
Returns the current size parameter for the given editor tools.
Definition: editor.cpp:1210
static void paste_tile(struct edit_buffer *ebuf, const struct tile *vtile, const struct tile *ptile_dest)
Helper function for edit_buffer_paste().
Definition: editor.cpp:1632
void editor_set_tool(enum editor_tool_type ett)
Set the current tool to be used by the editor.
Definition: editor.cpp:260
void editor_mouse_button_press(int canvas_x, int canvas_y, int button, int modifiers)
Handle a user's mouse button press at the given point on the map canvas.
Definition: editor.cpp:659
void editor_set_current_tile(const struct tile *ptile)
Sets the tile currently assumed to be under the user's mouse pointer.
Definition: editor.cpp:1009
void editor_mouse_move(int canvas_x, int canvas_y, int modifiers)
Handle the mouse moving over the map canvas.
Definition: editor.cpp:860
enum editor_tool_mode editor_tool_get_mode(enum editor_tool_type ett)
Get the mode for the tool.
Definition: editor.cpp:324
bool editor_tool_has_value_erase(enum editor_tool_type ett)
Returns TRUE if erase mode for the given tool erases by sub-value instead of any object corresponding...
Definition: editor.cpp:1368
void editor_tool_set_count(enum editor_tool_type ett, int count)
Sets the 'count' parameter of the tool to the given value.
Definition: editor.cpp:1255
struct edit_buffer * editor_get_copy_buffer()
Returns the copy buffer for the given tool.
Definition: editor.cpp:1768
void editor_tool_toggle_mode(enum editor_tool_type ett, enum editor_tool_mode etm)
Toggle the current tool mode between the given mode and ETM_PAINT (or ETM_COPY for the copy & paste t...
Definition: editor.cpp:1034
static void tool_init(enum editor_tool_type ett, const char *name, int flags, const char *tooltip)
Initialize editor tool data.
Definition: editor.cpp:142
void editor_selection_add(const struct tile *ptile)
Add the given tile to the current selection.
Definition: editor.cpp:1087
int editor_tool_get_value(enum editor_tool_type ett)
Get the current tool sub-value.
Definition: editor.cpp:395
static void editor_end_selection_rectangle(int canvas_x, int canvas_y)
Record and handle the end of the selection rectangle.
Definition: editor.cpp:719
void editor_tool_set_applied_player(enum editor_tool_type ett, int player_no)
Sets the editor tool's applied player number to the given value.
Definition: editor.cpp:1344
void editor_tool_cycle_mode(enum editor_tool_type ett)
Set the editor tool mode to the next available mode.
Definition: editor.cpp:1050
static void editor_grab_applied_player(const struct tile *ptile)
Set the editor's current applied player number according to what exists on the given tile.
Definition: editor.cpp:453
void editor_free()
Free the client's editor.
Definition: editor.cpp:247
void editor_ruleset_changed()
Adjust editor for changed ruleset.
Definition: editor.cpp:171
int editor_tool_get_count(enum editor_tool_type ett)
Returns the 'count' parameter for the editor tool.
Definition: editor.cpp:1244
void edit_buffer_copy(struct edit_buffer *ebuf, const struct tile *ptile)
Append a single tile to the copy buffer.
Definition: editor.cpp:1484
void editor_tool_set_size(enum editor_tool_type ett, int size)
Sets the size parameter for the given tool.
Definition: editor.cpp:1221
const char * editor_tool_get_tooltip(enum editor_tool_type ett)
Returns a translated "tooltip" description for the given tool type.
Definition: editor.cpp:1319
bool editor_tool_is_usable(enum editor_tool_type ett)
Returns TRUE if the given tool should be made available to the user via the editor GUI.
Definition: editor.cpp:345
void editor_init()
Initialize the client's editor state information to some suitable default values.
Definition: editor.cpp:184
bool editor_tool_has_applied_player(enum editor_tool_type ett)
Returns TRUE if the given tool makes use of the editor's applied player number.
Definition: editor.cpp:1356
const struct tile * edit_buffer_get_origin(const struct edit_buffer *ebuf)
Return the previously set origin, or nullptr if none.
Definition: editor.cpp:1912
void editor_notify_edit_finished()
Notify the server that a batch of edits has completed.
Definition: editor.cpp:892
bool editor_tool_has_size(enum editor_tool_type ett)
Return TRUE if the given editor tool uses the 'size' parameter.
Definition: editor.cpp:1199
void edit_buffer_clear(struct edit_buffer *ebuf)
Remove all copy data stored in the edit buffer.
Definition: editor.cpp:1449
void editor_clear()
Clear the editor data which is game dependent.
Definition: editor.cpp:236
QPixmap * editor_get_mode_sprite(enum editor_tool_mode etm)
Returns the editor sprite corresponding to the tool mode.
Definition: editor.cpp:1839
selection_modes
Definition: editor.cpp:38
@ SELECTION_MODE_ADD
Definition: editor.cpp:40
@ SELECTION_MODE_REMOVE
Definition: editor.cpp:41
@ SELECTION_MODE_NEW
Definition: editor.cpp:39
void editor_tool_set_value(enum editor_tool_type ett, int value)
Set the value ID for the given tool.
Definition: editor.cpp:383
bool editor_tool_has_value(enum editor_tool_type ett)
Returns TRUE if the given tool type has sub-values (e.g.
Definition: editor.cpp:371
static void popup_properties(struct tile *ptile)
Handle a request to edit the properties for the given tile.
Definition: editor.cpp:629
void editor_mouse_button_release(int canvas_x, int canvas_y, int button, int modifiers)
Handle the release of a mouse button click.
Definition: editor.cpp:792
const char * editor_tool_get_name(enum editor_tool_type ett)
Get the translated name of the given tool type.
Definition: editor.cpp:1151
static void editor_grab_tool(const struct tile *ptile)
Set the editor's current tool according to what exists at the given tile.
Definition: editor.cpp:485
enum editor_tool_type editor_get_tool()
Get the current tool used by the editor.
Definition: editor.cpp:276
static bool tile_really_has_any_specials(const struct tile *ptile)
Temporary convenience function to work-around the fact that certain special values (S_RESOURCE_VALID)...
Definition: editor.cpp:432
void edit_buffer_copy_square(struct edit_buffer *ebuf, const struct tile *center, int radius)
Copy from a square region of half-width 'radius' centered around 'center' into the buffer.
Definition: editor.cpp:1466
static void editor_start_selection_rectangle(int canvas_x, int canvas_y)
Record the start of the selection rectangle.
Definition: editor.cpp:408
static void editor_draw_selrect()
Draws the editor selection rectangle using draw_selection_rectangle().
Definition: editor.cpp:776
void editor_apply_tool_to_selection()
Apply the current editor tool to all tiles in the current selection.
Definition: editor.cpp:1120
void editor_apply_tool(const struct tile *ptile, bool part_of_selection)
Apply the current editor tool to the given tile.
Definition: editor.cpp:904
bool editor_tile_is_selected(const struct tile *ptile)
Returns TRUE if the given tile is selected.
Definition: editor.cpp:1109
void edit_buffer_set_origin(struct edit_buffer *ebuf, const struct tile *ptile)
Set the "origin" for subsequent copy operations.
Definition: editor.cpp:1900
bool editor_is_active()
Returns TRUE if the client is in edit mode.
Definition: editor.cpp:335
const char * editor_tool_get_value_name(enum editor_tool_type emt, int value)
Get the translated name of the given tool value.
Definition: editor.cpp:1164
static bool can_edit_tile_properties(struct tile *ptile)
Returns TRUE if the given tile has some objects with editable properties.
Definition: editor.cpp:619
void edit_buffer_paste(struct edit_buffer *ebuf, const struct tile *dest)
Paste the entire contents of the edit buffer using 'dest' as the origin.
Definition: editor.cpp:1734
const struct tile * editor_get_selection_center()
Returns the "center" tile of a group of selected tiles, or nullptr.
Definition: editor.cpp:1936
editor_tool_flags
Definition: editor.cpp:44
@ ETF_NO_FLAGS
Definition: editor.cpp:45
@ ETF_HAS_APPLIED_PLAYER
Definition: editor.cpp:49
@ ETF_HAS_SIZE
Definition: editor.cpp:47
@ ETF_HAS_VALUE_ERASE
Definition: editor.cpp:50
@ ETF_HAS_COUNT
Definition: editor.cpp:48
@ ETF_HAS_VALUE
Definition: editor.cpp:46
bool editor_tool_has_mode(enum editor_tool_type ett, enum editor_tool_mode etm)
Return TRUE if the given tool supports the given mode.
Definition: editor.cpp:302
int editor_tool_get_applied_player(enum editor_tool_type ett)
Returns the current applied player number for the editor tool.
Definition: editor.cpp:1333
void editor_selection_clear()
Unselect all selected tiles.
Definition: editor.cpp:1076
struct edit_buffer * edit_buffer_new(int type_flags)
Create a new edit buffer corresponding to all types set in 'type_flags'.
Definition: editor.cpp:1413
struct unit * editor_unit_virtual_create()
Creates a virtual unit (like unit_virtual_create) based on the current editor state.
Definition: editor.cpp:1385
bool editor_tool_has_count(enum editor_tool_type ett)
Return TRUE if it is meaningful for the given tool to use the 'count' parameter.
Definition: editor.cpp:1233
static void editor_resize_selection_rectangle(int canvas_x, int canvas_y)
Handle a change in the size of the selection rectangle.
Definition: editor.cpp:820
static void fill_tile_edit_packet(struct packet_edit_tile *packet, const struct tile *ptile)
Helper function to fill in an edit packet with the tile's current values.
Definition: editor.cpp:1598
static void tool_set_init_value(enum editor_tool_type ett)
Set tool to some value legal under current ruleset.
Definition: editor.cpp:96
static struct editor_state * editor
Definition: editor.cpp:91
void edit_buffer_free(struct edit_buffer *ebuf)
Free all memory allocated for the edit buffer.
Definition: editor.cpp:1431
QPixmap * editor_tool_get_sprite(enum editor_tool_type ett)
Returns a sprite containing an icon for the given tool type.
Definition: editor.cpp:1267
void editor_tool_set_mode(enum editor_tool_type ett, enum editor_tool_mode etm)
Set the mode for the editor tool.
Definition: editor.cpp:288
editor_tool_mode
Definition: editor.h:44
@ ETM_PASTE
Definition: editor.h:48
@ NUM_EDITOR_TOOL_MODES
Definition: editor.h:50
@ ETM_ERASE
Definition: editor.h:46
@ ETM_PAINT
Definition: editor.h:45
@ ETM_COPY
Definition: editor.h:47
@ EBT_RESOURCE
Definition: editor.h:143
@ EBT_CITY
Definition: editor.h:148
@ EBT_UNIT
Definition: editor.h:147
@ EBT_BASE
Definition: editor.h:145
@ EBT_ALL
Definition: editor.h:151
@ EBT_ROAD
Definition: editor.h:146
@ EBT_TERRAIN
Definition: editor.h:142
@ EBT_SPECIAL
Definition: editor.h:144
#define edit_buffer_type_iterate_end
Definition: editor.h:180
#define edit_buffer_type_iterate(ARG_ebuf, NAME_type)
Definition: editor.h:169
@ MOUSE_BUTTON_LEFT
Definition: editor.h:115
@ MOUSE_BUTTON_RIGHT
Definition: editor.h:117
@ MOUSE_BUTTON_MIDDLE
Definition: editor.h:116
@ NUM_OBJTYPES
Definition: editor.h:25
@ EKM_CTRL
Definition: editor.h:109
@ EKM_SHIFT
Definition: editor.h:107
@ EKM_ALT
Definition: editor.h:108
@ EKM_NONE
Definition: editor.h:106
editor_tool_type
Definition: editor.h:28
@ ETT_ROAD
Definition: editor.h:32
@ ETT_TERRAIN_SPECIAL
Definition: editor.h:31
@ ETT_UNIT
Definition: editor.h:34
@ ETT_VISION
Definition: editor.h:36
@ ETT_STARTPOS
Definition: editor.h:38
@ NUM_EDITOR_TOOL_TYPES
Definition: editor.h:41
@ ETT_CITY
Definition: editor.h:35
@ ETT_COPYPASTE
Definition: editor.h:39
@ ETT_TERRAIN_RESOURCE
Definition: editor.h:30
@ ETT_MILITARY_BASE
Definition: editor.h:33
@ ETT_TERRAIN
Definition: editor.h:29
struct extra_type * extra_by_number(int id)
Return extras type of given id.
Definition: extras.cpp:154
int extra_number(const struct extra_type *pextra)
Return the extra id.
Definition: extras.cpp:132
const char * extra_name_translation(const struct extra_type *pextra)
Return the (translated) name of the extra type.
Definition: extras.cpp:165
int extra_count()
Return the number of extra_types.
Definition: extras.cpp:127
#define extra_type_iterate(_p)
Definition: extras.h:279
#define extra_type_iterate_end
Definition: extras.h:285
#define is_extra_caused_by(e, c)
Definition: extras.h:182
#define extra_index(_e_)
Definition: extras.h:163
#define extra_type_by_cause_iterate_end
Definition: extras.h:307
#define extra_type_by_cause_iterate(_cause, _extra)
Definition: extras.h:299
#define EC_SPECIAL
Definition: fc_types.h:935
#define MAX_EXTRA_TYPES
Definition: fc_types.h:42
#define MAX_LEN_NAME
Definition: fc_types.h:61
#define PL_(String1, String2, n)
Definition: fcintl.h:54
#define _(String)
Definition: fcintl.h:50
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
void editgui_popup_properties(const struct tile_list *tiles, int objtype)
Stub for editor function.
Definition: gui_main.cpp:255
void editgui_refresh()
Stub for editor function.
Definition: gui_main.cpp:250
bool is_improvement(const struct impr_type *pimprove)
Is this building a regular improvement?
#define improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
const char * name
Definition: inputfile.cpp:118
#define fc_assert_ret(condition)
Definition: log.h:112
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Return the tile for the given cartesian (map) position.
Definition: map.cpp:391
void map_distance_vector(int *dx, int *dy, const struct tile *tile0, const struct tile *tile1)
Topology function to find the vector which has the minimum "real" distance between the map positions ...
Definition: map.cpp:1012
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition: map.h:312
#define square_iterate_end
Definition: map.h:315
#define MAP_TILE_OWNER_NULL
Definition: map.h:500
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition: map.h:164
void recenter_button_pressed(int canvas_x, int canvas_y)
Recenter the map on the canvas location, on user request.
void flush_dirty(void)
Flush all regions that have been previously marked as dirty.
Definition: view_map.cpp:507
void draw_selection_rectangle(int canvas_x, int canvas_y, int w, int h)
Area Selection.
Definition: view_map.cpp:546
struct player * player_by_number(const int player_id)
Return struct player pointer for the given player index.
Definition: player.cpp:768
int player_number(const struct player *pplayer)
Return the player index/number/id.
Definition: player.cpp:756
Road_type_id road_count()
Return the number of road_types.
Definition: road.cpp:41
#define MAX(x, y)
Definition: shared.h:48
size_t size
Definition: specvec.h:64
Definition: city.h:291
int id
Definition: city.h:296
struct player * owner
Definition: city.h:294
struct tile * tile
Definition: city.h:293
struct packet_ruleset_control control
Definition: game.h:74
struct connection conn
Definition: client_main.h:89
const struct tile * origin
Definition: editor.cpp:56
struct tile_list * vtiles
Definition: editor.cpp:55
int type_flags
Definition: editor.cpp:54
QPixmap * vision
Definition: tilespec.h:196
QPixmap * terrain
Definition: tilespec.h:195
QPixmap * copy
Definition: tilespec.h:195
QPixmap * copypaste
Definition: tilespec.h:195
QPixmap * unit
Definition: tilespec.h:196
QPixmap * brush
Definition: tilespec.h:195
QPixmap * paste
Definition: tilespec.h:195
QPixmap * road
Definition: tilespec.h:197
QPixmap * terrain_resource
Definition: tilespec.h:196
QPixmap * startpos
Definition: tilespec.h:195
QPixmap * terrain_special
Definition: tilespec.h:196
QPixmap * city
Definition: tilespec.h:196
QPixmap * military_base
Definition: tilespec.h:197
QPixmap * erase
Definition: tilespec.h:195
bool selrect_active
Definition: editor.cpp:77
int selrect_x
Definition: editor.cpp:80
enum selection_modes selection_mode
Definition: editor.cpp:85
struct editor_tool tools[NUM_EDITOR_TOOL_TYPES]
Definition: editor.cpp:72
struct edit_buffer * copybuf
Definition: editor.cpp:88
bool tool_active
Definition: editor.cpp:75
QSet< const struct tile * > * selected_tile_table
Definition: editor.cpp:87
int selrect_start_y
Definition: editor.cpp:79
int selrect_width
Definition: editor.cpp:82
int selrect_height
Definition: editor.cpp:83
int selrect_start_x
Definition: editor.cpp:78
enum editor_tool_type tool
Definition: editor.cpp:71
int selrect_y
Definition: editor.cpp:81
const struct tile * current_tile
Definition: editor.cpp:74
int size
Definition: editor.cpp:62
int flags
Definition: editor.cpp:60
int count
Definition: editor.cpp:63
enum editor_tool_mode mode
Definition: editor.cpp:61
const char * tooltip
Definition: editor.cpp:67
const char * name
Definition: editor.cpp:65
int applied_player_no
Definition: editor.cpp:64
int value
Definition: editor.cpp:66
bv_extra_flags flags
Definition: extras.h:115
Definition: player.h:231
Definition: tile.h:42
char * label
Definition: tile.h:57
int index
Definition: tile.h:43
struct unit_list * units
Definition: tile.h:50
struct player * extras_owner
Definition: tile.h:55
struct veteran_system * veteran
Definition: unittype.h:509
Definition: unit.h:134
int hp
Definition: unit.h:148
int homecity
Definition: unit.h:142
float gui_x0
float gui_y0
struct civ_map map
Definition: world_object.h:21
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
size_t fc_strlcpy(char *dest, const char *src, size_t n)
fc_strlcpy() provides utf-8 version of (non-standard) function strlcpy() It is intended as more user-...
Definition: support.cpp:412
#define sz_strlcpy(dest, src)
Definition: support.h:140
Terrain_type_id terrain_count()
Return the number of terrains.
Definition: terrain.cpp:93
struct terrain * terrain_by_number(const Terrain_type_id type)
Return the terrain for the given terrain index.
Definition: terrain.cpp:128
const char * terrain_name_translation(const struct terrain *pterrain)
Return the (translated) name of the terrain.
Definition: terrain.cpp:175
Terrain_type_id terrain_number(const struct terrain *pterrain)
Return the terrain index.
Definition: terrain.cpp:119
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Adds extra to tile.
Definition: tile.cpp:974
void tile_set_terrain(struct tile *ptile, struct terrain *pterrain)
Set the given terrain at the specified tile.
Definition: tile.cpp:114
void tile_virtual_destroy(struct tile *vtile)
Frees all memory used by the virtual tile, including freeing virtual units in the tile's unit list an...
Definition: tile.cpp:1051
void tile_set_resource(struct tile *ptile, struct extra_type *presource)
Set the given resource at the specified tile.
Definition: tile.cpp:355
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Return a known_type enumeration value for the tile.
Definition: tile.cpp:398
void tile_set_worked(struct tile *ptile, struct city *pcity)
Set the city/worker on the tile (may be nullptr).
Definition: tile.cpp:96
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
struct tile * tile_virtual_new(const struct tile *ptile)
Returns a virtual tile.
Definition: tile.cpp:997
#define tile_index(_pt_)
Definition: tile.h:70
#define tile_resource(_tile)
Definition: tile.h:84
@ TILE_UNKNOWN
Definition: tile.h:29
#define tile_list_iterate(tile_list, ptile)
Definition: tile.h:65
#define tile_terrain(_tile)
Definition: tile.h:93
#define tile_list_iterate_end
Definition: tile.h:67
static const bv_extras * tile_extras(const struct tile *ptile)
Definition: tile.h:102
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130
#define tile_owner(_tile)
Definition: tile.h:78
int tileset_tile_height(const struct tileset *t)
Return the tile height of the current tileset.
Definition: tilespec.cpp:383
int tileset_tile_width(const struct tileset *t)
Return the tile width of the current tileset.
Definition: tilespec.cpp:371
const struct editor_sprites * get_editor_sprites(const struct tileset *t)
Return all the sprites used for editor icons, images, etc.
Definition: tilespec.cpp:3531
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
Definition: unit.cpp:1490
bool unit_transported(const struct unit *pcargo)
Returns TRUE iff the unit is transported.
Definition: unit.cpp:2176
#define unit_owner(_pu)
Definition: unit.h:370
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
struct unit_type * utype_by_number(const Unit_type_id id)
Return a pointer for the unit type struct for the given unit type id.
Definition: unittype.cpp:103
enum unit_move_type utype_move_type(const struct unit_type *punittype)
Return move type of the unit type.
Definition: unittype.cpp:1247
Unit_type_id utype_count()
Return the number of unit types.
Definition: unittype.cpp:74
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
Definition: unittype.cpp:2151
const char * utype_name_translation(const struct unit_type *punittype)
Return the (translated) name of the unit type.
Definition: unittype.cpp:1256
Unit_type_id utype_number(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:91
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition: unittype.h:704
struct tile * canvas_pos_to_tile(float canvas_x, float canvas_y)
Finds the tile corresponding to pixel coordinates.
struct view mapview
void refresh_tile_mapcanvas(const tile *ptile, bool full_refresh)
Refreshes a single tile on the map canvas.
void update_map_canvas(int canvas_x, int canvas_y, int width, int height)
Update (refresh) the map canvas starting at the given tile (in map coordinates) and with the given di...
void update_map_canvas_visible()
Schedules an update of (only) the visible part of the map at the next unqueue_mapview_update().