2011-03-22 02:34:45 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ClumpPlacer
|
2011-04-16 06:04:06 +02:00
|
|
|
//
|
|
|
|
// Class for generating a roughly circular clump of points
|
|
|
|
//
|
|
|
|
// size: The average number of points in the clump
|
|
|
|
// coherence: How much the radius of the clump varies (1.0 = circle, 0.0 = very random)
|
|
|
|
// smoothness: How smooth the border of the clump is (1.0 = few "peaks", 0.0 = very jagged)
|
|
|
|
// failfraction: Percentage of place attempts allowed to fail (optional)
|
|
|
|
// x, z: Tile coordinates of placer center (optional)
|
|
|
|
//
|
2011-03-22 02:34:45 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-16 06:04:06 +02:00
|
|
|
function ClumpPlacer(size, coherence, smoothness, failFraction, x, z)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
this.size = size;
|
|
|
|
this.coherence = coherence;
|
|
|
|
this.smoothness = smoothness;
|
|
|
|
this.failFraction = (failFraction !== undefined ? failFraction : 0);
|
|
|
|
this.x = (x !== undefined ? x : -1);
|
2011-04-16 06:04:06 +02:00
|
|
|
this.z = (z !== undefined ? z : -1);
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ClumpPlacer.prototype.place = function(constraint)
|
|
|
|
{
|
2011-03-28 00:00:49 +02:00
|
|
|
// Preliminary bounds check
|
2011-04-16 06:04:06 +02:00
|
|
|
if (!g_Map.validT(this.x, this.z) || !constraint.allows(this.x, this.z))
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-03-28 00:00:49 +02:00
|
|
|
return undefined;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var retVec = [];
|
|
|
|
|
|
|
|
var size = getMapSize();
|
|
|
|
var gotRet = new Array(size);
|
|
|
|
for (var i = 0; i < size; ++i)
|
|
|
|
{
|
2011-03-28 00:00:49 +02:00
|
|
|
gotRet[i] = new Uint8Array(size); // bool / uint8
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var radius = sqrt(this.size / PI);
|
|
|
|
var perim = 4 * radius * 2 * PI;
|
|
|
|
var intPerim = ceil(perim);
|
|
|
|
|
|
|
|
var ctrlPts = 1 + Math.floor(1.0/Math.max(this.smoothness,1.0/intPerim));
|
|
|
|
|
|
|
|
if (ctrlPts > radius * 2 * PI)
|
2011-04-16 06:04:06 +02:00
|
|
|
{
|
|
|
|
ctrlPts = Math.floor(radius * 2 * PI) + 1;
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-03-28 00:00:49 +02:00
|
|
|
var noise = new Float32Array(intPerim); //float32
|
2011-03-22 02:34:45 +01:00
|
|
|
var ctrlCoords = new Float32Array(ctrlPts+1); //float32
|
2011-03-28 00:00:49 +02:00
|
|
|
var ctrlVals = new Float32Array(ctrlPts+1); //float32
|
|
|
|
|
|
|
|
// Generate some interpolated noise
|
2011-03-22 02:34:45 +01:00
|
|
|
for (var i=0; i < ctrlPts; i++)
|
|
|
|
{
|
|
|
|
ctrlCoords[i] = i * perim / ctrlPts;
|
|
|
|
ctrlVals[i] = 2.0*randFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
var c = 0;
|
|
|
|
var looped = 0;
|
|
|
|
for (var i=0; i < intPerim; i++)
|
|
|
|
{
|
|
|
|
if (ctrlCoords[(c+1) % ctrlPts] < i && !looped)
|
|
|
|
{
|
|
|
|
c = (c+1) % ctrlPts;
|
|
|
|
if (c == ctrlPts-1)
|
|
|
|
looped = 1;
|
|
|
|
}
|
|
|
|
|
2011-03-28 00:00:49 +02:00
|
|
|
// Cubic interpolation of ctrlVals
|
2011-03-22 02:34:45 +01:00
|
|
|
var t = (i - ctrlCoords[c]) / ((looped ? perim : ctrlCoords[(c+1)%ctrlPts]) - ctrlCoords[c]);
|
|
|
|
var v0 = ctrlVals[(c+ctrlPts-1)%ctrlPts];
|
|
|
|
var v1 = ctrlVals[c];
|
|
|
|
var v2 = ctrlVals[(c+1)%ctrlPts];
|
|
|
|
var v3 = ctrlVals[(c+2)%ctrlPts];
|
|
|
|
var P = (v3 - v2) - (v0 - v1);
|
|
|
|
var Q = (v0 - v1) - P;
|
|
|
|
var R = v2 - v0;
|
|
|
|
var S = v1;
|
|
|
|
|
|
|
|
noise[i] = P*t*t*t + Q*t*t + R*t + S;
|
|
|
|
}
|
|
|
|
|
|
|
|
var failed = 0;
|
|
|
|
for (var p=0; p < intPerim; p++)
|
|
|
|
{
|
|
|
|
var th = 2 * PI * p / perim;
|
|
|
|
var r = radius * (1 + (1-this.coherence)*noise[p]);
|
|
|
|
var s = sin(th);
|
|
|
|
var c = cos(th);
|
|
|
|
var xx=this.x;
|
2011-04-16 06:04:06 +02:00
|
|
|
var yy=this.z;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
|
|
|
for (var k=0; k < ceil(r); k++)
|
|
|
|
{
|
|
|
|
var i = Math.floor(xx);
|
|
|
|
var j = Math.floor(yy);
|
|
|
|
if (g_Map.validT(i, j) && constraint.allows(i, j))
|
|
|
|
{
|
|
|
|
if (!gotRet[i][j])
|
|
|
|
{ // Only include each point once
|
|
|
|
gotRet[i][j] = 1;
|
2011-04-16 06:04:06 +02:00
|
|
|
retVec.push(new PointXZ(i, j));
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
xx += s;
|
|
|
|
yy += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((failed > this.size*this.failFraction) ? undefined : retVec);
|
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RectPlacer
|
2011-04-16 06:04:06 +02:00
|
|
|
//
|
|
|
|
// Class for generating a rectangular block of points
|
|
|
|
//
|
|
|
|
// x1,z1: Top left corner of block
|
|
|
|
// x2,z2: Bottom right corner of block
|
|
|
|
//
|
2011-03-22 02:34:45 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-16 06:04:06 +02:00
|
|
|
function RectPlacer(x1, z1, x2, z2)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
this.x1 = x1;
|
2011-04-16 06:04:06 +02:00
|
|
|
this.z1 = z1;
|
2011-03-22 02:34:45 +01:00
|
|
|
this.x2 = x2;
|
2011-04-16 06:04:06 +02:00
|
|
|
this.z2 = z2;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-16 06:04:06 +02:00
|
|
|
if (x1 > x2 || z1 > z2)
|
2011-03-22 02:34:45 +01:00
|
|
|
error("RectPlacer: incorrect bounds on rect");
|
|
|
|
}
|
|
|
|
|
|
|
|
RectPlacer.prototype.place = function(constraint)
|
|
|
|
{
|
2011-03-28 00:00:49 +02:00
|
|
|
// Preliminary bounds check
|
2011-04-16 06:04:06 +02:00
|
|
|
if (!g_Map.validT(this.x1, this.z1) || !constraint.allows(this.x1, this.z1) ||
|
|
|
|
!g_Map.validT(this.x2, this.z2) || !constraint.allows(this.x2, this.z2))
|
2011-03-28 00:00:49 +02:00
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2011-03-22 02:34:45 +01:00
|
|
|
var ret = [];
|
|
|
|
|
|
|
|
var x2 = this.x2;
|
2011-04-16 06:04:06 +02:00
|
|
|
var z2 = this.z2;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
|
|
|
for (var x=this.x1; x < x2; x++)
|
|
|
|
{
|
2011-04-16 06:04:06 +02:00
|
|
|
for (var z=this.z1; z < z2; z++)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-16 06:04:06 +02:00
|
|
|
if (g_Map.validT(x, z) && constraint.allows(x, z))
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-16 06:04:06 +02:00
|
|
|
ret.push(new PointXZ(x, z));
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ObjectGroupPlacer
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function ObjectGroupPlacer() {}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
2011-04-16 06:04:06 +02:00
|
|
|
// SimpleObject
|
|
|
|
//
|
|
|
|
// Class specifying a type of entity that can be placed on the map
|
|
|
|
//
|
|
|
|
// type: The entity's template name
|
|
|
|
// minCount,maxCount: The number of objects to place
|
|
|
|
// minDistance,maxDistance: The distance between placed objects
|
|
|
|
// minAngle,maxAngle: The variation in angle of placed objects (optional)
|
|
|
|
//
|
2011-03-22 02:34:45 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function SimpleObject(type, minCount, maxCount, minDistance, maxDistance, minAngle, maxAngle)
|
|
|
|
{
|
|
|
|
this.type = type;
|
|
|
|
this.minCount = minCount;
|
|
|
|
this.maxCount = maxCount;
|
|
|
|
this.minDistance = minDistance;
|
|
|
|
this.maxDistance = maxDistance;
|
|
|
|
this.minAngle = (minAngle !== undefined ? minAngle : 0);
|
|
|
|
this.maxAngle = (maxAngle !== undefined ? maxAngle : 2*PI);
|
|
|
|
|
|
|
|
if (minCount > maxCount)
|
|
|
|
error("SimpleObject: minCount must be less than or equal to maxCount");
|
|
|
|
|
|
|
|
if (minDistance > maxDistance)
|
|
|
|
error("SimpleObject: minDistance must be less than or equal to maxDistance");
|
|
|
|
|
|
|
|
if (minAngle > maxAngle)
|
|
|
|
error("SimpleObject: minAngle must be less than or equal to maxAngle");
|
|
|
|
}
|
|
|
|
|
2011-04-16 06:04:06 +02:00
|
|
|
SimpleObject.prototype.place = function(cx, cz, player, avoidSelf, constraint)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
var failCount = 0;
|
|
|
|
var count = randInt(this.minCount, this.maxCount);
|
|
|
|
var resultObjs = [];
|
|
|
|
|
|
|
|
for (var i=0; i < count; i++)
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
var distance = randFloat(this.minDistance, this.maxDistance);
|
|
|
|
var direction = randFloat(0, 2*PI);
|
|
|
|
|
|
|
|
var x = cx + 0.5 + distance*cos(direction);
|
2011-04-16 06:04:06 +02:00
|
|
|
var z = cz + 0.5 + distance*sin(direction);
|
2011-03-22 02:34:45 +01:00
|
|
|
var fail = false; // reset place failure flag
|
|
|
|
|
2011-04-16 06:04:06 +02:00
|
|
|
if (!g_Map.validT(x, z))
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (avoidSelf)
|
|
|
|
{
|
|
|
|
var length = resultObjs.length;
|
|
|
|
for (var i = 0; (i < length) && !fail; i++)
|
|
|
|
{
|
|
|
|
var dx = x - resultObjs[i].x;
|
2011-04-16 06:04:06 +02:00
|
|
|
var dy = z - resultObjs[i].z;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
|
|
|
if ((dx*dx + dy*dy) < 1)
|
|
|
|
{
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fail)
|
|
|
|
{
|
2011-04-16 06:04:06 +02:00
|
|
|
if (!constraint.allows(Math.floor(x), Math.floor(z)))
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // if we got here, we're good
|
|
|
|
var angle = randFloat(this.minAngle, this.maxAngle);
|
2011-04-16 06:04:06 +02:00
|
|
|
resultObjs.push(new Entity(this.type, player, x, z, angle));
|
2011-03-22 02:34:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
{
|
|
|
|
failCount++;
|
|
|
|
if (failCount > 20) // TODO: Make this adjustable
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultObjs;
|
|
|
|
};
|
|
|
|
|
2011-04-16 06:04:06 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SimpleGroup
|
|
|
|
//
|
|
|
|
// Class for placing groups of different objects
|
|
|
|
//
|
|
|
|
// elements: Array of SimpleObjects
|
|
|
|
// avoidSelf: Objects will not overlap
|
|
|
|
// tileClass: Optional tile class to add with these objects
|
|
|
|
// x,z: Tile coordinates of center of placer
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function SimpleGroup(elements, avoidSelf, tileClass, x, z)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
this.elements = elements;
|
2011-03-23 09:12:04 +01:00
|
|
|
this.tileClass = (tileClass !== undefined ? getTileClass(tileClass) : undefined);
|
2011-03-22 02:34:45 +01:00
|
|
|
this.avoidSelf = (avoidSelf !== undefined ? avoidSelf : false);
|
|
|
|
this.x = (x !== undefined ? x : -1);
|
2011-04-16 06:04:06 +02:00
|
|
|
this.z = (z !== undefined ? z : -1);
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleGroup.prototype.place = function(player, constraint)
|
|
|
|
{
|
|
|
|
var resultObjs = [];
|
|
|
|
|
|
|
|
// Try placement of objects
|
|
|
|
var length = this.elements.length;
|
2011-04-21 16:13:13 +02:00
|
|
|
for (var i = 0; i < length; i++)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-16 06:04:06 +02:00
|
|
|
var objs = this.elements[i].place(this.x, this.z, player, this.avoidSelf, constraint);
|
2011-03-22 02:34:45 +01:00
|
|
|
if (objs === undefined)
|
|
|
|
{ // Failure
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-21 16:13:13 +02:00
|
|
|
for (var j = 0; j < objs.length; ++j)
|
|
|
|
{
|
|
|
|
resultObjs.push(objs[j]);
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add placed objects to map
|
|
|
|
length = resultObjs.length;
|
|
|
|
for (var i=0; i < length; i++)
|
|
|
|
{
|
2011-04-21 16:13:13 +02:00
|
|
|
g_Map.addObject(resultObjs[i]);
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-03-23 09:12:04 +01:00
|
|
|
if (this.tileClass !== undefined)
|
2011-03-22 02:34:45 +01:00
|
|
|
{ // Round object position to integer
|
2011-04-16 06:04:06 +02:00
|
|
|
this.tileClass.add(Math.floor(resultObjs[i].tileX), Math.floor(resultObjs[i].tileZ));
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|