User Tools

Site Tools


4rpl:commands:setcelloccupiedcount

This is an old revision of the document!


Index
Unit

:!: Available in version 1.3 and later.

SetCellOccupiedCount

SetCellOccupiedCount(<-cellX <-cellZ <-count)

Description

Sets the occupied count for a map cell. Extreme caution must be exercised when using this command. Normally units will increase the count for cells they occupy when they land or are created. The units will also decrease the count by 1 whenever they take off or are destroyed. Messing with the count can create 'phantom' areas on the map where units can't land or where they can stack. Units do not know that you have altered the count! So if you change the occupied count, a unit will still decrease the count by 1 when it takes off or is destroyed.

The effect of SetCellOccupiedCount does not persist between save and loads, as all units rebuild their cell occupied counts whenever the game is loaded and units are rebuilt from the save file.

Examples

#Make an area on the map that nothing can land in.
once
   20 10 do
      20 10 do
         SetCellOccupiedCount(I J 10000)
      loop
   loop
endonce
 
 
#Make an area on the map always have a count of 0.  This means units can stack on top of each other in this area.
10 0 do
   10 0 do
      SetCellOccupiedCount(I J 0)
   loop
loop 

Handling all the cases

Here's an example of how to handle all the cases with setting custom occupied count on a unit. This is an example of a 9×9 unit that only wants the outer rim of the unit to occupy space, with a hollow middle. Make sure the unit is set to not occupy land in unit settings.

:Awake
	once
		RegisterForMSG("MSG_PostUpdate" "PostUpdate")
	endonce
:Once
	@Awake
:PostUpdate
	Self GetUnitMoveCell pop ->POU_iMoveCell
	Self GetUnitCell ->POU_iZ ->POU_iX
 
	#If a movable unit is moved by the player
	#Remove custom occupied cells when the unit takes off
	#Add them back when the unit has landed
	<-POU_iMoveCell <-POU_iLastMoveCell neq if
		<-POU_iMoveCell -1 eq if
			Self GetUnitCell TRUE @SetCustomOccupiedLand
		else
			Self GetUnitCell FALSE @SetCustomOccupiedLand
		endif
	#Update occupied count when the unit is forcibly moved in the editor
	#Only occurs when the unit is landed
	else <-POU_iMoveCell -1 eq if
		<-POU_iLastX <-POU_iX neq <-POU_iLastZ <-POU_iZ neq or if
			<-POU_iLastX <-POU_iLastZ FALSE @SetCustomOccupiedLand
			<-POU_iX <-POU_iZ TRUE @SetCustomOccupiedLand
		endif
	endif endif
 
	<-POU_iZ <-POU_iX ->POU_iLastX ->POU_iLastZ
	<-POU_iMoveCell ->POU_iLastMoveCell
#i1 i2 b3 -
#Uniformly set or unset a chunk of occupied land
#Arguments: CellX and CellY of the last position (if unsetting) or current position (if setting),
#           boolean that determines if to set or unset
#Result: None
:SetCustomOccupiedLand
	->SOL_bSet ->SOL_iCellZ ->SOL_iCellX
	4 ->SOL_iRadius
	<-SOL_iCellZ <-SOL_iRadius add 1 add <-SOL_iCellZ <-SOL_iRadius sub do
		<-SOL_iCellX <-SOL_iRadius add 1 add <-SOL_iCellX <-SOL_iRadius sub do
			<-SOL_iCellX <-SOL_iRadius sub I eq
			<-SOL_iCellX <-SOL_iRadius add I eq or
			<-SOL_iCellZ <-SOL_iRadius sub J eq or
			<-SOL_iCellZ <-SOL_iRadius add J eq or if
				I J GetCellOccupiedCount ->SOL_iCount
				<-SOL_bSet if
					I J <-SOL_iCount 1 add SetCellOccupiedCount
				else
					I J <-SOL_iCount 1 sub SetCellOccupiedCount
				endif
			endif
		loop
	loop

Index

4rpl/commands/setcelloccupiedcount.1631717612.txt.gz · Last modified: 2025/02/14 14:56 (external edit)