HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Calculation Errors?

05-13-2009, 10:32 AM#1
wraithseeker
Collapse JASS:
library Knockback initializer Init requires DestructableLib, IsTerrainWalkable, GroupUtils, AutoIndex

globals
    private constant real    TIME         = 0.03                     // The timer interval.
    private constant real    CHAINRADIUS  = 128.00                   // The radius a unit can get chained when near the unit target.
    private constant real    SPEEDFACTOR  = 0.75                     // How much speed will be reduced when a unit chains another unit.
    private constant real    RADIUS       = 128.00                   // The radius to check tree.
    private constant real    HEIGHTLEVEL  = 200.00                   // The default height level that stops chaining or killing trees when in the air.
    private constant real    FACTOR       = 0.3333                   // Or 0.25 for smoothness.
    private constant string  GROUND       = "MDX\\Dust.mdx"          // The effect for ground.
    private constant string  WATER        = "MDX\\SlideWater.mdx"    // The effect for water.
    private constant string  COLLISION    = "MDX\\DustAndRocks.mdx"  // The effect when at cliff
    private constant string  ATTACHPOINT  = "origin"                 // The attachment point for effects
endglobals

globals
    private constant integer INVULNERABLE   = 'Avul'               
    private constant integer FLYID          = 'Amrf' 
    private rect     TreeRect               = null
    private boolexpr TreeCheck              = null
    private boolexpr ChainFilter            = null
    private boolexpr LineFilter             = null
    private real MapMaxX                    = 0
    private real MapMaxY                    = 0
    private real MapMinX                    = 0
    private real MapMinY                    = 0
    private constant real GRAVITY = 900
    private location L = Location(0,0)
endglobals

function ParabolaZ takes real h, real d, real x returns real
  return (4 * h / d) * (d - x) * (x / d)
endfunction

private function IsPointOutside takes real x, real y returns boolean
    return (x > MapMaxX or y > MapMaxY or x < MapMinX or y < MapMinY)
endfunction

private interface face
    method OnStart takes nothing returns nothing defaults nothing
    method OnStop takes nothing returns nothing defaults nothing
    method OnStopCondition takes nothing returns boolean defaults false
    method OnChain takes nothing returns nothing defaults nothing
endinterface
    

struct Knock extends face
    private static timer Timer = CreateTimer()
    private static integer Count = 0
    private static Knock array D
    private static Knock data
    private static integer array Entries
    private     integer      mode            = 0
    private     group        hit             = null
    private     group        linehit         = null
    private     real         VariableSpeed   = 0
    private     effect      effects          = null
    unit        source          = null
    unit      target          = null
    unit        dashtarget      = null
    integer     Mode            = 0
    real        cos             = 0.
    real        sin             = 0.
    real        speed           = 0.
    real        decrement       = 0.
    real        distance        = 0.
    real        Increment       = 0.
    real        positionZ       = 0.
    real        LineDamage      = 0.
    real x=0
    real y=0
    real z=0
    real        gravity         = 0.
    string      SFX             = ""
    string      attachpoint     = "origin"
    location    TargetPoint     = null
    boolean     trees           = false
    boolean     chain           = false
    boolean     fly             = false
    boolean     ToTarget        = false
    boolean     Move            = false
    
private static method CheckTrees takes nothing returns boolean
    return IsDestructableTree(GetFilterDestructable())
endmethod

private static method Trees takes nothing returns nothing
    call KillDestructable(GetEnumDestructable())
endmethod

private static method ChainCheck takes nothing returns boolean
    local Knock d = .data
    return d.target != GetFilterUnit() and IsUnitEnemy(GetFilterUnit(),GetOwningPlayer(d.source)) and IsUnitType(GetFilterUnit(), UNIT_TYPE_GROUND) == true and IsUnitType(GetFilterUnit(), UNIT_TYPE_MECHANICAL) == false and GetWidgetLife(GetFilterUnit()) > 0.405 and GetUnitAbilityLevel(GetFilterUnit(),INVULNERABLE) <= 0
endmethod

private static method LineCheck takes nothing returns boolean
    local Knock d = .data
    return GetWidgetLife(GetFilterUnit()) > 0.405 and IsUnitEnemy(GetFilterUnit(),GetOwningPlayer(d.source)) and GetFilterUnit() != d.source
endmethod
    
method TerrainCheck takes nothing returns integer
    local real x = GetUnitX(.target)
    local real y = GetUnitY(.target)
    local real height = GetUnitFlyHeight(.target)
    if IsTerrainWalkable(x + 50.00 * .cos,y + 50.00 * .sin) == false then
        return 3
    else
        if IsTerrainPathable(x,y,PATHING_TYPE_FLOATABILITY) then
            return 1
        elseif not IsTerrainPathable(x,y,PATHING_TYPE_WALKABILITY) then
            return 2
        endif
    endif 
    return 0
    endmethod
        
static method KnockbackStop takes unit target returns boolean
    local Knock this
    local integer id = GetUnitId(target)
    local integer i = .Count - 1
    if id != 0 then
        if .Count > 0 then
            set .D[i]= .D[.Count]
        else 
            call PauseTimer(.Timer) 
        endif
        call .destroy()
        return true
    endif
    return false
endmethod

static method IsKnockedBack takes unit target returns boolean
    local Knock this
    local integer id = GetUnitId(target)
    return .Entries[id] != 0
endmethod
    
static method create takes unit source, unit target, real angle, real speed returns Knock
    local Knock d = Knock.allocate()
    local integer i = GetUnitId(target)
    local real destx = 0.
    local real desty = 0.
    local real destz = 0.
    local real length = 0.
    set destx = GetUnitX(target) - GetUnitX(source)
    set desty = GetUnitY(target) - GetUnitY(source)
    set length = SquareRoot(destx*destx + desty*desty)
    set destx = GetUnitX(target) + (destx / length) * 600
    set desty = GetUnitY(target) + (desty / length) * 600
    set d.x = (destx - GetUnitX(target) / 600) * d.speed
    set d.y = (desty - GetUnitY(target) / 600 ) *d.speed 
    call MoveLocation(L,GetUnitX(target),GetUnitY(target))
    set d.z = (destz- GetLocationZ(L) / 600 ) *d.speed+(600 / d.speed * GRAVITY*TIME)
    set .D[.Count] = d
       if .Count == 0 then
        call TimerStart(.Timer,TIME,true,function Knock.action)
    endif
    set .Count = .Count + 1
    set .Entries[i] = .Entries[i] + 1
    if d.OnStart.exists then
        call d.OnStart()
    endif
    return d
endmethod
    
static method get takes unit u returns integer
    local integer id = GetUnitId(u)
    return id
endmethod

private method onDestroy takes nothing returns nothing
    call SetUnitFlyHeight(.target,GetUnitDefaultFlyHeight(.target),0)
    call SetUnitPathing(.target,true)
endmethod
        
private static method action takes nothing returns nothing
    local Knock d = 0
    local Knock this = 0
    local real    x    = 0.00
    local real    y    = 0.00
    local real    cx   = 0.00
    local real    cy   = 0.00
    local real    tx   = 0.00
    local real    ty   = 0.00
    local integer id = 0
    local integer mode = 0
    local real height  = 0.00
    local group g      = NewGroup()
    local real angle   = 0.00
    local integer i = 0
    local unit t       = null
    local real speed
    
    loop
        exitwhen i >= .Count
        set this = .D[i]
        set id = GetUnitId(.target)
        set x = GetUnitX(.target)
        set y = GetUnitY(.target)
        set height = GetUnitFlyHeight(.target)
        set mode = .mode
        set .z = .z - GRAVITY * TIME
        call MoveLocation(L,GetUnitX(.target),GetUnitY(.target))
        call SetUnitPosition(.target,GetUnitX(.target)+.x*TIME,GetUnitY(.target)+.y*TIME)
      //  call SetUnitY(.target,GetUnitY(.target)+.y*TIME)
      call UnitAddAbility(.target,FLYID)
      call UnitRemoveAbility(.target,FLYID)
        call SetUnitFlyHeight(.target,GetLocationZ(L) + (.z * TIME),0)
        if height <= 0 then
            set .Entries[i] = .Entries[i] - 1
            call .destroy()
        endif
    set height = GetUnitFlyHeight(.target)   
        set i = i + 1
    endloop
endmethod
    
static method onInit takes nothing returns nothing
    set TreeRect    = Rect(0.00,0.00,1.00,1.00)
    set TreeCheck   = Filter(function Knock.CheckTrees)
    set ChainFilter = Filter(function Knock.ChainCheck)
    set LineFilter  = Filter(function Knock.LineCheck)
endmethod
endstruct

private function Init takes nothing returns nothing
    set MapMaxX     = GetRectMaxX(bj_mapInitialPlayableArea)
    set MapMaxY     = GetRectMaxY(bj_mapInitialPlayableArea)
    set MapMinX     = GetRectMinX(bj_mapInitialPlayableArea)
    set MapMinY     = GetRectMinY(bj_mapInitialPlayableArea)     
endfunction

endlibrary

Don't tell me about how inefficient this is, does anyone know of any calculation errors there is?

Right now the unit never reaches the ground.

600 is a fixed value for some testing.