I'm making a tower defense map with several special effect and other odd towers and rules. One of the towers is a magnet that is supposed to pull units towards it. An important detail is the map has no maze and no terrain and all units move towards a random point in a region called EndPoint. I don't mind if a reply is in either straight Jass or GUI triggers. I intend to convert this all to Jass to optimize it anyway.
Here's the code I have:
The algorithm for this trigger is as follows:
Pick every unit within DensityAffectMinimum of CurrentMagnetUnit
- Calculate the distance
- Calculate a normal vector from PickedUnit to CurrentMagnetUnit
- Instantly Move PickedUnit towards CurrentMagnetUnit
- Move PickedUnit to Random Location in EndPoint
The trouble is, Warcraft 3 units have no momentum so it's impossible to just nudge a unit towards another. I've done a physics simulation of this in C++, which is where I got the idea from. So, I'm forced to use Unit - Move (Instantly). The problem is it looks jerky and it forces a reset of the unit's original destination because Unit - Move cancels the previous order. Because of that, I have to make the unit pick a new random point in EndPoint to move towards.
Does anyone have a better solution? I thought if all else fails I may just force any unit in range move through the magnet to the other side. With that, it would only attempt to be a magnet from one side or it would never allow a unit to pass it.
Here's the code I have:
-
Magnet Pull Copy Copy
-
Events
-
Time - Every 1.00 seconds of game time
-
-
Conditions
-
Actions
-
Unit Group - Pick every unit in (Units of type Magnet) and do (Actions)
-
Loop - Actions
-
Set CurrentMagnetUnit = (Picked unit)
-
Set DensityUnit = (Picked unit)
-
Set DensityUnitType = Magnet
-
Set DensityDistance = 2000.00
-
Trigger - Run GetDensity <gen> (ignoring conditions)
-
Set DensityValue = (DensityValue x 5.00)
-
If (DensityValue Greater than or equal to 30.00) then do (Set DensityValue = 30.00) else do (Do nothing)
-
Set TempInt = 5
-
Unit Group - Pick every unit in (Units within DensityAffectMinimum of (Position of CurrentMagnetUnit) matching ((Owner of (Matching unit)) Equal to Neutral Hostile)) and do (Actions)
-
Loop - Actions
-
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
-
If - Conditions
-
TempInt Greater than 0
-
-
Then - Actions
-
Set TempA = (Distance between (Position of (Picked unit)) and (Position of CurrentMagnetUnit))
-
Set TempB = ((X of (Position of CurrentMagnetUnit)) - (X of (Position of (Picked unit))))
-
Set TempC = ((Y of (Position of CurrentMagnetUnit)) - (Y of (Position of (Picked unit))))
-
Set TempB = (TempB / TempA)
-
Set TempC = (TempC / TempA)
-
Game - Display to (All players) the text: (VecX: + ((String(TempB, 1, 2)) + (; VecY: + (String(TempC, 1, 2)))))
-
Game - Display to (All players) the text: (Old Pos: + ((String((X of (Position of (Picked unit))), 1, 2)) + (; + (String((Y of (Position of (Picked unit))), 1, 2)))))
-
Unit - Move (Picked unit) instantly to ((Position of (Picked unit)) offset by ((DensityValue x TempB), (DensityValue x TempC)))
-
Unit - Order (Picked unit) to Move To (Random point in Path End <gen>)
-
Game - Display to (All players) the text: (New Pos: + ((String((X of (Position of (Picked unit))), 1, 2)) + (; + (String((Y of (Position of (Picked unit))), 1, 2)))))
-
If (TempInt Equal to 0) then do (Skip remaining actions) else do (Do nothing)
-
-
Else - Actions
-
-
-
-
-
-
-
The algorithm for this trigger is as follows:
Pick every unit within DensityAffectMinimum of CurrentMagnetUnit
- Calculate the distance
- Calculate a normal vector from PickedUnit to CurrentMagnetUnit
- Instantly Move PickedUnit towards CurrentMagnetUnit
- Move PickedUnit to Random Location in EndPoint
The trouble is, Warcraft 3 units have no momentum so it's impossible to just nudge a unit towards another. I've done a physics simulation of this in C++, which is where I got the idea from. So, I'm forced to use Unit - Move (Instantly). The problem is it looks jerky and it forces a reset of the unit's original destination because Unit - Move cancels the previous order. Because of that, I have to make the unit pick a new random point in EndPoint to move towards.
Does anyone have a better solution? I thought if all else fails I may just force any unit in range move through the magnet to the other side. With that, it would only attempt to be a magnet from one side or it would never allow a unit to pass it.
Last edited: