/
control.js
1145 lines (937 loc) · 32.8 KB
/
control.js
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
Freeciv - Copyright (C) 2009 - Andreas Røsdal andrearo@pvv.ntnu.no
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.
***********************************************************************/
var mouse_x;
var mouse_y;
var prev_mouse_x;
var prev_mouse_y;
var keyboard_input = true;
var unitpanel_active = false;
var allow_right_click = false;
var current_focus = [];
var goto_active = false;
/* Selecting unit from a stack without popup. */
var SELECT_POPUP = 0;
var SELECT_SEA = 1;
var SELECT_LAND = 2;
var SELECT_APPEND = 3;
var intro_click_description = true;
var goto_request_map = {};
var goto_turns_request_map = {};
var current_goto_path = [];
var current_goto_turns = 0;
/****************************************************************************
...
****************************************************************************/
function control_init()
{
// Register keyboard and mouse listener using JQuery.
$(document).keydown (keyboard_listener);
$("#canvas").mouseup(mapview_mouse_click);
$("#canvas").mousedown(mapview_mouse_down);
$("#city_canvas").click(city_mapview_mouse_click);
$("#turn_done_button").click(send_end_turn);
$("#freeciv_logo").click(function(event) {
window.open('http://www.freeciv.net/', '_new');
});
$("#game_text_input").keydown(function(event) {
return check_text_input(event, $("#game_text_input"));
});
$("#game_text_input").focus(function(event) {
keyboard_input=false;
});
$("#game_text_input").blur(function(event) {
keyboard_input=true;
});
/* disable text-selection, as this gives wrong mouse cursor
* during drag to goto units. */
document.onselectstart = function(){ return false; }
}
/****************************************************************************
...
****************************************************************************/
function mouse_moved_cb(e)
{
mouse_x = 0;
mouse_y = 0;
if (!e) {
var e = window.event;
}
if (e.pageX || e.pageY) {
mouse_x = e.pageX;
mouse_y = e.pageY;
} else {
if (e.clientX || e.clientY) {
mouse_x = e.clientX;
mouse_y = e.clientY;
}
}
if (mapview_canvas != null) {
mouse_x = mouse_x - mapview_canvas.offsetLeft;
mouse_y = mouse_y - mapview_canvas.offsetTop;
}
if (client.conn.playing == null) return;
/* Request preview goto path */
if (goto_active && current_focus.length > 0) {
var ptile = canvas_pos_to_tile(mouse_x, mouse_y);
if (ptile != null && goto_request_map[ptile['x'] + "," + ptile['y']] == null) {
preview_goto_path(current_focus[0]['id'], ptile['x'], ptile['y']);
}
}
if (C_S_RUNNING == client_state()) {
update_mouse_cursor();
}
}
/****************************************************************************
...
****************************************************************************/
function update_mouse_cursor()
{
var ptile = canvas_pos_to_tile(mouse_x, mouse_y);
if (ptile == null) return;
var punit = find_visible_unit(ptile);
var pcity = tile_city(ptile);
if (goto_active) {
mapview_canvas.style.cursor = "crosshair";
} else if (pcity != null && city_owner_player_id(pcity) == client.conn.playing.playerno) {
mapview_canvas.style.cursor = "pointer";
} else if (punit != null && punit['owner'] == client.conn.playing.playerno) {
mapview_canvas.style.cursor = "move";
} else {
mapview_canvas.style.cursor = "default";
}
}
/****************************************************************************
...
****************************************************************************/
function check_text_input(event,chatboxtextarea) {
if (event.keyCode == 13 && event.shiftKey == 0) {
var message = $(chatboxtextarea).val();
message = escape(message.replace(/^\s+|\s+$/g,""));
$(chatboxtextarea).val('');
$(chatboxtextarea).focus();
keyboard_input = true;
var test_packet = {"type" : packet_chat_msg_req, "message" : message};
var myJSONText = JSON.stringify(test_packet);
send_request (myJSONText);
return false;
}
}
/****************************************************************************
Return TRUE iff a unit on this tile is in focus.
****************************************************************************/
function get_focus_unit_on_tile(ptile)
{
var funits = get_units_in_focus();
if (funits == null) return null;
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
if (punit['x'] == ptile['x'] && punit['y'] == ptile['y']) {
return punit;
}
}
return null;
}
/****************************************************************************
Return TRUE iff this unit is in focus.
TODO: not implemented yet.
****************************************************************************/
function unit_is_in_focus(cunit)
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
if (punit['id'] == cunit['id']) {
return true;
}
}
return false;
}
/****************************************************************************
Returns a list of units in focus.
****************************************************************************/
function get_units_in_focus()
{
return current_focus;
}
/**************************************************************************
If there is no unit currently in focus, or if the current unit in
focus should not be in focus, then get a new focus unit.
We let GOTO-ing units stay in focus, so that if they have moves left
at the end of the goto, then they are still in focus.
**************************************************************************/
function update_unit_focus()
{
if (active_city != null) return; /* don't change focus while city dialog is active.*/
/* iterate zero times for no units in focus,
* otherwise quit for any of the conditions. */
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
if (unit_has_goto(punit)
&& punit['movesleft'] > 0
&& punit['done_moving'] == false
&& punit['ai'] == false) {
return;
}
}
advance_unit_focus();
}
/**************************************************************************
This function may be called from packhand.c, via update_unit_focus(),
as a result of packets indicating change in activity for a unit. Also
called when user press the "Wait" command.
FIXME: Add feature to focus only units of a certain category.
**************************************************************************/
function advance_unit_focus()
{
if (client_is_observer()) return;
var funits = get_units_in_focus();
var candidate = find_best_focus_candidate(false);
if (candidate == null) {
candidate = find_best_focus_candidate(true);
}
if (candidate != null) {
set_unit_focus_and_redraw(candidate);
} else {
/* Couldn't center on a unit, then try to center on a city... */
current_focus = []; /* Reset focus units. */
update_unit_info_label(current_focus);
$("#game_unit_orders_default").hide();
/* find a city to focus on. */
for (city_id in cities) {
var pcity = cities[city_id];
if (city_owner_player_id(pcity) == client.conn.playing.playerno) {
center_tile_mapcanvas(city_tile(pcity));
break;
}
}
}
}
/**************************************************************************
Enables and disables the correct units commands for the unit in focus.
**************************************************************************/
function update_unit_order_commands()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
var ptype = unit_type(punit);
if (ptype['name'] == "Settlers" || ptype['name'] == "Workers"
|| ptype['name'] == "Engineers") {
$("#order_road").show();
$("#order_mine").show();
$("#order_irrigate").show();
$("#order_fortify").hide();
$("#order_sentry").hide();
$("#order_explore").hide();
$("#order_auto_settlers").show();
if (player_invention_state(client.conn.playing, 65) == TECH_KNOWN) {
$("#order_railroad").show();
}
} else {
$("#order_road").hide();
$("#order_railroad").hide();
$("#order_mine").hide();
$("#order_irrigate").hide();
$("#order_fortify").show();
$("#order_auto_settlers").hide();
$("#order_sentry").show();
$("#order_explore").show();
}
if (ptype['name'] == "Settlers" || ptype['name'] == "Engineers") {
$("#order_build_city").show();
} else {
$("#order_build_city").hide();
}
}
$("#game_unit_orders_default").css("left", Math.floor((mapview_canvas.width
- $("#game_unit_orders_default").width()) / 2));
}
/**************************************************************************
...
**************************************************************************/
function init_game_unit_panel()
{
unitpanel_active = true;
$("#game_unit_panel").attr("title", "Units");
$("#game_unit_panel").dialog({
bgiframe: true,
modal: false,
width: "250px",
height: "auto",
resizable: false,
dialogClass: 'unit_dialog',
position: ["right", "bottom"],
close: function(event, ui) { unitpanel_active = false;}
});
$("#game_unit_panel").dialog('open');
$(".unit_dialog div.ui-dialog-titlebar").css("height", "5px");
$(".unit_dialog div.ui-dialog-content").css("padding", "5px 0");
$("#ui-dialog-title-game_unit_panel").css("margin-top", "-5px");
$("#ui-dialog-title-game_unit_panel").css("font-size", "10px");
$("#game_unit_panel").parent().css("overflow", "hidden");
}
/**************************************************************************
Find the nearest available unit for focus, excluding any current unit
in focus unless "accept_current" is TRUE. If the current focus unit
is the only possible unit, or if there is no possible unit, returns NULL.
**************************************************************************/
function find_best_focus_candidate(accept_current)
{
if (client_is_observer()) return null;
for (unit_id in units) {
var punit = units[unit_id];
if ((!unit_is_in_focus(punit) || accept_current)
&& punit['owner'] == client.conn.playing.playerno
&& punit['activity'] == ACTIVITY_IDLE
&& punit['movesleft'] > 0
&& punit['done_moving'] == false
&& punit['ai'] == false
&& punit['transported'] == false) {
return punit;
}
}
return null;
}
/**************************************************************************
Sets the focus unit directly. The unit given will be given the
focus; if NULL the focus will be cleared.
This function is called for several reasons. Sometimes a fast-focus
happens immediately as a result of a client action. Other times it
happens because of a server-sent packet that wakes up a unit.
**************************************************************************/
function set_unit_focus(punit)
{
current_focus = [];
current_focus[0] = punit;
}
/**************************************************************************
See set_unit_focus()
**************************************************************************/
function set_unit_focus_and_redraw(punit)
{
current_focus = [];
if (punit == null) {
current_focus = [];
} else {
current_focus[0] = punit;
}
auto_center_on_focus_unit();
update_unit_info_label(current_focus);
update_unit_order_commands();
$("#game_unit_orders_default").show();
}
/**************************************************************************
...
**************************************************************************/
function set_unit_focus_and_activate(punit)
{
set_unit_focus_and_redraw(punit);
request_new_unit_activity(punit, ACTIVITY_IDLE);
}
/**************************************************************************
See set_unit_focus_and_redraw()
**************************************************************************/
function city_dialog_activate_unit(punit)
{
request_new_unit_activity(punit, ACTIVITY_IDLE);
close_city_dialog();
set_unit_focus_and_redraw(punit);
}
/**************************************************************************
Center on the focus unit, if off-screen and auto_center_on_unit is true.
**************************************************************************/
function auto_center_on_focus_unit()
{
if (active_city != null) return; /* don't change focus while city dialog is active.*/
var ptile = find_a_focus_unit_tile_to_center_on();
if (ptile != null && auto_center_on_unit) {
center_tile_mapcanvas(ptile);
}
}
/****************************************************************************
Finds a single focus unit that we can center on. May return NULL.
****************************************************************************/
function find_a_focus_unit_tile_to_center_on()
{
var funit = current_focus[0];
if (funit == null) return null;
return map_pos_to_tile(funit['x'], funit['y']);
}
/**************************************************************************
Return a pointer to a visible unit, if there is one.
**************************************************************************/
function find_visible_unit(ptile)
{
var panyowned = null;
var panyother = null;
var ptptother = null;
var pfocus = null;
/* If no units here, return nothing. */
if (unit_list_size(tile_units(ptile))==0) {
return null;
}
/* If the unit in focus is at this tile, show that on top */
var pfocus = get_focus_unit_on_tile(ptile);
if (pfocus != null) {
return pfocus;
}
/* If a city is here, return nothing (unit hidden by city). */
if (tile_city(ptile) != null) {
return null;
}
/* TODO: add missing C logic here.*/
var vunits = tile_units(ptile);
for (var i = 0; i < vunits.length; i++) {
var aunit = vunits[i];
if (aunit['anim_list'] != null && aunit['anim_list'].length > 0) {
return aunit;
}
}
for (var i = 0; i < vunits.length; i++) {
var tunit = vunits[i];
if (tunit['transported'] == false) {
return tunit;
}
}
return tile_units(ptile)[0];
}
/**********************************************************************
TODO: not complete yet
***********************************************************************/
function get_drawable_unit(ptile, citymode)
{
var punit = find_visible_unit(ptile);
if (punit == null) return null;
/*if (citymode != null && unit_owner(punit) == city_owner(citymode))
return null;*/
if (!unit_is_in_focus(punit) || current_focus.length > 0 ) {
return punit;
} else {
return null;
}
}
/**************************************************************************
Handles everything when the user clicked a tile
**************************************************************************/
function do_map_click(ptile, qtype)
{
if (ptile == null) return;
if (goto_active) {
if (current_focus.length > 0) {
var punit = current_focus[0];
if (punit['x'] == ptile['x'] && punit['y'] == ptile['y']) {
/* if unit moved by click and drag to the same tile, then deactivate goto. */
deactivate_goto();
return;
}
var packet = {"type" : packet_unit_move, "unit_id" : punit['id'], "x": ptile['x'], "y": ptile['y'] };
send_request (JSON.stringify(packet));
}
deactivate_goto();
update_unit_focus();
} else {
var sunits = tile_units(ptile);
var pcity = tile_city(ptile);
if (pcity != null) {
if (pcity['owner'] == client.conn.playing.playerno) {
show_city_dialog(pcity);
}
return;
}
if (sunits != null && sunits.length == 0) {
/* Clicked on a tile with no units. */
set_unit_focus_and_redraw(null);
} else if (sunits != null && sunits.length > 0 ) {
if (sunits[0]['owner'] == client.conn.playing.playerno) {
if (sunits.length == 1) {
/* A single unit has been clicked with the mouse. */
var unit = sunits[0];
set_unit_focus_and_activate(unit);
} else {
/* more than one unit is on the selected tile. */
set_unit_focus_and_redraw(sunits[0]);
update_select_unit_dialog(sunits);
}
}
}
if (left_click_center()) {
center_tile_mapcanvas(ptile)
}
}
}
/**************************************************************************
Callback to handle keyboard events
**************************************************************************/
function keyboard_listener(ev)
{
// Check if focus is in chat field, where these keyboard events are ignored.
if (!keyboard_input) return;
if (!ev) ev = window.event;
var keyboard_key = String.fromCharCode(ev.keyCode);
civclient_handle_key(keyboard_key, ev.keyCode, ev['ctrlKey'], ev['altKey'], ev['shiftKey'] );
}
/**************************************************************************
Handles everything when the user typed on the keyboard.
**************************************************************************/
function
civclient_handle_key(keyboard_key, key_code, ctrl, alt, shift)
{
switch (keyboard_key) {
case 'B':
request_unit_build_city();
break;
case 'G':
if (current_focus.length > 0) {
activate_goto();
}
break;
case 'X':
key_unit_auto_explore();
break;
case 'A':
key_unit_auto_settle();
break;
case 'R':
key_unit_road();
break;
case 'F':
key_unit_fortify();
break;
case 'I':
key_unit_irrigate();
break;
case 'S':
key_unit_sentry();
break;
case 'P':
key_unit_pillage();
break;
case 'M':
key_unit_mine();
break;
case 'Q':
if (alt) civclient_benchmark(0);
break;
case 'D':
if (alt) show_debug_info();
break;
case 'L':
key_unit_railroad();
break;
};
switch (key_code) {
case 35: //1
case 97:
key_unit_move(DIR8_SOUTH);
break;
case 40: // 2
case 98:
key_unit_move(DIR8_SOUTHEAST);
break;
case 34: // 3
case 99:
key_unit_move(DIR8_EAST);
break;
case 37: // 4
case 100:
key_unit_move(DIR8_SOUTHWEST);
break;
case 39: // 6
case 102:
key_unit_move(DIR8_NORTHEAST);
break;
case 36: // 7
case 103:
key_unit_move(DIR8_WEST);
break;
case 38: // 8
case 104:
key_unit_move(DIR8_NORTHWEST);
break;
case 33: // 9
case 105:
key_unit_move(DIR8_NORTH);
break;
};
}
/**************************************************************************
...
**************************************************************************/
function activate_goto()
{
goto_active = true;
mapview_canvas.style.cursor = "crosshair";
if (current_focus.length > 0) {
var ptile = canvas_pos_to_tile(mouse_x, mouse_y);
if (ptile != null) {
preview_goto_path(current_focus[0]['id'], ptile['x'], ptile['y']);
}
if (intro_click_description) {
add_chatbox_text("Click on the tile to send this unit to.");
intro_click_description = false;
}
}
}
/**************************************************************************
...
**************************************************************************/
function deactivate_goto()
{
goto_active = false;
mapview_canvas.style.cursor = "default";
goto_request_map = {};
goto_turns_request_map = {};
current_goto_path = [];
}
/**************************************************************************
Ends the current turn.
**************************************************************************/
function send_end_turn()
{
var packet = {"type" : packet_player_phase_done, "turn" : game_info['turn']};
send_request (JSON.stringify(packet));
}
/**************************************************************************
Tell the units in focus to auto explore.
**************************************************************************/
function key_unit_auto_explore()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_EXPLORE);
}
update_unit_focus();
}
/**************************************************************************
Tell the units in focus to sentry.
**************************************************************************/
function key_unit_sentry()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_SENTRY);
}
update_unit_focus();
}
/**************************************************************************
Tell the units in focus to fortify.
**************************************************************************/
function key_unit_fortify()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_FORTIFYING);
}
update_unit_focus();
}
/**************************************************************************
Tell the units in focus to irrigate.
**************************************************************************/
function key_unit_irrigate()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_IRRIGATE);
}
update_unit_focus();
}
/**************************************************************************
Tell the units in focus to pillage.
**************************************************************************/
function key_unit_pillage()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_PILLAGE);
}
update_unit_focus();
}
/**************************************************************************
Tell the units in focus to mine.
**************************************************************************/
function key_unit_mine()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_MINE);
}
update_unit_focus();
}
/**************************************************************************
Tell the units in focus to build road.
**************************************************************************/
function key_unit_road()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_ROAD);
}
update_unit_focus();
}
/**************************************************************************
Tell the units in focus to build railroads.
**************************************************************************/
function key_unit_railroad()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_new_unit_activity(punit, ACTIVITY_RAILROAD);
}
update_unit_focus();
}
/**************************************************************************
Call to request (from the server) that the focus unit is put into
autosettler mode.
**************************************************************************/
function key_unit_auto_settle()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
request_unit_autosettlers(punit);
}
update_unit_focus();
}
/**************************************************************************
...
**************************************************************************/
function request_new_unit_activity(punit, activity)
{
var packet = {"type" : packet_unit_change_activity, "unit_id" : punit['id'],
"activity" : activity, "activity_target" : S_LAST, "activity_base" : 0};
send_request (JSON.stringify(packet));
}
/****************************************************************************
Call to request (from the server) that the settler unit is put into
autosettler mode.
****************************************************************************/
function request_unit_autosettlers(punit)
{
if (punit != null ) {
var packet = {"type" : packet_unit_autosettlers, "unit_id" : punit['id']};
send_request (JSON.stringify(packet));
}
}
/****************************************************************************
Request that a city is built.
****************************************************************************/
function request_unit_build_city()
{
if (current_focus.length > 0) {
var punit = current_focus[0];
if (punit != null) {
if (punit['movesleft'] == 0) {
add_chatbox_text("Unit has no moves left to build city");
return;
}
var ptype = unit_type(punit);
if (ptype['name'] == "Settlers" || ptype['name'] == "Engineers") {
var packet = {"type" : packet_city_name_suggestion_req, "unit_id" : punit['id'] };
send_request (JSON.stringify(packet));
}
}
}
}
/**************************************************************************
Tell the units in focus to disband.
**************************************************************************/
function key_unit_disband()
{
var funits = get_units_in_focus();
for (var i = 0; i < funits.length; i++) {
var punit = funits[i];
var packet = {"type" : packet_unit_disband, "unit_id" : punit['id'] };
send_request (JSON.stringify(packet));
/* Also remove unit immediately in client, to ensure it is removed. */
clear_tile_unit(punit);
client_remove_unit(punit);
}
}
/**************************************************************************
Moved the unit in focus in the specified direction.
**************************************************************************/
function key_unit_move(dir)
{
if (current_focus.length > 0) {
var punit = current_focus[0];
if (punit == null) return;
var ptile = map_pos_to_tile(punit['x'], punit['y']);
if (ptile == null) return;
var newtile = mapstep(ptile, dir);
if (newtile == null) return;
var packet = {"type" : packet_unit_move, "unit_id" : punit['id'], "x": newtile['x'], "y": newtile['y'] };
send_request (JSON.stringify(packet));
}
deactivate_goto();
// update focus to next unit after 500ms.
setTimeout("update_unit_focus();", 500);
}
/**************************************************************************
...
**************************************************************************/
function process_diplomat_arrival(pdiplomat, target_id)
{
var pcity = game_find_city_by_number(target_id);
var punit = game_find_unit_by_number(target_id);
if (punit != null) {
popup_diplomat_dialog(pdiplomat, punit, null);
} else if (pcity != null) {
popup_diplomat_dialog(pdiplomat, null, pcity);
}
}
/**************************************************************************
...
**************************************************************************/
function left_click_center()
{
return (is_iphone());
}
/****************************************************************************
Calculates a preview of the goto path, based on info in the client only.
FIXME: This doesn't support map wrapping.
****************************************************************************/
function preview_goto_path(unit_id, dst_x, dst_y)
{
var punit = units[unit_id];
current_goto_path = [];
generate_preview_path(punit['x'], punit['y'], dst_x, dst_y);
update_map_canvas_check();
}
/**************************************************************************
...
**************************************************************************/