Game Development Community

Resource generation tweak

by James Novosel · in RTS Starter Kit · 01/04/2005 (2:16 pm) · 4 replies

I started an implementation of a teleport pad for the RTS kit. Not wanting to reinvent the wheel I decided to clone some code from the resourceGeneration.cs. The teleport code worked fine except that the RTSUnit being teleported would teleport outside the missionarea. It was then that I noticed that the randomly generated trees were also appearing outside of the missionarea, specifically they seem to appear to be to the north and east of the missionarea box. They also appeared to be not really random but in straight rows. Further investigation lead me to the following tweaks to function seedResource and function getTerrainLevel in resourceGeneration.cs.

Changes/tweaks are blocked with "// jwn tweak ----------------------------"
Commented lines noted with "//jwn"

The main change involves calculating the maxX and maxY allowable coordinates of the missionArea.

All randomly generared items are truly random and within the missionarea with this tweak/change.

Note - the code presented is from the original resource generation code posted in the RTS forum. This is not from the RM_BA_Villager resource (although I think it uses the same code with minor changes to generate rocks as well as trees).

My thanks for both of these resources !

Hope this helps if anyone else has noticed the resources being outside the missionarea. If anyone encouters problems with this tweak or sees a more efficient way to do this please post.

(Sorry for the text wrapping, if there is a better way to post code let me know).
( Code follows in next post)

#1
01/04/2005 (2:17 pm)
Function seedResource(%type, %seed){
%minX = getWord(MissionArea.Area, 0);
%minY = getWord(MissionArea.Area, 1);
%maxX = getWord(MissionArea.Area, 2);
%maxY = getWord(MissionArea.Area, 3);
//echo(MissionArea.Area);
for(%i = %minX; %i < %maxX; %i +=%seed){
for(%j = %minY; %j < %maxY; %j +=%seed){
%rd = getRandom(%seed);
//even odd chance to spawn
if((%rd % 2) == 0){

// jwn tweak ----------------------------------------------------------------
// jwn elim %z = getWord(getTerrainLevel(%i SPC %j SPC 500, 1), 2);
// jwn - use the position returned instead
%z = getTerrainLevel(%i SPC %j SPC 500, 1);
// jwn tweak ----------------------------------------------------------------

%resource = new StaticShape() {
scale = "0.25 0.25 0.25";
dataBlock = %type;
};

// jwn tweak ----------------------------------------------------------------
// jwn elim %resource.setTransform( %i SPC %j SPC %z SPC"0 0 0.5 3.14159" );
// jwn use the position returned %z
%resource.setTransform(%z SPC "0 0 0.5 3.14159" );
// jwn tweak ----------------------------------------------------------------


// MissionGroup.add(%resource);
ResourceSet.add(%resource);
//for(%k = 0; %k < ClientGroup.getCount(); %k++ ){
// %cl = ClientGroup.getObject(%k);
// %resource.scopeToClient(%cl);
//}
}
}
}
}

(More code follows in next post)
#2
01/04/2005 (2:18 pm)
Function getTerrainLevel(%pos,%rad)
{
// jwn tweak ----------------------------------------------------------------
// MissionArea.Area = -210 -145 310 320
// For true random generation we need to get the min and max allowable X coordinates
// and the min and max allowable Y coordinates from -210 -145 310 320, then use random
// numbers within these ranges.
// The minX and minY are given as -210 and -145 and represent the lower left coordinate
// of the missionarea.
// The maxX and maxY are not given (they are not 310 and 320). Positioning objects near
// the upper right coordinates of the missionarea editor yielded coordinates of approx 100,171
//
// The maxX and maxY can be roughly approximated using:
// maxX = maxX - abs(minY) = 310 - abs(-210) = 100
// maxY = maxY - abs(minX) = 320 - abs(-145) = 175
//
// So, we need random X coordinate >= -210 and <= 100 and
// random Y coordinate >= -145 and <= 175.
//
// begin add jwn
%minX = getWord(MissionArea.Area, 0);
%minY = getWord(MissionArea.Area, 1);
%maxX = getWord(MissionArea.Area, 2);
%maxY = getWord(MissionArea.Area, 3);

%minX2 = 0;
%minY2 = 0;

// jwn in lieu of abs value function ?
if(%minX < 0)
{
%minX2 = %minX * -1;
}
if(%minY < 0)
{
%minY2 = %minY * -1;
}

%maxX = %maxX - %minX2;
%maxY = %maxY - %minY2;
// end add jwn
// jwn tweak ----------------------------------------------------------------



while(%retries < 1000)
{

// jwn tweak ----------------------------------------------------------------
// begin add jwn - get an x/y coordinate within the missionarea
//
// end add jwn

// jwn elim: %x = getWord(%pos, 0) + mFloor(getRandom(%rad * 2) - %rad);
// jwn elim: %y = getWord(%pos, 1) + mFloor(getRandom(%rad * 2) - %rad);

// jwn use randoms instead
%x = getRandom(%minX,%maxX);
%y = getRandom(%minY,%maxY);

// jwn tweak ----------------------------------------------------------------

%z = getWord(%pos, 2) + mFloor(getRandom(%rad * 2) - %rad);

%start = %x @ " " @ %y @ " 5000";
%end = %x @ " " @ %y @ " -1";
%ground = containerRayCast(%start, %end, $TypeMasks::TerrainObjectType, 0);
%z = getWord(%ground, 3);
%z += 0.05;

error ("Spawn Position : " @ %x SPC %y);
error ("Ground Level : " @ %z);


%position = %x @ " " @ %y @ " " @ %z;

%mask = ($TypeMasks::VehicleObjectType |
$TypeMasks::MoveableObjectType |
$TypeMasks::StaticShapeObjectType |
$TypeMasks::ForceFieldObjectType |
$TypeMasks::InteriorObjectType |
$TypeMasks::ItemObjectType);

if (ContainerBoxEmpty(%mask,%position,3.5))
{
error ("Spawn Position Is Good");
return %position;
}
else
%retries++;
}
return "0 0 1300 1 0 0 0";
}
#3
01/04/2005 (2:25 pm)
Darn, change the minX2 and minY2 to account for positive numbers (added else conditions)

This:

// jwn in lieu of abs value function ?
if(%minX < 0)
{
%minX2 = %minX * -1;
}
if(%minY < 0)
{
%minY2 = %minY * -1;
}


Should be:

// jwn in lieu of abs value function ?
if(%minX < 0)
{
%minX2 = %minX * -1;
}
else
{
%minX2 = minX;
}
if(%minY < 0)
{
%minY2 = %minY * -1;
}
else
{
%minY2 = %minY;
}

(Just in case your missionarea minX and minY are positive numbers)

(Sorry - saw this reviewing the post.......)
#4
01/04/2005 (6:35 pm)
You know you can edit your posts, right?