Help Needed On setting A Totem As A Variable

Started by Jacobwde12, December 22, 2015, 08:02:16 PM

Previous topic - Next topic

Jacobwde12

Hi! I need help with a script. I'm trying to make the script target a totem, however it fails to do so and only targets the unit with a UID of 0. Anyone know how to get it to target a totem correctly? The code targeting the totem is at 29-37.

# Corruption Tower.crpl
# Created on: 12/22/2015 6:23:41 PM
# ------------------------------------------
$rotationRate:0.01
$imageToRotate:"main"
$originalChargingTime:2400
$target:0

once

endonce

# Make a part spin around
Self <-imageToRotate GetImageRotation ->currentRotation
<-currentRotation <-rotationRate add ->currentRotation
Self <-imageToRotate <-currentRotation SetImageRotation

GetTimer0 SetPopUpText

GetTimer0 eq0 if
<-originalChargingTime SetTimer0
@Fire
endif

:Fire
   CurrentCoords 9999 GetUnitsInRange ->unit
   ShowTraceLog
   <-unit trace
   while <-target eq0 if
   repeat
   if (GetUnitType(<-unit) neq("TOTEM"))
   <-unit pop
   else
   <-unit ->target
   endif endif
   
   endwhile
   
   
    #Fire Charging Laser
self "beam" "Custom4" SetImage
self "beam" 255 255 0 255 SetImageColor
self "beam" 1.5 SetImageScaleY

<-target CONST_PIXELCOORDX GetUnitAttribute ->targetX
<-target CONST_PIXELCOORDY GetUnitAttribute ->targetY

self CONST_PIXELCOORDX GetUnitAttribute ->selfX
self CONST_PIXELCOORDY GetUnitAttribute ->selfY

<-targetX <-selfX sub ->deltaX
<-targetY <-selfY sub ->deltaY
<-deltaY <-deltaX atan2 ->angle
<-selfX <-selfY <-targetX <-targetY Distance ->distance
<-distance 24 div ->beamScaleX

<-deltaX 2 div ->beamX
<-deltaY 2 div ->beamY

SetImagePosition(self "beam" <-beamX <-beamY -0.02)
SetImageScaleX(self "beam" <-beamScaleX)
SetImageRotation(self "beam" <-angle)



<-img 1 add ->img
Delay(170)
self "beam" 255 0 0 0 SetImageColor
    SetImage(self "beam" "NONE")
SetImage(self "damage" "NONE")

Builder17

#1
if (GetUnitType(<-unit) neq("TOTEM"))
Should it be eq instead neq?
And why empty once block?
Does script works other ways how it should?

Jacobwde12

It pops the unit from the stack if it's not a totem otherwise it sets it as a variable. I tried setting it from neq to eq but it in the trace log it says it exceeded maximum number of instructions. The script should be targeting a totem, but it only targets the unit with the UID of 0.

Builder17

This rotating thing works as it should?
And GetUnitsInRange doesn't return totems , it should be GetAllUnitsInRange.
I don't yet know , is there all okey in while command.

Jacobwde12

OHHHHH!!!! Thank you so much! I changed the script trying to see if other things would work. But Apparently GetAllUnitsInRange worked! Thanks Builder!  :D

# Corruption Tower.crpl
# Created on: 12/22/2015 6:23:41 PM
# ------------------------------------------
$rotationRate:0.01
$imageToRotate:"main"
$originalChargingTime:2400
$target:0
$unitType:"TOTEM"
$usedTarget:0

once
0 ->STATE_CHARGE
1 ->STATE_TARGET
2 ->STATE_FIRE

0 ->state
endonce
if (<-state eq (<-STATE_CHARGE))
@Charge
else if (<-state eq (<-STATE_TARGET))
@Target
else if (<-state eq (<-STATE_FIRE))
@Fire
endif endif endif

   
# Make a part spin around
Self <-imageToRotate GetImageRotation ->currentRotation
<-currentRotation <-rotationRate add ->currentRotation
Self <-imageToRotate <-currentRotation SetImageRotation


GetTimer0 SetPopUpText

:Charge
GetTimer0 eq0 if
<-originalChargingTime SetTimer0
1 ->state
endif

:Target
# Perform first check
GetAllUnitsInRange(0 0 9999 1) 0 do ->unit
   ShowTraceLog
   <-unit trace
   <-target trace
   #GetUnitType(<-unit) trace
   <-target eq0 if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif
   
#Perform second Check to make sure we don't use the same totem twice
<-target <-usedTarget eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif
   loop
   
   
   
:Fire   
    #Fire Charging Laser
self "beam" "Custom4" SetImage
self "beam" 255 255 0 255 SetImageColor
self "beam" 1.5 SetImageScaleY

<-target CONST_PIXELCOORDX GetUnitAttribute ->targetX
<-target CONST_PIXELCOORDY GetUnitAttribute ->targetY
Trace2("The Target Location is" <-targetX <-targetY)

self CONST_PIXELCOORDX GetUnitAttribute ->selfX
self CONST_PIXELCOORDY GetUnitAttribute ->selfY

<-targetX <-selfX sub ->deltaX
<-targetY <-selfY sub ->deltaY
<-deltaY <-deltaX atan2 ->angle
<-selfX <-selfY <-targetX <-targetY Distance ->distance
<-distance 24 div ->beamScaleX

<-deltaX 2 div ->beamX
<-deltaY 2 div ->beamY

SetImagePosition(self "beam" <-beamX <-beamY -0.02)
SetImageScaleX(self "beam" <-beamScaleX)
SetImageRotation(self "beam" <-angle)



<-img 1 add ->img
Delay(170)
self "beam" 255 0 0 0 SetImageColor
    SetImage(self "beam" "NONE")
SetImage(self "damage" "NONE")
<-target ->usedTarget
0 ->state

J

After calling the command GetUnitsInRange you can first get the amount of unit UID's from the stack, and you were checking that number every time. Instead iterate through all UID's found and save it if it's a totem. This should get the totem UID:

:Fire
   CurrentCoords 9999 1 GetAllUnitsInRange
   0 do
      ->unit
      if (GetUnitType(<-unit) eq("TOTEM"))
         <-unit ->target
      endif
   loop


edit: seems like you already found a solution while I was typing this

Karsten75

Quote from: J on December 23, 2015, 10:19:36 AM
After calling the command GetUnitsInRange you can first get the amount of unit UID's from the stack, and you were checking that number every time. Instead iterate through all UID's found and save it if it's a totem. This should get the totem UID:


Your code may need adjustment if there are multiple totems found.

Jacobwde12

Quote from: Karsten75 on December 23, 2015, 11:24:04 AM
Quote from: J on December 23, 2015, 10:19:36 AM
After calling the command GetUnitsInRange you can first get the amount of unit UID's from the stack, and you were checking that number every time. Instead iterate through all UID's found and save it if it's a totem. This should get the totem UID:


Your code may need adjustment if there are multiple totems found.

I found a really messy way around it and it works! :D

# Corruption Tower.crpl
# Created on: 12/22/2015 6:23:41 PM
# ------------------------------------------
$rotationRate:0.01
$imageToRotate:"main"
$originalChargingTime:2400
$phaseTwoChargingTime:1800
$phaseThreeChargingTime:900
$target:0
$unitType:"TOTEM"
$usedTarget:0
$firingPhase:0

once
0 ->STATE_CHARGE
1 ->STATE_TARGET
2 ->STATE_FIRE

0 ->state
endonce
if (<-state eq (<-STATE_CHARGE))
@Charge
else if (<-state eq (<-STATE_TARGET))
@Target
else if (<-state eq (<-STATE_FIRE))
@Fire
endif endif endif

   
# Make a part spin around
Self <-imageToRotate GetImageRotation ->currentRotation
<-currentRotation <-rotationRate add ->currentRotation
Self <-imageToRotate <-currentRotation SetImageRotation


GetTimer0 SetPopUpText

:Charge
GetTimer0 eq0 if

<-firingPhase 2 eq if
<-phaseThreeChargingTime SetTimer0
1 ->state
endif

<-firingPhase 1 eq if
<-phaseTwoChargingTime SetTimer0
1 ->state
2 ->firingPhase
endif

<-firingPhase eq0 if
<-originalChargingTime SetTimer0
1 ->firingPhase
endif

endif

:Target
# Perform first check
GetAllUnitsInRange(0 0 9999 1) 0 do ->unit
   ShowTraceLog
   <-unit trace
   <-target trace
   #GetUnitType(<-unit) trace
   <-target eq0 if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif
   
#Perform second Check to make sure we don't use the same totem twice until all totems have been checked.
<-target <-usedTarget eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif
   
#Perform 3rd Check, etc.
<-target <-usedTarget2 eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif

   
<-target <-usedTarget3 eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif

<-target <-usedTarget4 eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif
   
<-target <-usedTarget5 eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
0 ->usedTarget7
2 ->state
   else
   <-unit pop
   endif endif
   
<-target <-usedTarget6 eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
2 ->state
   else
   <-unit pop
   endif endif
   
#Perform the check and reset to reuse the totems again.   
<-target <-usedTarget7 eq if
   if (GetUnitType(<-unit) eq(<-unitType))
    <-unit ->target
0 ->usedTarget
0 ->usedTarget2
0 ->usedTarget3
0 ->usedTarget4
0 ->usedTarget5
0 ->usedTarget6
2 ->state
   else
   <-unit pop
   endif endif
   loop

   
   
:Fire   
    #Fire Charging Laser
self "beam" "Custom4" SetImage
self "beam" 255 0 0 255 SetImageColor
self "beam" 1.5 SetImageScaleY

<-target CONST_PIXELCOORDX GetUnitAttribute ->targetX
<-target CONST_PIXELCOORDY GetUnitAttribute ->targetY
Trace2("The Target Location is" <-targetX <-targetY)

self CONST_PIXELCOORDX GetUnitAttribute ->selfX
self CONST_PIXELCOORDY GetUnitAttribute ->selfY

<-targetX <-selfX sub ->deltaX
<-targetY <-selfY sub ->deltaY
<-deltaY <-deltaX atan2 ->angle
<-selfX <-selfY <-targetX <-targetY Distance ->distance
<-distance 24 div ->beamScaleX

<-deltaX 2 div ->beamX
<-deltaY 2 div ->beamY

SetImagePosition(self "beam" <-beamX <-beamY -0.02)
SetImageScaleX(self "beam" <-beamScaleX)
SetImageRotation(self "beam" <-angle)



<-img 1 add ->img
Delay(170)
self "beam" 255 0 0 0 SetImageColor
    SetImage(self "beam" "NONE")
SetImage(self "damage" "NONE")

<-usedTarget eq0 if
<-target ->usedTarget
else if (<-usedTarget2 eq0)
<-target ->usedTarget2
else if (<-usedTarget3 eq0)
<-target ->usedTarget3
else if (<-usedTarget4 eq0)
<-target ->usedTarget4
else if (<-usedTarget5 eq0)
<-target ->usedTarget5
else if (<-usedTarget6 eq0)
<-target ->usedTarget6
else if (<-usedTarget7 eq0)
<-target ->usedTarget7

endif endif endif endif endif endif endif
0 ->state