Platformer AI Engine v1.5
@Stage\n\n@Player\n\ndefine physics\nset [🔒 xv v] to ((🔒 xv) * (0.8))\nchange x by (🔒 xv)\nset [🔒 slope v] to [1]\nrepeat until <<(🔒 slope) > [5]> or <not <touching (map v)?>>>\n    change [🔒 slope v] by (1)\n    change y by (1)\nend\nif <(🔒 slope) > [5]> then\n    change y by (-5)\n    repeat until <not <touching (map v)?>>\n        change x by ((([abs v] of (🔒 xv) ) / (🔒 xv)) * (-2.5))\n    end\n    set [🔒 xv v] to [0]\nend\nif <not <touching (map v)?>> then\n    change [🔒 yv v] by (-1)\nend\nchange y by (🔒 yv)\nif <touching (map v)?> then\n    repeat until <not <touching (map v)?>>\n        change y by ((([abs v] of (🔒 yv) ) / (🔒 yv)) * (-1))\n    end\n    set [🔒 yv v] to [0]\nend\n\nwhen flag clicked\nset size to (40) %\nshow\nforever\n    broadcast (reset v)\n    set [🔒 xv v] to [0]\n    set [🔒 yv v] to [0]\n    set [🔒 slope v] to [0]\n    repeat until <<(y position) < [-178]> or <touching (ai v)?>>\n        physics\n        change [🔒 xv v] by (<<<key (d v) pressed?> or <key (right arrow v) pressed?>> or <<mouse down?> and <(mouse x) > [0]>>> - <<<key (a v) pressed?> or <key (left arrow v) pressed?>> or <<mouse down?> and <(mouse x) < [0]>>>)\n        change y by (-1)\n        if <<touching (map v)?> and <<<key (w v) pressed?> or <key (up arrow v) pressed?>> or <<mouse down?> and <(mouse y) > [0]>>>> then\n            set [🔒 yv v] to [16]\n        end\n        change y by (1)\n    end\nend\n\nwhen I receive [reset v]\nif <<([costume # v] of [map v]) = [1]> or <([costume # v] of [map v]) = [2]>> then\n    go to x: (0) y: (0)\nend\nif <([costume # v] of [map v]) = [3]> then\n    go to x: (0) y: (120)\nend\nif <([costume # v] of [map v]) > [3]> then\n    go to x: (170) y: (50)\nend\nif <([costume # v] of [map v]) = [7]> then\n    go to x: (0) y: (0)\nend\nset [🔒 xv v] to [0]\nset [🔒 yv v] to [0]\nset [🔒 slope v] to [0]\n\n@AI\n\ndefine physics\nset [🔒 xv v] to ((🔒 xv) * (0.8))\nchange x by (🔒 xv)\nset [🔒 slope v] to [1]\nrepeat until <<(🔒 slope) > [5]> or <not <touching (map v)?>>>\n    change [🔒 slope v] by (1)\n    change y by (1)\nend\nif <(🔒 slope) > [5]> then\n    change y by (-5)\n    repeat until <not <touching (map v)?>>\n        change x by ((([abs v] of (🔒 xv) ) / (🔒 xv)) * (-2.5))\n    end\n    set [🔒 xv v] to [0]\nend\nif <not <touching (map v)?>> then\n    change [🔒 yv v] by (-1)\nend\nchange y by (🔒 yv)\nif <touching (map v)?> then\n    repeat until <not <touching (map v)?>>\n        change y by ((([abs v] of (🔒 yv) ) / (🔒 yv)) * (-1))\n    end\n    set [🔒 yv v] to [0]\nend\n\nwhen flag clicked\nset size to (40) %\nshow\n\ndefine handle: player below\nif <([x position v] of [player v]) > (x position)> then\n    set [🔒 decision v] to [right]\nelse\n    set [🔒 decision v] to [left]\nend\nset [🔒 last x v] to [9999]\nset [🔒 last y v] to (y position)\nrepeat until <<<([abs v] of (([y position v] of [player v]) - (y position)) ) < [5]> or <((y position) + (5)) < (🔒 last y)>> or <<(y position) < [-178]> or <touching (player v)?>>>\n    if <(🔒 decision) = [right]> then\n        change [🔒 xv v] by (1)\n    else\n        change [🔒 xv v] by (-1)\n    end\n    change y by (-1)\n    if <touching (map v)?> then\n        change y by (1)\n        change x by (([abs v] of (🔒 xv) ) / (🔒 xv))\n        if <touching (map v)?> then\n            set [🔒 last x v] to (x position)\n            set [🔒 yv v] to [16]\n        end\n        change x by ((0) - (([abs v] of (🔒 xv) ) / (🔒 xv)))\n    else\n        change y by (1)\n    end\n    if <([abs v] of ((🔒 last x) - (x position)) ) < [3]> then\n        set [🔒 last x v] to [9999]\n        if <(🔒 decision) = [right]> then\n            set [🔒 decision v] to [left]\n        else\n            set [🔒 decision v] to [right]\n        end\n    end\n    physics\nend\n\ndefine handle: player above\nif <([x position v] of [player v]) > (x position)> then\n    set [🔒 decision v] to [right]\nelse\n    set [🔒 decision v] to [left]\nend\nset [🔒 last x v] to [9999]\nset [🔒 done? v] to [false]\nrepeat until <<(🔒 done?) = [true]> or <<(y position) < [-178]> or <touching (player v)?>>>\n    change y by (-1)\n    if <touching (map v)?> then\n        change y by (1)\n        find above platform limit: [90]\n        change y by (-1)\n        if <(🔒 collision?) = [true]> then\n            if <(🔒 decision) = [right]> then\n                change [🔒 xv v] by (1)\n            else\n                change [🔒 xv v] by (-1)\n            end\n        else\n            if <(🔒 decision) = [right]> then\n                change [🔒 xv v] by (1)\n            else\n                change [🔒 xv v] by (-1)\n            end\n            change y by (-1)\n            if <touching (map v)?> then\n                set [🔒 yv v] to [16]\n            end\n            change y by (1)\n            physics\n            change y by (-1)\n            repeat until <touching (map v)?>\n                change y by (1)\n                change [🔒 xv v] by (<([x position v] of [player v]) > (x position)> - <([x position v] of [player v]) < (x position)>)\n                physics\n                change y by (-1)\n            end\n            set [🔒 done? v] to [true]\n        end\n    end\n    change y by (1)\n    change y by (-1)\n    if <touching (map v)?> then\n        change y by (1)\n        change x by (([abs v] of (🔒 xv) ) / (🔒 xv))\n        if <touching (map v)?> then\n            set [🔒 last x v] to (x position)\n            set [🔒 yv v] to [16]\n        end\n        change x by ((0) - (([abs v] of (🔒 xv) ) / (🔒 xv)))\n    else\n        change y by (1)\n    end\n    if <([abs v] of ((🔒 last x) - (x position)) ) < [3]> then\n        set [🔒 last x v] to [9999]\n        if <(🔒 decision) = [right]> then\n            set [🔒 decision v] to [left]\n        else\n            set [🔒 decision v] to [right]\n        end\n    end\n    physics\nend\n\nwhen I receive [reset v]\nstop [other scripts in sprite v]\nif <<([costume # v] of [map v]) = [1]> or <<([costume # v] of [map v]) = [2]> or <([costume # v] of [map v]) = [4]>>> then\n    go to x: (0) y: (120)\nend\nif <([costume # v] of [map v]) = [3]> then\n    go to x: (0) y: (0)\nend\nif <([costume # v] of [map v]) > [3]> then\n    go to x: (-170) y: (50)\nend\nif <([costume # v] of [map v]) = [7]> then\n    go to x: (0) y: (120)\nend\nset [🔒 xv v] to [0]\nset [🔒 yv v] to [0]\nset [🔒 slope v] to [0]\nshow\nrepeat until <(y position) < [-178]>\n    change [🔒 xv v] by (<([x position v] of [player v]) > (x position)> - <([x position v] of [player v]) < (x position)>)\n    change y by (-1)\n    if <touching (map v)?> then\n        change y by (1)\n        change x by (([abs v] of (🔒 xv) ) / (🔒 xv))\n        if <touching (map v)?> then\n            set [🔒 yv v] to [16]\n        end\n        change x by ((0) - (([abs v] of (🔒 xv) ) / (🔒 xv)))\n        change x by ((30) * (([abs v] of (🔒 xv) ) / (🔒 xv)))\n        change y by (-1)\n        if <not <touching (map v)?>> then\n            find adjacent platform limit: [90]\n            if <(🔒 collision?) = [true]> then\n                set [🔒 yv v] to [16]\n            else\n                find below platform limit: [200]\n                if <(🔒 collision?) = [false]> then\n                    change y by (1)\n                    change x by ((-30) * (([abs v] of (🔒 xv) ) / (🔒 xv)))\n                    handle: gap\n                    change y by (-1)\n                    change x by ((30) * (([abs v] of (🔒 xv) ) / (🔒 xv)))\n                end\n            end\n            change y by (1)\n            change x by ((-30) * (([abs v] of (🔒 xv) ) / (🔒 xv)))\n        else\n            change y by (1)\n            change x by ((-30) * (([abs v] of (🔒 xv) ) / (🔒 xv)))\n        end\n    else\n        change y by (1)\n    end\n    physics\n    if <([abs v] of (([x position v] of [player v]) - (x position)) ) < [3]> then\n        if <([y position v] of [player v]) < (y position)> then\n            handle: player below\n        end\n        if <([y position v] of [player v]) > (y position)> then\n            handle: player above\n        end\n    end\nend\nbroadcast (reset v)\n\ndefine find above platform limit: (limit)\nset [🔒 counter v] to [0]\nrepeat until <<touching (map v)?> or <<(🔒 counter) > (limit)> or <touching (_edge_ v)?>>>\n    change [🔒 counter v] by (1)\n    change y by (1)\nend\nif <touching (map v)?> then\n    set [🔒 collision? v] to [true]\nelse\n    set [🔒 collision? v] to [false]\nend\nchange y by ((0) - (🔒 counter))\n\ndefine handle: gap\nif <(([abs v] of (🔒 xv) ) / (🔒 xv)) = [1]> then\n    set [🔒 decision v] to [left]\nelse\n    set [🔒 decision v] to [right]\nend\nset [🔒 last x v] to [9999]\nset [🔒 last y v] to (y position)\nrepeat until <<((y position) + (5)) < (🔒 last y)> or <<(y position) < [-178]> or <touching (player v)?>>>\n    if <(🔒 decision) = [right]> then\n        change [🔒 xv v] by (1)\n    else\n        change [🔒 xv v] by (-1)\n    end\n    change y by (-1)\n    if <touching (map v)?> then\n        change y by (1)\n        change x by (([abs v] of (🔒 xv) ) / (🔒 xv))\n        if <touching (map v)?> then\n            set [🔒 last x v] to (x position)\n            set [🔒 yv v] to [16]\n        end\n        change x by ((0) - (([abs v] of (🔒 xv) ) / (🔒 xv)))\n    else\n        change y by (1)\n    end\n    if <([abs v] of ((🔒 last x) - (x position)) ) < [3]> then\n        set [🔒 last x v] to [9999]\n        if <(🔒 decision) = [right]> then\n            set [🔒 decision v] to [left]\n        else\n            set [🔒 decision v] to [right]\n        end\n    end\n    physics\nend\n\ndefine find adjacent platform limit: (limit)\nset [🔒 counter v] to [0]\nrepeat until <<touching (map v)?> or <<([abs v] of (🔒 counter) ) > (limit)> or <touching (_edge_ v)?>>>\n    change [🔒 counter v] by (([abs v] of (🔒 xv) ) / (🔒 xv))\n    change x by (([abs v] of (🔒 xv) ) / (🔒 xv))\nend\nif <touching (map v)?> then\n    set [🔒 collision? v] to [true]\nelse\n    set [🔒 collision? v] to [false]\nend\nchange x by ((0) - (🔒 counter))\n\ndefine find below platform limit: (limit)\nset [🔒 counter v] to [0]\nrepeat until <<touching (map v)?> or <<(🔒 counter) > (limit)> or <touching (_edge_ v)?>>>\n    change [🔒 counter v] by (1)\n    change y by (-1)\nend\nif <touching (map v)?> then\n    set [🔒 collision? v] to [true]\nelse\n    set [🔒 collision? v] to [false]\nend\nchange y by (🔒 counter)\n\n@Map\n\nwhen flag clicked\ngo to x: (0) y: (0)\nset size to (100) %\nswitch costume to (map1 v)\nshow\nforever\n    wait until <key (space v) pressed?>\n    next costume\n    if <(costume [name v]) = [map7]> then\n        switch costume to (map1 v)\n    end\n    broadcast (reset v)\n    wait until <not <key (space v) pressed?>>\nend\n\n@Thumb\n\nwhen flag clicked\nhide\nforever\n    reset timer\nend\n\nwhen [timer v] > (0)\ngo to x: (0) y: (0)\nswitch costume to (dummy v)\nset size to (100) %\nswitch costume to (thumb v)\ngo to [front v] layer\nshow\n\n
WAD / arrow keys / touch to move\nSpace to change map\n\nAI can target you if you're above or below it - just see for yourself\n\nMay be buggy sometimes but it works for the most part\n\n▶ Feel free to use with credit\n\nThe AI may not work in every situation depending on the game you're trying to make - if you have any questions, feel free to ask\n\n▼ Updates\nv1.1 - Bug fix\nv1.2 - More bug fixes/improvements\nv1.3 - Reshared to make huge improvement to code which reduces the chances of bugs\nv1.4 - Improvements | AI can now jump over gaps that it is capable of crossing and taking a different route if the gap is too large | new maps (also includes new bugs)\nv1.5 - Fixed edge glitch\n\n#ai #games #platformer #platformers #tutorials #pathfinding