File: mapctrl_common.c

package info (click to toggle)
freeciv 2.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 95,924 kB
  • ctags: 20,829
  • sloc: ansic: 231,256; sh: 4,872; makefile: 2,867; python: 1,259; yacc: 318
file content (739 lines) | stat: -rw-r--r-- 23,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/********************************************************************** 
 Freeciv - Copyright (C) 2002 - The Freeciv Poject
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <stdlib.h>		/* qsort */

#include "fcintl.h"
#include "log.h"
#include "support.h"

#include "combat.h"
#include "unitlist.h"

#include "agents.h"
#include "chatline_common.h"
#include "cityrep_g.h"
#include "civclient.h"
#include "climisc.h"
#include "clinet.h"
#include "cma_core.h"
#include "control.h"
#include "fcintl.h"
#include "goto.h"
#include "mapctrl_g.h"
#include "mapview_g.h"
#include "options.h"
#include "overview_common.h"
#include "tilespec.h"

#include "mapctrl_common.h"

/* Selection Rectangle */
static int rec_anchor_x, rec_anchor_y;  /* canvas coordinates for anchor */
static struct tile *rec_canvas_center_tile;
static int rec_corner_x, rec_corner_y;  /* corner to iterate from */
static int rec_w, rec_h;                /* width, heigth in pixels */

bool rbutton_down = FALSE;
bool rectangle_active = FALSE;
static bool rectangle_append;

/* This changes the behaviour of left mouse
   button in Area Selection mode. */
bool tiles_hilited_cities = FALSE;

/* The mapcanvas clipboard */
struct city_production clipboard = {.value = -1};

/* Goto with drag and drop. */
bool keyboardless_goto_button_down = FALSE;
bool keyboardless_goto_active = FALSE;
struct tile *keyboardless_goto_start_tile;

/* Update the workers for a city on the map, when the update is received */
struct city *city_workers_display = NULL;

static bool turn_done_state;
static bool is_turn_done_state_valid = FALSE;

/*************************************************************************/

static void clipboard_send_production_packet(struct city *pcity);
static void define_tiles_within_rectangle(void);

/**************************************************************************
 Called when Right Mouse Button is depressed. Record the canvas
 coordinates of the center of the tile, which may be unreal. This
 anchor is not the drawing start point, but is used to calculate
 width, height. Also record the current mapview centering.
**************************************************************************/
void anchor_selection_rectangle(int canvas_x, int canvas_y,
				bool append)
{
  struct tile *ptile = canvas_pos_to_nearest_tile(canvas_x, canvas_y);

  tile_to_canvas_pos(&rec_anchor_x, &rec_anchor_y, ptile);
  rec_anchor_x += tileset_tile_width(tileset) / 2;
  rec_anchor_y += tileset_tile_height(tileset) / 2;
  /* FIXME: This may be off-by-one. */
  rec_canvas_center_tile = get_center_tile_mapcanvas();
  rec_w = rec_h = 0;
  rectangle_append = append;
}

/**************************************************************************
 Iterate over the pixel boundaries of the rectangle and pick the tiles
 whose center falls within. Axis pixel incrementation is half tile size to
 accomodate tilesets with varying tile shapes and proportions of X/Y.

 These operations are performed on the tiles:
 -  Make tiles that contain owned cities hilited
    on the map and hilited in the City List Window.

 Later, I'll want to add unit hiliting for mass orders.       -ali
**************************************************************************/
static void define_tiles_within_rectangle(void)
{
  const int W = tileset_tile_width(tileset),   half_W = W / 2;
  const int H = tileset_tile_height(tileset),  half_H = H / 2;
  const int segments_x = abs(rec_w / half_W);
  const int segments_y = abs(rec_h / half_H);

  /* Iteration direction */
  const int inc_x = (rec_w > 0 ? half_W : -half_W);
  const int inc_y = (rec_h > 0 ? half_H : -half_H);
  int x, y, x2, y2, xx, yy;
  int units = 0;

  y = rec_corner_y;
  for (yy = 0; yy <= segments_y; yy++, y += inc_y) {
    x = rec_corner_x;
    for (xx = 0; xx <= segments_x; xx++, x += inc_x) {
      struct tile *ptile;

      /*  For diamond shaped tiles, every other row is indented.
       */
      if ((yy % 2 ^ xx % 2) != 0) {
	continue;
      }

      ptile = canvas_pos_to_tile(x, y);
      if (!ptile) {
	continue;
      }

      /*  "Half-tile" indentation must match, or we'll process
       *  some tiles twice in the case of rectangular shape tiles.
       */
      tile_to_canvas_pos(&x2, &y2, ptile);

      if ((yy % 2) != 0 && ((rec_corner_x % W) ^ abs(x2 % W)) != 0) {
	continue;
      }

      /*  Tile passed all tests; process it.
       */
      if (ptile->city && city_owner(ptile->city) == game.player_ptr) {
	/* FIXME: handle rectangle_append */
        map_deco[ptile->index].hilite = HILITE_CITY;
        tiles_hilited_cities = TRUE;
      }
      unit_list_iterate(ptile->units, punit) {
	if (unit_owner(punit) == game.player_ptr) {
	  if (units == 0 && !rectangle_append) {
	    set_unit_focus(punit);
	  } else {
	    add_unit_focus(punit);
	  }
	  units++;
	}
      } unit_list_iterate_end;
    }
  }

  /* Hilite in City List Window */
  if (tiles_hilited_cities) {
    hilite_cities_from_canvas();      /* cityrep.c */
  }
}

/**************************************************************************
 Called when mouse pointer moves and rectangle is active.
**************************************************************************/
void update_selection_rectangle(int canvas_x, int canvas_y)
{
  const int W = tileset_tile_width(tileset),    half_W = W / 2;
  const int H = tileset_tile_height(tileset),   half_H = H / 2;
  static struct tile *rec_tile = NULL;
  int diff_x, diff_y;
  struct tile *center_tile;
  struct tile *ptile;

  ptile = canvas_pos_to_nearest_tile(canvas_x, canvas_y);

  /*  Did mouse pointer move beyond the current tile's
   *  boundaries? Avoid macros; tile may be unreal!
   */
  if (ptile == rec_tile) {
    return;
  }
  rec_tile = ptile;

  /* Clear previous rectangle. */
  dirty_all();
  flush_dirty();

  /*  Fix canvas coords to the center of the tile.
   */
  tile_to_canvas_pos(&canvas_x, &canvas_y, ptile);
  canvas_x += half_W;
  canvas_y += half_H;

  rec_w = rec_anchor_x - canvas_x;  /* width */
  rec_h = rec_anchor_y - canvas_y;  /* height */

  /* FIXME: This may be off-by-one. */
  center_tile = get_center_tile_mapcanvas();
  map_distance_vector(&diff_x, &diff_y, center_tile, rec_canvas_center_tile);

  /*  Adjust width, height if mapview has recentered.
   */
  if (diff_x != 0 || diff_y != 0) {

    if (tileset_is_isometric(tileset)) {
      rec_w += (diff_x - diff_y) * half_W;
      rec_h += (diff_x + diff_y) * half_H;

      /* Iso wrapping */
      if (abs(rec_w) > map.xsize * half_W / 2) {
        int wx = map.xsize * half_W,  wy = map.xsize * half_H;
        rec_w > 0 ? (rec_w -= wx, rec_h -= wy) : (rec_w += wx, rec_h += wy);
      }

    } else {
      rec_w += diff_x * W;
      rec_h += diff_y * H;

      /* X wrapping */
      if (abs(rec_w) > map.xsize * half_W) {
        int wx = map.xsize * W;
        rec_w > 0 ? (rec_w -= wx) : (rec_w += wx);
      }
    }
  }

  if (rec_w == 0 && rec_h == 0) {
    rectangle_active = FALSE;
    return;
  }

  /* It is currently drawn only to the screen, not backing store */
  rectangle_active = TRUE;
  draw_selection_rectangle(canvas_x, canvas_y, rec_w, rec_h);
  rec_corner_x = canvas_x;
  rec_corner_y = canvas_y;
}

/**************************************************************************
  Redraws the selection rectangle after a map flush.
**************************************************************************/
void redraw_selection_rectangle(void)
{
  if (rectangle_active) {
    draw_selection_rectangle(rec_corner_x, rec_corner_y, rec_w, rec_h);
  }
}

/**************************************************************************
  Redraws the selection rectangle after a map flush.
**************************************************************************/
void cancel_selection_rectangle(void)
{
  if (rectangle_active) {
    rectangle_active = FALSE;
    rbutton_down = FALSE;
    dirty_rect(rec_corner_x, rec_corner_y, rec_w, rec_h);
  }
}

/**************************************************************************
...
**************************************************************************/
bool is_city_hilited(struct city *pcity)
{
  return map_deco[pcity->tile->index].hilite == HILITE_CITY;
}

/**************************************************************************
 Remove hiliting from all tiles, but not from rows in the City List window.
**************************************************************************/
void cancel_tile_hiliting(void)
{
  if (tiles_hilited_cities)  {
    tiles_hilited_cities = FALSE;

    whole_map_iterate(ptile) {
      map_deco[ptile->index].hilite = HILITE_NONE;
    } whole_map_iterate_end;

    update_map_canvas_visible();
  }
}

/**************************************************************************
 Action depends on whether the mouse pointer moved
 a tile between press and release.
**************************************************************************/
void release_right_button(int canvas_x, int canvas_y)
{
  if (rectangle_active) {
    define_tiles_within_rectangle();
    update_map_canvas_visible();
  } else {
    recenter_button_pressed(canvas_x, canvas_y);
  }
  rectangle_active = FALSE;
  rbutton_down = FALSE;
}

/**************************************************************************
 Left Mouse Button in Area Selection mode.
**************************************************************************/
void toggle_tile_hilite(struct tile *ptile)
{
  struct city *pcity = ptile->city;

  if (map_deco[ptile->index].hilite == HILITE_CITY) {
    map_deco[ptile->index].hilite = HILITE_NONE;
    if (pcity) {
      toggle_city_hilite(pcity, FALSE); /* cityrep.c */
    }
  }
  else if (pcity && city_owner(pcity) == game.player_ptr) {
    map_deco[ptile->index].hilite = HILITE_CITY;
    tiles_hilited_cities = TRUE;
    toggle_city_hilite(pcity, TRUE);
  }
  else  {
    return;
  }

  refresh_tile_mapcanvas(ptile, FALSE, TRUE);
}

/**************************************************************************
  The user pressed the overlay-city button (t) while the mouse was at the
  given canvas position.
**************************************************************************/
void key_city_overlay(int canvas_x, int canvas_y)
{
  struct tile *ptile = canvas_pos_to_tile(canvas_x, canvas_y);

  if (can_client_change_view() && ptile) {
    struct unit *punit;
    struct city *pcity = find_city_or_settler_near_tile(ptile, &punit);

    if (pcity) {
      toggle_city_color(pcity);
    } else if (punit) {
      toggle_unit_color(punit);
    }
  }
}

/**************************************************************************
 Shift-Left-Click on owned city or any visible unit to copy.
**************************************************************************/
void clipboard_copy_production(struct tile *ptile)
{
  struct city *pcity = ptile->city;

  if (!can_client_issue_orders()) {
    return;
  }

  if (pcity) {
    if (city_owner(pcity) != game.player_ptr)  {
      return;
    }
    clipboard = pcity->production;
  } else {
    struct unit *punit = find_visible_unit(ptile);
    if (!punit) {
      return;
    }
    if (!can_player_build_unit_direct(game.player_ptr, unit_type(punit)))  {
      create_event(ptile, E_BAD_COMMAND,
		   _("You don't know how to build %s!"),
		   unit_name_translation(punit));
      return;
    }
    clipboard.is_unit = TRUE;
    clipboard.value = unit_type(punit)->index;
  }
  upgrade_canvas_clipboard();

  create_event(ptile, E_CITY_PRODUCTION_CHANGED, /* ? */
	       _("Copy %s to clipboard."),
	       clipboard.is_unit
	       ? utype_name_translation(utype_by_number(clipboard.value))
	       : improvement_name_translation(clipboard.value));
}

/**************************************************************************
 If City tiles are hilited, paste into all those cities.
 Otherwise paste into the one city under the mouse pointer.
**************************************************************************/
void clipboard_paste_production(struct city *pcity)
{
  if (!can_client_issue_orders()) {
    return;
  }
  if (clipboard.value == -1) {
    create_event(pcity->tile, E_BAD_COMMAND, _("Clipboard is empty."));
    return;
  }
  if (!tiles_hilited_cities) {
    if (pcity && city_owner(pcity) == game.player_ptr) {
      clipboard_send_production_packet(pcity);
    }
    return;
  }
  else {
    connection_do_buffer(&aconnection);
    city_list_iterate(game.player_ptr->cities, pcity) {
      if (is_city_hilited(pcity)) {
        clipboard_send_production_packet(pcity);
      }
    } city_list_iterate_end;
    connection_do_unbuffer(&aconnection);
  }
}

/**************************************************************************
...
**************************************************************************/
static void clipboard_send_production_packet(struct city *pcity)
{
  if ((clipboard.is_unit == pcity->production.is_unit
       && clipboard.value == pcity->production.value)
      || !city_can_build_impr_or_unit(pcity, clipboard)) {
    return;
  }

  dsend_packet_city_change(&aconnection, pcity->id, clipboard.value,
			   clipboard.is_unit);
}

/**************************************************************************
 A newer technology may be available for units.
 Also called from packhand.c.
**************************************************************************/
void upgrade_canvas_clipboard(void)
{
  if (!can_client_issue_orders()) {
    return;
  }
  if (clipboard.is_unit)  {
    struct unit_type *u
      = can_upgrade_unittype(game.player_ptr, utype_by_number(clipboard.value));

    if (u)  {
      clipboard.value = u->index;
    }
  }
}

/**************************************************************************
...
**************************************************************************/
void release_goto_button(int canvas_x, int canvas_y)
{
  struct tile *ptile = canvas_pos_to_tile(canvas_x, canvas_y);

  if (keyboardless_goto_active && hover_state == HOVER_GOTO && ptile) {
    do_unit_goto(ptile);
    set_hover_state(NULL, HOVER_NONE, ACTIVITY_LAST, ORDER_LAST);
    update_unit_info_label(get_units_in_focus());
  }
  keyboardless_goto_active = FALSE;
  keyboardless_goto_button_down = FALSE;
  keyboardless_goto_start_tile = NULL;
}

/**************************************************************************
 The goto hover state is only activated when the mouse pointer moves
 beyond the tile where the button was depressed, to avoid mouse typos.
**************************************************************************/
void maybe_activate_keyboardless_goto(int canvas_x, int canvas_y)
{
  struct tile *ptile = canvas_pos_to_tile(canvas_x, canvas_y);

  if (ptile && get_num_units_in_focus() > 0
      && !same_pos(keyboardless_goto_start_tile, ptile)
      && can_client_issue_orders()) {
    keyboardless_goto_active = TRUE;
    request_unit_goto(ORDER_LAST);
  }
}

/**************************************************************************
 Return TRUE iff the turn done button is enabled.
**************************************************************************/
bool get_turn_done_button_state()
{
  if (!is_turn_done_state_valid) {
    update_turn_done_button_state();
  }
  assert(is_turn_done_state_valid);

  return turn_done_state;
}

/**************************************************************************
  Scroll the mapview half a screen in the given direction.  This is a GUI
  direction; i.e., DIR8_NORTH is "up" on the mapview.
**************************************************************************/
void scroll_mapview(enum direction8 gui_dir)
{
  int gui_x = mapview.gui_x0, gui_y = mapview.gui_y0;

  if (!can_client_change_view()) {
    return;
  }

  gui_x += DIR_DX[gui_dir] * mapview.width / 2;
  gui_y += DIR_DY[gui_dir] * mapview.height / 2;
  set_mapview_origin(gui_x, gui_y);
}

/**************************************************************************
  Do some appropriate action when the "main" mouse button (usually
  left-click) is pressed.  For more sophisticated user control use (or
  write) a different xxx_button_pressed function.
**************************************************************************/
void action_button_pressed(int canvas_x, int canvas_y,
			   enum quickselect_type qtype)
{
  struct tile *ptile = canvas_pos_to_tile(canvas_x, canvas_y);

  if (can_client_change_view() && ptile) {
    /* FIXME: Some actions here will need to check can_client_issue_orders.
     * But all we can check is the lowest common requirement. */
    do_map_click(ptile, qtype);
  }
}

/**************************************************************************
  Wakeup sentried units on the tile of the specified location.
**************************************************************************/
void wakeup_button_pressed(int canvas_x, int canvas_y)
{
  struct tile *ptile = canvas_pos_to_tile(canvas_x, canvas_y);

  if (can_client_issue_orders() && ptile) {
    wakeup_sentried_units(ptile);
  }
}

/**************************************************************************
  Adjust the position of city workers from the mapview.
**************************************************************************/
void adjust_workers_button_pressed(int canvas_x, int canvas_y)
{
  int city_x, city_y;
  enum city_tile_type worker;
  struct tile *ptile = canvas_pos_to_tile(canvas_x, canvas_y);

  if (can_client_issue_orders() && ptile) {
    struct city *pcity = find_city_near_tile(ptile);

    if (pcity && !cma_is_city_under_agent(pcity, NULL)) {
      if (!map_to_city_map(&city_x, &city_y, pcity, ptile)) {
	assert(0);
      }

      worker = get_worker_city(pcity, city_x, city_y);
      if (worker == C_TILE_WORKER) {
	dsend_packet_city_make_specialist(&aconnection, pcity->id,
					  city_x, city_y);
      } else if (worker == C_TILE_EMPTY) {
	dsend_packet_city_make_worker(&aconnection, pcity->id,
				      city_x, city_y);
      } else {
	/* If worker == C_TILE_UNAVAILABLE then we can't use this tile.  No
	 * packet is sent and city_workers_display is not updated. */
	return;
      }

      /* When the city info packet is received, update the workers on the
       * map.  This is a bad hack used to selectively update the mapview
       * when we receive the corresponding city packet. */
      city_workers_display = pcity;
    }
  }
}

/**************************************************************************
  Recenter the map on the canvas location, on user request.  Usually this
  is done with a right-click.
**************************************************************************/
void recenter_button_pressed(int canvas_x, int canvas_y)
{
  /* We use the "nearest" tile here so off-map clicks will still work. */
  struct tile *ptile = canvas_pos_to_nearest_tile(canvas_x, canvas_y);

  if (can_client_change_view() && ptile) {
    center_tile_mapcanvas(ptile);
  }
}

/**************************************************************************
 Update the turn done button state.
**************************************************************************/
void update_turn_done_button_state()
{
  bool new_state;

  if (!is_turn_done_state_valid) {
    turn_done_state = FALSE;
    is_turn_done_state_valid = TRUE;
    set_turn_done_button_state(turn_done_state);
    freelog(LOG_DEBUG, "setting turn done button state init %d",
	    turn_done_state);
  }

  new_state = (can_client_issue_orders()
	       && !game.player_ptr->phase_done && !agents_busy()
	       && !turn_done_sent);
  if (new_state == turn_done_state) {
    return;
  }

  freelog(LOG_DEBUG, "setting turn done button state from %d to %d",
	  turn_done_state, new_state);
  turn_done_state = new_state;

  set_turn_done_button_state(turn_done_state);

  if (turn_done_state) {
    if (waiting_for_end_turn
	|| (game.player_ptr
	    && game.player_ptr->ai.control
	    && !ai_manual_turn_done)) {
      send_turn_done();
    } else {
      update_turn_done_button(TRUE);
    }
  }
}

/**************************************************************************
  Update the goto/patrol line to the given map canvas location.
**************************************************************************/
void update_line(int canvas_x, int canvas_y)
{
  struct tile *ptile;

  switch (hover_state) {
  case HOVER_GOTO:
  case HOVER_PATROL:
  case HOVER_CONNECT:
    ptile = canvas_pos_to_tile(canvas_x, canvas_y);

    is_valid_goto_draw_line(ptile);
  default:
    break;
  };
}

/****************************************************************************
  Update the goto/patrol line to the given overview canvas location.
****************************************************************************/
void overview_update_line(int overview_x, int overview_y)
{
  struct tile *ptile;
  int x, y;

  switch (hover_state) {
  case HOVER_GOTO:
  case HOVER_PATROL:
  case HOVER_CONNECT:
    overview_to_map_pos(&x, &y, overview_x, overview_y);
    ptile = map_pos_to_tile(x, y);

    is_valid_goto_draw_line(ptile);
  default:
    break;
  };
}

/****************************************************************************
  We sort according to the following logic:

  - Transported units should immediately follow their transporter (note that
    transporting may be recursive).
  - Otherwise we sort by ID (which is what the list is originally sorted by).
****************************************************************************/
static int unit_list_compare(const void *a, const void *b)
{
  const struct unit *punit1 = *(struct unit **)a;
  const struct unit *punit2 = *(struct unit **)b;

  if (punit1->transported_by == punit2->transported_by) {
    /* For units with the same transporter or no transporter: sort by id. */
    /* Perhaps we should sort by name instead? */
    return punit1->id - punit2->id;
  } else if (punit1->transported_by == punit2->id) {
    return 1;
  } else if (punit2->transported_by == punit1->id) {
    return -1;
  } else {
    /* If the transporters aren't the same, put in order by the
     * transporters. */
    const struct unit *ptrans1 = game_find_unit_by_number(punit1->transported_by);
    const struct unit *ptrans2 = game_find_unit_by_number(punit2->transported_by);

    if (!ptrans1) {
      ptrans1 = punit1;
    }
    if (!ptrans2) {
      ptrans2 = punit2;
    }

    return unit_list_compare(&ptrans1, &ptrans2);
  }
}

/****************************************************************************
  Fill and sort the list of units on the tile.
****************************************************************************/
void fill_tile_unit_list(const struct tile *ptile, struct unit **unit_list)
{
  int i = 0;

  /* First populate the unit list. */
  unit_list_iterate(ptile->units, punit) {
    unit_list[i] = punit;
    i++;
  } unit_list_iterate_end;

  /* Then sort it. */
  qsort(unit_list, i, sizeof(*unit_list), unit_list_compare);
}