HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Help on custom vision system.

05-28-2009, 04:05 AM#1
cleeezzz
System now works but it needs optimization because it lags, anyone?

Collapse JASS:
library FogSys initializer Init requires HandyFunctions

globals
    private unit array Units
    private real hdiff = 50
    private real ai = 20
    private real di = 100
endglobals      

function AddUnit takes unit u returns nothing
        local integer i =0
        loop
             exitwhen Units[i]==null
             set i =i+1
        endloop
        set Units[i]=u  
endfunction

private function DestroyFog takes destructable d returns nothing
        call TriggerSleepAction(.05)
        call RemoveDestructable(d)
endfunction

private function CheckRadius takes real rad, unit u returns nothing
        local integer i = 0
        local real r = 0
        local real h
        local real h2
        local real cx
        local real cy
        local real x
        local real y
        local real angle
        local player p
        local boolean b
        local destructable d
        local real height
        set cx = GetUnitX(u)
        set cy = GetUnitY(u)
        set p = GetOwningPlayer(u)
        loop
            exitwhen i * ai > 360
            set r = 0
            set b = false
            set angle = i * ai * bj_DEGTORAD
            loop
                exitwhen r > rad or b == true
                set x = PolarProjectionX(cx, r, angle)
                set y = PolarProjectionY(cy, r, angle)
                set h = GetZ(cx,cy)
                set h2 = GetZ(x,y)
                if (h2 - hdiff > h) then
                    set d = CreateDestructable('YTlb', x, y, 0, 0,1 )
                    call DestroyFog.execute(d)
                    set height = GetDestructableOccluderHeight(d)
                    if GetLocalPlayer() != p then
                        set height = 0
                    endif
                    call SetDestructableOccluderHeight(d,height)
                    set d = null
                    set b = true
                endif
                set r = r + di
            endloop
            set i = i + 1
        endloop
        set p = null
endfunction

private function CheckPlayers takes nothing returns nothing
        local integer i = 0
        loop
        exitwhen Units[i] == null
                call CheckRadius(1100,Units[i])
                set i = i + 1
        endloop
endfunction

private function Init takes nothing returns nothing
        local timer t = NewTimer()
        call TimerStart(t,.05,true,function CheckPlayers)
endfunction


endlibrary


Collapse JASS:
library HandyFunctions initializer Init requires CSData, CSSafety

globals
    // CONFIGURABLES:
    private constant integer FlyTrick = 'Amrf'  // Raw code of Crow Form
    private constant integer Dummy_id = 'n000'  // Raw code of your maps dummy unit
    // DON'T NEED TO BE CHANGED:
    private timer gametime = null
    private rect Trees_in_rect = null
    private real Game_maxX
    private real Game_maxY
    private real Game_minX
    private real Game_minY
    private unit Caster
    private group Damage_group = CreateGroup()
    private group AddGroup
    private integer i = 0
    private unit ru
    private integer count
endglobals

// Returns a safe X location, so units do not go outside map bounds
function SafeX takes real x returns real
    if x<Game_minX then
        return Game_minX
    elseif x>Game_maxX then
            return Game_maxX
    endif
    return x
endfunction

// Returns a safe Y location, so units do not go outside map bounds
function SafeY takes real y returns real
    if y<Game_minY then
        return Game_minY
    elseif y>Game_maxY then
            return Game_maxY
    endif
    return y
endfunction

function SetUnitZ takes unit u, real height returns nothing
    call UnitAddAbility(u, 'Arav')
    call UnitRemoveAbility(u, 'Arav')
    call SetUnitFlyHeight(u, height, 0)
endfunction

function GetUnitZ takes unit u returns real
    local location tp = GetUnitLoc(u)
    local real z = GetLocationZ(tp)
    call RemoveLocation(tp)
    set tp = null
    return GetUnitFlyHeight(u) + z    
endfunction

function GetZ takes real x, real y returns real
    local location tp = Location(x,y)
    local real z = GetLocationZ(tp)
    call RemoveLocation(tp)
    set tp = null
    return z    
endfunction

// Creates a dummy caster needed for Preload (below)
function CreateCaster takes real x, real y returns unit
    set Caster = CreateUnit(Player(15),Dummy_id,x,y,0.00)      
    call UnitAddAbility(Caster,FlyTrick)
    call UnitRemoveAbility(Caster,FlyTrick)
    call UnitApplyTimedLife(Caster,'BTLF',5.00)
    return Caster
endfunction

// Preloads an ability, reduces lag
function PreLoadAbil takes integer abil returns nothing
    set Caster = CreateCaster(0.00,0.00)
    call UnitAddAbility(Caster,abil)
endfunction  

// Angle between co-ordinates
function AngleXY takes real x, real y, real xx, real yy returns real
    return Atan2((yy-y),(xx-x))
endfunction

// Distance between co-ordinates
function DistanceXY takes real x1, real y1, real x2, real y2 returns real
        return SquareRoot((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
endfunction

function HRad2FRad takes real radian returns real
    if radian >= 0 and radian <= 6.283185307 then
        return radian
    else
        return radian + 6.283185307
    endif
    return 0.
endfunction

function LimRad takes real radian returns real
    if radian >=0 and radian <= 6.283185307 then
        return radian
    elseif radian < 0 then
        return 6.283185307 + radian
    elseif radian > 6.283185307 then
        return radian - 6.283185307
    endif
    return 0.
endfunction

function BetweenRad takes real radian, real angle, real between returns boolean
    local real lowbound
    local real highbound
    set lowbound = angle - between
    set highbound = angle + between
    if lowbound < 0 then
        if (LimRad(lowbound) <= radian and radian <= 6.283185307) or (0 <= radian and radian <= highbound) then
            return true
        endif
    elseif highbound > 6.283185307 then
        if (lowbound <= radian and radian <= 6.283185307) or (0 <= radian and radian <= LimRad(highbound)) then
            return true
        endif
    elseif lowbound <= radian and radian <= highbound then
        return true
    endif
    return false
endfunction
// Used with below function
function KillEnumDestructable takes nothing returns nothing
        call KillDestructable(GetEnumDestructable())    
endfunction

// Kills trees/destructables
function KillTrees takes real x, real y, real radius returns nothing
        set Trees_in_rect = Rect(x-radius,y-radius,x+radius,y+radius)
        call EnumDestructablesInRect(Trees_in_rect,null,function KillEnumDestructable)
        call RemoveRect(Trees_in_rect)
endfunction

// Checks for pathability (very basic)
function Pathability takes real x, real y returns boolean
    return IsTerrainPathable(x,y, PATHING_TYPE_WALKABILITY)
endfunction

// Used with below function
function IsPointWater takes real x, real y returns boolean
        return IsTerrainPathable(x,y,PATHING_TYPE_WALKABILITY) and not(IsTerrainPathable(x,y,PATHING_TYPE_AMPHIBIOUSPATHING))
endfunction

// Checks if point is water
function IsPointWaterLoc takes location loc returns boolean
        return IsPointWater(GetLocationX(loc),GetLocationY(loc))
endfunction

// filters for enemies, who are alive and are not structures
function FilterIsEnemyAliveNotStructure takes nothing returns boolean
    return IsUnitEnemy(GetFilterUnit(), bj_groupEnumOwningPlayer) and GetUnitState(GetFilterUnit(),UNIT_STATE_LIFE)>.405 and IsUnitType(GetFilterUnit(),UNIT_TYPE_STRUCTURE)==false
endfunction
    
// Damages an area, uses the above filter
function DamageArea takes unit Damager, real Area, real x, real y, real Dmg, attacktype Attacktype, damagetype Damagetype returns nothing
    local unit FoG
   
    set bj_groupEnumOwningPlayer = GetOwningPlayer(Damager)
    call GroupClear(Damage_group)
    call GroupEnumUnitsInRange(Damage_group, x, y, Area, Condition(function FilterIsEnemyAliveNotStructure))
                                                                                                  // Function that loops through the CreateGroup()
    loop                                                                                          // and deals damage to each enemy in the area.                                                                                          //
        set FoG = FirstOfGroup(Damage_group)
        exitwhen FoG == null
        call GroupRemoveUnit(Damage_group,FoG)
        call UnitDamageTarget(Damager,FoG,Dmg,false,false,Attacktype,Damagetype,null)
    endloop
endfunction

// Adds Text tags over a unit
function TextTagUnit takes string text, unit targ, integer red, integer green, integer blue, integer alpha, real velocity, real duration returns nothing
    local texttag t = CreateTextTag()
               
    call SetTextTagText(t, text, 0.025)
    call SetTextTagPosUnit(t, targ,15)
    call SetTextTagColor(t, red, green, blue, alpha)
    call SetTextTagVelocity(t, 0, velocity)
    call SetTextTagVisibility(t, true)
    call SetTextTagFadepoint(t, 2)
    call SetTextTagLifespan(t, duration)
    call SetTextTagPermanent(t, false)
               
    set t = null
endfunction

function PolarProjectionX takes real x, real distance, real angle returns real
    return x + distance * Cos(angle)
endfunction

function PolarProjectionY takes real y, real distance, real angle returns real
    return y + distance * Sin(angle)
endfunction

// Better alternative to PolledWait()
function PolledWait2 takes real duration returns nothing
    local real timeRemaining
    local real st = TimerGetElapsed(gametime)
               
    if duration > 0. then
        loop
            set timeRemaining = duration - TimerGetElapsed(gametime) + st
            exitwhen timeRemaining <= 0
            if timeRemaining > 2.00 then
                call TriggerSleepAction(0.1 * timeRemaining)
            else
                call TriggerSleepAction(0.0)
            endif
        endloop
    endif
endfunction

// Replicates a sim error
function SimError takes player ForPlayer, string msg returns nothing
    local sound error = CreateSoundFromLabel("InterfaceError",false,false,false,10,10)
   
    if (GetLocalPlayer() == ForPlayer) then
        call ClearTextMessages()
        call DisplayTimedTextToPlayer(ForPlayer,0.52,-1.00,2.00,"|cffffcc00"+msg+"|r")
        call StartSound(error)
    endif
   
    call KillSoundWhenDone(error)
    set error=null
endfunction

function Add takes nothing returns nothing
    if GetEnumUnit() != null and GetUnitState(GetEnumUnit(), UNIT_STATE_LIFE) > .405 then
        call GroupAddUnit(AddGroup, GetEnumUnit())
    endif
endfunction

function Remove takes nothing returns nothing
    if IsUnitInGroup(GetEnumUnit(), AddGroup) then
        call GroupRemoveUnit(AddGroup, GetEnumUnit())
    endif
endfunction

function GroupAddGroupAdv takes group sourceGroup, group destGroup returns nothing
    set AddGroup = null
    set AddGroup = destGroup
    call ForGroup(sourceGroup, function Add)
endfunction

function GroupRemoveGroupAdv takes group sourceGroup, group destGroup returns nothing
    set AddGroup = null
    set AddGroup = destGroup
    call ForGroup(sourceGroup, function Remove)
endfunction
    
function CountPlayers takes nothing returns nothing
    set i = i + 1
endfunction

function CountPlayersInForce takes force Players returns integer
    set i = 0
    call ForForce(Players, function CountPlayers)
    return i
endfunction
    

// Starts the game timer, and sets boundaries
private function Init takes nothing returns nothing
    set gametime = CreateTimer()
    call TimerStart(gametime,1000000.00,false,null)
    set Game_maxX = GetRectMaxX(bj_mapInitialPlayableArea)-50.00
    set Game_maxY = GetRectMaxY(bj_mapInitialPlayableArea)-50.00
    set Game_minX = GetRectMinX(bj_mapInitialPlayableArea)+50.00
    set Game_minY = GetRectMinY(bj_mapInitialPlayableArea)+50.00
endfunction

function ItemCheck takes unit u, integer itemid returns integer
    local integer index
    local item    indexItem
    local integer n = 0
    set index = 0
        loop
            exitwhen index > 5
            set indexItem = UnitItemInSlot(u, index)
                if indexItem != null then
                    if GetItemTypeId(indexItem) == itemid then
                        set n = n + 1
                    endif
                endif
            set index = index + 1
        endloop
    set indexItem = null
    return n
endfunction

private function Count takes nothing returns nothing
    set count = count + 1
endfunction

function GroupGetRandomUnit takes group g returns unit
    local integer r
    local unit fog
    local integer i = 1
    local group ng = NewGroup()
    set count = 0
    call GroupAddGroupAdv(g,ng)
    call ForGroup(ng, function Count)
    set r = GetRandomInt(1, 6) 
    loop
        set fog = FirstOfGroup(ng)
        exitwhen fog == null
        if r == i then
            set ru = fog
        endif            
        set i = i + 1
        call GroupRemoveUnit(ng, fog)
    endloop
    set fog = null
    call ReleaseGroup(ng)
    set ng = null
    return ru
endfunction    
    
endlibrary

it continually drops fps till my game literally freezes, any suggestions?