• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Is this effiecient and leakless ?

Status
Not open for further replies.
Level 9
Joined
Jan 3, 2010
Messages
359
hello, i've created a system that will change the camera when a hero come inside a house, and i wanted to ask is this efficient and leakless ?
Credits to Opposum for his Oppicam, and the almighty vexorian for his Table :D

JASS:
library HouseCam requires Table, ThirdPersonCam, FixedCam
    
globals
    private Table HouseTable = 0
    private integer Houses = 0
endglobals

private function Conditions takes nothing returns boolean
    return GetTriggerUnit() == hero
endfunction

private function opendoor takes nothing returns nothing
    if GetDestructableTypeId(GetEnumDestructable()) == 'B001'  then
        call SetDestructableAnimation(GetEnumDestructable(), "stand alternate")
    endif
endfunction

private function closedoor takes nothing returns nothing
    if GetDestructableTypeId(GetEnumDestructable()) == 'B001'  then
        call SetDestructableAnimation(GetEnumDestructable(), "stand")
    endif
endfunction

function H2I takes handle h returns integer
    return GetHandleId(h)
endfunction

struct Houses
    region reg = CreateRegion()
    real regX = 0
    real regY = 0
    trigger EnterHouse =CreateTrigger()
    trigger OutHouse = CreateTrigger()
    
    private static method create takes rect whichRect returns thistype
        local thistype this = thistype.allocate()
        
        call RegionAddRect(.reg, whichRect)
        set .regX = GetRectCenterX(whichRect)
        set .regY = GetRectCenterY(whichRect)
        call TriggerRegisterEnterRegion(.EnterHouse, .reg, null)
        call TriggerRegisterLeaveRegion(.OutHouse, .reg, null)
        set HouseTable[H2I(.reg)] = this
        call TriggerAddAction(.EnterHouse, function thistype.enter)
        call TriggerAddCondition(.EnterHouse, Condition(function Conditions))
        call TriggerAddAction(.OutHouse, function thistype.out)
        call TriggerAddCondition(.OutHouse, Condition(function Conditions))
        
        return thistype
    endmethod
        
    private static method enter takes nothing returns nothing
        local player p = GetOwningPlayer(GetEnteringUnit())
        local thistype this = HouseTable[H2I(GetTriggeringRegion())]
        
        call EnableThirdPersonCam(p, null, 0)
        call EnableFixedCam(p, GetEnteringUnit(), .regX, .regY, 200, 1)
        call EnumDestructablesInRect(bj_mapInitialPlayableArea, null, function opendoor)
    endmethod
    
    private static method out takes nothing returns nothing
        local player p = GetOwningPlayer(GetLeavingUnit())
        local thistype this = HouseTable[H2I(GetTriggeringRegion())]
        
        call EnableFixedCam(p, null, 0, 0, 0, 0)
        call EnableThirdPersonCam(p, GetLeavingUnit(), 1)
        call EnumDestructablesInRect(bj_mapInitialPlayableArea, null, function closedoor)
    endmethod

    private static method onInit takes nothing returns nothing
        set HouseTable = Table.create()
        
        call Houses.create(gg_rct_H1)
        call Houses.create(gg_rct_H2)
    
    endmethod
    
endstruct
    
endlibrary
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I'm curious as to why you return thistype instead of this in your private constructor.

I have no idea what library ThirdPersonCam or library FixedCam do but in assumption that they do not leak and you're not using them inefficiently somehow then the code appears to be "leak-free" and efficient but what is this library supposed to do?
 
Level 9
Joined
Jan 3, 2010
Messages
359
I'm curious as to why you return thistype instead of this in your private constructor.

I have no idea what library ThirdPersonCam or library FixedCam do but in assumption that they do not leak and you're not using them inefficiently somehow then the code appears to be "leak-free" and efficient but what is this library supposed to do?

This is the library for library ThirdPersonCam and library FixedCam, it's a custom camera system :D

as for the thistype, i just copy from someone's library, honestly this is the first fully working vJASS i've ever made :D, i don't know if thistype can be exchanged with this ?


this library triggers an automatic camera change, if a hero go inside a house, the camera will change to library FixedCam, if go outside change to library ThirdPersonCam
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
You should be returning this, but believe me when you are using a static method use a name other than this - it should be reserved specifically for instance-methods (which are non-static methods) where the language actually makes sense. Using it for a syntax shortcut (so that you don't have to type name.) is unnecessary and it can make a little more complicated code harder to read.

Though I can imagine why something like this would never need to be destroyed in a game, the only thing I can seem to find that is wrong with this code is that when the struct is destroyed the associated handles are not properly destroyed/removed.

It would be as easy as implementing this method in the struct:

JASS:
method onDestroy takes nothing returns nothing
    call RemoveRegion(reg)
    call DestroyTrigger(EnterHouse)
    call DestroyTrigger(OutHouse)
endmethod

In this case I doubt its necessary, but whatever.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
It would be easier to simply deactivate the triggers than destroy the struct, if that's what you were planning on doing though. If you were to make the houses unable to be entered at night time then the enter-region event would theoretically never be triggered.
 
Level 19
Joined
Feb 4, 2009
Messages
1,313
you are picking all destructibles in the map every time and avoid the filter function
faster way would be to not use the action function part at all and do the actions in the filter function part
fastest way would be to use 1. to save all doors you got this way in an array and use this instead

the H2I wrapper function is nice if you don't want to write much but it's not much different from the BJs
back then an own function for typecasting was necessary but now it isn't
unnecessary overhead
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I believe if you pass the function H2I through the War3 Map Optimizer it will inline it though, so it really doesn't make a big difference.

For the destructible enumeration, the overhead that is saved by allocating the operations from the actions code to the filter code is almost irrelevant.
 
Status
Not open for further replies.
Top