-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerchFuncs.js
More file actions
518 lines (455 loc) · 13.7 KB
/
merchFuncs.js
File metadata and controls
518 lines (455 loc) · 13.7 KB
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
var avgIslandDist = 0.000500;
var renderDist = 0.010000;
var latSeed = 4829;
var lngSeed = 8513;
var time = 0;
var sellValue = 0.85;
var priceChangePerUnit = 0.02;
var priceChangeRange = avgIslandDist * 5;
var shouldDrawIslands = true;
var currentPosition;
var commodities = {};
commodities.wheat = {
name: "wheat",
freqs: [0.006, 0.017, 0.027],
color: "00FF00",
atten: 0.5,
draw: false,
floorPrice: 10,
ceilingPrice: 50,
icons: []
}
commodities.corn = {
name: "corn",
freqs: [0.0065, 0.0165, 0.028],
color: "00FF00",
atten: 0.5,
draw: false,
floorPrice: 12,
ceilingPrice: 60,
icons: []
}
commodities.grapes = {
name: "grapes",
freqs: [0.0063, 0.0175, 0.029],
color: "00FF00",
atten: 0.5,
draw: false,
floorPrice: 10,
ceilingPrice: 50,
icons: []
}
commodities.apples = {
name: "apples",
freqs: [0.005, 0.016, 0.029],
color: "FF0000",
atten: 0.4,
draw: false,
floorPrice: 30,
ceilingPrice: 150,
icons: []
}
commodities.silver = {
name: "silver",
freqs: [0.002, 0.011, 0.019],
color: "FFFF00",
atten: 0.2,
draw: false,
floorPrice: 100,
ceilingPrice: 400,
icons: []
}
var player = {
funds: 500,
maxCargo: 100,
tradeHist: {
}
}
var map;
var cLat = 59.323718;
var cLng = 18.071131;
var center;
var islands = [];
var islandWindow;
function initialize() {
center = new google.maps.LatLng(cLat,cLng);
//center = new google.maps.LatLng(0.000000,18.270000);
var myOptions = {
center: center,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(event) {
cLat = e("renderCenterLat").value = event.latLng.lat();
cLng = e("renderCenterLng").value = event.latLng.lng();
updateRenderingValues();
});
islandWindow = new google.maps.InfoWindow({
});
var commoditiesDivHTML = "";
var playerHoldDivHTML = "";
for(commodityName in commodities)
{
var commodity = commodities[commodityName];
commoditiesDivHTML += "<div id='" + commodityName + "Controls' style='float:left; width:150px; height:90%; padding: 5px; border: 5px white groove;'>" +
commodityName + "<br>" +
"<br>" +
"Render <input id='render" + commodityName + "Checkbox' type='checkbox' onclick='commodities." + commodityName + ".draw=this.checked;createCommodities(center, commodities." + commodityName + ");'/><br>" +
"Color:<input type='text' id='" + commodityName + "ColorBox' style='width:100px;' onchange='commodities." + commodityName + ".color = this.value'/><br>" +
"Floor :<input type='text' id='" + commodityName + "FloorPBox' style='width:100px;' onchange='commodities." + commodityName + ".floorPrice = this.value'/><br>" +
"Ceilin:<input type='text' id='" + commodityName + "CeilPBox' style='width:100px;' onchange='commodities." + commodityName + ".ceilingPrice = this.value'/><br>" +
"Freq1:<input type='text' id='" + commodityName + "Freq1Box' style='width:100px;' onchange='commodities." + commodityName + ".freqs[0] = this.value'/><br>" +
"Freq2:<input type='text' id='" + commodityName + "Freq2Box' style='width:100px;' onchange='commodities." + commodityName + ".freqs[1] = this.value'/><br>" +
"Freq3:<input type='text' id='" + commodityName + "Freq3Box' style='width:100px;' onchange='commodities." + commodityName + ".freqs[2] = this.value'/><br>" +
"Attenuation:<input type='text' id='" + commodityName + "AttenBox' style='width:100px;' onchange='commodities." + commodityName + ".atten = this.value'/><br>" +
"</div>";
playerHoldDivHTML += commodityName + ": <label type='text' id='player" + commodityName + "Label' style='width:100px;'/></label><br>"
player.tradeHist[commodityName] = [];
player[commodityName] = 0;
}
e('ControlsRow2').innerHTML = commoditiesDivHTML;
e('playerHold').innerHTML = playerHoldDivHTML;
e('playerMaxCargo').innerHTML = player.maxCargo;
updateInterface();
redraw();
}
function updateInterface()
{
e("renderCenterLat").value = cLat;
e("renderCenterLng").value = cLng;
e("renderDistance").value = renderDist;
e("renderIslandsCheckbox").checked = shouldDrawIslands;
e("avgIslandDistBox").value = avgIslandDist;
e("latSeedBox").value = latSeed;
e("lngSeedBox").value = lngSeed;
updatePlayerPanel();
for(commodityName in commodities)
{
e("render" + commodityName + "Checkbox").checked = commodities[commodityName].draw;
e(commodityName + "ColorBox").value = commodities[commodityName].color;
e(commodityName + "FloorPBox").value = commodities[commodityName].floorPrice;
e(commodityName + "CeilPBox").value = commodities[commodityName].ceilingPrice;
e(commodityName + "Freq1Box").value = commodities[commodityName].freqs[0];
e(commodityName + "Freq2Box").value = commodities[commodityName].freqs[1];
e(commodityName + "Freq3Box").value = commodities[commodityName].freqs[2];
e(commodityName + "AttenBox").value = commodities[commodityName].atten;
}
}
function updatePlayerPanel()
{
e("playerFundsLabel").innerHTML = player.funds;
e("playerTotalCargo").innerHTML = totalCargo();
for(commodityName in commodities)
{
e("player" + commodityName + "Label").innerHTML = player[commodityName];
}
}
function redraw()
{
createIslands(center);
for(commodityName in commodities)
{
createCommodities(center, commodities[commodityName]);
}
}
function updateRenderingValues()
{
center = new google.maps.LatLng(cLat,cLng);
redraw();
}
var filterLines = [];
function createCommodities(latLng, commodity)
{
for (i in commodity.icons) {
commodity.icons[i].setMap(null);
}
commodity.icons.length = 0;
if(!commodity.draw)
{
return;
}
var maxComoditiesPerAxis = renderDist / avgIslandDist;
for(var j = -maxComoditiesPerAxis; j < maxComoditiesPerAxis; j++)
{
var adjY = latLng.lat() + avgIslandDist * j;
var cy = adjY - adjY % avgIslandDist;
var realXIslandDist = realDistance(avgIslandDist, cy);
var realXQuantum = realDistance(avgIslandDist, cy);
for(var i = -maxComoditiesPerAxis; i < maxComoditiesPerAxis; i++)
{
var adjX = latLng.lng() + realXIslandDist * i;
var cx = adjX - adjX % realXQuantum;
var coordsSW = new google.maps.LatLng(cy, cx);
var coordsNE = new google.maps.LatLng(coordsSW.lat() + avgIslandDist, coordsSW.lng() + realXIslandDist);
var value = getCommodityValue(coordsSW, commodity);
//var green = Math.floor(255*value);
//var red = 255 - green;
commodity.icons.push(new google.maps.Rectangle({
bounds: new google.maps.LatLngBounds(coordsSW, coordsNE),
strokeWeight: 0,
fillColor: "#" + commodity.color,
fillOpacity: (value - commodity.floorPrice)/(commodity.ceilingPrice - commodity.floorPrice),
//fillColor: "#" + red.toString(16) + green.toString(16) + "00",
//fillOpacity: 0.5,
map: map
})
);
}
}
}
function e(elementName)
{
return document.getElementById(elementName);
}
function getCommodityValue(latLng, commodity)
{
var valueSum = 0;
var weightSum = 0;
var weight = 1;
for(var i = 0; i < commodity.freqs.length; i++) {
valueSum += getWaveValue(commodity.freqs[i], latLng) * weight;
weightSum += weight;
weight *= commodity.atten;
}
var percentValue = valueSum/weightSum;
var normalValue = commodity.floorPrice + (commodity.ceilingPrice - commodity.floorPrice) * percentValue;
var inflation = 1;
var tradeHist = player.tradeHist[commodity.name];
for(var i = 0; i < tradeHist.length; i++)
{
console.log("TradeHist coord:" + tradeHist[i].coord + " value:" + tradeHist[i].value);
var dy = tradeHist[i].coord.lat() - latLng.lat();
//var dx = realDistance(tradeHist[i].coord.lng() - latLng.lng(), (tradeHist[i].coord.lat() + latLng.lat())/2);
var dx = tradeHist[i].coord.lng() - latLng.lng()
var dist = Math.sqrt(dx*dx + dy*dy);
var infDist = priceChangeRange - dist;
infDist = (infDist > 0)?infDist:0;
console.log("" + dy + " " + dx + " " + infDist);
inflation += (infDist/priceChangeRange) * tradeHist[i].value * priceChangePerUnit;
}
return normalValue * inflation;
}
function getWaveValue(freq, latLng)
{
y = latLng.lat()/avgIslandDist;
cLat = (y - y % (1/freq)) * avgIslandDist;
x = latLng.lng()/realDistance(avgIslandDist, cLat);
/*var polyPoints = [
new google.maps.LatLng(cLat, latLng.lng()),
new google.maps.LatLng(cLat, latLng.lng() + realDistance(avgIslandDist, cLat))
];
filterLines.push(new google.maps.Polyline({
path: polyPoints,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2,
map: map
})
);*/
var value = (Math.cos(y*freq*2*Math.PI + Math.PI) + Math.cos(x*freq*2*Math.PI + Math.PI))/2;
//value = value + Math.sin(y*0.7)*Math.sin(x*0.7)*0.5;
//value = (value + 1)/2;
value = (value>0)?value:0;
return value;
}
function createIslands(latLng)
{
for (i in islands) {
islands[i].marker.setMap(null);
}
islands.length = 0;
if(!shouldDrawIslands)
{
return;
}
//console.log("cx: " + cx + " cy: " + cy);
var maxIslandsPerAxis = renderDist / avgIslandDist;
for(var j = -maxIslandsPerAxis; j < maxIslandsPerAxis; j++)
{
var adjY = latLng.lat() + avgIslandDist * j;
var cy = adjY - adjY % avgIslandDist;
var realXIslandDist = realDistance(avgIslandDist, cy);
var realXQuantum = realDistance(avgIslandDist, cy);
for(var i = -maxIslandsPerAxis; i < maxIslandsPerAxis; i++)
{
var adjX = latLng.lng() + realXIslandDist * i;
var cx = adjX - adjX % realXQuantum;
var coords = randomizeIslandPosition(cy, cx, avgIslandDist, realXQuantum, avgIslandDist, realXIslandDist);
//var coords = new google.maps.LatLng(cy, cx);
var island={marker:new google.maps.Marker({
position: coords,
icon: "images/island.png",
map: map,
clickable:true
})
};
setInfoWindowToIsland(island);
islands.push(island);
}
}
}
function setInfoWindowToIsland(island)
{
google.maps.event.addListener(island.marker, 'click', function(event) {
showInfoWindow(island);
});
}
function showInfoWindow(island)
{
currentPosition = island.marker.getPosition();
updateInfoWindow();
islandWindow.open(map, island.marker);
}
function updateInfoWindow()
{
var windowContent = "";
for(commodityName in commodities)
{
var commodity = commodities[commodityName];
var buyPrice = Math.floor(getCommodityValue(currentPosition, commodity));
var sellPrice = Math.floor(buyPrice * sellValue);
windowContent += commodityName + ": " + buyPrice + "$ <input type='button' value='Buy' onclick='buyCommodity(commodities." + commodityName + ")'/> - " + sellPrice + "$<input type='button' value='Sell' onclick='sellCommodity(commodities." + commodityName + ")'/><br>"
}
islandWindow.setContent(windowContent);
}
function buyCommodity(commodity)
{
var price = Math.floor(getCommodityValue(currentPosition, commodity));
if(player.funds < price || totalCargo() >= player.maxCargo)
{
return;
}
player.funds -= price;
player[commodity.name] += 1;
var tradeHist = player.tradeHist[commodity.name];
var existed = false;
for(var i = 0; i < tradeHist.length; i++)
{
if(tradeHist[i].coord.equals(currentPosition))
{
existed = true;
tradeHist[i].value += 1;
if(tradeHist[i].value == 0)
{
tradeHist.splice(i,1);
}
break;
}
}
if(!existed)
{
tradeHist.push({
coord: currentPosition,
value: 1
});
}
updateInfoWindow();
updatePlayerPanel();
}
function sellCommodity(commodity)
{
var price = Math.floor(getCommodityValue(currentPosition, commodity) * sellValue);
if(player[commodity.name] < 1)
{
return;
}
player.funds += price;
player[commodity.name] -= 1;
var tradeHist = player.tradeHist[commodity.name];
var existed = false;
for(var i = 0; i < tradeHist.length; i++)
{
if(tradeHist[i].coord.equals(currentPosition))
{
existed = true;
tradeHist[i].value -= 1;
if(tradeHist[i].value == 0)
{
tradeHist.splice(i,1);
}
break;
}
}
if(!existed)
{
tradeHist.push({
coord: currentPosition,
value: -1
});
}
updateInfoWindow();
updatePlayerPanel();
}
function totalCargo()
{
var total = 0;
for(commodityName in commodities)
{
total += player[commodityName];
}
return total;
}
function randomizeIslandPosition(lat, lng, yQuantum, xQuantum, yAvgDist, xAvgDist)
{
var latByQuant = lat / yQuantum;
var lngByQuant = lng / xQuantum;
var iter = ((((latByQuant - Math.floor(latByQuant/100) * 100) + 1) * ((lngByQuant - Math.floor(lngByQuant/10) * 10) + 1)) % 15) + 1
var latRand = myRand(latSeed, iter);
var lngRand = myRand(lngSeed, iter);
var newLat = lat + yAvgDist * 0.6 * lngRand;
var newLng = lng + xAvgDist * 0.6 * latRand;
return new google.maps.LatLng(newLat, newLng);
}
function myRand(seed, iter)
{
//console.log(iter);
if(iter == undefined)
{
iter = 1;
}
for(var i = 0; i < iter; i++)
{
seed = Math.floor((((Math.floor((seed * seed)/100))/10000) % 1) *10000);
//console.log("seed" + seed);
}
return seed / 10000;
}
function realDistance(dist,lat)
{
var newDist = dist / Math.cos(Math.PI * lat / 180);
//console.log("latitude:" + latLng.lat() + " dist:" + dist + " newDist:" + newDist);
return newDist
}
function AdvanceTime()
{
time += 1;
for(commodityName in player.tradeHist)
{
var commodity = player.tradeHist[commodityName];
for(var i = 0; i < commodity.length; i++)
{
if(commodity[i].value < 0)
{
commodity[i].value += 1;
if(commodity[i].value > 0)
{
commodity[i].value = 0;
}
}
else
{
commodity[i].value -= 1;
if(commodity[i].value < 0)
{
commodity[i].value = 0;
}
}
if(commodity[i].value == 0)
{
commodity.splice(i,1);
}
}
}
updateInfoWindow();
}