Moving units when buildings are put atop them
by Sean Taylor · in RTS Starter Kit · 06/13/2009 (5:11 am) · 6 replies
Ok, I don't know how to make an official mod posting so Im just going to wing it!
Title: checkBuildingFirstBuild
PreReq: TGE, RTS SK, World Dom mod + 1.5 update
Purpose: This adds a simple collision check between units and buildings when the buildings are first made. It moves all units away from the building.
Adds:
Ok, now on with the code! This was done completely in script, so there is no need to recompile your engines!
Firstly, we add our function into a new file, just to keep it from getting confused elsewhere, server/scripts/core/collisionCheck.cs...
I don't know if anyone had this annoyingly happen, but buildings can move! Now that isn't right in most cases. So I went with a quick-fix. Go into server/scripts/avatars/player.cs and add the bolded line into the setMoveGoal function.
And finally you make a simple call to the function in the server/scripts/item/building.cs file, at the end of the serverCmdPlaceBuilding function...
Just to note, I also have another version of this that I am working on after I added the villagers move to building to build mod, which changed around some of these functions. The next one will also have the unit go to a bordering position to the building-to-be so that the building doesn't just appear on top of its head! Gimme feedback please!
--Sean
Title: checkBuildingFirstBuild
PreReq: TGE, RTS SK, World Dom mod + 1.5 update
Purpose: This adds a simple collision check between units and buildings when the buildings are first made. It moves all units away from the building.
Adds:
- Simple collision of building and unit
- Keeps buildings from moving
Ok, now on with the code! This was done completely in script, so there is no need to recompile your engines!
Firstly, we add our function into a new file, just to keep it from getting confused elsewhere, server/scripts/core/collisionCheck.cs...
function checkBuildingFirstBuild(%building) {
echo("------- checkBuildingFirstBuild Function started -------");
// Get all units within bounding box's radius
// at building's position
initContainerRadiusSearch(%building.getPosition(), 5.0, $TypeMasks::ShapeBaseObjectType);
// Determine their position to the building's position
// and determine a location to aim at
// find terrain z height at (x,y) position of terrain
// then pass to %obj.setMoveGoal() for each unit
while ((%targetObject = containerSearchNext()) != 0) {
%objName = %targetObject.getClassName();
if (%objName $= "RTSUnit" && %targetObject.getID() != %building.getID()) {
echo("Snagged a unit in my Box!! Object (" @ %targetObject.getID() @ ") Name: " @ %targetObject.getClassName());
%vectMove = -VectorSub(%building.getPosition(), %targetObject.getPosition());
%vectMoveCast = VectorAdd(%targetObject.getPosition(), (2.0 * %vectMove));
// getTerrainHeight("123 456");
%vectMoveX = getWord( %vectMoveCast, 0);
%vectMoveY = getWord( %vectMoveCast, 1);
%vectMoveZ = getTerrainHeight(%vectMoveX SPC %vectMoveY);
echo("Our new projected location is (" @ %vectMoveX @ ", " @ %vectMoveY @ ", "@ %vectMoveZ @")");
%vectMoveCast = setWord(%vectMoveCast, 2, %vectMoveZ);
//now move the unit
//if (%targetObject.getClassName() != "RTSBuilding")
%targetObject.setMoveGoal(%vectMoveCast);
}
}
}I don't know if anyone had this annoyingly happen, but buildings can move! Now that isn't right in most cases. So I went with a quick-fix. Go into server/scripts/avatars/player.cs and add the bolded line into the setMoveGoal function.
...
function Player::setMoveGoal(%this, %dest)
{
[b]// buildings should not move!!
if (getBuildingIndexFromUnitTypeName(%this.getRTSUnitTypeName()) != -1)
return;[/b]
if (%this.generatePath(%dest))
{
%this.curGoal = %dest;
...And finally you make a simple call to the function in the server/scripts/item/building.cs file, at the end of the serverCmdPlaceBuilding function...
...
echo("serverCmdPlaceBuilding()--building (" @ %b @ "):");
[b]checkBuildingFirstBuild(%b);[/b]
%b.initBuildingActions();
}Just to note, I also have another version of this that I am working on after I added the villagers move to building to build mod, which changed around some of these functions. The next one will also have the unit go to a bordering position to the building-to-be so that the building doesn't just appear on top of its head! Gimme feedback please!
--Sean
About the author
#2
06/14/2009 (9:30 am)
Didnt tried this Sean, but really happy to see some good work on this latitudes! =)
#3
Im running TGE 1.5.2 + RTS kit + world domination mod ... and this doesn't seem to change any behavior.
I select BUILD and place the building -- it immediatly drops into the work, right on top the the workers and they just stand there.
I tried deleting all the DSO files and still no change -- also double checked my code matched yours.
Any ideas??
01/19/2010 (12:50 am)
Hmm - are there any pre-reqs for this to work ?Im running TGE 1.5.2 + RTS kit + world domination mod ... and this doesn't seem to change any behavior.
I select BUILD and place the building -- it immediatly drops into the work, right on top the the workers and they just stand there.
I tried deleting all the DSO files and still no change -- also double checked my code matched yours.
Any ideas??
#4
01/19/2010 (1:07 am)
It seems collisionCheck.cs isnt being called ... when I run the game,its not creating a .dso file for it.
#5
01/19/2010 (10:58 am)
I haven't worked on this in a while due to life things, but I believe your problem lies within the main Game.cs file, though I could be wrong. Whichever file it is your problem is that you don't have a line that calls the file to be included in the build process so the code isn't being parsed and compiled to be used. So the quick fix is to find the file that includes the other files and add a line that includes the collissionCheck.cs file.
#6
And I just put the new script right before the function that calls it in building.cs instead of trying to make a new file.
01/20/2010 (5:13 pm)
Must have been a typo on my part - it worked great on a clean buildAnd I just put the new script right before the function that calls it in building.cs instead of trying to make a new file.
Torque Owner Sean Taylor
function getBuildingIndexFromUnitTypeName( %unitTypeName ) { // simple indexer to map rts unit type names (only thing client knows about) // to an index for use in arrays switch$(%UnitTypeName) { case "testbuilding": return 0; case "factory": return 0; case "shop": return 1; case "barracks": return 2; case "farm": return 3; case "foundry": return 4; case "townCenter": return 5; default: return -1; } }