var gameType; var map; var maptt; var centreMode = 1; var centreOnTxt; var centreOnOwn; var plPlanets; var inUpdate = true; //-------------------------------------------------------------------------------------- // PLANETS AND NEBULAS //-------------------------------------------------------------------------------------- function Planet(system,id,name,status,relation,tag) { this.id = id; this.name = name.replace(/&/g,'&').replace(//g,'>'); this.status = status; this.opacity = (status == "1") ? 1 : 0; this.relation = relation; this.tag = tag; this.system = system; this.drawName = Planet_drawName; this.drawTag = Planet_drawTag; } function Planet_drawName() { var str = ''; if (this.opacity) { str += '[P]'; } else { str += '[P]'; } str += '' + this.name + ''; return str; } function Planet_drawTag() { var str = ''; if (this.opacity) { str += '[P]N/A'; } else { str += '[P]' + '[' + this.tag + ']'; } return str; } function Nebula(id,opacity,name) { this.id = id; this.name = name; this.opacity = opacity; this.drawName = Nebula_drawName; this.drawTag = Nebula_drawTag; } function Nebula_drawName() { var str = '' + this.name + ''; return str; } function Nebula_drawTag() { var str = 'N/A'; return str; } //-------------------------------------------------------------------------------------- // SYSTEMS AND UNCHARTED SPACE //-------------------------------------------------------------------------------------- function System(id,x,y,md5,nebula,prot) { this.id = id; this.x = parseInt(x, 10); this.y = parseInt(y, 10); this.md5 = md5; this.nebula = nebula; this.protection = (prot != '0'); this.planets = new Array(); this.update = Math.round((new Date().getTime()) / 1000); this.drawPlanets = System_drawPlanets; this.drawAlliances = System_drawAlliances; } function System_drawPlanets() { var str = ''; var i; for (i=0;i'; } function System_drawAlliances() { var str = '
'; var i; for (i=0;i'; } function Uncharted() { this.md5 = '-'; this.update = Math.round((new Date().getTime()) / 1000); this.drawPlanets = Uncharted_getCode; this.drawAlliances = Uncharted_getCode; } function Uncharted_getCode() { var i, str = '
'; for (i=0;i<6;i++) str += ''; return str + '
'+uncharted+'
'; } //-------------------------------------------------------------------------------------- // THE MAP //-------------------------------------------------------------------------------------- function MapListItem(s,p,o) { this.system = s; this.planet = p; this.orbit = o; } function Map(mode) { this.systems = new Hashtable(); this.cx = false; this.cy = false; this.size = false; this.refresh = ''; this.mode = mode; this.list = new Array(); this.sortType = 1; this.sortDir = false; this.setParm = Map_setParm; this.checkUpdates = Map_checkUpdates; this.parse = Map_parse; this.clearCache = Map_clearCache; this.draw = Map_draw; this.mapGridBegin = Map_gridBegin; this.mapGridEnd = Map_gridEnd; this.mapListBegin = Map_listBegin; this.mapListEnd = Map_listEnd; this.mapList = Map_list; this.mapPlanets = Map_planets; this.mapAlliances = Map_alliances; } function Map_setParm(cx,cy,size) { if (cx == this.cx && cy == this.cy && size == this.size) return; this.cx = cx; this.cy = cy; this.size = size; var mod; mod = this.size % 2; this.minX = this.cx + (mod - this.size) / 2; this.maxX = this.cx + (this.size + mod - 2) / 2; this.minY = this.cy + (mod - this.size) / 2; this.maxY = this.cy + (this.size + mod - 2) / 2; this.checkUpdates(); } function Map_checkUpdates() { var i,j,as = new Array(), str; var now = Math.round((new Date().getTime()) / 1000); for (i=this.minX;i<=this.maxX;i++) for (j=this.minY;j<=this.maxY;j++) { var str = i + ',' + j, s = this.systems.get(str); if (!s) as.push(str + ','); else if (now - s.update > 60) { as.push(str + ',' + s.md5); s.update = now; } } this.refresh = as.join('#'); } function Map_clearCache() { var i, s, lk = this.systems.keys(), nk = 0; var now = Math.round((new Date().getTime()) / 1000); var Mx=this.maxX+4,mx=this.minX-4, My=this.maxY+4,my=this.minY-4; for (i=0;i=mx&&s.y<=My&&s.y>=my) || nk > 50 || now-s.update<300) { nk ++; continue; } this.systems.remove(lk[i]); } } function Map_parse(data) { var l = data.split('\n'); var st = 0, s, pdat, i; var mod = '#'; while (l.length) { i = l.shift(); if (st == 0) { var a = i.split('#'); if (a[0] == '') { s = new Uncharted(); } else { s = new System(a[0], a[1], a[2], a[3], a[4], a[5]); st = (a[4] > 0) ? 3 : 1; } this.systems.put(a[1] + "," + a[2], s); mod += a[1] + "," + a[2] + '#'; } else if (st == 1) { pdat = i.split('#'); st ++; } else if (st == 2) { s.planets.push(new Planet(s, pdat.shift(), i, pdat.shift(), pdat.shift(), pdat.join('#'))); st = (s.planets.length == 6) ? 0 : 1; } else if (st == 3) { pdat = i.split('#'); s.planets.push(new Nebula(pdat.shift(), pdat.shift(), pdat.join('#'))); if (s.planets.length == 6) st = 0; } } this.refresh = ""; } function Map_draw() { var str; switch (this.mode) { case 0: str = this.mapGridBegin() + this.mapPlanets() + this.mapGridEnd(); break; case 1: str = this.mapGridBegin() + this.mapAlliances() + this.mapGridEnd(); break; case 2: str = this.mapListBegin() + this.mapList() + this.mapListEnd(); break; } document.getElementById('jsmapint').innerHTML = str; } function Map_gridBegin() { var i, tmp = ""; tmp += ""; tmp += ""; tmp += ""; tmp += ""; for (i=this.minX;i<=this.maxX;i++) tmp += ""; tmp += ""; return tmp; } function Map_gridEnd() { var i, tmp = ""; for (i=this.minX;i<=this.maxX;i++) tmp += ""; tmp += ""; tmp += ""; tmp += ""; tmp += ""; tmp += "
 -X"; tmp += ""+dirs[0]+"X 
Y"+i+"Y
-Y "+i+" -Y
 -X"; tmp += ""+dirs[1]+"X 
"; return tmp; } function Map_planets() { var j, i, str = ''; for (j=this.maxY;j>=this.minY;j--) { str += ''; if (j == this.maxY) { str += ""; str += ""+dirs[2]+""; } str += ""+j+""; for (i=this.minX;i<=this.maxX;i++) str += '' + this.systems.get(i+','+j).drawPlanets() + ''; str += ""+j+""; if (j == this.maxY) { str += ""; str += ""+dirs[3]+""; } str += ''; } return str; } function Map_alliances() { var j, i, str = ''; for (j=this.maxY;j>=this.minY;j--) { str += ''; if (j == this.maxY) { str += ""; str += ""+dirs[2]+""; } str += ""+j+""; for (i=this.minX;i<=this.maxX;i++) str += '' + this.systems.get(i+','+j).drawAlliances() + ''; str += ""+j+""; if (j == this.maxY) { str += ""; str += ""+dirs[3]+""; } str += ''; } return str; } function Map_listBegin() { var hdTypes = ['Coord','Name','Alliance','Opacity','Protected']; var hdCols = ['', ' class="pname" colspan="2"', '', '', '']; var i,j,o; this.list = new Array(); for (i=this.minX;i<=this.maxX;i++) for (j=this.minY;j<=this.maxY;j++) { var s = this.systems.get(i+','+j); if (!s || !s.planets) continue; for (k=0;k<6;k++) this.list.push(new MapListItem(s,s.planets[k],k+1)); } var smet = 'this.list.sort(spl_' + hdTypes[this.sortType] + '_' + (this.sortDir ? "desc" : "asc") + ')'; eval(smet); var str = ''; if (hdNames.length == 4 && gameType != 1) { hdNames.push(lcHeader[gameType]); } for (i = 0; i < hdNames.length; i ++) { str += ''; if (this.sortType == i) { str += ''; } str += hdNames[i]; if (this.sortType == i) { str += '' + (this.sortDir ? dirs[0] : dirs[1]) + ''; } str += ''; } str += ''; return str; } function Map_listEnd() { return '
'; } function Map_list() { var i, str = ''; for (i = 0; i < this.list.length; i ++) { str += ','; str += this.list[i].orbit + ')'; str += ''; } else { str += ' class="pimg">[P]'; } } else { str += ' class="pname" colspan="2">'; } str += '' + this.list[i].planet.name + ''; if (this.list[i].system.nebula == 0) { str += '[' + this.list[i].planet.tag + ']'; } else { str += '-'; } str += '' + this.list[i].planet.opacity; if (gameType != 1) { str += '' + (this.list[i].system.protection == 0 ? yesNo[1] : yesNo[0]); } str += ''; } return str; } //-------------------------------------------------------------------------------------- // INITIALISATION AND GENERIC FUNCTIONS //-------------------------------------------------------------------------------------- function PlayerPlanet(id, name) { this.id = id; this.name = name; } function mapInit(data) { gameType = document.getElementById('gametype').innerHTML; // Receiving: mode followed by x#y#is_id#id_or_name followed by (id#name)* var l = data.split('\n'); var m = parseInt(l.shift(), 10); var a = l.shift().split('#'); plPlanets = new Array(); while (l.length > 0) { var p = l.shift().split('#'); plPlanets.push(new PlayerPlanet(p.shift(), p.join('#'))); } centreOn = (a[2] == 1) ? a[3] : ""; centreOnOwn = (a[2] == 0) ? a[3] : 0; map = new Map(m); map.setParm(parseInt(a[0],10),parseInt(a[1],10),5); drawMapLayout(); x_updateData(map.refresh, mapDataReceived); setTimeout('maintainMap()', 11000); } function drawMapLayout() { var str = " Help" + "
 "; document.getElementById('jsmap').innerHTML = "" + str + "
"; drawMapControls(); setTimeout('x_getPlayerPlanets(playerPlanetsReceived)', 300000); } function playerPlanetsReceived(data) { plPlanets = new Array(); if (data != "") { var l = data.split('\n'); while (l.length > 0) { var p = l.shift().split('#'); plPlanets.push(new PlayerPlanet(p.shift(), p.join('#'))); } } drawPlayerList(); setTimeout('x_getPlayerPlanets(playerPlanetsReceived)', 300000); } function mapDataReceived(data) { if (data != "") map.parse(data); map.draw(); inUpdate = false; } function maintainMap() { if (inUpdate) { setTimeout('maintainMap()', 500); return; } inUpdate = true; map.clearCache(); map.checkUpdates(); refreshMap(); setTimeout('maintainMap()', 11000); } function refreshMap() { if (map.refresh == '') { map.draw(); inUpdate = false; } else x_updateData(map.refresh, mapDataReceived); } function resultReceived(data) { if (data == "ERR") { inUpdate = false; return; } var a = data.split('#'); if (a[2] == 1) { centreOn = a[3]; centreMode = 2; document.getElementById('cm2').checked = true; document.getElementById('cp').value = centreOn; } else { centreOnOwn = a[3]; centreMode = 1; document.getElementById('cm1').checked = true; drawPlayerList(); } document.getElementById('cx').value = a[0]; document.getElementById('cy').value = a[1]; map.setParm(parseInt(a[0],10),parseInt(a[1],10),map.size); refreshMap(); } function drawPlayerList() { var i, str = ''; document.getElementById('plpl').innerHTML = str; } //-------------------------------------------------------------------------------------- // CONTROLS //-------------------------------------------------------------------------------------- function setSize(s) { if (inUpdate) { setTimeout('setSize('+s+')', 500); return; } inUpdate = true; map.setParm(map.cx,map.cy,parseInt(s,10)); refreshMap(); } function setMode(m) { if (inUpdate) { setTimeout('setMode("'+m+'")', 500); return; } inUpdate = true; map.mode = parseInt(m,10); map.draw(); inUpdate = false; } function moveUp() { if (inUpdate) { setTimeout('moveUp()', 50); return; } inUpdate = true; document.getElementById('cy').value = map.cy + 1; document.getElementById('cm0').checked = true; centreMode = 0; map.setParm(map.cx, map.cy+1, map.size); refreshMap(); } function moveDown() { if (inUpdate) { setTimeout('moveDown()', 50); return; } inUpdate = true; document.getElementById('cy').value = map.cy - 1; document.getElementById('cm0').checked = true; centreMode = 0; map.setParm(map.cx, map.cy-1, map.size); refreshMap(); } function moveLeft() { if (inUpdate) { setTimeout('moveLeft()', 50); return; } inUpdate = true; document.getElementById('cx').value = map.cx - 1; document.getElementById('cm0').checked = true; centreMode = 0; map.setParm(map.cx-1, map.cy, map.size); refreshMap(); } function moveRight() { if (inUpdate) { setTimeout('moveRight()', 50); return; } inUpdate = true; document.getElementById('cx').value = map.cx + 1; document.getElementById('cm0').checked = true; centreMode = 0; map.setParm(map.cx+1, map.cy, map.size); refreshMap(); } function manualUpdate() { if (inUpdate) { setTimeout('manualUpdate()', 500); return; } inUpdate = true; if (centreMode == 0) { var tx, nx, ty, ny; tx = document.getElementById('cx').value; ty = document.getElementById('cy').value; if (tx == '-0') tx = '0'; if (ty == '-0') ty = '0'; nx = parseInt(tx,10); ny = parseInt(ty,10); if (tx != nx.toString() || ty != ny.toString()) { inUpdate = false; return; } map.setParm(nx, ny, map.size); refreshMap(); } else if (centreMode == 1) { var i; for (i=0;ib.system.x); else if (a.system.y!=b.system.y) sup = (a.system.y>b.system.y); else sup = (a.orbit>b.orbit); return sup?1:-1; } function spl_Coord_desc(a,b) { var sup; if (a.system.x!=b.system.x) sup = (a.system.x>b.system.x); else if (a.system.y!=b.system.y) sup = (a.system.y>b.system.y); else sup = (a.orbit>b.orbit); return sup?-1:1; } function spl_Name_asc(a,b) { var ra = a.planet.name.toLowerCase(); var rb = b.planet.name.toLowerCase(); return (ra < rb) ? -1 : 1; } function spl_Name_desc(a,b) { var ra = a.planet.name.toLowerCase(); var rb = b.planet.name.toLowerCase(); return (ra > rb) ? -1 : 1; } function spl_Alliance_asc(a,b) { var ra, rb; ra = (a.system.nebula == 0) ? a.planet.tag.toLowerCase() : ""; rb = (b.system.nebula == 0) ? b.planet.tag.toLowerCase() : ""; return (ra < rb) ? -1 : (ra > rb ? 1 : spl_Name_asc(a,b)); } function spl_Alliance_desc(a,b) { var ra, rb; ra = (a.system.nebula == 0) ? a.planet.tag.toLowerCase() : ""; rb = (b.system.nebula == 0) ? b.planet.tag.toLowerCase() : ""; return (ra < rb) ? 1 : (ra > rb ? -1 : spl_Name_desc(a,b)); } function spl_Opacity_asc(a,b) { var ra = parseInt(a.planet.opacity, 10); var rb = parseInt(b.planet.opacity, 10); return (ra < rb) ? -1 : ((ra > rb) ? 1 : spl_Coord_asc(a,b)); } function spl_Opacity_desc(a,b) { var ra = parseInt(a.planet.opacity, 10); var rb = parseInt(b.planet.opacity, 10); return (ra < rb) ? 1 : ((ra > rb) ? -1 : spl_Coord_desc(a,b)); } function spl_Protected_asc(a,b) { var ra = a.system.protection, rb = b.system.protection; return (ra && !rb) ? -1 : ((rb && ! ra) ? 1 : spl_Coord_asc(a, b)); } function spl_Protected_desc(a,b) { var ra = a.system.protection, rb = b.system.protection; return (ra && !rb) ? 1 : ((rb && ! ra) ? -1 : spl_Coord_desc(a, b)); }