Function PlaceUnit

Modders can post their questions on scripting and more.

Moderators: Slitherine Core, BA Moderators

Post Reply
Amaris
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 929
Joined: Fri Jul 23, 2010 11:08 am
Location: France
Contact:

Function PlaceUnit

Post 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) ;
	}
}
pipfromslitherine
Site Admin
Site Admin
Posts: 9937
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

Thanks for the bug find :). I'll take a look and make sure the fix won't break anything.

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

Pip,

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

Thanks.
Post Reply

Return to “Battle Academy : Modders Corner ”