User Tools

Site Tools


4rpl:commands:setcelloccupiedcount

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
4rpl:commands:setcelloccupiedcount [2021/09/15 15:31] – improved code Grabz4rpl:commands:setcelloccupiedcount [2025/02/14 14:57] (current) – external edit 127.0.0.1
Line 13: Line 13:
 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. 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.+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.\\ 
 +:WARNING: This also applies to using this API in :Gameloaded and :Awake (when game is also loaded). This API will not function as desired should it be called in :Gameloaded or :Awake (:Awake will work fine when placing units, just not when units are loaded in). This is due to the game rebuilding cell occupy counts latter than :Awake and :Gameloaded, causing the effects of this API used in such functions to be overwritten. 
 + 
 +If you're trying to make a custom unit occupy cells in a custom shape rather than the default box shape, refer to [[cw4:4rpl_tools#custom_celloccupiedcount_shape|Custom CellOccupiedCount shape]] for a copy-paste ready script that simplifies creating one and handles all of the difficult parts of using SetCellOccupiedCount, including handling unit movement.
  
 ===== Examples ===== ===== Examples =====
Line 33: Line 36:
    loop    loop
 loop  loop 
-</code> 
- 
-===== 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 9x9 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. 
- 
-This code is universal and should handle all the cases properly, the only thing you have to edit is which cells around the unit should be changed by editing the contents of the SetCustomOccupiedLand function. 
-<code 4rpl> 
-:awake 
- once 
- RegisterForMSG("MSG_PostUpdate" "PostUpdate") 
- endonce 
-:once 
- @awake 
-:destroyed 
- <-G_iCurrentlyOccupiedX <-G_iCurrentlyOccupiedZ FALSE @SetCustomOccupiedLand 
-:PostUpdate 
- Self GetUnitMoveCell ->POU_iMoveZ ->POU_iMoveX 
- <-POU_iMoveX ->POU_iMoveCell 
- Self GetUnitCell ->POU_iZ ->POU_iX 
-  
- #Set the initial configuration of our unit  
- once 
- <-POU_iX <-POU_iZ TRUE @SetCustomOccupiedLand 
- <-POU_iX <-POU_iZ ->G_iCurrentlyOccupiedZ ->G_iCurrentlyOccupiedX 
- endonce 
-  
- #If the unit has been ordered to move 
- <-POU_iMoveCell <-POU_iLastMoveCell neq <-POU_iMoveCell -1 neq and if 
- #If the unit is already moving and has been reordered to move, 
- #we clear the occupied count from the area we ordered the unit to previously 
- <-POU_iLastMoveCell -1 neq if 
- <-POU_iLastMoveX <-POU_iLastMoveZ FALSE @SetCustomOccupiedLand 
- #If the unit was not moving and this is the first order, 
- #we clear the occupied count from the area the unit was sitting on 
- else 
- <-POU_iX <-POU_iZ FALSE @SetCustomOccupiedLand 
- endif 
-  
- #We set the occupied land at the new target location our unit is meant to land in 
- <-POU_iMoveX <-POU_iMoveZ TRUE @SetCustomOccupiedLand 
- <-POU_iMoveX <-POU_iMoveZ ->G_iCurrentlyOccupiedZ ->G_iCurrentlyOccupiedX 
- #If the unit is stationary 
- else <-POU_iMoveCell <-POU_iLastMoveCell eq <-POU_iMoveCell -1 eq and if 
- #If the unit was moved in the editor, clear last position and set new position 
- <-POU_iLastX <-POU_iX neq <-POU_iLastZ <-POU_iZ neq or if 
- <-POU_iLastX <-POU_iLastZ FALSE @SetCustomOccupiedLand 
- <-POU_iX <-POU_iZ TRUE @SetCustomOccupiedLand 
- <-POU_iX <-POU_iZ ->G_iCurrentlyOccupiedZ ->G_iCurrentlyOccupiedX 
- endif 
- endif endif 
-  
- <-POU_iMoveZ <-POU_iMoveX ->POU_iLastMoveX ->POU_iLastMoveZ 
- <-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 
 </code> </code>
  
 <=[[4rpl:start| Index]] <=[[4rpl:start| Index]]
4rpl/commands/setcelloccupiedcount.1631719869.txt.gz · Last modified: 2025/02/14 14:56 (external edit)