Zigzags

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
Jeebus... I cannot get a zigzag to work. I am trying to make a spell that involves moving the caster in a zigzag towards the target location, but, as the level of the spell increases, I want to be able to add more points into the zigzag, making denser... if that makes sense. Any help? Perhaps a method for moving the unit? (I really want to use SetUnitX and SetUnitY, not "move"). I know Pyro made/was working on some spell with a zigzag in it...
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
A basic zigzag function would be (replace global with handle system / strcuts):

JASS:
function zigzag takes nothing returns nothing
local location p= GetUnitLoc(GetTriggerUnit())
local location p2
local real distance = 200.
local real angle = GetUnitFacing(GetTriggerUnit())

if udg_global == 0 then
    set udg_global = 1
    set angle = angle + 90.
else
    set udg_global = 0
    st angle = angle - 90
endif

set p2=PolarProjectionBJ(p,distance,angle)
call SetUnitPositionLoc(p2)
endfunction


Something like that.

Work from there.
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
Believe me... I have... and it does not work...

The following code has been JASSed to reduce strain on your eyes...

JASS:
    private function Duration takes integer level returns real
		return 1.5
	endfunction
	
	private function ZigzagPoints takes integer level returns integer
		if(level<3)then
			return 4
		elseif(level<5)then
			return 5
		endif
		return 6
	endfunction
	
	private function Conditions takes nothing returns boolean
		return GetSpellAbilityId()==SpellId
	endfunction
	
	public struct Cache
		unit caster=null
		player owner=null
		timer t=null
		trigger trig=null
		triggeraction trigact=null
		real cx=0.
		real cy=0.
		real ox=0.
		real oy=0.
		real nx=0.
		real ny=0.
		real npx=0.
		real npy=0.
		real tx=0.
		real ty=0.
		real direction=1.
		real cang=.0
		real ang=0.
		real dur=0.
		real cdur=0.
		real mdur=0.
		real seclength=0.
		real speed=0.
		real cos=0.
		real sin=0.
		region map=null
		integer zigcount=0
		integer level=0
		integer points=0
	endstruct
	
	private function Colide takes nothing returns nothing
		local Cache cache=GetStructB(GetTriggeringTrigger())
		//Damage...
	endfunction
	
	private function TimerEnd takes nothing returns nothing
		local Cache cache=GetStructA(GetExpiredTimer())
		set cache.dur=cache.dur+.05
		call SetUnitX(cache.caster,cache.nx)
		call SetUnitY(cache.caster,cache.ny)
		call SetUnitFacing(cache.caster,cache.ang)
		if(cache.dur>=cache.cdur)then //Change Course
			set cache.zigcount=cache.zigcount+1
			set cache.direction=cache.direction*-1.
			set cache.ox=cache.npx
			set cache.oy=cache.npy
			if(cache.zigcount>=cache.points)then //Last Zigzag
				set cache.npx=cache.tx
				set cache.npy=cache.ty
				set cache.cdur=cache.mdur
			else
				set cache.npx=(cache.cx+((cache.seclength*cache.zigcount)-(cache.seclength*.5))*Cos(cache.cang))+Width*(cache.cang+90)*cache.direction //The Zigpoint X
				set cache.npy=(cache.cy+((cache.seclength*cache.zigcount)-(cache.seclength*.5))*Sin(cache.cang))+Width*(cache.cang+90)*cache.direction //The Zigpoint Y
				set cache.cdur=((cache.mdur/cache.points)*cache.zigcount)-((cache.mdur/cache.points)*.5)
			endif
			set cache.ang=Atan2(cache.npy-cache.oy,cache.npx-cache.ox) //The angle of the current Zig
			set cache.cos=Cos(cache.ang)
			set cache.sin=Sin(cache.ang)
		endif
		//Set New Coords
		set cache.nx=GetUnitX(cache.caster)+cache.speed*cache.cos
		set cache.ny=GetUnitY(cache.caster)+cache.speed*cache.sin
		if(cache.dur>cache.mdur)then //Clean Up
			call PauseTimer(cache.t)
			call ClearStructA(cache.t)
			call DestroyTimer(cache.t)
			call TriggerRemoveAction(cache.trig,cache.trigact)
			call ClearStructB(cache.trig)
			call DestroyTrigger(cache.trig)
			call PauseUnit(cache.caster,false)
			call cache.destroy()
		endif
	endfunction
	
	private function Cast takes nothing returns nothing
		local Cache cache=Cache.create()
		local location tar=GetSpellTargetLoc()
		
		set cache.caster=GetTriggerUnit()
		call PauseUnit(cache.caster,true)
		call IssueImmediateOrder(cache.caster,"stop")
		set cache.owner=GetOwningPlayer(cache.caster)
		set cache.level=GetUnitAbilityLevel(cache.caster,SpellId)
		set cache.points=ZigzagPoints(cache.level)
		set cache.mdur=Duration(cache.level)
		set cache.zigcount=0
		
		set cache.seclength=Length/cache.points //The length along the center line of each section
		set cache.direction=1.              //1 = Left or -1 = Right
		set cache.cx=GetUnitX(cache.caster) //X of caster
		set cache.cy=GetUnitY(cache.caster) //Y of caster
		set cache.ox=cache.cx               //The 'old' X
		set cache.oy=cache.cy               //The 'old' Y
		set cache.tx=GetLocationX(tar)      //'Target' X
		set cache.ty=GetLocationY(tar)      //'Target' Y
		set cache.cang=Atan2(cache.ty-cache.cy,cache.tx-cache.cx) // The center angle
		set cache.npx=(cache.cx+(cache.seclength*.5)*Cos(cache.cang))+Width*(cache.cang+90)*cache.direction //The Zigpoint X
		set cache.npy=(cache.cy+(cache.seclength*.5)*Sin(cache.cang))+Width*(cache.cang+90)*cache.direction //The Zigpoint Y
		set cache.ang=Atan2(cache.npy-cache.oy,cache.npx-cache.ox) //The angle of the current Zig
		set cache.cos=Cos(cache.ang)
		set cache.sin=Sin(cache.ang)
		set cache.cdur=(cache.mdur/cache.points)*.5
		set cache.speed=SquareRoot((cache.ox-cache.npx)*(cache.ox-cache.npx)+(cache.oy-cache.npy)*(cache.oy-cache.npy))/cache.cdur
		set cache.nx=cache.ox+cache.speed*cache.cos //The next X to be moved to
		set cache.ny=cache.oy+cache.speed*cache.sin //The next Y to be moved to
		
		set cache.t=CreateTimer()
		call SetStructA(cache.t,cache)
		set cache.trig=CreateTrigger()
		call SetStructB(cache.trig,cache)
		call TriggerRegisterUnitInRange(cache.trig,cache.caster,ColideRange,Filter(function AlwaysTrue))
		set cache.trigact=TriggerAddAction(cache.trig,function Colide)
		call TimerStart(cache.t,.05,true,function TimerEnd)
		
		call RemoveLocation(tar)
		set tar=null
	endfunction

	//===========================================================================
	public function InitTrig takes nothing returns nothing
		local integer a=0
		set gg_trg_Zigger=CreateTrigger()
		loop
			exitwhen a>11
			call TriggerRegisterPlayerUnitEvent(gg_trg_Zigger,Player(a),EVENT_PLAYER_UNIT_SPELL_EFFECT,Filter(function AlwaysTrue))
			set a=a+1
		endloop
		call TriggerAddCondition(gg_trg_Zigger,Condition(function Conditions))
		call TriggerAddAction(gg_trg_Zigger,function Cast)
	endfunction



As you can (or maybe can't) see, the real problem stems from the fact that I want to be able to dynamically change the number of points along the zigzag, while still keeping the same length. So, the higher the level of the spell, the denser the zigzag.

Code:
Compare :

45 + 90 + 90 + 90 + 45

to

10 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 + 10

The second would have significantly more points, yet they would both get you the same distance.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Right, since I'm no genius at JASS writing, I'll write it in my own special way (oh gnoes)


Point variables:
- Basepoint1
- Basepoint2
- Startpos
- Endpos
- Temppos
- Movepos

Integer variables:
- ZZPointamount (amount of ZZ points you want (caster/target point not included)
- ZZModifier = 1
- ZZcount

Real variables:
- ZZwidth (= total zigzag width / 2)
- ZZdistance
- CTT_Angle (= CasterToTargetAngle)


Code(ish):

JASS:

constant real ZZwidth = ???? // user defined, keep in mind that this is actually half the width of the entire zigzag spanwidth
constant integer ZZpointamount = ???? // constant, or defined by ability level... your pick really


set Startpos = casting unit
set Endpos = target point/unit
set ZZdistance = distance between Startpos and Endpos
set CTT_Angle = angle between Startpos and Endpos
set Basepoint1 = point at ZZwidth distance of Startpos towards CTT_angle+90°
set Basepoint2 = point at ZZwidth distance of Startpos towards CTT_angle-90°

while (ZZcount < ZZpointamount)
{
    set ZZcount = ZZcount + 1
    set Temppos = point at ZZcount*(ZZdistance/ZZPointamount) distance of Startpos towards CTT_angle
    set Movepos = point at ZZwidth distance of Temppos towards CTT_angle+(ZZmodifier*90°)
    set ZZmodifier = 0 - ZZmodifier
    order unit to move to Movepos
    wait untill unit reaches Movepos
}

order unit to move to Endpos


This should divide the distance between caster and target by the amount of zigzag points you want, then alternate from left to right every time (= why ZZmodifier changed from one to minus one and back all the time) over a width you can set.

Forgive the While loop and crappy JASS, I just started out in it, so I used a bit of pseudo coding left and right ;)

Hope you understand where I'm going with this.
 

martix

There is no spoon
Reaction score
49
Coordinates usually means vector and trigonometry calc's. So this requires you to know your average high-school physics.
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
I can do trig. Its dynamicly generating the points that I'm having issues with. Magentix' method seems good to me. I hadn't though of using a basepoint relate to width. I will try that, and see if I can't get this to work. Thus far, I have only been able to crash the game (I moved my unit out of the map, I presume).
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top