forked from Maruno17/pokemon-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
townmapgen.html
373 lines (352 loc) · 11 KB
/
townmapgen.html
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
<body>
<div id="map">
<img name="map" src="Graphics/Pictures/mapRegion0.png" alt="" width="480" height="320"/>
</div>
<form name="mf" action="javascript:void(null)">
<div>
Map Filename (in Graphics/Pictures/): <input type="text" name="filename" value="mapRegion0.png"/>
<input type="button" name="btnChange" value="Change"/><br/ >
Square width: <input type="text" name="sqwidth" size="4" min="1" max="16" value="16"/>
Square height: <input type="text" name="sqheight" size="4" min="1" max="16" value="16"/>
<input type="button" name="btnRefresh" value="Refresh"/><br/ >
Region Name: <input type="text" name="name" value="Sample Region"/>
<input type="button" name="btnChange2" value="Change"/>
</div>
</form>
<hr/>
<form name="f" action="javascript:void(null)">
<table>
<tr>
<td>Current Position:</td>
<td><input type="text" name="curpos" readonly="readonly"/></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="locname"/><td>
</tr>
<tr>
<td>Point of Interest:</td>
<td><input type="text" name="poi"/><td>
</tr>
<tr>
<td>Fly Destination:</td>
<td><input type="text" name="healing"/><td>
</tr>
<tr>
<td>Switch:</td>
<td><input type="text" name="swtch"/><td>
</tr>
</table>
<div>
<input type="button" name="btnSave" value="Save" disabled="disabled"/>
<input type="button" name="btnCancel" value="Cancel"/><br/ >
Single section from townmap.txt (without section heading):<br/ >
<textarea name="data" rows="10" cols="70"></textarea><br/ >
<input type="button" name="btnLoad" value="Load"/> (To load data into the editor)<br/ >
Copy the data above into townmap.txt when you're done.
</div>
</form>
<script type="text/javascript">
choiceX=-1
choiceY=-1
mappoints=[]
mpelements=[]
function target(event){
return event.target||event.srcElement
}
function addChild(e){
if(document.documentElement){
document.documentElement.appendChild(e)
} else{
document.body.appendChild(e)
}
}
function removeChild(e){
if(document.documentElement){
document.documentElement.removeChild(e)
} else{
document.body.removeChild(e)
}
}
function positionedOffset(element) {
var valueT = 0, valueL = 0;
do {
valueT += element.offsetTop || 0;
valueL += element.offsetLeft || 0;
element = element.offsetParent;
if (element) {
p = element.style.position;
if (p == 'relative' || p == 'absolute') break;
}
} while (element);
return [valueL, valueT];
}
function pointerX(event) {
return event.pageX || (event.clientX +
(document.documentElement.scrollLeft || document.body.scrollLeft));
}
function pointerY(event) {
return event.pageY || (event.clientY +
(document.documentElement.scrollTop || document.body.scrollTop));
}
function elementPos(event){
t=target(event)
po=positionedOffset(t)
return [(pointerX(event))-po[0],(pointerY(event))-po[1]]
}
function elementPosObject(event,other){
t=other
po=positionedOffset(t)
return [(pointerX(event))-po[0],(pointerY(event))-po[1]]
}
function attachEvent(element,name,observer,useCapture){
if (element.addEventListener) {
element.addEventListener(name, observer, useCapture||false);
} else if (element.attachEvent) {
element.attachEvent('on' + name, observer);
}
}
function qstr(text){
var temp;
temp = text.toString();
while (true){
if (temp == '') return text;
if (temp.indexOf("'") >= 0) break;
if (temp.indexOf('"') >= 0) break;
if (temp.indexOf(' ') >= 0) break;
if (temp.indexOf(',') >= 0) break;
return temp;
}
// temp = temp.replace(/\'/g,"''");
// temp = temp.replace(/\"/g,"\"\"");
return temp
// return "\"" + temp + "\"";
}
function CsvNextToken(dataobj){
var i;
var text;
var c,n;
var inside;
var value;
var q = '';
inside = false;
value = '';
var skip = false;
var data=dataobj[0]
if (data == '') return '';
text = '';
for (i = 0; i < data.length; ++i){
if (skip){
skip = false;
continue;
}
c = data.charAt(i);
n = '';
if (inside){
if (c == q) {
if (i < data.length-1) n = data.charAt(i+1);
if (n == q){
value = value + c;
skip = true;
continue;
}
inside = false;
continue;
}
value = value + c;
continue;
}
// if ((c == '"') || (c == "'")){
// inside = true;
// q = c;
// continue;
// }
if (c == ',') break;
value = value + c;
}
if (i >= (data.length-1)) { dataobj[0]= ''; }
else{dataobj[0]=data.substr(i+1);}
dataobj[0]=dataobj[0].replace(/^\s+/,"").replace(/\s+$/,"")
return value;
}
///////////////////////////
function loadMapPoints(){
mappoints=[]
lines=document.f.data.value.split("\n")
for(var i=0;i<lines.length;i++){
lines[i]=lines[i].replace(/\s+$/,"")
if(!/^\#/.test(lines[i])&&!/^\s*$/.test(lines[i])){
e=/^\s*(\w+)\s*=\s*(.*)$/.exec(lines[i])
if(!e){
alert("Bad line syntax in line "+(i+1))
}
if(e[1]=="Filename"){
o=document.getElementById("map")
o.innerHTML='<img name="map" src="Graphics/Pictures/'+e[2]+'" alt="" width="480" height="320"/'+'>'
} else if(e[1]=="Name"){
document.mf.name.value=e[2]
} else if(e[1]=="Point"){
data=[e[2]]
mappt=[]
mappt[0]=parseInt(CsvNextToken(data),10)
mappt[1]=parseInt(CsvNextToken(data),10)
mappt[2]=CsvNextToken(data)
mappt[3]=CsvNextToken(data)
mappt[4]=[CsvNextToken(data),CsvNextToken(data),CsvNextToken(data)]
mappt[5]=CsvNextToken(data)
mappoints[mappoints.length]=mappt
}
}
}
}
function createMapPoint(imgfile,x,y){
e=document.createElement("div")
e.innerHTML="<img src='"+imgfile+"' alt=''/>"
e.style.left=elempos[0]+x*document.mf.sqwidth.value+(document.mf.sqwidth.value-8)/2
e.style.top=elempos[1]+y*document.mf.sqheight.value+(document.mf.sqheight.value-8)/2
e.style.position="absolute"
attachEvent(e,"mousemove",MapMouseMove)
attachEvent(e,"click",MapClick)
return e
}
function showMapPoints(){
for(var i=0;i < mpelements.length ; i++){ //>
removeChild(mpelements[i])
}
mpelements=[]
elempos=positionedOffset(document.images.map)
for(var i=0;i<mappoints.length;i++){//>
e=createMapPoint("knownpoint.bmp",mappoints[i][0],mappoints[i][1])
addChild(e)
mpelements[mpelements.length]=e
}
if(choiceX!=-1 || choiceY!=-1){
e=createMapPoint("selpoint.bmp",choiceX,choiceY)
addChild(e)
mpelements[mpelements.length]=e
}
}
function MapMouseMove(e){
if(choiceX==-1 && choiceY==-1){
elempos=elementPosObject(e,document.getElementById("map"))
if (elempos[0]>=0 && elempos[0]<480 && elempos[1]>=0 && elempos[1]<320){
setMapPoint(Math.floor(elempos[0]/document.mf.sqwidth.value),
Math.floor(elempos[1]/document.mf.sqheight.value))
}
}
}
function MapClick(e){
elempos=elementPosObject(e,document.getElementById("map"))
if(elempos[0]>=0 && elempos[0]<480 && elempos[1]>=0 && elempos[1]<320){
choiceX=Math.floor(elempos[0]/document.mf.sqwidth.value)
choiceY=Math.floor(elempos[1]/document.mf.sqheight.value)
setMapPoint(Math.floor(elempos[0]/document.mf.sqwidth.value),
Math.floor(elempos[1]/document.mf.sqheight.value))
document.f.btnSave.disabled=false
showMapPoints()
}
}
function getbase(fn){
if(fn.lastIndexOf("/")>=0){
fn=fn.substr(fn.lastIndexOf("/")+1)
}
if(fn.lastIndexOf("\\")>=0){
fn=fn.substr(fn.lastIndexOf("\\")+1)
}
return fn
}
function genMapPoints(){
ret=""
ret+="Filename = "+qstr(getbase(document.images.map.src))+"\r\n"
ret+="Name = "+qstr(document.mf.name.value)+"\r\n"
for(var i=0;i<mappoints.length;i++){
ret+="Point = "+[mappoints[i][0],mappoints[i][1],
qstr(mappoints[i][2]),qstr(mappoints[i][3]),
mappoints[i][4][0]!=null?mappoints[i][4][0]:"",
mappoints[i][4][1]!=null?mappoints[i][4][1]:"",
mappoints[i][4][2]!=null?mappoints[i][4][2]:"",
mappoints[i][5]]+"\r\n"
}
document.f.data.value=ret
showMapPoints()
}
function setMapPoint(x,y){
document.f.curpos.value=[x,y]
for(var i=0;i<mappoints.length;i++){
if(mappoints[i][0]==x && mappoints[i][1]==y){
document.f.locname.value=mappoints[i][2]
document.f.poi.value=mappoints[i][3]
document.f.healing.value=mappoints[i][4]
document.f.swtch.value=mappoints[i][5]
return
}
}
document.f.locname.value=""
document.f.poi.value=""
document.f.healing.value=""
document.f.swtch.value=""
}
function addMapPoint(x,y){
for(var i=0;i<mappoints.length;i++){
if(mappoints[i][0]==x && mappoints[i][1]==y){
mappoints[i][2]=document.f.locname.value
mappoints[i][3]=document.f.poi.value
mappoints[i][4]=document.f.healing.value.split(",")
mappoints[i][5]=document.f.swtch.value
return
}
}
mappoints[mappoints.length]=[x,y,
document.f.locname.value,
document.f.poi.value,
document.f.healing.value.split(","),
document.f.swtch.value,
]
}
attachEvent(window,"load",function(e){
genMapPoints()
attachEvent(document.images.map,"mouseout",function(e){
if(choiceX==-1 && choiceY==-1){
document.f.curpos.value=""
}
})
attachEvent(document.getElementById("map"),"mousemove",MapMouseMove)
attachEvent(document.getElementById("map"),"click",MapClick)
attachEvent(document.f.btnSave,"click",function(e){
if(choiceX!=-1 || choiceY!=-1){
addMapPoint(choiceX,choiceY)
genMapPoints()
target(e).disabled=true
choiceX=choiceY=-1
showMapPoints()
}
})
attachEvent(document.f.btnCancel,"click",function(e){
if(choiceX!=-1||choiceY!=-1){
setMapPoint(choiceX,choiceY)
choiceX=choiceY=-1
showMapPoints()
}
})
attachEvent(document.f.btnLoad,"click",function(e){
loadMapPoints()
genMapPoints()
choiceX=choiceY=-1
showMapPoints()
})
attachEvent(document.mf.btnChange,"click",function(e){
document.images.map.src="Graphics/Pictures/"+document.mf.filename.value
genMapPoints()
showMapPoints()
})
attachEvent(document.mf.btnChange2,"click",function(e){
genMapPoints()
showMapPoints()
})
attachEvent(document.mf.btnRefresh,"click",function(e){
choiceX=choiceY=-1
document.f.curpos.value=""
showMapPoints()
})
})
</script>