(solved, thanks to Grayzzur) Another broken script concerning GetUnitsInRange

Started by Clean0nion, December 24, 2013, 06:35:08 PM

Previous topic - Next topic

Clean0nion

So, as those of who who frequent the chat will know, I'm working on a new map. This map, like Domorthea, contains a spore tower, except this time it's a lot more complicated.

The script can be found at http://pastebin.com/ackdnMn7 or inside the spoiler at the bottom of this post.

What the script is supposed to do: a spore tower that switches between four modes.
Mode 1: shoots spores at structures
Mode 2: shoots spores at attacking weapons
Mode 3: shoots spores at defensive weapons and structures
Mode 4: shoots spores at random units (not yet coded)

For modes 1 - 3, I've attempted to add a GetUnitsInRange function. If I don't, I have to use RandUnitCoords.

By using RandUnitCoords, the script will only check one unit to see if it matches what it's searching. This is fine, unless the unit does not match the search. If so, it just returns a negative and the script moves on.

For example, if the script is searching for cannons, mortars, berthae, strafers, and bombers, and it's scanning a collector, this will return a negative and the script will move on. However, if there was a mortar on the map, then that negative result was wrong!

So I tried using GetUnitsInRange to force it to scan every unit, to always return an accurate result. However... nothing seems to happen. Either it's always returning an inaccurate result, or it's always returning a negative result. Either way, it's not working as intended.

Essentially, here's a summary of what the script actually does:
10 Changes mode flawlessly
20 On the next mode, PopupText declares that there is no available target
30 GO TO 10

As well as putting the script in the spoiler and uploading it to pastebin, I've also attached the entire map, so that if you have no idea what's going on with this script or just want to see the fancy effects I've added to this unit, you can download that and hopefully you can help. (the script in question is gun.crpl and the unit is on the circular, shielded island in the top-left of the map.)

Thank you very much!

Script
# gun.crpl
# Created on: 12/21/2013 4:54:47 PM
# ------------------------------------------

#Switches periodically between four modes.
#MODE 1: STRUCTURE
#       Aims for relays, collectors, guppies, ore mines, siphons and forges.
#MODE 2: ATTACK
#       Aims for pulse cannons, mortars, berthae, strafers, bombers.
#MODE 3: DEFENSE
#       Aims for shields, beams, snipers, terps, and sprayers.
#MODE 4: SPORES
#       Releases random, aimless spores.

once
       1 ->mode
       Self "base" "Custom3_128" SetImage
       Self "base" 2 2 SetImageScale
       Self "main" "Custom3" SetImage
       1 SetPopupTextAlwaysVisible
       CreateList ->UIDs
endonce

@RunOperation

:RunOperation
       <-mode 1 eq if
               "MODE: Anti-Structural" SetPopupText
               0 0 9999 GetUnitsInRange ->Namount
               ClearStack
               0 0 9999 GetUnitsInRange 0 do
                       <-UIDs Swap AppendToList
               loop
               <-UIDs GetListCount 0 do                                      
                       ->targetUID
                       <-targetUID CONST_COORDX GetUnitAttribute ->tx
                       <-targetUID CONST_COORDY GetUnitAttribute ->ty
                       #RandUnitCoords swap ->tx ->ty
                       #<-tx <-ty GetUnitAt ->targetUID
                       <-targetUID GetUnitType ->targetType
                       <-targetType "RELAY" eq
                       <-targetType "COLLECTOR" eq
                       <-targetType "GUPPY" eq
                       <-targetType "OREMINE" eq
                       <-targetType "SIPHON" eq
                       <-targetType "FORGE" eq
                       or or or or or if
                               1 ->success
                               Break
                       else
                               0 ->success
                       endif
               loop
               1 <-success eq if
                       @Fire
                       @ChangeMode
               else
                       "MODE: Anti-Structural
Error: No target" SetPopupText
                       150 Delay
                       @ChangeMode
               endif


       else
       <-mode 2 eq if
               "MODE: Anti-Attack" SetPopupText
               0 0 9999 GetUnitsInRange ->Namount
               ClearStack
               0 0 9999 GetUnitsInRange 0 do
                       <-UIDs Swap AppendToList
               loop
               <-UIDs GetListCount 0 do                                      
                       ->targetUID
                       <-targetUID CONST_COORDX GetUnitAttribute ->tx
                       <-targetUID CONST_COORDY GetUnitAttribute ->ty
                       #RandUnitCoords swap ->tx ->ty
                       #<-tx <-ty GetUnitAt ->targetUID
                       <-targetUID GetUnitType ->targetType
                       <-targetType "PULSECANNON" eq
                       <-targetType "MORTAR" eq
                       <-targetType "BERTHA" eq
                       <-targetType "STRAFER" eq
                       <-targetType "BOMBER" eq
                       or or or or if
                               1 ->success
                               Break
                       else
                               0 ->success
                       endif
               loop
               1 <-success eq if
                       @Fire
                       @ChangeMode
               else
                       "MODE: Anti-Attack
Error: No target" SetPopupText
                       150 Delay
                       @ChangeMode
               endif


       else
       <-mode 3 eq if
               "MODE: Anti-Defensive" SetPopupText
               0 0 9999 GetUnitsInRange ->Namount
               ClearStack
               0 0 9999 GetUnitsInRange 0 do
                       <-UIDs Swap AppendToList
               loop
               <-UIDs GetListCount 0 do                                      
                       ->targetUID
                       <-targetUID CONST_COORDX GetUnitAttribute ->tx
                       <-targetUID CONST_COORDY GetUnitAttribute ->ty
                       #RandUnitCoords swap ->tx ->ty
                       #<-tx <-ty GetUnitAt ->targetUID
                       <-targetUID GetUnitType ->targetType
                       <-targetType "SHIELD" eq
                       <-targetType "BEAM" eq
                       <-targetType "SNIPER" eq
                       <-targetType "TERP" eq
                       <-targetType "SPRAYER" eq
                       or or or or if
                               1 ->success
                               Break
                       else
                               0 ->success
                       endif
               loop
               1 <-success eq if
                       @Fire
                       @ChangeMode
               else
                       "MODE: Anti-Defensive
Error: No target" SetPopupText
                       150 Delay
                       @ChangeMode
               endif


       else
       <-mode 4 eq if
               @Spore
               @ChangeMode
       endif endif endif endif

:ChangeMode
       "Changing mode" SetPopupText
       <-mode 1 add ->mode
       <-mode 5 eq if
               1 ->mode
       endif
       90 0 do
               Self "base" Self "base" GetImageRotation 0.0174532925 sub SetImageRotation
       1 Delay
       loop
       "Mode: " <-mode concat SetPopupText
       Self "base" 0 SetImageRotation
       Self "base" "Custom" <-mode concat "_128" concat SetImage
       Self "main" "Custom" <-mode concat SetImage
       30 Delay

:Fire
       <-targetType @GetUnitTypeCount ->typeCount
       10 0 do
               "Launching " <-typeCount concat " spores
at " concat <-targetType concat " in " concat 10 I sub concat " seconds" concat SetPopupText
               30 Delay
       loop
       <-typeCount 0 do
               CurrentCoords <-tx <-ty 1 10 CreateSpore
               20 Delay
       loop

:Spore


#"REACTOR" @GetUnitTypeCount ->reactorCount
:GetUnitTypeCount
       ->searchType
       0 ->count
       do (GetUnitsInRange(0 0 9999) 0)
               ->unitUID
               if(GetUnitType(<-unitUID) eq(<-searchType))
                       <-count add(1) ->count
               endif
       loop
       <-count
[close]

Grayzzur

As I mentioned to Clean0nion in chat
                <-UIDs GetListCount 0 do                                       
                        ->targetUID

Should be
                <-UIDs GetListCount 0 do                                       
                        <-UIDs I GetListElement ->targetUID

"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker