forked from freeciv/freeciv-web
/
tilespec.js
1107 lines (900 loc) · 34.7 KB
/
tilespec.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 num_cardinal_tileset_dirs = 4;
var cardinal_tileset_dirs = [DIR8_NORTH, DIR8_EAST, DIR8_SOUTH, DIR8_WEST];
var NUM_CORNER_DIRS = 4;
var DIR4_TO_DIR8 = [ DIR8_NORTH, DIR8_SOUTH, DIR8_EAST, DIR8_WEST];
var current_select_sprite = 0;
var max_select_sprite = 4;
/* Items on the mapview are drawn in layers. Each entry below represents
* one layer. The names are basically arbitrary and just correspond to
* groups of elements in fill_sprite_array(). Callers of fill_sprite_array
* must call it once for each layer. */
var LAYER_TERRAIN1 = 0;
var LAYER_TERRAIN2 = 1;
var LAYER_TERRAIN3 = 2;
var LAYER_ROADS = 3;
var LAYER_SPECIAL1 = 4;
var LAYER_CITY1 = 5;
var LAYER_UNIT = 6;
var LAYER_FOG = 7;
var LAYER_FOCUS_UNIT = 8;
var LAYER_CITYBAR = 9;
var LAYER_COUNT = 10;
// these layers are not used at the moment, for performance reasons.
//var LAYER_BACKGROUND = ; (not in use)
//var LAYER_GOTO = ; (not in use)
//var LAYER_EDITOR = ; (not in use)
//var LAYER_GRID* = ; (not in use)
/* An edge is the border between two tiles. This structure represents one
* edge. The tiles are given in the same order as the enumeration name. */
var EDGE_NS = 0; /* North and south */
var EDGE_WE = 1; /* West and east */
var EDGE_UD = 2; /* Up and down (nw/se), for hex_width tilesets */
var EDGE_LR = 3; /* Left and right (ne/sw), for hex_height tilesets */
var EDGE_COUNT = 4;
var MATCH_NONE = 0;
var MATCH_SAME = 1; /* "boolean" match */
var MATCH_PAIR = 2;
var MATCH_FULL = 3;
var CELL_WHOLE = 0; /* entire tile */
var CELL_CORNER = 1; /* corner of tile */
/* Darkness style. Don't reorder this enum since tilesets depend on it. */
/* No darkness sprites are drawn. */
var DARKNESS_NONE = 0;
/* 1 sprite that is split into 4 parts and treated as a darkness4. Only
* works in iso-view. */
var DARKNESS_ISORECT = 1;
/* 4 sprites, one per direction. More than one sprite per tile may be
* drawn. */
var DARKNESS_CARD_SINGLE = 2;
/* 15=2^4-1 sprites. A single sprite is drawn, chosen based on whether
* there's darkness in _each_ of the cardinal directions. */
var DARKNESS_CARD_FULL = 3;
/* Corner darkness & fog. 3^4 = 81 sprites. */
var DARKNESS_CORNER = 4;
var dither_offset_x = [normal_tile_width/2, 0, normal_tile_width/2, 0];
var dither_offset_y = [0, normal_tile_height/2, normal_tile_height/2, 0];
var terrain_match = {"t.l0.hills1" : MATCH_NONE,
"t.l0.mountains1" : MATCH_NONE,
"t.l0.plains1" : MATCH_NONE,
"t.l0.desert1" : MATCH_NONE
};
/****************************************************************************
Fill in the sprite array for the given tile, city, and unit.
ptile, if specified, gives the tile. If specified the terrain and specials
will be drawn for this tile. In this case (map_x,map_y) should give the
location of the tile.
punit, if specified, gives the unit. For tile drawing this should
generally be get_drawable_unit(); otherwise it can be any unit.
pcity, if specified, gives the city. For tile drawing this should
generally be tile_city(ptile); otherwise it can be any city.
citymode specifies whether this is part of a citydlg. If so some drawing
is done differently.
****************************************************************************/
function fill_sprite_array(layer, ptile, pedge, pcorner, punit, pcity, citymode)
{
var sprite_array = [];
var do_draw_unit = (punit != null && (draw_units || ptile == null || (draw_focus_unit
&& unit_is_in_focus(punit))));
switch (layer) {
case LAYER_TERRAIN1:
if (ptile != null) {
var tterrain_near = tile_terrain_near(ptile);
var pterrain = tile_terrain(ptile);
sprite_array = sprite_array.concat(fill_terrain_sprite_layer(0, ptile, pterrain, tterrain_near));
}
break;
case LAYER_TERRAIN2:
if (ptile != null) {
var tterrain_near = tile_terrain_near(ptile);
var pterrain = tile_terrain(ptile);
sprite_array = sprite_array.concat(fill_terrain_sprite_layer(1, ptile, pterrain, tterrain_near));
}
break;
case LAYER_TERRAIN3:
if (ptile != null) {
var tterrain_near = tile_terrain_near(ptile);
var pterrain = tile_terrain(ptile);
sprite_array = sprite_array.concat(fill_terrain_sprite_layer(2, ptile, pterrain, tterrain_near));
sprite_array = sprite_array.concat(fill_irrigation_sprite_array(ptile, pcity));
}
break;
case LAYER_ROADS:
if (ptile != null) {
sprite_array = sprite_array.concat(fill_road_rail_sprite_array(ptile, pcity));
}
break;
case LAYER_SPECIAL1:
if (ptile != null) {
var spec_sprite = get_tile_specials_sprite(ptile);
if (spec_sprite != null) sprite_array.push(spec_sprite);
if (contains_special(ptile, S_MINE)) {
sprite_array.push({"key" : "tx.mine"});
}
if (contains_special(ptile, S_HUT)) {
sprite_array.push({"key" : "tx.village"});
}
if (contains_special(ptile, S_POLLUTION)) {
sprite_array.push({"key" : "tx.pollution"});
}
}
break;
case LAYER_CITY1:
if (pcity != null) {
sprite_array.push(get_city_flag_sprite(pcity));
sprite_array.push(get_city_sprite(pcity));
sprite_array = sprite_array.concat(get_city_size_sprites(pcity));
}
if (active_city != null && ptile != null && ptile['worked'] != null && active_city['id'] == ptile['worked']) {
sprite_array.push(get_city_active_worked_sprite());
var dx = city_tile(active_city)['x'] - ptile['x'];
var dy = city_tile(active_city)['y'] - ptile['y'];
var idx = get_city_dxy_to_index(dx, dy);
var food_output = active_city['food_output'].substring(idx, idx + 1);
var shield_output = active_city['shield_output'].substring(idx, idx + 1);
var trade_output = active_city['trade_output'].substring(idx, idx + 1);
sprite_array.push(get_city_food_output_sprite(food_output));
sprite_array.push(get_city_shields_output_sprite(shield_output));
sprite_array.push(get_city_trade_output_sprite(trade_output));
} else if (active_city != null && ptile != null && ptile['worked'] != 0) {
sprite_array.push(get_city_invalid_worked_sprite());
}
break;
case LAYER_UNIT:
case LAYER_FOCUS_UNIT:
if (do_draw_unit && XOR(layer == LAYER_UNIT, unit_is_in_focus(punit)) && active_city == null) {
var stacked = true; /*ptile != null && (unit_list_size(ptile->units) > 1);*/
var backdrop = false; /* !pcity;*/
var stacked = ptile;
if (unit_is_in_focus(punit)) {
sprite_array.push(get_select_sprite());
}
/* TODO: Special case for drawing the selection rectangle. The blinking
* unit is handled separately, inside get_drawable_unit(). */
sprite_array = sprite_array.concat(fill_unit_sprite_array(punit, stacked, backdrop));
}
break;
case LAYER_FOG:
sprite_array = sprite_array.concat(fill_fog_sprite_array(ptile, pedge, pcorner));
break;
case LAYER_CITYBAR:
if (pcity != null) {
sprite_array.push(get_city_info_text(pcity));
}
break;
}
return sprite_array;
}
/****************************************************************************
Add sprites for the base tile to the sprite list. This doesn't
include specials or rivers.
****************************************************************************/
function fill_terrain_sprite_layer(layer_num, ptile, pterrain, tterrain_near)
{
var result = [];
result = result.concat(fill_terrain_sprite_array(layer_num, ptile, pterrain, tterrain_near));
/* FIXME: handle blending and darkness. */
return result;
}
/****************************************************************************
Helper function for fill_terrain_sprite_layer.
****************************************************************************/
function fill_terrain_sprite_array(l, ptile, pterrain, tterrain_near)
{
if (tile_types_setup["l" + l + "." + pterrain['graphic_str']] == null) {
//console.log("missing " + "l" + l + "." + pterrain['graphic_str']);
return [];
}
var dlp = tile_types_setup["l" + l + "." + pterrain['graphic_str']];
switch (dlp['sprite_type']) {
case CELL_WHOLE:
{
switch (dlp['match_style']) {
case MATCH_NONE:
{
var result_sprites = [];
if (dlp['dither'] == true) {
for (var i = 0; i < num_cardinal_tileset_dirs; i++) {
var dir = cardinal_tileset_dirs[i];
if (ts_tiles[tterrain_near[DIR4_TO_DIR8[i]]['graphic_str']] == null) continue;
var near_dlp = tile_types_setup["l" + l + "." + tterrain_near[DIR4_TO_DIR8[i]]['graphic_str']];
var terrain_near = (near_dlp['dither'] == true) ? tterrain_near[DIR4_TO_DIR8[i]]['graphic_str'] : pterrain['graphic_str'];
var dither_tile = i + pterrain['graphic_str'] + "_" + terrain_near;
var x = dither_offset_x[i];
var y = dither_offset_y[i];
result_sprites.push({"key": dither_tile, "offset_x" : x, "offset_y" : y});
}
return result_sprites;
} else {
return [ {"key" : "t.l" + l + "." + pterrain['graphic_str'] + 1} ];
}
break;
}
case MATCH_SAME:
{
var tileno = 0;
var this_match_type = ts_tiles[pterrain['graphic_str']]['layer' + l + '_match_type'];
for (var i = 0; i < num_cardinal_tileset_dirs; i++) {
var dir = cardinal_tileset_dirs[i];
if (ts_tiles[tterrain_near[i]['graphic_str']] == null) continue;
var that = ts_tiles[tterrain_near[i]['graphic_str']]['layer' + l + '_match_type'];
if (that == this_match_type) {
tileno |= 1 << i;
}
}
return [ {"key" : "t.l" + l + "." + pterrain['graphic_str'] + "_" + cardinal_index_str(tileno)} ];
break;
}
}
}
case CELL_CORNER:
{
/* Divide the tile up into four rectangular cells. Each of these
* cells covers one corner, and each is adjacent to 3 different
* tiles. For each cell we pick a sprite based upon the adjacent
* terrains at each of those tiles. Thus, we have 8 different sprites
* for each of the 4 cells (32 sprites total).
*
* These arrays correspond to the direction4 ordering. */
var W = normal_tile_width;
var H = normal_tile_height;
var iso_offsets = [ [W / 4, 0], [W / 4, H / 2], [W / 2, H / 4], [0, H / 4]];
var this_match_index = ('l' + l + '.' + pterrain['graphic_str'] in tile_types_setup) ? tile_types_setup['l' + l + '.' + pterrain['graphic_str']]['match_index'][0] : -1;
var that_match_index = ('l' + l + '.' + pterrain['graphic_str'] in tile_types_setup) ? tile_types_setup['l' + l + '.' + pterrain['graphic_str']]['match_index'][1] : -1;
var result_sprites = [];
/* put corner cells */
for (var i = 0; i < NUM_CORNER_DIRS; i++) {
var count = dlp['match_indices'];
var array_index = 0;
var dir = dir_ccw(DIR4_TO_DIR8[i]);
var x = iso_offsets[i][0];
var y = iso_offsets[i][1];
var m = [('l' + l + '.' + tterrain_near[dir_ccw(dir)]['graphic_str'] in tile_types_setup) ? tile_types_setup['l' + l + '.' + tterrain_near[dir_ccw(dir)]['graphic_str']]['match_index'][0] : -1 ,
('l' + l + '.' + tterrain_near[dir]['graphic_str'] in tile_types_setup) ? tile_types_setup['l' + l + '.' + tterrain_near[dir]['graphic_str']]['match_index'][0] : -1,
('l' + l + '.' + tterrain_near[dir_cw(dir)]['graphic_str'] in tile_types_setup) ? tile_types_setup['l' + l + '.' + tterrain_near[dir_cw(dir)]['graphic_str']]['match_index'][0] : -1];
/* synthesize 4 dimensional array? */
switch (dlp['match_style']) {
case MATCH_NONE:
/* We have no need for matching, just plug the piece in place. */
break;
case MATCH_SAME:
var b1 = (m[2] != this_match_index) ? 1 : 0;
var b2 = (m[1] != this_match_index) ? 1 : 0;
var b3 = (m[0] != this_match_index) ? 1 : 0;
array_index = array_index * 2 + b1;
array_index = array_index * 2 + b2;
array_index = array_index * 2 + b3;
break;
case MATCH_PAIR:
// FIXME: This doesn't work!
/*var b1 = (m[2] == that_match_index) ? 1 : 0;
var b2 = (m[1] == that_match_index) ? 1 : 0;
var b3 = (m[0] == that_match_index) ? 1 : 0;
array_index = array_index * 2 + b1;
array_index = array_index * 2 + b2;
array_index = array_index * 2 + b3;*/
return [];
break;
case MATCH_FULL:
{
var n = [];
var j = 0;
for (; j < 3; j++) {
var k = 0;
for (; k < count; k++) {
n[j] = k; /* default to last entry */
if (m[j] == dlp['match_index'][k]) {
break;
}
}
}
array_index = array_index * count + n[2];
array_index = array_index * count + n[1];
array_index = array_index * count + n[0];
}
break;
};
array_index = array_index * NUM_CORNER_DIRS + i;
result_sprites.push({"key" : cellgroup_map[pterrain['graphic_str'] + "." + array_index] + "." + i, "offset_x" : x, "offset_y" : y});
}
return result_sprites;
break;
}
}
return [];
}
/**********************************************************************
Determine the sprite_type string.
***********************************************************************/
function check_sprite_type(sprite_type)
{
if (sprite_type == "corner") {
return CELL_CORNER;
}
if (sprite_type == "single") {
return CELL_WHOLE;
}
if (sprite_type == "whole") {
return CELL_WHOLE;
}
return CELL_WHOLE;
}
/**************************************************************************
...
**************************************************************************/
function fill_unit_sprite_array(punit, stacked, backdrop)
{
var unit_offset = get_unit_anim_offset(punit);
var result = [ get_unit_nation_flag_sprite(punit),
{"key" : unit_type(punit)['graphic_str'],
"offset_x": unit_offset['x'] + unit_offset_x,
"offset_y" : unit_offset['y'] - unit_offset_y} ];
var activities = get_unit_activity_sprite(punit);
if (activities != null) {
activities['offset_x'] = activities['offset_x'] + unit_offset['x'];
activities['offset_y'] = activities['offset_y'] + unit_offset['y'];
result.push(activities);
}
result.push(get_unit_hp_sprite(punit));
return result;
}
/**************************************************************************
Return the tileset name of the direction. This is similar to
dir_get_name but you shouldn't change this or all tilesets will break.
**************************************************************************/
function dir_get_tileset_name(dir)
{
switch (dir) {
case DIR8_NORTH:
return "n";
case DIR8_NORTHEAST:
return "ne";
case DIR8_EAST:
return "e";
case DIR8_SOUTHEAST:
return "se";
case DIR8_SOUTH:
return "s";
case DIR8_SOUTHWEST:
return "sw";
case DIR8_WEST:
return "w";
case DIR8_NORTHWEST:
return "nw";
};
return "";
}
/****************************************************************************
Return a directional string for the cardinal directions. Normally the
binary value 1000 will be converted into "n1e0s0w0". This is in a
clockwise ordering.
****************************************************************************/
function cardinal_index_str(idx)
{
var c = "";
for (var i = 0; i < num_cardinal_tileset_dirs; i++) {
var value = (idx >> i) & 1;
c += dir_get_tileset_name(cardinal_tileset_dirs[i]) + value;
}
return c;
}
/**********************************************************************
Return the flag graphic to be used by the city.
***********************************************************************/
function get_city_flag_sprite(pcity) {
var owner_id = pcity['owner'];
if (owner_id == null) return {};
var owner = players[owner_id];
if (owner == null) return {};
var nation_id = owner['nation'];
if (nation_id == null) return {};
var nation = nations[nation_id];
if (nation == null) return {};
return {"key" : "f." + nation['graphic_str'],
"offset_x" : city_flag_offset_x,
"offset_y" : - city_flag_offset_y};
}
/**********************************************************************
Return the sprite for a active city worked tile.
***********************************************************************/
function get_city_active_worked_sprite() {
return {"key" : "city_active",
"offset_x" : 0,
"offset_y" : 0};
}
/**********************************************************************
...
***********************************************************************/
function get_city_food_output_sprite(num) {
return {"key" : "city.t_food_" + num,
"offset_x" : normal_tile_width/4,
"offset_y" : -normal_tile_height/4};
}
/**********************************************************************
...
***********************************************************************/
function get_city_shields_output_sprite(num) {
return {"key" : "city.t_shields_" + num,
"offset_x" : normal_tile_width/4,
"offset_y" : -normal_tile_height/4};
}
/**********************************************************************
...
***********************************************************************/
function get_city_trade_output_sprite(num) {
return {"key" : "city.t_trade_" + num,
"offset_x" : normal_tile_width/4,
"offset_y" : -normal_tile_height/4};
}
/**********************************************************************
Return the sprite for an invalid city worked tile.
***********************************************************************/
function get_city_invalid_worked_sprite() {
return {"key" : "city_invalid",
"offset_x" : 0,
"offset_y" : 0};
}
/**********************************************************************
Return the size graphic to be used by the city.
***********************************************************************/
function get_city_size_sprites(pcity) {
var result = [];
var size = pcity['size'];
var size_str = size + '';
var lsb = 0;
var msb = 0;
if (size_str.length == 1) {
lsb = size_str;
msb = 0;
} else if (size_str.length == 2) {
lsb = size_str.substring(1, 2);
msb = size_str.substring(0, 1);
}
result.push({"key" : "city.size_" + lsb,
"offset_x" : city_size_offset_x,
"offset_y" : - city_size_offset_y});
if (size_str.length == 2) {
result.push({"key" : "city.size_" + msb + "0",
"offset_x" : city_size_offset_x,
"offset_y" : - city_size_offset_y});
}
return result;
}
/**********************************************************************
...
***********************************************************************/
function get_unit_nation_flag_sprite(punit)
{
var owner_id = punit['owner'];
var owner = players[owner_id];
var nation_id = owner['nation'];
var nation = nations[nation_id];
var unit_offset = get_unit_anim_offset(punit);
return {"key" : "f.shield." + nation['graphic_str'],
"offset_x" : unit_flag_offset_x + unit_offset['x'],
"offset_y" : - unit_flag_offset_y + unit_offset['y']};
}
/**********************************************************************
...
***********************************************************************/
function get_unit_hp_sprite(punit)
{
var hp = punit['hp'];
var unit_type = unit_types[punit['type']];
var max_hp = unit_type['hp'];
var healthpercent = 10 * Math.floor((10 * hp) / max_hp);
var unit_offset = get_unit_anim_offset(punit);
return {"key" : "unit.hp_" + healthpercent,
"offset_x" : unit_flag_offset_x + -25 + unit_offset['x'],
"offset_y" : - unit_flag_offset_y - 15 + unit_offset['y']};
}
/**********************************************************************
...
***********************************************************************/
function get_unit_activity_sprite(punit)
{
var activity = punit['activity'];
/* don't draw activity for enemy units */
if (client.conn.playing == null || punit['owner'] != client.conn.playing.playerno) {
return null;
}
if (unit_has_goto(punit)) {
return {"key" : "unit.goto",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
}
switch (activity) {
case ACTIVITY_POLLUTION:
return {"key" : "unit.fallout",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_ROAD:
return {"key" : "unit.road",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_MINE:
return {"key" : "unit.mine",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_IRRIGATE:
return {"key" : "unit.irrigate",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_FORTIFIED:
return {"key" : "unit.fortified",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_FORTRESS:
return {"key" : "unit.fortress",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_SENTRY:
return {"key" : "unit.sentry",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_RAILROAD:
return {"key" : "unit.road",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_PILLAGE:
return {"key" : "unit.pillage",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_GOTO:
return {"key" : "unit.goto",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_EXPLORE:
return {"key" : "unit.auto_explore",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_TRANSFORM:
return {"key" : "unit.transform",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
case ACTIVITY_FORTIFYING:
return {"key" : "unit.fortifying",
"offset_x" : unit_activity_offset_x,
"offset_y" : - unit_activity_offset_y}
break;
}
return null;
}
/****************************************************************************
Return the sprite in the city_sprite listing that corresponds to this
city - based on city style and size.
See also load_city_sprite, free_city_sprite.
****************************************************************************/
function get_city_sprite(pcity)
{
var owner_id = pcity['owner'];
var player = players[owner_id];
var style_id = player['city_style'];
var city_rule = city_rules[style_id];
var size = 0;
if (pcity['size'] >=4 && pcity['size'] <=7) {
size = 4;
} else if (pcity['size'] >=8 && pcity['size'] <=11) {
size = 8;
} else if (pcity['size'] >=12 && pcity['size'] <=15) {
size = 12;
} else if (pcity['size'] >=16) {
size = 16;
}
var city_walls = pcity['walls'] ? "wall" : "city";
var gfx = city_rule['graphic'];
if (gfx.length <= 2) {
/* FIXME: This is a hack, because sometimes the 'graphic' field contains garbage. */
gfx = city_rule['oceanic_graphic'];
}
return {"key" : gfx + "_" + city_walls + "_" + size,
"offset_x": 0, "offset_y" : -unit_offset_y};
}
/****************************************************************************
Add sprites for fog (and some forms of darkness).
****************************************************************************/
function fill_fog_sprite_array(ptile, pedge, pcorner)
{
var i, tileno = 0;
if (pcorner == null) return [];
for (i = 3; i >= 0; i--) {
var unknown = 0, fogged = 1, known = 2;
var value = -1;
if (pcorner['tile'][i] == null) {
value = fogged;
} else {
switch (tile_get_known(pcorner['tile'][i])) {
case TILE_KNOWN_SEEN:
value = known;
break;
case TILE_KNOWN_UNSEEN:
value = fogged;
break;
case TILE_UNKNOWN:
value = unknown;
break;
};
}
tileno = tileno * 3 + value;
}
if (tileno >= 80) return [];
return [{"key" : fullfog[tileno]}];
}
/****************************************************************************
...
****************************************************************************/
function get_select_sprite()
{
current_select_sprite = ((current_select_sprite + 1) % max_select_sprite);
return {"key" : "unit.select" + current_select_sprite };
}
/****************************************************************************
...
****************************************************************************/
function get_city_info_text(pcity)
{
return {"key" : "city_text", "text" : unescape(pcity['name']),
"offset_x": citybar_offset_x, "offset_y" : citybar_offset_y};
}
/****************************************************************************
...
****************************************************************************/
function get_tile_specials_sprite(ptile)
{
if (ptile == null || ptile['resource'] == null) return null;
var resource = resources[ptile['resource']];
if (resource == null) return null;
return {"key" : resource['graphic_str']} ;
}
/****************************************************************************
...
****************************************************************************/
function get_unit_image_sprite(punit)
{
var tag = unit_type(punit)['graphic_str'];
if (tileset[tag] == null) return null;
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];
var width = tileset[tag][2];
var height = tileset[tag][3];
return {"tag": tag,
"image-src" : "/tileset/freeciv-web-tileset.png",
"tileset-x" : tileset_x,
"tileset-y" : tileset_y,
"width" : width,
"height" : height
};
}
/****************************************************************************
...
****************************************************************************/
function get_unit_type_image_sprite(punittype)
{
var tag = punittype['graphic_str'];
if (tileset[tag] == null) return null;
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];
var width = tileset[tag][2];
var height = tileset[tag][3];
return {"tag": tag,
"image-src" : "/tileset/freeciv-web-tileset.png",
"tileset-x" : tileset_x,
"tileset-y" : tileset_y,
"width" : width,
"height" : height
};
}
/****************************************************************************
...
****************************************************************************/
function get_improvement_image_sprite(pimprovement)
{
var tag = pimprovement['graphic_str'];
if (tileset[tag] == null) {
tag = pimprovement['graphic_alt'];
if (tileset[tag] == null) return null;
}
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];
var width = tileset[tag][2];
var height = tileset[tag][3];
return {"tag": tag,
"image-src" : "/tileset/freeciv-web-tileset.png",
"tileset-x" : tileset_x,
"tileset-y" : tileset_y,
"width" : width,
"height" : height
};
}
/****************************************************************************
...
****************************************************************************/
function get_technology_image_sprite(ptech)
{
var tag = ptech['graphic_str'];
if (tileset[tag] == null) return null;
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];
var width = tileset[tag][2];
var height = tileset[tag][3];
return {"tag": tag,
"image-src" : "/tileset/freeciv-web-tileset.png",
"tileset-x" : tileset_x,
"tileset-y" : tileset_y,
"width" : width,
"height" : height
};
}
/****************************************************************************
...
****************************************************************************/
function get_player_fplag_sprite(pplayer)
{
var pnation = nations[pplayer['nation']];
var tag = "f." + pnation['graphic_str'];
if (tileset[tag] == null) return null;
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];
var width = tileset[tag][2];
var height = tileset[tag][3];
return {"tag": tag,
"image-src" : "/tileset/freeciv-web-tileset.png",
"tileset-x" : tileset_x,
"tileset-y" : tileset_y,
"width" : width,
"height" : height
};
}
/****************************************************************************
...
****************************************************************************/
function get_nation_flag_sprite(pnation)
{
var tag = "f." + pnation['graphic_str'];
if (tileset[tag] == null) return null;
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];
var width = tileset[tag][2];
var height = tileset[tag][3];
return {"tag": tag,
"image-src" : "/tileset/freeciv-web-tileset.png",
"tileset-x" : tileset_x,
"tileset-y" : tileset_y,
"width" : width,
"height" : height
};
}
/****************************************************************************
...
****************************************************************************/
function get_treaty_agree_thumb_up()
{
var tag = "treaty.agree_thumb_up";
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];
var width = tileset[tag][2];
var height = tileset[tag][3];
return {"tag": tag,
"image-src" : "/tileset/freeciv-web-tileset.png",
"tileset-x" : tileset_x,
"tileset-y" : tileset_y,
"width" : width,
"height" : height
};
}
/****************************************************************************
...
****************************************************************************/
function get_treaty_disagree_thumb_down()
{
var tag = "treaty.disagree_thumb_down";
var tileset_x = tileset[tag][0];
var tileset_y = tileset[tag][1];