Page 1 of 1

Function PlaceUnit

Posted: Thu Feb 10, 2011 12:13 pm
by Amaris
I noticed that all units loaded in a transport with the function PlaceUnit(x, y, facing, side, team, type) are assigned to the team 0 regardless of the setting team :shock:

Code: Select all

FUNCTION PlaceUnit(x, y, facing, side, team, type)
{
	int id ;
	int radius ;
	int x1 ;
	int y1 ;
	int dx ;
	int dy ;
	int deploy ;

	// place the unit out of map bounderies, so we can test its type later on
	id = AddUnit(15, 15, side, type) ;

	// check if tile is unoccupied and not blocked
	if( (GetUnitOnTile(x, y) == -1) && (IsTileBlocked(id, x, y) == 0) )
	{
		dx = x ;
		dy = y ;

		deploy = 1 ;
	}
	else
	{
		// if target tile was occupied, try adjacent tiles
		radius = 1 ;
		for(x1=0; x1<=radius*2; x1++)
		{
			for(y1=0; y1<=radius*2; y1++)
			{
				dx = x+x1-radius ;
				dy = y+y1-radius ;
	
				if( (IsTileBlocked(id, dx, dy) == 0) && (IsValidTile(dx, dy) == 1) && (deploy == 0) && (GetTileCost(id, dx, dy) <GetBaseAttrib> -1 )
			{
				LoadUnit(GetGlobal("gTransport"), id) ;
				SetGlobal("gTransport", id) ;
			}
			else
			{
				// remove unit if transport does not exist
				RemoveUnit(id, 1) ;
			}
		}
		else
		{
			UnitDeploy(id, dx, dy) ;

			// Set team
			SetUnitTeam(id, team) ;

			// Set facing
			SetUnitFacing(id, facing * 45) ;

			// Store id in global variable
			SetGlobal("gTransport", id) ;
		}
	}
	else
	{
		RemoveUnit(id, 1) ;

		// mark gTransport as invalid in case id was a transport unit
		SetGlobal("gTransport", -1) ;
	}
}
You must add the line:

Code: Select all

SetUnitTeam(id, team) ;
in case of loaded unit

Code: Select all

if( facing == 8 )
So:

Code: Select all

FUNCTION PlaceUnit2(x, y, facing, side, team, type)
{
	int id ;
	int radius ;
	int x1 ;
	int y1 ;
	int dx ;
	int dy ;
	int deploy ;

	// place the unit out of map bounderies, so we can test its type later on
	id = AddUnit(15, 15, side, type) ;

	// check if tile is unoccupied and not blocked
	if( (GetUnitOnTile(x, y) == -1) && (IsTileBlocked(id, x, y) == 0) )
	{
		dx = x ;
		dy = y ;

		deploy = 1 ;
	}
	else
	{
		// if target tile was occupied, try adjacent tiles
		radius = 1 ;
		for(x1=0; x1<=radius*2; x1++)
		{
			for(y1=0; y1<=radius*2; y1++)
			{
				dx = x+x1-radius ;
				dy = y+y1-radius ;
	
				if( (IsTileBlocked(id, dx, dy) == 0) && (IsValidTile(dx, dy) == 1) && (deploy == 0) && (GetTileCost(id, dx, dy) <GetBaseAttrib> -1 )
			{
				//Fix team setting in case of loaded unit       <------------------ here
				SetUnitTeam(id, team) ;
				LoadUnit(GetGlobal("gTransport"), id) ;
				SetGlobal("gTransport", id) ;
			}
			else
			{
				// remove unit if transport does not exist
				RemoveUnit(id, 1) ;
			}
		}
		else
		{
			UnitDeploy(id, dx, dy) ;

			// Set team
			SetUnitTeam(id, team) ;

			// Set facing
			SetUnitFacing(id, facing * 45) ;

			// Store id in global variable
			SetGlobal("gTransport", id) ;
		}
	}
	else
	{
		RemoveUnit(id, 1) ;

		// mark gTransport as invalid in case id was a transport unit
		SetGlobal("gTransport", -1) ;
	}
}

Posted: Thu Feb 10, 2011 4:19 pm
by pipfromslitherine
Thanks for the bug find :). I'll take a look and make sure the fix won't break anything.

Cheers

Pip

Posted: Fri Feb 11, 2011 1:12 am
by Merr
Pip,

The PlaceUnit function is another one that doesn't check for a water tile either ... don't forget!

Thanks.