From 48514872f3898f91badb712c9eb0ea114abbc0b5 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Fri, 16 Mar 2012 16:58:16 +0000
Subject: [PATCH] Start of a random universe generator.  Currently creates a 3D
 model, sorts by distance from Sol (0,0,0) and generates warps based on
 distance to adjacent sectors.

---
 xtrn/tw2/bang.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
 create mode 100644 xtrn/tw2/bang.js

diff --git a/xtrn/tw2/bang.js b/xtrn/tw2/bang.js
new file mode 100644
index 0000000000..a4d8a36679
--- /dev/null
+++ b/xtrn/tw2/bang.js
@@ -0,0 +1,96 @@
+/*
+ * Generates a new universe...
+ *
+ * Sectors are solar systems... and are located in three dimensions.
+ * then scaled to the universe
+ *
+ */
+
+var sectors=1000;
+var universe=[];
+var ports=[];
+var planets=[];
+var i,j;
+var sol;
+var warpradius=1.5;
+
+function SectorDistance(other)
+{
+	return(Math.sqrt(
+		Math.pow(this.X-other.X,2)+
+		Math.pow(this.Y-other.Y,2)+
+		Math.pow(this.Z-other.Z,2)
+	));
+}
+
+function CubicSector(x,y,z)
+{
+	this.X=x;				// -1 to 1
+	this.Y=y;				// -1 to 1
+	this.Z=z;				// -1 to 1
+	this.R=Math.sqrt(			// Radius from earth (Arbitraries)
+		Math.pow(this.X,2)+
+		Math.pow(this.Y,2)+
+		Math.pow(this.Z,2)
+	);
+	this.Az=Math.atan2(this.Y,this.X);	// Azimuth from coreward (Radians)
+	this.Al=Math.acos(this.Z/this.R);	// Altitude from galactic plane (Radians)
+	this.Distance=SectorDistance;
+}
+
+function SphereSector(R, Al, Az)
+{
+	this.Az=Az;
+	this.Al=Al;
+	this.R=R;
+	this.X=this.R*Math.cos(Az)*Math.sin(Al);
+	this.Y=this.R*Math.sin(Al)*Math.sin(Az);
+	this.Z=this.R*Math.cos(Al);
+	this.Distance=SectorDistance;
+}
+
+var sol=new CubicSector(0,0,0);
+
+for(i=0; i<sectors; i++) {
+	if(i==0) {
+			universe.push(sol);
+	}
+	else {
+		var x,y,z;
+
+check:
+		do {
+			x=Math.random()*2-1;
+			y=Math.random()*2-1;
+			z=Math.random()*2-1;
+			for(j in universe) {
+				if(parseInt(universe[j].X*10000)==parseInt(x*10000)
+						&& parseInt(universe[j].Y*10000)==parseInt(y*10000)
+						&& parseInt(universe[j].Z*10000)==parseInt(z*10000)) {
+					log("Duplicate location");
+					continue check;
+				}
+			}
+			universe.push(new CubicSector(x,y,z));
+		} while(0);
+	}
+}
+
+universe.sort(function(a,b) { return(a.Distance(sol) - b.Distance(sol)) });
+var wr=Math.pow((2*2*2/sectors)/((4/3)*Math.PI),1/3)*warpradius;	// warpradius 1.5 times the average sector sphere.
+var totalwarps=0;
+for(i in universe) {
+	universe[i].Warps=[];
+	for(j in universe) {
+		if(i==j)
+			continue;
+		if(universe[i].Distance(universe[j]) <= wr) {
+			universe[i].Warps.push(j);
+			totalwarps++;
+		}
+	}
+	writeln(format("%f/%f/%f - %f",universe[i].X,universe[i].Y,universe[i].Z,universe[i].Distance(sol)));
+	writeln("  Warps: "+universe[i].Warps.join(', '));
+}
+log("Radius="+wr);
+log("Average warps per sector: "+(totalwarps/sectors));
-- 
GitLab