global gPac, gpacw, whatkey, mHeight ,mWidth, gDirPac, newkey, gscore, ghostlist, characterlist

global gpacH, gpacV, pacStat, gMunched, --bober

global ghostathomelist, ghostActivelist

 

 

property gDirGhost, countme, nextdir, gGhostW, gGhost,

property gAccel, gCycle, gCycleNum, gStunTime, gStunlength

property dirlist, pathlist, homelist

property frontpix, leftpix, rightpix, downpix, uppix

property thequad, therep, repNum, totalreps,  targetV, targetH

property gDirV, gDirH, gDisV, gDisH, startRun, eatmetime, watchoutTime

Property ghActive, ghStage

 

---------------------------- USER DEFINED PROPERTYS ----------------------

 

Property pGhostName

Property iniGhostV, iniGhostH

Property pState, pStage

Property pDirGhost

 

--------------------------------------------------------------------------

 

on getpropertydescriptionlist

 

  plist = [:]

 

  plist [#pGhostName] = [#Comment:"Ghost Name", #Format:#Symbol, #Range:[#Blinky, #Pinky, #Inky, #Clyde], #Default: #Blinky]

  plist [#iniGhostV] = [#Comment:"Vertical Position", #Format:#Integer, #Range:[182, 225], #Default: 182]

  plist [#iniGhostH] = [#Comment:"Horisontal Position", #Format:#Integer, #Range:[203, 175, 232], #Default: 203]

 

 

  plist [#pState] = [#Comment:"Ghost State", #Format:#Symbol, #Range:[#on, #off, #Stun], #Default: #Stun]

  plist [#pStage] = [#Comment:"Ghost Stage", #Format:#String, #Range:["findHome", "athome", "leaveHome", "enterhome", "Run", "Hunt", "Path"], #Default: "Hunt"]

 

  plist [#pDirGhost] = [#Comment:"Initial Direction", #Format:#String, #Range:["up", "down", "left", "right"], #Default: "up"]

 

  return plist

 

end

 

 

on beginsprite me

 

  if pStage = "atHome" then

    ghostatHomelist.add(me.spritenum)

  else if pStage = "Path" then

    ghostActivelist.add(me)

  end if

 

  characterlist.add(me.spritenum)

 

  ghostlist.add(me)

 

  initialVars(me)

 

  pathlist = getGhostPath(pGhostName)

 

 

 

end

 

 

 

on initialVars me

 

 

 

  nextdir = 0

  dirlist = []

 

  totalReps = 0

 

  gCycleNum = 4

  gAccel = 0

 

  eatmetime = 200

  watchoutTime = 100

  gStunlength = 40

 

 

--  bober = sprite(me.spritenum)

  gGhost = sprite(me.spritenum)

 

  gGhost.locz = 500

 

  gghost.visible = False

  --  gGhost.playBackMode = #lockStep

    gghost.stop()

 

  ghactive = pState

  ghstage = pStage

 

  --  mWidth = (the stage).rect.width

  --  mHeight = (the stage).rect.height

 

  gGhostW = gGhost.width /2

 

 

 

  gDirGhost = pDirGhost

 

 

  gGhost.loch = iniGhostH

  gGhost.locv = iniGhostV

 

 

end

 

 

on enterframe me

 

  GhostAI(me

 

  ghostBoundaries(me)

 

end

 

 

on exitframe me

 

  if ghactive = #on then

   

    if gpac.loc.inside(gghost.rect.inflate(-2, -2)) then

     

      paclives("subtract")

      gpac.stop()

      exit

     

    end if

   

   

   

  else if ghstage = "run" then

   

    if gpac.loc.inside(gghost.rect.inflate(-2, -2)) then

     

      if countme then

       

        countme = 0

       

        ghostEaten(me)

       

        gscore = gscore + (value(ghostpoints()))

       

        member("tscoreval").text = string(gscore)

       

        if string(gscore) > value(member("thighval").text) then

          member("thighval").text = member("tscoreval").text

        end if

       

        exit

       

      end if

     

    end if

   

  end if

end

 

 

on ghostEaten me

 

  gMunched = gMunched + 1

 

  call (#ghostStun, ghostlist)

 

  pacStat = #stun

 

  ghstage = "findHome" --<--- This can only be set after the ghostStun handler is called

 

  gpac.visible = false

 

  member("pointAward").text = ghostpoints()

 

  gghost.member = "pointAward"

 

  gStunTime = the ticks

 

end

 

 

on ghostStun me

 

  if ghstage <> "findhome" then

   

    startRun = (startRun + gStunlength)

    ghActive = #stun

   

  end if

 

end

 

 

on ghostpoints me

 

  case gMunched of

     

    1:

      return string(200)

     

    2:

      return string(400)

     

    3:

      return string(800)

     

    4:

      return string(1600)  

  end case

 

end

 

 

on ghostActivate me, action

 

  ghostvisible(me)

 

  case ghstage of

     

    "findHome", "athome", "leaveHome", "run", "enterhome":

     

      ghactive = #off

     

    otherwise

     

      ghactive = #on

     

  end case

 

  case action of

    #play:

      gghost.play()

  end case

end

 

 

on ghostRun me

  startRun = the ticks

  gMunched = 0

 

  if ghactive = #on then

   

    ghactive = #off

    ghstage = "run"

    countme = 1

    gdirghost = changeDir()

    ghRepNumber(me)

    gghost.member = "eatme"

   

  end if

 

end

 

 

 

on ghostvisible me

 

  gghost.visible = True

  gghost.member = string(pGhostName)

 

end

 

 

on ghostVanish me

 

  initialVars(me)

 

end

 

 

on ghostLeaveHome me

  if ghstage = "athome" then

    ghActive = #off

    ghstage = "leaveHome"

    ghostactivelist.add(me)

  end if

end

 

 

on ghostHunt me

  if ghstage = "path" then

   

    gdirghost = changeDir()

    ghRepNumber(me)

   

    ghstage = "hunt"

   

  end if

end

 

-----------------------------------

 

 

on GhostAI me

 

 

  case ghactive of

     

    #on:

     

      case ghstage of

         

        "random":

          ghostPath(me)

         

        "path":

          ghostPath(me)

          

        "Hunt":

          ghostPath(me)

         

      end case

     

    #off:

     

      case ghstage of

         

        "run":

          ghostPath(me)

         

        "athome": ----------- Pace in the home

          ghostPath(me)

         

        "leaveHome": -------- Come out of home

          ghostPath(me)

         

        "findHome": --------- Find way home

          ghostPath(me)

         

        "enterhome":

          ghostPath(me)

         

      end case 

     

     

    #stun:

     

      case ghstage of

         

        "findhome":

         

          if the ticks > (gStunTime + gStunlength) then

            call(#ghostActivate, ghostlist)

              gghost.member = "eatme"

           

            pacStat = void

            gpac.visible = true

          end if

         

      end case    

     

  end case

 

end

 

 

on changeDir me

 

  case gDirGhost of

     

    "up":

      return "down"

     

    "down":

      return "up"

     

    "left":

      return "right"

     

    "right":

      return "left"

     

  end case

 

end

 

 

on GetDir me

 

  case ghstage of

     

    "random", "run":

     

      return dirlist.getat(random(dirlist.count))

     

    "path":

     

      if nextdir = pathlist.count then

       

        ghstage = "Hunt"

        nextdir = 0

        return pathlist.getat(pathlist.count)

       

      else

       

        nextdir = nextdir + 1

        return pathlist.getat(nextdir)

       

      end if

     

    "leaveHome":

     

      if gghost.loch = (mWidth/2) then

        return "up"

       

      else if gghost.locv > iniGhostV then

        return "up"

       

      else if gghost.locv < iniGhostV then

        return "down"

       

      else if gghost.loch > (mWidth/2) then

        return "left"

       

      else if gghost.loch < (mWidth/2) then

        return "right"

       

      end if

     

     

    "enterHome":

     

      if gghost.locv <> 225 then

        return "down"

       

      else if iniGhostH < gghost.loch then

        return "left"

       

      else if iniGhostH > gghost.loch then

        return "right"

       

      end if

     

     

    "findHome", "Hunt":

     

      repeat with i in homelist

       

        searchDir = dirlist.getpos(i)

        if searchDir <> 0 then

          return i

          exit

        end if

       

      end repeat

     

      return dirlist.getat(random(dirlist.count))

     

     

  end case

 

end

 

 

on pathHome me

 

  case ghstage of

     

     

    "findhome", "hunt":

     

      homelist = [] 

      whatQuad(me)

     

     

  end case

end me

 

 

on whatQuad me

  case ghstage of

     

    "findhome":

     

      targetV =  182 --(mheight/2)

      targetH = (mWidth/2)

     

     

      gDisV = gghost.locv - targetV

      if gDisV = gDisV.abs then

        gDirV = "up"

      else

        gDirV = "down"

      end if

     

     

      gDisH = gghost.loch - targetH

      if gDisH = gDisH.abs then

        gDirH = "left"

      else

        gDirH = "right" 

      end if

     

     

      if gDisV.abs > gDisH.abs then

        homelist.add(gDirV)

        homelist.add(gDirH)

      else

        homelist.add(gDirH)

        homelist.add(gDirV)

      end if

     

     

     

    "Hunt":

     

      targetV = gpac.locv

      targetH = gpac.loch

     

     

      gDisV = gghost.locv - targetV

      if gDisV = gDisV.abs then

        gDirV = "up"

      else

        gDirV = "down"

      end if

     

     

      gDisH = gghost.loch - targetH

      if gDisH = gDisH.abs then

        gDirH = "left"

      else

        gDirH = "right" 

      end if

     

      if pGhostName = #Pinky then

        ambush(me)

      end if

     

      if gDisV.abs > gDisH.abs then

        homelist.add(gDirV)

        homelist.add(gDirH)

      else

        homelist.add(gDirH)

        homelist.add(gDirV)

      end if

     

     

  end case

end

 

 

on ambush me

  case gDirGhost of

     

    "up", "down":

     

      if gDisV.abs < 60 then

        homelist.add(gDirH)

        homelist.add(gDirV)

        exit

      end if

     

    "left", "right":

     

      if gDisH.abs < 50 then

        homelist.add(gDirV)

        homelist.add(gDirH)

        exit

      end if

  end case

end

 

 

on ghRepNumber me

 

  case ghStage of

     

    "path":

      totalreps = 4

     

    "Random":

      totalreps = 4

     

    "Run":

      totalreps = 1

     

    "findHome":

      totalreps = 10

     

    "enterHome":

      totalreps = 2

     

    "enterHome":

      totalreps = 1

     

    "atHome":

      totalreps = 1

     

    "leaveHome":

      totalreps = 1

     

    "Hunt":

     

      totalreps = 4

     

      totalreps = totalreps + gAccel

     

  end case

 

  gCycle = gCycle + 1

 

  if gCycle < gCycleNum then

    gAccel = 0

  else

    gAccel = 1

    gCycle = 0

  end if

 

 

end

 

 

on makeDirlist me

 

  if uppix = 3 then

    dirlist.add("up")

  end if

 

  if downpix = 3 then

    dirlist.add("down"

  end if

 

  if leftpix = 3 then

    dirlist.add("left")

  end if

 

  if rightpix = 3 then

    dirlist.add("right")

  end if

 

end

 

on ghostPath me

 

  case ghstage of

     

    "atHome":

     

      repeat with repNum = totalreps down to 0

       

        if (gGhost.locv ) = (iniGhostV - 10) or (gGhost.locv ) = (iniGhostV + 10) then

         

          gDirGhost = changeDir()

          ghostmove(me)

         

          exit

        end if

       

        ghostmove(me)

       

      end repeat

      ghRepNumber(me)

     

     

    "leaveHome":

     

      repeat with repNum = totalreps down to 0

       

        gDirGhost = GetDir()

       

        findoption(me)

        if leftpix = 3 then

         

          ghactive = #on

          ghstage = "Path"

          nextdir = 0

          gDirGhost = GetDir()

         

          exit

        end if

       

        ghostmove(me)

       

      end repeat

      ghRepNumber(me)

     

     

    "findHome":

     

      repeat with repNum = totalreps down to 0

        dirlist = []

        findoption(me)

        makeDirlist(me)

        dirlist.deleteone(changeDir())

       

        crossroads(me)

       

        if gghost.loch = (mWidth/2) and gghost.locv = 182 then

          ghstage = "enterHome"

         

          exit

        end if

       

        ghostmove(me)

       

      end repeat

      ghRepNumber(me)

     

     

    "enterHome":

     

      repeat with repNum = totalreps down to 0

       

        gDirGhost = GetDir()

       

        if gghost.locv = 225 then

         

          ghstage = "leaveHome"

          gghost.member = string(pGhostName)

         

          exit

        end if

       

        ghostmove(me)

       

      end repeat

      ghRepNumber(me)

     

     

    "Hunt":

     

      repeat with repNum = totalreps down to 0

       

        dirlist = []

        findoption(me)

        makeDirlist(me)

        dirlist.deleteone(changeDir())

       

        crossroads(me)

       

        ghostmove(me)

       

      end repeat

      ghRepNumber(me)   

     

     

     

    otherwise

     

     

      if ghstage = "run" then

       

        if the ticks > (startRun + eatmeTime) then

          gghost.member = "watchOut"

          if the ticks > (startRun + eatmeTime + watchoutTime) then

            gMunched = 0

            ghstage = "Random"

            ghactive = #on

            gghost.member = string(pGhostName)

            exit   

          end if

         

        else

         

          gghost.member = "eatme"

         

        end if

       

      end if

     

     

      repeat with repNum = totalreps down to 0

        dirlist = []

        findoption(me)

        makeDirlist(me)

        dirlist.deleteone(changeDir())

       

        crossroads(me)

       

        ghostmove(me)

       

      end repeat

      ghRepNumber(me)

     

  end case

 

end

 

 

on crossroads (me)

 

 

  case ghstage of

     

    "findHome", "Hunt":

     

      case gDirGhost of

         

        "up","down":

         

          if leftpix = 3 or rightpix = 3 then

           

            pathhome(me)

            gDirGhost = GetDir()

           

            ghostmove(me)

            totalreps = (repNum-1)

            

            ghostPath(me)

           

            abort

          end if

         

         

        "left","right":

         

          if uppix = 3 or downpix = 3 then

           

            pathhome(me)

            gDirGhost = GetDir()

           

            ghostmove(me)

            totalreps = (repNum-1)

           

            ghostPath(me)

           

            abort

          end if

         

      end case

     

     

     

    otherwise

     

     

      case gDirGhost of

         

        "up","down":

         

          if leftpix = 3 or rightpix = 3 then

            gDirGhost = GetDir()

           

            ghostmove(me)

            totalreps = (repNum-1)

           

            ghostPath(me)

           

            abort

          end if

         

         

        "left","right":

         

          if uppix = 3 or downpix = 3 then

            gDirGhost = GetDir()

           

            ghostmove(me)

            totalreps = (repNum-1)

           

            ghostPath(me)

           

            abort

          end if

         

      end case

  end case  

end

 

 

on ghostmove me

 

  case gDirGhost of

     

    "up":

      gGhost.locv = gGhost.locv - 1

     

    "down":

      gGhost.locv = gGhost.locv + 1

     

    "left":

      gGhost.loch = gGhost.loch - 1

     

    "right":

      gGhost.loch = gGhost.loch + 1

     

  end case

 

end

 

 

on findpath me

 

  if gDirGhost = "left" then

    frontpix = member("path").image.getpixel ((gGhost.loch - 1), (gGhost.locv), #integer)

  else if gDirGhost = "right" then

    frontpix = member("path").image.getpixel ((gGhost.loch + 1), (gGhost.locv), #integer)

  else if gDirGhost = "down" then

    frontpix = member("path").image.getpixel ((gGhost.loch), (gGhost.locv + 1), #integer)

  else if gDirGhost = "up" then

    frontpix = member("path").image.getpixel ((gGhost.loch), (gGhost.locv - 1), #integer)

  end if

 

  findoption(me)

 

end

 

 

on findoption me

 

  leftpix    = member("path").image.getpixel ((gGhost.loch - gGhostW - 1), (gGhost.locv), #integer)

  rightpix   = member("path").image.getpixel ((gGhost.loch + gGhostW + 1), (gGhost.locv), #integer)

  uppix      = member("path").image.getpixel ((gGhost.loch), (gGhost.locv - gGhostW - 1), #integer)

  downpix    = member("path").image.getpixel ((gGhost.loch), (gGhost.locv + gGhostW + 1), #integer)

 

end

 

 

on ghostBoundaries me ---- To check if this sprite has gone off the stage Then set it's new location

 

  if gGhost.loch - gGhostW > mWidth then

    gGhost.loch = 0 - gGhostW

    exit

   

  else if gGhost.locv - gGhostW > mHeight then 

    gGhost.locv = 0 - gGhostW

    exit

   

  else if gGhost.loch + gGhostW < 0 then

    gGhost.loch = mWidth + gGhostW

    exit

   

  else if gGhost.locv + gGhostW < 0 then

    gGhost.locv = mHeight + gGhostW

    exit

   

  end if

 

end

 

 

on getGhostPath pGhostName

 

  case pGhostName of

     

    #Blinky:

      return ["left","left","down","left","up","up","right","right","right","right","right","up","right","down","left","down","right","up","left","down"]

     

    #Pinky:

      return ["left","left","down","left","up","up","up","left","down","right","up","left","down","right"]

     

    #Inky:

      return ["left","left","down","left","down","down","down","left","left","down","right","up","left","up","right","up","left","up","up","left"]

     

    #Clyde:

      return ["right","right","down","right","down","down","down","right","right","down","left","up","right","up","left","up","right","up","up","right"]

     

  end case

 

end