-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
253 lines (217 loc) · 7.37 KB
/
map.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
var tile_size = 80;
var terrain_lookup = {
" " : "",
"_" : "terrain01.png",
"B" : "terrain02.png",
"F" : "terrain03.png",
"M" : "terrain04.png",
"S" : "terrain05.png",
"D" : "terrain06.png",
"w" : "terrain07.png",
"P" : "terrain08.png",
"+" : "terrain09.png",
};
function debug( message ) {
$("#debug").append( message + "<br/>" );
}
function createMap( map_name ) {
var map_url = "../xml/" + map_name;
$("#title").html( "Loading " + map_name );
$.ajax({
url : map_url,
type: "GET",
success: function(data, textStatus, jqXHR)
{
var xml = jqXHR.responseText;
var xmlDoc = $.parseXML( xml );
var $xml = $( xmlDoc );
var title = $xml.find( "title" ).text();
$("#title").html( title );
var width = $xml.find( "width" ).text();
var height = $xml.find( "height" ).text();
drawEmptyMap( width, height );
loadTerrain( height, $xml );
loadBases( $xml );
loadUnits( $xml );
},
error: function (jqXHR, textStatus, errorThrown)
{
debug( "failed: " + errorThrown );
}
});
}
function drawEmptyMap( width, height ) {
// Create a blank map of the correct size
var html = "";
for (y = 0; y < height; y++) {
html = html + '<div class="map_row" id="row_' + y + '">';
for (x = 0; x < width; x++) {
html = html + '<div class="map_cell" id="cell_' + x + '_' + y + '">';
html = html + '</div>';
}
html = html + '</div>';
}
$("#map_area").append(html);
$(".map_row:odd").addClass("map_row_odd");
}
function loadTerrain( rows, $xml ) {
// Put the correct images into each of the backgrounds
var tile_set = $xml.find( "tile_set" ).text().toLowerCase();
var $map_data = $xml.find( "map_data" );
$map_data.contents().each( function() {
var $this = $(this)
var row_data = $this.text();
var x = 0;
row_data.split(", ").forEach(function(tile) {
// Strip the single quotes
var tile = tile.substring(1, tile.length-1);
if ($.trim(tile) != "") {
var terrain = terrain_lookup[tile];
var cellid = "#cell_" + x + "_" + $this.index();
var $cell = $( cellid );
$cell.css("background-image", "url(/assets/tiles80/" + tile_set + "/" + terrain);
$cell.prop('title', cellid + '\ntile = ' + tile + '\nterrain = ' + terrain );
}
x = x + 1;
});
});
removeBlankRows();
removeBlankCols();
adjustMapArea();
}
function loadBases( $xml ) {
var $base_data = $xml.find( "player_bases" );
$base_data.contents().each( function() {
var $player = $(this);
$player.contents().each( function() {
if (this.localName != "bases") {
var x = $(this).find("x").text();
var y = $(this).find("y").text();
var cellid = "#cell_" + x + "_" + y;
var $cell = $( cellid );
$cell.append('<img src="/assets/units80/sapien_base.' + $player.index() + '.png" />');
$cell.css('padding', '0px 0px');
}
});
});
}
function loadUnits( $xml ) {
var $unit_data = $xml.find( "unit_data" );
$unit_data.contents().each( function() {
var $player = $(this);
var player = $player.index();
$player.contents().each( function() {
var unit = "";
switch( this.localName ) {
case "Basic":
unit = ["marine", "underling", "mecha"];
break;
case "Medic":
unit = ["engineer", "infector", "assimilator"];
break;
case "Range":
unit = ["battery", "wyrm", "walker"];
break;
case "Tank":
unit = ["tank", "pinzer", "plasma_tank"];
break;
case "Light":
unit = ["marauder", "swarmer", "speeder"];
break;
case "Boat":
unit = ["destroyer", "leviathan", "hydronaut"];
break;
case "Medium":
unit = ["helicopter", "garuda", "eclipse"];
break;
case "Converted":
unit = ["infected_marine", "cyber_underling", "mecha_2"];
break;
default:
return;
}
$(this).contents().each( function() {
var x = $(this).find("x").text();
var y = $(this).find("y").text();
var cellid = "#cell_" + x + "_" + y;
var $cell = $( cellid );
$player.index()
$cell.append('<img src="/assets/units80/' + unit[player % 3] + '.' + player + '.png" />');
$cell.css('padding', '0px 0px');
});
});
});
}
/* Remove empty rows and columns starting from the edges.
Means we don't remove empty rows/columns from the middle as that would change the map */
function removeBlankRows() {
// Starting at the top find all empty rows till we get to a row with a tile and remove them
$(".map_row").each( checkCells );
// Starting at the bottom find all empty rows till we get to a row with a tile and remove them
$($(".map_row").get().reverse()).each( checkCells );
}
function checkCells( i, element ) {
$this = $(element);
var done = false;
$this.children().each( function() {
if (hasTerrain( this )) {
done = true;
return false;
}
});
if (done) {
return false;
}
$this.remove();
}
function hasTerrain( element ) {
return $(element).css("background-image") != "none";
}
function removeBlankCols() {
// Starting at the left find all empty rows till we get to a row with a tile and remove them
var done = false;
while (!done) {
$(".map_cell:first-child").each( function() {
if ( hasTerrain( this ) ) {
done = true;
return false;
}
});
if (!done) {
$(".map_cell:first-child").remove();
}
}
// Starting at the right find all empty rows till we get to a row with a tile and remove them
done = false;
while (!done) {
$(".map_cell:last-child").each( function() {
if ( hasTerrain( this ) ) {
done = true;
return false;
}
});
if (!done) {
$(".map_cell:last-child").remove();
}
}
}
function adjustMapArea() {
if ($(".map_row:first").hasClass("map_row_odd")) {
$("#map_area").css("padding-top", "40px");
}
if ($(".map_row:last").hasClass("map_row_odd")) {
$("#map_area").css("padding-bottom", "40px");
}
$(".map_row:not(.map_row_odd) > :first-child").each( function() {
if ( hasTerrain( this ) ) {
$("#map_area").css("padding-left", "25px");
return false;
}
});
$(".map_row_odd > :last-child").each( function() {
if ( hasTerrain( this ) ) {
$("#map_area").css("padding-right", "25px");
return false;
}
});
}