// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// swsg/control.js
//
// copyright (c) 2009-2010 drow <drow@bin.sh>
// all rights reserved.

var form="planet_form",name_rpc="/name/rpc.cgi",worldgen="/world/worldgen.cgi",seed=Math.floor(Math.random()*2147483647)+1,system={star:{table:{dice:"1d100","01-05":{dice:"1d100","01":{dice:"1d100","01":"O1d10-1 Ia Blue Supergiant","02-20":"O1d10-1 Ia Blue-white Supergiant","21-40":"A1d10-1 Ia White Supergiant","41-95":"K1d10-1 Ia Orange Supergiant","96-00":"M1d10-1 Ia Red Supergiant"},"02-03":{dice:"1d100","01":"O1d10-1 Ib Blue Supergiant","02-20":"B1d10-1 Ib Blue-white Supergiant","21-40":"A1d10-1 Ib White Supergiant",
"41-95":"K1d10-1 Ib Orange Supergiant","96-00":"M1d10-1 Ib Red Supergiant"},"04-07":{dice:"1d100","01-20":"B1d10-1 II Blue-white Giant","21-40":"A1d10-1 II White Giant","41-95":"K1d10-1 II Orange Giant","96-00":"M1d10-1 II Red Giant"},"08-31":{dice:"1d100","01-20":"B1d10-1 III Blue-white Giant","21-40":"A1d10-1 III White Giant","41-80":"K1d10-1 III Orange Giant","81-00":"M1d10-1 III Red Giant"},"32-00":{dice:"1d100","01-20":"B1d10-1 IV Blue-white Subgiant","21-40":"A1d10-1 IV White Subgiant","41-60":"K1d10-1 IV Orange Subgiant",
"61-00":"M1d10-1 IV Red Subgiant"}},"06-95":{dice:"1d100","01":"B1d10-1 V Blue-white Main Sequence","02-20":"A1d10-1 V White Main Sequence","21-40":"F1d10-1 V Yellow-white Main Sequence","41-70":"G1d10-1 V Yellow Main Sequence","71-90":"K1d10-1 V Orange Main Sequence","91-00":"M1d10-1 V Red Dwarf"},"96-00":{dice:"1d100","01-20":"DA1d9 White Dwarf","21-40":"DB1d9 White Dwarf","41-60":"DQ1d9 White Dwarf","61-80":"DZ1d9 White Dwarf","81-00":"DC1d9 White Dwarf"}}},jovian_icon:{table:{dice:"1d6","1-3":"jupiter",
"4-6":"saturn"}}},planet={keys:["function","government","type","terrain","temperature","gravity","atmosphere","hydrosphere","day","year","starport","population","tech_level"],display:["type","terrain","temperature","gravity","atmosphere","hydrosphere","day","year","starport","population","function","government","tech_level"],"function":{title:"Function:",table:{dice:"d6pct","11":"abandoned_colony","12":"academic","13":"administrative","14-21":"agriculture","22":"colony","23":"disaster","24":"entertainment",
"25-26":"exploration","31":"hidden_base","32-33":"homeworld","34":"luxury_goods","35-41":{dice:"1d6","1-2":"manufacturing_low","3-4":"manufacturing_mid","5-6":"manufacturing_high"},"42":"military","43-46":"mining","51-55":"natural_resources","56":"research","61":"service","62-63":"subsistence","64-66":"trade"},data:{abandoned_colony:{title:"Abandoned Colony"},academic:{title:"Academic",mod:{starport:1,tech_level:1}},administrative:{title:"Administrative/Government",mod:{starport:1,tech_level:1}},
agriculture:{title:"Agriculture",incom:["asteroid_belt","artificial","floating_city","space_station","barren"]},colony:{title:"Colony"},disaster:{title:"Disaster",mod:{atmosphere:3,starport:-3,tech_level:-2}},entertainment:{title:"Entertainment"},exploration:{title:"Exploration",mod:{starport:-2,tech_level:-2}},hidden_base:{title:"Hidden Base"},homeworld:{title:"Homeworld"},luxury_goods:{title:"Luxury Goods"},manufacturing_low:{title:"Manufacturing/Processing (Low Tech)"},manufacturing_mid:{title:"Manufacturing/Processing (Mid Tech)",
mod:{starport:2,tech_level:2}},manufacturing_high:{title:"Manufacturing/Processing (High Tech)",mod:{starport:3,tech_level:3}},military:{title:"Military",mod:{starport:3,tech_level:2}},mining:{title:"Mining",mod:{starport:2,tech_level:1}},natural_resources:{title:"Natural Resources"},research:{title:"Research"},service:{title:"Service",mod:{starport:1,tech_level:2}},subsistence:{title:"Subsistence"},trade:{title:"Trade",mod:{starport:3,tech_level:2}}}},government:{title:"Government:",table:{dice:"d6pct",
"11":"alliance","12":"anarchy","13-16":"competing_states","21-22":"corporate_owned","23-24":"dictatorship","25":"family","26-31":"feudalism","32":"guild","33-42":"imperial_governor","43-45":"govt_military","46-52":"monarchy","53":"organized_crime","54":"participatory","55":"rebel_alliance","56":"representative","61":"selection","62":"theocracy","63-66":"tribal"},data:{alliance:{title:"Alliance/Federation"},anarchy:{title:"Anarchy"},competing_states:{title:"Competing States"},corporate_owned:{title:"Corporate Owned",
mod:{starport:3,tech_level:2}},dictatorship:{title:"Dictatorship"},family:{title:"Family"},feudalism:{title:"Feudalism"},guild:{title:"Guild/Professional Organizations"},imperial_governor:{title:"Imperial Governor"},govt_military:{title:"Military"},monarchy:{title:"Monarchy"},organized_crime:{title:"Organized Crime"},participatory:{title:"Participatory Democracy"},rebel_alliance:{title:"Rebel Alliance"},representative:{title:"Representative Democracy"},selection:{title:"Ruler by Selection/Rite"},
theocracy:{title:"Theocracy"},tribal:{title:"Tribal"}}},type:{title:"Type:",table:{dice:"2d6","2-9":"terrestrial","10":"satellite","11":"asteroid_belt","12":{dice:"1d6","1-2":"artificial","3-4":"floating_city","5-6":"space_station"}},data:{terrestrial:{title:"Terrestrial"},satellite:{title:"Satellite"},asteroid_belt:{title:"Asteroid Belt",mod:{population:-2},incom:["stone","feudal","industrial","terrain","temperature","gravity","atmosphere","hydrosphere","day","year"],worldgen:[{image:"asteroid,drakath.jpg",
byline:"Asteroid by",credit:"Armin Schieb",link:"http://drakath-terracube.blogspot.com/"},{image:"asteroid,shoofly-stock.jpg",byline:"Space Station model by",credit:"Shoofly Stock",link:{url_fmt:"deviantart",user:"shoofly-stock"}},{image:"offworld,bruno_werneck.jpg",credit:[{byline:"Illustration by",credit:"Bruno Werneck",link:"http://www.brunowerneck.com/"},{credit:"Posthuman Studios, LLC",link:"http://eclipsephase.com/"}]},{image:"outpost,digitalblasphemy.jpg",byline:"Outpost by",credit:"Ryan Bliss, Digital Blasphemy",
link:"http://digitalblasphemy.com/"}]},artificial:{title:"Artificial",mod:{population:-2},incom:["stone","feudal","industrial","atomic","terrain","temperature","gravity","atmosphere","hydrosphere","day","year"],worldgen:[{image:"allied_base,skynet3020.jpg",byline:"Allied Fleets Base by",credit:"Skynet3020",link:{url_fmt:"r-gallery",id:"513664"}},{image:"foghar,sigmaworld.jpg",byline:"Planet Foghar: Colony 7C of the Thulaner by",credit:"SIGMAWORLD",link:{url_fmt:"renderosity",id:"452776"}},{image:"industrial_rapsody,alexwild.jpg",
byline:"Industrial Rapsody by",credit:"Alex Wild",link:{url_fmt:"deviantart",user:"alexwild"}},{image:"olympus,paul_davies.jpg",credit:[{byline:"Olympus by",credit:"Paul Davies",link:"http://www.pdportraits.co.uk/"},{credit:"Posthuman Studios, LLC",link:"http://eclipsephase.com/"}]},{image:"winter_outpost,skynet3020.jpg",byline:"Outpost in Winter Wasteland by",credit:"Skynet3020",link:{url_fmt:"r-gallery",id:"513664"}}]},floating_city:{title:"Artificial",mod:{population:-2},incom:["stone","feudal",
"industrial","atomic","terrain","temperature","gravity","atmosphere","hydrosphere","day","year"],worldgen:[{image:"aerostat,paul_davies.jpg",credit:[{byline:"Venus Aerostat by",credit:"Paul Davies",link:"http://www.pdportraits.co.uk/"},{credit:"Posthuman Studios, LLC",link:"http://eclipsephase.com/"}]},{image:"biodome,digitalblasphemy.jpg",byline:"Biodome by",credit:"Ryan Bliss, Digital Blasphemy",link:"http://digitalblasphemy.com/"},{image:"cloud_city.jpg",byline:"Cloud City from",credit:"The Empire Strikes Back, &copy; Lucasfilm Ltd."},
{image:"cloud_city,jfliesenborghs.jpg",byline:"Cloud City by",credit:"Jean-Francois Liesenborghs",link:{url_fmt:"deviantart",user:"jfliesenborghs"}},{image:"gothic,jfliesenborghs.jpg",byline:"Gothic by",credit:"Jean-Francois Liesenborghs",link:{url_fmt:"deviantart",user:"jfliesenborghs"}}]},space_station:{title:"Artificial",mod:{population:-2},incom:["stone","feudal","industrial","atomic","terrain","temperature","gravity","atmosphere","hydrosphere","day","year"],worldgen:[{image:"aziath_station,madaboutgames.jpg",
byline:"Aziath Space Station by",credit:"Shaun Williams",link:{url_fmt:"r-gallery",id:"585204"}},{image:"frontier,the_customs,alexwild.jpg",byline:"Frontier: The Customs by",credit:"Alex Wild",link:{url_fmt:"deviantart",user:"alexwild"}},{image:"space_city,tlbklaus.jpg",byline:"Space City by",credit:"TLBKlaus",link:{url_fmt:"deviantart",user:"tlbklaus"}},{image:"space_station,bruno_werneck.jpg",credit:[{byline:"Illustraton by",credit:"Bruno Werneck",link:"http://www.brunowerneck.com/"},{credit:"Posthuman Studios, LLC",
link:"http://eclipsephase.com/"}]},{image:"the_hole,jfliesenborghs.jpg",byline:"The Hole by",credit:"Jean-Francois Liesenborghs",link:{url_fmt:"deviantart",user:"jfliesenborghs"}}]}}},terrain:{title:"Terrain:",table:{dice:"d6pct","11":"barren","12-13":"cave","14":"crater_field","15-16":"desert","21-24":"forest","25-26":"glacier","31-32":"jungle","33-34":"mountain","35-41":"ocean","42-44":"plain","45-46":"plateau","51-52":"urban","53-61":"wetlands","62-63":"volcanic","64-66":"special"},data:{barren:{title:"Barren",
mod:{atmosphere:3,population:-2},incom:["moist","saturated"],worldgen:{palette:"Barren"}},cave:{title:"Cave",mod:{atmosphere:2,population:-2}},crater_field:{title:"Crater Field"},desert:{title:"Desert",incom:["moist","saturated"],worldgen:{palette:"Barren"}},forest:{title:"Forest",incom:["searing","frigid","arid"]},glacier:{title:"Glacier",incom:["searing","hot","temperate","arid","dry"]},jungle:{title:"Jungle",incom:["cool","frigid","arid","dry"]},mountain:{title:"Mountain",incom:["saturated"]},
ocean:{title:"Ocean",mod:{population:-1},incom:["arid","dry"]},plain:{title:"Plain",incom:["searing","frigid","arid","saturated"]},plateau:{title:"Plateau",incom:["saturated"]},urban:{title:"Urban",mod:{population:1}},wetlands:{title:"Wetlands",incom:["searing","frigid","arid","dry"]},volcanic:{title:"Volcanic",mod:{atmosphere:8,population:-2}},special:{title:"Special"}}},temperature:{title:"Temperature:",table:{dice:"2d6","2":"searing","3-4":"hot","5-9":"temperate","10-11":"cool","12":"frigid"},
data:{searing:{title:"Searing",worldgen:{pct_ice:0}},hot:{title:"Hot",worldgen:{pct_ice:0}},temperate:{title:"Temperate",worldgen:{pct_ice:10}},cool:{title:"Cool",worldgen:{pct_ice:25}},frigid:{title:"Frigid",worldgen:{pct_ice:90}}}},gravity:{title:"Gravity:",table:{dice:"2d6","2":"zero_gravity","3-4":"light","5-11":"standard","12":"heavy"},data:{zero_gravity:{title:"Zero Gravity"},light:{title:"Light",mod:{atmosphere:2}},standard:{title:"Standard"},heavy:{title:"Heavy",mod:{atmosphere:2}}}},atmosphere:{title:"Atmosphere:",
table:{dice:"2d6","2":"none","3-9":"type_i","10":"type_ii","11":"type_iii","12":"type_iv"},data:{none:{title:"None"},type_i:{title:"Type I (Breathable)"},type_ii:{title:"Type II (Breath Mask Suggested)"},type_iii:{title:"Type III (Breath Mask Required)"},type_iv:{title:"Type IV (Environment Suit Required)"}}},hydrosphere:{title:"Hydrosphere:",table:{dice:"2d6","2":"arid","3-4":"dry","5-9":"moderate","10-11":"moist","12":"saturated"},data:{arid:{title:"Arid",worldgen:{pct_water:10}},dry:{title:"Dry",
worldgen:{pct_water:33}},moderate:{title:"Moderate",worldgen:{pct_water:66}},moist:{title:"Moist",worldgen:{pct_water:90}},saturated:{title:"Saturated",worldgen:{pct_water:95}}}},day:{title:"Length of Day:",table:{dice:"1d6","1-2":"2d6 + 10 hours","3-4":"1d6 + 20 hours","5":"1d6 + 25 hours","6":"1d6 + 30 hours"}},year:{title:"Length of Year:",table:{dice:"1d6","1":"75 + 1d6 * 15 days","2":"150 + 1d6 * 15 days","3-4":"225 + 1d6 * 15 days","5":"300 + 1d6 * 15 days","6":"375 + 1d6 * 15 days"}},starport:{title:"Starport:",
table:{dice:"2d6","2":"landing_field","3-5":"limited_services","6-8":"standard_class","9-11":"stellar_class","12":"imperial_class"},data:{landing_field:{title:"Landing Field"},limited_services:{title:"Limited Services"},standard_class:{title:"Standard Class"},stellar_class:{title:"Stellar Class"},imperial_class:{title:"Imperial Class"}}},population:{title:"Population:",table:{dice:"1d6","1":{dice:"1d6","1-2":"1d9","3-4":"1d9 * 10","5-6":"1d9 * 100"},"2-3":{dice:"1d6","1-2":"1d9 thousand","3-4":"(1d89 + 10) thousand",
"5-6":"(1d89 + 10) * 10 thousand"},"4-5":{dice:"1d6","1-2":"(1d89 + 10) / 10 million","3-4":"(1d89 + 10) million ","5-6":"(1d89 + 10) * 10 million"},"6":{dice:"1d6","1-2":"(1d89 + 10) / 10 billion","3-4":"(1d89 + 10) billion ","5-6":"(1d89 + 10) * 10 billion"}}},tech_level:{title:"Tech Level:",table:{dice:"2d6","2":"stone","3":"feudal","4":"industrial","5":"atomic","6-7":"information","8-12":"space"},data:{stone:{title:"Stone"},feudal:{title:"Feudal"},industrial:{title:"Industrial"},atomic:{title:"Atomic"},
information:{title:"Information"},space:{title:"Space"}}},trade_goods:{table:{dice:"1d8","1":"low_technology","2":"mid_technology","3":"high_technology","4":"metals","5":"minerals","6":"luxury_goods","7":"foodstuffs","8":"medicinal_goods"}},mod_list:{},mod:{},inc_list:{},incom:[]},roman=["I","II","III","IV","V","VI","VII","VIII","IX","X"],station=["Base","City","Station"],wdc=["O","O","B","B","A","A","F","F","G","G"],fmt={stat:'<tr><td class="section">#{title}</td><td class="value"><b>#{value}</b></td><td class="ctrl">#{control}</td></tr>',
desc:'<tr><td rowspan="2" class="section">#{title}</td><td class="value"><b>#{value}</b></td><td rowspan="2" class="ctrl">#{control}</td></tr><tr><td class="value">#{desc}</td></tr>',new_stat:'<input type="image" src="/icons/roll.gif" onclick="new_stat(\'#{key}\');" />',icon_path:"/scifi/system/#{type}/#{name}.jpg",span:'<span id="#{id}">#{text}</span>',image_path:"/scifi/swsg/images/#{type}/#{image}",img:'<img src="#{src}" alt="" />',link_out:'<a href="#{href}" target="out">#{text}</a>'};
Object.keys(fmt).each(function(a){fmt[a]=new Template(fmt[a])});var url_fmt={deviantart:"http://#{user}.deviantart.com/",renderosity:"http://www.renderosity.com/homepage.php?userid=#{id}","r-gallery":"http://www.renderosity.com/homepage.php?page=3&userid=#{id}"};Object.keys(url_fmt).each(function(a){url_fmt[a]=new Template(url_fmt[a])});function init_form(){client_form(form);get_name();update_planet()}
function get_name(){var a={type:"Star Wars Planet",n:1};a={method:"get",parameters:a,onSuccess:function(b){got_name(b.responseText)},onFailure:function(b){rpc_error(b.statusText)},onException:function(){rpc_error("System Error")}};new Ajax.Request(name_rpc,a)}function got_name(a){system.name=a;update_name()}function rpc_error(a){$("system_name").update(a)}
function update_planet(){var a={};planet.keys.each(function(c){if(compat(c))a[c]=planet_stat(c)});var b="";planet.display.each(function(c){if(a[c])b+=a[c]});$("planet").update(b);system.name&&update_name();worldgen&&update_map();if(planet.type.sel!=system.planet_type){new_system();system.planet_type=planet.type.sel}}
function planet_stat(a){var b;b=planet[a].exact?planet[a].exact:select(a);var c;if(planet[a].data&&planet[a].data[b]){c=planet[a].data[b];set_mod(a,c)}c=c&&c.title?c.title:b;b=$(b)?$(b).innerHTML:"";return fmt_row({title:planet[a].title,value:c,desc:b,new_stat:true,key:a})}
function select(a){for(var b=planet[a].table,c;!c;){if(!planet[a].roll)planet[a].roll=sel_roll(b.dice);var d=planet[a].roll;if(planet.mod&&planet.mod[a])d=add_mod(b.dice,d,planet.mod[a]);d=select_from(b,d);if(compat(d))c=d;else planet[a].roll=0}if(/\d+d\d+/.exec(c)){c=roll_dice(c);planet[a].exact=c}return planet[a].sel=c}function sel_roll(a){return a=="d6pct"?rd_dice(1,6)*10+rd_dice(1,6):parseInt(roll_dice(a))}
function add_mod(a,b,c){if(a=="d6pct"){b=(Math.floor(b/10)-1)*6+b%10;b+=c;if(b<1)b=1;if(b>36)b=36;b=(Math.floor((b-1)/6)+1)*10+(b-1)%6+1}else if(m=/(\d+)d(\d+)/.exec(a)){a=parseInt(m[1]);var d=parseInt(m[2]);d=a*d;b+=c;if(b<a)b=a;if(b>d)b=d}return b}
function select_from(a,b){b||(b=sel_roll(a.dice));var c;Object.keys(a).each(function(d){if(match=/(\d+)-(\d+)/.exec(d)){var g=parseInt(match[1]),f=match[2]=="00"?100:parseInt(match[2]);if(b>=g&&b<=f)c=a[d]}else if(match=/(\d+)/.exec(d)){g=parseInt(match[1]);if(b==g)c=a[d]}});if(c.dice)c=select_from(c);return c}function compat(a){return!(planet.incom&&planet.incom.include(a))}
function set_mod(a,b){if(b.mod)planet.mod_list[a]=b.mod;else planet.mod_list[a]&&delete planet.mod_list[a];var c={};Object.keys(planet.mod_list).each(function(g){Object.keys(planet.mod_list[g]).each(function(f){if(c[f])c[f]+=planet.mod_list[g][f];else c[f]=planet.mod_list[g][f]})});planet.mod=c;if(b.incom)planet.inc_list[a]=b.incom;else planet.inc_list[a]&&delete planet.inc_list[a];var d=[];Object.keys(planet.inc_list).each(function(g){planet.inc_list[g].each(function(f){d.push(f)})});planet.incom=
d}function fmt_row(a){a.control=a.new_stat?eval_fmt("new_stat",a):"";return a=a.desc?eval_fmt("desc",a):eval_fmt("stat",a)}function eval_fmt(a,b){return fmt[a].evaluate(b)}
function update_name(){$("system_name").update(system.name);var a;if(planet.type.sel=="terrestrial"){planet.name=system.name;a=planet.name}else if(planet.type.sel=="satellite"){planet.name=system.name+" "+roman[seed%10];a=system.name}else if(planet.type.sel=="asteroid_belt"){planet.name=system.name+" Belt";a=planet.name}else{planet.name=planet.type.sel=="artificial"?system.name+" "+station[seed%3]:planet.type.sel=="floating_city"?system.name+" "+station[seed%2+1]:planet.type.sel=="space_station"?
system.name+" "+station[seed%2+1]:system.name;a=system.name}$("planet_name").update(planet.name);$("planet_marker")&&$("planet_marker").update(a)}
function update_map(){var a={};planet.keys.each(function(d){var g=planet[d].sel;if(planet[d].data&&planet[d].data[g]){d=planet[d].data[g];if(map_proto=d.worldgen){if(Object.isArray(map_proto)){d=map_proto.length;d=Math.floor(Math.random()*d);map_proto=map_proto[d]}Object.keys(map_proto).each(function(f){a[f]=map_proto[f]})}}});if(a.disable){$("planet_map").update("");$("image_credit").update("")}else if(a.image){var b=build_img(image_path(planet.type.sel,a.image));$("planet_map").update(b);if(a.credit){b=
build_credit(a);$("image_credit").update(b)}}else{var c={projection:"Square",height:400,palette:"Alternate",seed:seed,iter:5E3,pct_water:50,pct_ice:10,rotate:0};Object.keys(a).each(function(d){c[d]=a[d]});b=$H(c).toQueryString();b=build_img(worldgen+"?"+b);$("planet_map").update(b);$("image_credit").update($("fwg_credit").innerHTML)}}function image_path(a,b){return eval_fmt("image_path",{type:a,image:b})}function build_img(a){return eval_fmt("img",{src:a})}
function build_credit(a){if(Object.isArray(a.credit))return a.credit.map(build_credit).join("<br />");else{text=a.credit;if(a.link)text=build_link(a.link,text);if(a.byline)text=a.byline+" "+text;return text}}function build_link(a,b){if(fmt_id=a.url_fmt)a=url_fmt[fmt_id].evaluate(a);return eval_fmt("link_out",{href:a,text:b})}
function new_system(){var a=new_star();a=fmt_row({title:a.icon,value:a.type});var b=seed%8+1,c=seed%Math.floor(b/2),d=Math.floor(b*2/3),g={type:"Asteroid Belt",icon:system_icon("worlds","gaspra")},f=Math.floor(Math.random()*6);for(i=0;i<c;i++){for(var h=0;!h;){var e=Math.floor((Math.random()*9+f)/2);e==5&&f==5||(h=e)}e=h<5?new_planet({i:i,realm:"inner"}):h==5?g:new_jovian({i:i,realm:"inner"});a+=fmt_row({title:e.icon,value:e.type,desc:e.desc});f=h}if(planet.type.sel=="terrestrial"){e=new_planet({realm:"inner",
icon:"earth"});e.name=planet.name?planet.name:""}else if(planet.type.sel=="satellite"){e=new_jovian({realm:"inner",primary:1});e.name=system.name?system.name:""}else if(planet.type.sel=="asteroid_belt"){e=g;e.name=planet.name?planet.name:"";desc=""}else if(planet.type.sel=="artificial"){e=new_planet({realm:"inner"});e.name=system.name?system.name:""}else if(planet.type.sel=="floating_city"){e=new_jovian({realm:"inner"});e.name=system.name?system.name:""}else if(planet.type.sel=="space_station"){h=
Math.floor((Math.random()*9+f)/2);e=h<5?new_planet({realm:"inner"}):new_jovian({realm:"inner"});e.name=system.name?system.name:""}e.name=eval_fmt("span",{id:"planet_marker",text:e.name});a+=fmt_row({title:e.icon,value:e.name,desc:e.desc});for(i=c+1;i<b;i++){c=i<d?"inner":"outer";for(h=0;!h;){e=Math.floor((Math.random()*9+f)/2);e==5&&f==5||(h=e)}e=h<5?new_planet({i:i,realm:c}):h==5?g:new_jovian({i:i,realm:c});a+=fmt_row({title:e.icon,value:e.type,desc:e.desc});f=h}$("system").update(a)}
function new_star(){var a=select_from(system.star.table);if(/\d+d\d+/.exec(a))a=roll_dice(a);var b;if(match=/^([OBAFGKM])/.exec(a))b=match[1];else if(match=/^D[A-Z]+(\d+)/.exec(a))b=wdc[parseInt(match[1])];return{type:a,"class":b,icon:system_icon("stars",b)}}
function new_planet(a){if(a.icon)a.icon=system_icon("worlds",a.icon);if(a.realm=="inner"){if(!a.type)a.type="Rock Planet";if(!a.icon)a.icon=system_icon("worlds","tethys")}else{if(!a.type)a.type="Ice Planet";if(!a.icon)a.icon=system_icon("worlds","enceladus")}if(!a.desc){var b=[];if(string=get_moons(5,2,"moon"))b.push(string);a.desc=b.join(", ")}return a}
function new_jovian(a){if(a.icon)a.icon=system_icon("worlds",a.icon);if(a.realm=="inner"){if(!a.type)a.type="Jovian Planet";if(!a.icon){var b=select_from(system.jovian_icon.table);a.icon=system_icon("worlds",b)}}else{if(!a.type)a.type="Jovian Planet";if(!a.icon)a.icon=system_icon("worlds","neptune")}if(!a.desc){b=[];if(string=get_moons(80,19,"small moon"))b.push(string);if(string=get_moons(20,4,"large moon"))b.push(string);a.primary&&b.push("1 terrestrial moon");a.desc=b.join(", ")}return a}
function system_icon(a,b){return eval_fmt("img",{src:icon_path(a,b)})}function icon_path(a,b){return eval_fmt("icon_path",{type:a,name:b})}function get_moons(a,b,c){a=Math.floor(Math.random()*a)-b;b="";if(a>0)b+=a+" "+c;if(a>1)b+="s";return b}function new_stat(a){planet[a].roll=0;planet[a].exact="";set_mod("new_stat",{incom:[planet[a].sel]});update_planet()}document.observe("dom:loaded",init_form);
