Creating a Custom Unit to be Placed over Ore Deposit

Started by Gameboytm101, June 23, 2016, 04:49:03 PM

Previous topic - Next topic

Gameboytm101

Hello everyone.
As you may or may not have seen, I have been dabbling into creating custom units, using scripts found in Estrox by Mikoz as a base.
One such unit was going to be placed ontop of Ore Deposits. However, since this is one of my first attempts at CRPL, I am quite lost at this point.
What I still need to code:

  • Ore Deposit Detection
  • If Ore Deposit is detected at center of unit-to-be-placed, make placementability valid
  • If no Ore Deposit is detected at center of unit-to-be-placed, make placementability invalid
I'm also uncertain if this is even possible.

J

Take a look at the PAC map tower placement. Those things can only be built on power zones.

GoodMorning

Also, you will need that the UnitType of an ore deposit is "OREDEPOSIT" in the same way as it is "POWERZONE" for PZs.
A narrative is a lightly-marked path to another reality.

Gameboytm101

Well, I kind-of got it working, but now there's an odd issue. There are multiple valid placements per ore deposit, and some of their positions don't make sense.
N is invalid tile, Y is valid tile, + is the center of the ore deposit (and also a valid tile). The unit I want to build is 3 by 3 tiles.
N N N N N N N
N Y Y Y Y Y N
N Y Y Y Y Y N
N Y Y + Y Y N
N Y Y Y Y N N
N Y Y Y N N N
N N N N N N N


Here are the relevant snippets of code if you need to take a look at it:

SetCurrentCoords(GetMouseCell)
1 ->PlacementValid
do (CurrentY add(2) dup sub(3))
do (CurrentX add(2) dup sub(3))
if (GetVoid(I J))
0 ->PlacementValid
endif
if (GetCellOccupiedCount(I J))
0 ->PlacementValid
endif
if (GetWall(I J) gt(0))
0 ->PlacementValid
endif
GetAllUnitsInRange(I J 0 TRUE) 0 do if(GetUnitType "OREDEPOSIT" eq)
1 ->PlacementValid
else
0 ->PlacementValid
loop
endif
loop
loop
if (CurrentX lt(1) or (CurrentY lt(1) or (CurrentX gte(MapWidth sub(1)) or (CurrentY gte(MapHeight sub(1))))))
SetImageColor(Self "main" 255 255 255 0)
SetImageColor(Self "border" 255 255 255 0)
SetUnitAttribute(Self CONST_CONNECTABLE 0)
0 ->PlacementValid


GoodMorning

#4
Short answer, I think: Check the deposit once, then use the do loops to check for obstruction. Currently the loops can find void, and then a deposit later, and the deposit will overwrite the invalidity. Also, this shouldn't compile, as you have an endif outside the loop over GetUnitsInRange.
Finally, your last if doesn't check PlacementValid.

As ever, Trace, Trace2, TraceStack.

If you can't get it working, one structure is below. Trace what you can from yours first, and you will probably learn to fix it yourself. Be warned that though mine is based on the snippet you showed, it may not work in the same context, but you should be able to see what changed.

Hope this helps.


SetCurrentCoords(GetMouseCell)
0 ->PlacementValid
GetAllUnitsInRange(CurrentCoords 0 TRUE) 0 do if(GetUnitType "OREDEPOSIT" eq)
1 ->PlacementValid
endif loop
if (CurrentX lt(1) or (CurrentY lt(1) or (CurrentX gte(MapWidth sub(1)) or (CurrentY gte(MapHeight sub(1))))))
0 ->PlacementValid endif
do (CurrentY add(2) dup sub(3))
do (CurrentX add(2) dup sub(3))
if (GetVoid(I J))
0 ->PlacementValid
endif
if (GetCellOccupiedCount(I J))
0 ->PlacementValid
endif
if (GetWall(I J) gt(0))
0 ->PlacementValid
endif
loop
loop
<-PlacementValid if
SetImageColor(Self "main" 255 255 255 0)
SetImageColor(Self "border" 255 255 255 0)
SetUnitAttribute(Self CONST_CONNECTABLE 0)
endif
A narrative is a lightly-marked path to another reality.

Gameboytm101

With a few edits to the fixed code you provided, I managed to get it to work. Thanks!

GoodMorning

Do you see what was going wrong earlier? If so, I have succeeded well.
A narrative is a lightly-marked path to another reality.