Welcome to the new Diaspora forums, please let us know if you see anything broken! Notice: Some users may need to reupload their avatars due to an issue during forum setup!

Fixing an elevator - fixing someone else's code

woothiewoothie Posts: 44Registered
edited February 2014 in Help and Tutorials #1
I'm sure the topic will make any coder/scripter shiver, as not only is looking through other people's code a hazzle and a general pain in the ass, but other people's code made a long time ago might just be far worse. Mmmaybe.

403 lines of E2 below (pasting it in the e2 editor makes it easier to read, obviously.)
@name G400 AAF Lift
persist Type Setup Que Start Next Stop Holo [E O B L Light]:entity [Stops]:table
# Made By Ghost400 #

if(tickClk()){
    if(Stop > Start){
        SData = Stops[Start,array]
        NData = Stops[Next,array]
        
        Pos = NData[1,entity]:toWorld(NData[2,vector])
        Dist = L:pos():distance(Pos)
        Force = Dist < 5 ? Pos - L:pos() : (Pos - L:pos()):normalized() * 5
        Angle = SData[1,entity]:angles():setYaw(B:angles():yaw())
        
        if(Dist < 1){
            if(Next == Stop){
                runOnTick(0)
                soundStop(1)
                Start = Stop
                L:soundPlay(3, 1, "ambient/levels/citadel/pod_close1.wav")
                L:parentTo(NData[1,entity])
                L:propFreeze(1)
                L:setSkin(NData[1,entity]:getSkinCount() == L:getSkinCount() ? NData[1,entity]:getSkin() : NData[1,entity]:getSkin() * 2)
                
                Light:propDelete()
                holoDelete(3)
            }
            else{
                Start = Next
                Next = Stop > Start ? Start + 1 : Start - 1
                
                Data = Stops[Next,array]
                holoPos(3, Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,65)))
                holoParent(3, Data[1,entity])
            }
        }
        
        if(SData[4,number] == 1){
            if(L:pos():distance(SData[1,entity]:toWorld(SData[2,vector])) > 25){
                SData[3,entity]:soundPlay(2, 1, "ambient/machines/thumper_top.wav")
                SData[3,entity]:sequenceReset(0)
                SData[3,entity]:propNotSolid(0)
                SData[3,entity]:propFreeze(1)
                SData[3,entity]:setPos(SData[3,entity]:pos())
                SData[3,entity]:setAng(SData[3,entity]:angles())
                SData[4,number] = 0
            }
        }
        
        if(NData[4,number] == 0){
            if(L:pos():distance(NData[1,entity]:toWorld(NData[2,vector])) < 100){
                NData[3,entity]:soundPlay(2, 1, "ambient/machines/thumper_top.wav")
                NData[3,entity]:sequenceReset(1)
                NData[3,entity]:propNotSolid(1)
                NData[4,number] = 1
            }
        }
    }
    else{
        SData = Stops[Start,array]
        NData = Stops[Next,array]
        
        Pos = NData[1,entity]:toWorld(NData[2,vector])
        Dist = L:pos():distance(Pos)
        Force = Dist < 5 ? Pos - L:pos() : (Pos - L:pos()):normalized() * 5
        Angle = NData[1,entity]:angles():setYaw(B:angles():yaw())
        
        if(Dist < 1){
            if(Next == Stop){
                runOnTick(0)
                soundStop(1)
                Start = Stop
                L:soundPlay(3, 1, "ambient/levels/citadel/pod_close1.wav")
                L:parentTo(NData[1,entity])
                L:propFreeze(1)
                L:setSkin(NData[1,entity]:getSkinCount() == L:getSkinCount() ? NData[1,entity]:getSkin() : NData[1,entity]:getSkin() * 2)
                
                Light:propDelete()
                holoDelete(3)
            }
            else{
                Start = Next
                Next = Stop > Start ? Start + 1 : Start - 1
                
                Data = Stops[Next,array]
                holoPos(3, Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,65)))
                holoParent(3, Data[1,entity])
            }
        }
        
        if(SData[4,number] == 1){
            if(L:pos():distance(SData[1,entity]:toWorld(SData[2,vector])) > 100){
                SData[3,entity]:soundPlay(2, 1, "ambient/machines/thumper_top.wav")
                SData[3,entity]:sequenceReset(0)
                SData[3,entity]:propNotSolid(0)
                SData[3,entity]:propFreeze(1)
                SData[3,entity]:setPos(SData[3,entity]:pos())
                SData[3,entity]:setAng(SData[3,entity]:angles())
                SData[4,number] = 0
            }
        }
        
        if(NData[4,number] == 0){
            if(L:pos():distance(NData[1,entity]:toWorld(NData[2,vector])) < 50){
                NData[3,entity]:soundPlay(2, 1, "ambient/machines/thumper_top.wav")
                NData[3,entity]:sequenceReset(1)
                NData[3,entity]:propNotSolid(1)
                NData[4,number] = 1
            }
        }
    }
    
    L:applyForce((Force * 10 - L:vel()) * 50000)
    L:applyTorque((150 * L:toLocal(rotationVector(quat(Angle) / quat(L)) + L:pos()) - 15 * L:angVelVector()) * L:inertia())
}
elseif(clk()){
    interval(250)
    if(!Setup){
        if(Start == Stop){
            OnPad = 0
            
            foreach(K,V:entity=players()){
                Distance = V:pos():distance(L:pos())
                
                if(V:isOnGround() & Distance < L:radius()){
                    OnPad = 1
                }
                
                if(V:keyUse()){
                    Aim = V:aimEntity()
                    AimPos = V:aimPos()
                    
                    if(Aim == L & Distance < L:radius()){
                        if(inrange(L:toLocal(AimPos):x(), 0, L:boxSize():x()/2)){
                            if(Stops:exists(Start - 1)){
                                runOnTick(1)
                                OnPad = 0
                                Stop = Start - 1
                                Next = Stop > Start ? Start + 1 : Start - 1
                                L:soundPlay(1, 0, "ambient/levels/citadel/field_loop" + randint(1, 4):toString() + ".wav")
                                L:soundPlay(3, 1, "ambient/levels/citadel/pod_open1.wav")
                                L:deparent()
                                L:setPos(L:pos())
                                L:setAng(L:angles())
                                L:propFreeze(0)
                                L:propGravity(0)
                                
                                Data = Stops[Next,array]
                                holoCreate(3, Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,55)), vec(), Data[1,entity]:angles())
                                holoParent(3, Data[1,entity])
                                Light = propSpawn("models/props_silo/light_red2.mdl", Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,65)), Data[1,entity]:angles(), 1)
                                Light:propNotSolid(1)
                                Light:parentTo(holoEntity(3))
                                break
                            }
                        }
                        else{
                            if(Stops:exists(Start + 1)){
                                runOnTick(1)
                                OnPad = 0
                                Stop = Start + 1
                                Next = Stop > Start ? Start + 1 : Start - 1
                                L:soundPlay(1, 0, "ambient/levels/citadel/field_loop" + randint(1, 4):toString() + ".wav")
                                L:soundPlay(3, 1, "ambient/levels/citadel/pod_open1.wav")
                                L:deparent()
                                L:setPos(L:pos())
                                L:setAng(L:angles())
                                L:propFreeze(0)
                                L:propGravity(0)
                                
                                Data = Stops[Next,array]
                                holoCreate(3, Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,55)), vec(), Data[1,entity]:angles())
                                holoParent(3, Data[1,entity])
                                Light = propSpawn("models/props_silo/light_red2.mdl", Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,65)), Data[1,entity]:angles(), 1)
                                Light:propNotSolid(1)
                                Light:parentTo(holoEntity(3))
                                break
                            }
                        }
                    }
                    else{
                        Found = array()
                        for(I=1, Stops:count()){
                            if(Aim == Stops[I,array][1,entity]){
                                Found[I,vector] = Stops[I,array][2,vector]
                            }
                            elseif(Aim == Stops[I,array][3,entity]){
                                Found[I,vector] = Stops[I,array][2,vector]
                            }
                        }
                        Dist = 10000000
                        Closest = 0
                        foreach(K2,V2:vector=Found){
                            if(AimPos:distance(Aim:toWorld(V2)) < Dist){
                                Dist = AimPos:distance(Aim:toWorld(V2))
                                Closest = K2
                            }
                        }
                        
                        if(Closest != 0 & Closest != Start){
                            runOnTick(1)
                            Stop = Closest
                            Next = Stop > Start ? Start + 1 : Start - 1
                            L:soundPlay(1, 0, "ambient/levels/citadel/field_loop" + randint(1, 4):toString() + ".wav")
                            L:soundPlay(3, 1, "ambient/levels/citadel/pod_open1.wav")
                            L:deparent()
                            L:setPos(L:pos())
                            L:setAng(L:angles())
                            L:propFreeze(0)
                            L:propGravity(0)
                            
                            Data = Stops[Next,array]
                            holoCreate(3, Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,55)), vec(), Data[1,entity]:angles())
                            holoParent(3, Data[1,entity])
                            Light = propSpawn("models/props_silo/light_red2.mdl", Data[1,entity]:toWorld(Data[2,vector] + vec(0,0,65)), Data[1,entity]:angles(), 1)
                            Light:propNotSolid(1)
                            Light:parentTo(holoEntity(3))
                            break
                        }
                    }
                }
            }
            
            if(OnPad == 1 & Holo == 0){
                Holo = 1
                holoAlpha(1, 255)
                holoAlpha(2, 255)
            }
            elseif(OnPad == 0 & Holo == 1){
                Holo = 0
                holoAlpha(1, 0)
                holoAlpha(2, 0)
            }
        }
    }
    else{
        if(Que > 1){
            Que--
            Data = Stops[Que + 1,array]
            if(Type == 1){
                Ent = propSpawn("models/SmallBridge/SEnts/sbahatchelevs.mdl", Data[1,entity]:toWorld(Data[2,vector]), Data[1,entity]:angles(), 1)
                Ent:parentTo(Data[1,entity])
                Ent:sequenceSet(0)
                Ent:setSkin(Data[1,entity]:getSkin())
                Data[3,entity] = Ent
                Data[4,number] = 0
            }
            elseif(Type == 2){
                Ent = propSpawn("models/SmallBridge/SEnts/sbahatchelevl.mdl", Data[1,entity]:toWorld(Data[2,vector]), Data[1,entity]:angles(), 1)
                Ent:parentTo(Data[1,entity])
                Ent:sequenceReset(0)
                Ent:setSkin(Data[1,entity]:getSkin())
                Data[3,entity] = Ent
                Data[4,number] = 0
            }
        }
        elseif(Que == 1){
            Setup = 0
            Data = Stops[1,array]
            if(Type == 1){
                L = propSpawn("models/SmallBridge/Elevators,Small/sbselevp0.mdl", Data[1,entity]:toWorld(Data[2,vector]), Data[1,entity]:angles(), 1)
            }
            elseif(Type == 2){
                L = propSpawn("models/SmallBridge/Elevators,Large/sblelevp0.mdl", Data[1,entity]:toWorld(Data[2,vector]), Data[1,entity]:angles(), 1)
            }
            L:parentTo(Data[1,entity])
            L:setMass(50000)
            L:setSkin(Data[1,entity]:getSkinCount() == L:getSkinCount() ? Data[1,entity]:getSkin() : Data[1,entity]:getSkin() * 2)
            
            Box = L:boxSize()
            holoCreate(1, L:toWorld(L:boxCenter() + vec(Box:x()/4, 0, Box:z()/2)), vec(Box:x()/2 - 5, Box:y() - 5, 0.5)/12, L:angles(), vec(200,50,50), "plane")
            holoAlpha(1, 0)
            holoParent(1, L)
            holoCreate(2, L:toWorld(L:boxCenter() + vec(-Box:x()/4, 0, Box:z()/2)), vec(Box:x()/2 - 5, Box:y() - 5, 0.5)/12, L:angles(), vec(50,200,50), "plane")
            holoAlpha(2, 0)
            holoParent(2, L)
        }
    }
}
elseif(first()){
    interval(250)
    runOnLast(1)
    propSpawnEffect(0)
    
    E = entity()
    O = owner()
    Ent = E:isWeldedTo()
    Setup = 1
    Start = 1
    Stop = 1
    
    rangerPersist(1)
    rangerHitEntities(1)
    rangerHitWater(0)
    rangerIgnoreWorld(1)
    rangerFilter(players())
    
    R1 = rangerOffset(Ent:boxCenterW(), Ent:toWorld(Ent:boxCenter() - vec(0,0,10000)))
    R2 = rangerOffset(Ent:boxCenterW(), Ent:toWorld(Ent:boxCenter() - vec(0,0,10000)))
    if(R1:entity()){
        Ent = R1:entity()
        M = Ent:model():explode("/")[4,string]
        if(M:find("sbselevb") | M:find("sbbridgevisorb")){
            B = Ent
            Type = 1
        }
        elseif(M:find("sblelevb")){
            B = Ent
            Type = 2
        }
        elseif(M:find("sbselevslant")){
            printColor(vec(200,50,50), "[Lift]", vec(255,255,255), ": This Part Is Not Supported")
        }
        elseif(R2:entity()){
            Ent = R2:entity()
            M = Ent:model():explode("/")[4,string]
            if(M:find("sbselevb") | M:find("sbbridgevisorb")){
                B = Ent
                Type = 1
            }
            elseif(M:find("sblelevb")){
                B = Ent
                Type = 2
            }
            elseif(M:find("sbselevslant")){
                printColor(vec(200,50,50), "[Lift]", vec(255,255,255), ": This Part Is Not Supported")
            }
        }
        else{
            printColor(vec(200,50,50), "[Lift]", vec(255,255,255), ": No Base Found")
        }
    }
    elseif(R2:entity()){
        Ent = R2:entity()
        M = Ent:model():explode("/")[4,string]
        if(M:find("sbselevb") | M:find("sbbridgevisorb")){
            B = Ent
            Type = 1
        }
        elseif(M:find("sblelevb")){
            B = Ent
            Type = 2
        }
        elseif(M:find("sbselevslant")){
            printColor(vec(200,50,50), "[Lift]", vec(255,255,255), ": This Part Is Not Supported")
        }
    }
    else{
        printColor(vec(200,50,50), "[Lift]", vec(255,255,255), ": No Base Found")
    }
    
    if(B){
        Stops:pushArray(array(B, vec(0,0, B:boxCenter():z() - B:boxSize():z()/2 + 5)))
        R = rangerOffset(B:boxCenterW(), B:toWorld(B:boxCenter() + vec(0,0,10000)))
        
        if(R:entity()){
            findIncludePlayerProps(O)
            findInBox(B:toWorld(B:boxMin() / 2), B:toWorld(B:boxMax() / 2 + vec(0,0,R:distance())))
            findSortByDistance(B:boxCenterW())
            
            foreach(K,V:entity=findToArray()){
                M = V:model():explode("/")[4,string]
                if(Type == 1){
                    if(M:find("sbselevm") | M:find("sbbridgevisorm")){
                        Stops:pushArray(array(V, vec(0,0, V:boxCenter():z() - V:boxSize():z()/2 + 5)))
                    }
                    elseif(M:find("sbselevt") | M:find("sbbridgevisort")){
                        Stops:pushArray(array(V, vec(0,0, V:boxCenter():z() - V:boxSize():z()/2 + 5)))
                    }
                    elseif(M:find("sbhuble")){
                        Stops:pushArray(array(V, vec(0,0, V:boxCenter():z() - V:boxSize():z()/2 + 5)))
                        Stops:pushArray(array(V, vec(0,0, V:boxCenter():z() - 65.35 + 5)))
                        Stops:pushArray(array(V, vec(0,0, V:boxCenter():z() + 64.85 + 5)))
                    }
                }
                else{
                    if(M:find("sblelevm")){
                        Stops:pushArray(array(V, vec(0,0, V:boxCenter():z() - V:boxSize():z()/2 + 5)))
                    }
                    elseif(M:find("sblelevt")){
                        Stops:pushArray(array(V, vec(0,0, V:boxCenter():z() - V:boxSize():z()/2 + 5)))
                    }
                }
            }
            
            Que = Stops:count()
            printColor(vec(200,50,50), "[Lift]", vec(255,255,255), ": " + Que:toString() + " Stops Found")
        }
        else{
            printColor(vec(200,50,50), "[Lift]", vec(255,255,255), ": No Top Found")
        }
    }
}
elseif(dupefinished()){
    reset()
}
elseif(last()){
    for(I=1, Stops:count()){
        Stops[I,array][3,entity]:propDelete()
    }
    L:propDelete()
    Light:propDelete()
}

Either way, I would love to have some help in restoring this chip. Essentially what it does is make an elevator out of smallbridge elevator props automaticly. At some point I might consider learning all the things used in here and rewriting it to work with modbridge, although that could prove hard (and probably is).

The code doesn't throw out any errors of any kind, and the syntax is as far as I'm aware in good order. The E2 finds all the stops in a constraption and creates hatches on all the floors, enabling traversing over said floor without having the elevator be there. You can call for the elevator by pressing E on the prop if I remember correctly, and when on the elevator you can press either the big-ass green holo to go up or the big-ass red holo to go down. Both of thees holos are located on the floor of the elevator platform. Whenever it goes up or down, the floor that is located beneath or above it (depends on what direction it's going obviously) will be alerted that the elevator is comming with an alert effect thing being spawned in the centre of the prop.

I can only comprehend parts of the code, and I don't see anything that's obviously out of place. This code, as I remember it, makes for a very stable (non-murderating) and optimized elevator. I have yet to try it with GHD, and I don't think I'll have the chance unless I decide to learn E2 properly and throughoutly or get some help. This is why I'm here c:

TL;DR: I have an elevator code and it used to make a good elevator, please fix.

Leave a Comment

bolditalicunderlinestrikecodeimageurlquotespoiler
Drop image/file