(include "sci.sh")
(use "Main")
(use "PolyPath")
(use "CueObj")
(use "Obj")
(script 950)



(class CueObj of Script
    (properties
        client 0
        state -1
        start 0
        timer 0
        cycles 0
        seconds 0
        lastSeconds 0
        ticks 0
        lastTicks 0
        register 0
        script 0
        caller 0
        next 0
        theVerb 0
    )

    (method (changeState newState)
        (switch (= state newState)
            (case 1
                ((send gEgo:setHeading(GetAngle((send gEgo:x) (send gEgo:y) (send client:x) (send client:y)) self)) (send gTheDoits:add(self))  )
            )
            (case 2
                (= cycles 3  )
            )
            (case 3
                ((send gTheDoits:delete(self)) 
                (if (not 
                (if (IsObject(client) and IsObject((send client:actions)))
                    (send ((send client:actions)):doVerb(theVerb))
                )
)
                    (send client:doVerb(theVerb))
                )
 = state 0 )
            )
        )
    )

)
(class Feature of Obj
    (properties
        x 0
        y 0
        z 0
        heading 0
        noun 0
        modNum -1
        nsTop 0
        nsLeft 0
        nsBottom 0
        nsRight 0
        sightAngle 26505
        actions 0
        onMeCheck 26505
        state 0
        approachX 0
        approachY 0
        approachDist 0
        _approachVerbs 0
    )

    (method (init param1)
        (self:initialize(
            (if (paramTotal)
                param1
            )(else
                0
            )
))
        (if ((self:respondsTo(5)))
            (send gCast:add(self))
        )(else
            (send gFeatures:add(self))
        )
    )


    (method (dispose)
        (if (actions)
            (send actions:dispose())
            = actions 0
        )
        (if (IsObject(onMeCheck) and not (& state $0004))
            (send onMeCheck:dispose())
            = onMeCheck 0
        )
        (send gFeatures:delete(self))
        (if (& state $0001)
            Memory(3 name)
            = name 0
        )
        (super:dispose())
    )


    (method (initialize param1)
        (if (paramTotal and param1)
            (self:perform(param1))
        )(else
            (if (gLb2FtrInit)
                (self:perform(gLb2FtrInit))
            )
        )
    )


    (method (handleEvent pEvent)
        (if ((send pEvent:claimed))
            return 1
        )(else
            (if (((& (send pEvent:type) $4000) and (self:onMe(pEvent))) and (self:isNotHidden()))
                (CueObj:
                    state(0)
                    cycles(0)
                    client(self)
                    theVerb((send pEvent:message))
                )
                (send pEvent:claimed(1))
                (if (((((send gUser:canControl()) and (& (send gEgo:state) $0002)) and (> GetDistance((send gEgo:x) (send gEgo:y) approachX approachY) approachDist)) and gLb2ApproachCode) and (& _approachVerbs (send gLb2ApproachCode:doit((send pEvent:message)))))
                    (send gEgo:setMotion(PolyPath approachX (+ (send gEgo:z) approachY) CueObj))
                )(else
                    (send gEgo:setMotion(0))
                    (if ((self:facingMe()))
                        (CueObj:changeState(3))
                    )
                )
            )
        )
        return (send pEvent:claimed)
    )


    (method (doVerb param1)
        (var temp0, temp1)
        = temp0 
            (if (gLb2DoVerbCode)
            )(else
                dftDoVerb
            )
        (if (== modNum -1)
            = modNum gModNum
        )
        (if (global90 and GetMessage(0 modNum noun param1 0 1))
            (send gTestMessager:say(noun param1 0 0 0 modNum))
        )(else
            (send temp0:doit(param1 self))
        )
    )


    (method (notFacing)
        (var temp0)
        (send gEgo:setMotion(0))
        (CueObj:
            client(self)
            state(0)
            cycles(0)
            cue()
        )
    )


    (method (facingMe theTheGEgo)
        (var theGEgo, temp1)
        (if (paramTotal)
            = theGEgo theTheGEgo
        )(else
            (if ((send gCast:contains(gEgo)))
                = theGEgo gEgo
            )(else
                return 1
            )
        )
        (if (> (= temp1 Abs((- GetAngle((send theGEgo:x) (send theGEgo:y) x y) (send theGEgo:heading)))) 180)
            = temp1 (- 360 temp1)
        )
        return 
            (if (<= temp1 sightAngle)
                return 1
            )(else
                (if (<> sightAngle 26505)
                    (self:notFacing())
                )
                return 0
            )
    )


    (method (isNotHidden)
        return 1
    )


    (method (onMe param1 param2)
        (var temp0, temp1)
        (if (IsObject(param1))
            = temp0 (send param1:x)
            = temp1 (send param1:y)
        )(else
            = temp0 param1
            = temp1 param2
        )
        return 
            (if (& state $0004)
                (if (not 
                (if ((nsLeft or nsRight) or nsTop)
                )(else
                    nsBottom
                )
 or ((((<= nsLeft temp0) and (<= temp0 nsRight)) and (<= nsTop temp1)) and (<= temp1 nsBottom)))
                    & onMeCheck OnControl(4 temp0 temp1)
                )(else
                    0
                )
            )(else
                (if (IsObject(onMeCheck))
                    AvoidPath(temp0 temp1 onMeCheck)
                )(else
                    (if (not 
                    (if ((nsLeft or nsRight) or nsTop)
                    )(else
                        nsBottom
                    )
 or ((((<= nsLeft temp0) and (<= temp0 nsRight)) and (<= nsTop temp1)) and (<= temp1 nsBottom)))
                        1
                    )(else
                        0
                    )
                )
            )
    )


    (method (approachVerbs param1)
        (var temp0, temp1)
        = _approachVerbs 0
        (if ((paramTotal and gLb2ApproachCode) and param1[0])
            = temp0 0
            (while (< temp0 paramTotal)
                = temp1 (send gLb2ApproachCode:doit(param1[temp0]))
                (self:_approachVerbs((| (self:_approachVerbs) temp1)))
                ++temp0
            )
        )
    )


    (method (setName param1)
        = state (| state $0001)
        = name Memory(1 (+ StrLen(param1) 1))
        StrCpy(name param1)
    )


    (method (setOnMeCheck theOnMeCheck theOnMeCheck_2)
        (var temp0)
        (switch (theOnMeCheck)
            (case 26505
                (= onMeCheck theOnMeCheck = state (& state $fffb)  )
            )
            (case 2
                (= onMeCheck theOnMeCheck_2[0] = state (& state $fffb)  )
            )
            (case 1
                (= temp0 (= onMeCheck 0) (while (< temp0 (- paramTotal 1))= onMeCheck (| onMeCheck theOnMeCheck_2[temp0]) ++temp0  ) = state (| state $0004) )
            )
        )
    )

)
(instance dftDoVerb of Code
    (properties)

    (method (doit)
        return 1
    )

)
(class Actions of Code
    (properties)

    (method (doVerb)
        return 0
    )

)
