AI and Pathfinding CS 4730 Computer Game Design - - PowerPoint PPT Presentation

ai and pathfinding
SMART_READER_LITE
LIVE PREVIEW

AI and Pathfinding CS 4730 Computer Game Design - - PowerPoint PPT Presentation

AI and Pathfinding CS 4730 Computer Game Design Some slides courtesy Tiffany Barnes, NCSU CS 4730 AI Strategies ReacAon vs. DeliberaAon


slide-1
SLIDE 1

CS ¡4730 ¡

AI ¡and ¡Pathfinding ¡

CS ¡4730 ¡– ¡Computer ¡Game ¡Design ¡ ¡ Some ¡slides ¡courtesy ¡Tiffany ¡Barnes, ¡NCSU ¡

slide-2
SLIDE 2

CS ¡4730 ¡

AI ¡Strategies ¡

  • ReacAon ¡vs. ¡DeliberaAon ¡
  • When ¡having ¡the ¡NPC ¡make ¡a ¡decision, ¡how ¡

much ¡thought ¡goes ¡into ¡the ¡next ¡move? ¡

  • How ¡is ¡the ¡AI ¡different ¡in: ¡

– Frozen ¡Synapse ¡ – Kingdom ¡Hearts ¡ – CivilizaAon ¡ – Halo ¡

2

slide-3
SLIDE 3

CS ¡4730 ¡

AI ¡Strategies ¡

  • ReacAon-­‑Based ¡

– Fast, ¡but ¡limited ¡capabiliAes ¡

  • ImplementaAons ¡

– Finite-­‑State ¡Machines ¡ – Rule-­‑Based ¡Systems ¡ – Set ¡PaVern ¡

3

slide-4
SLIDE 4

CS ¡4730 ¡

AI ¡Strategies ¡

  • DeliberaAon-­‑Based ¡

– Much ¡slower, ¡but ¡more ¡adaptable ¡

  • ImplementaAons ¡

– A* ¡/ ¡Dijkstra ¡ – Roadmaps ¡ – GeneAc ¡Algorithms ¡

4

slide-5
SLIDE 5

CS ¡4730 ¡

DeliberaAon ¡

  • Goal ¡is ¡to ¡emphasize ¡making ¡the ¡best ¡possible ¡

decision ¡by ¡searching ¡across ¡all ¡possibiliAes ¡

  • Thus, ¡deliberaAon ¡tends ¡to ¡use ¡various ¡search ¡

algorithms ¡across ¡data ¡structures ¡that ¡contain ¡ the ¡opAon ¡space ¡

5

slide-6
SLIDE 6

CS ¡4730 ¡

“Sense ¡– ¡Plan ¡– ¡Act” ¡

  • Sense ¡(or ¡perceive) ¡a ¡sufficiently ¡complete ¡

model ¡of ¡the ¡world ¡

  • Plan ¡by ¡searching ¡over ¡possible ¡future ¡

situaAons ¡that ¡would ¡result ¡from ¡taking ¡various ¡ acAons ¡

  • Act ¡by ¡execuAng ¡the ¡best ¡course ¡of ¡acAon ¡
  • Each ¡possible ¡outcome ¡is ¡effecAvely ¡scored ¡by ¡

a ¡“metric ¡of ¡success” ¡that ¡indicates ¡whether ¡ the ¡choice ¡should ¡be ¡taken ¡or ¡not ¡

6

slide-7
SLIDE 7

CS ¡4730 ¡

DeliberaAve ¡vs. ¡ReacAve ¡

  • These ¡are ¡NOT ¡mutually ¡exclusive! ¡
  • You ¡can ¡have ¡reacAve ¡policies ¡for ¡immediate ¡

threats ¡ ¡

– Incoming ¡PC ¡fire ¡ – Environmental ¡destrucAon ¡

  • And ¡you ¡can ¡have ¡deliberaAve ¡policies ¡for ¡long-­‑

term ¡planning ¡

– Build ¡orders ¡and ¡posiAoning ¡

7

slide-8
SLIDE 8

CS ¡4730 ¡

Core ¡QuesAons ¡

  • How ¡do ¡you ¡represent ¡knowledge ¡about ¡the ¡

current ¡task, ¡environment, ¡PC, ¡etc? ¡

  • How ¡do ¡you ¡find ¡acAons ¡that ¡allow ¡the ¡goal ¡to ¡

be ¡met? ¡

8

slide-9
SLIDE 9

CS ¡4730 ¡

Knowledge ¡RepresentaAon ¡

  • A ¡decision ¡tree ¡is ¡a ¡common ¡way ¡to ¡represent ¡

knowledge ¡

  • The ¡root ¡node ¡is ¡the ¡current ¡state ¡of ¡the ¡game ¡

state ¡WRT ¡the ¡NPC ¡/ ¡AI ¡

  • Thus, ¡deliberaAve ¡AI ¡techniques ¡are ¡effecAvely ¡

versions ¡of ¡search ¡and ¡shortest-­‑path ¡ algorithms! ¡

9

slide-10
SLIDE 10

CS ¡4730 ¡

Tic-­‑Tac-­‑Toe ¡

  • What ¡is ¡the ¡decision ¡tree ¡for ¡Tic-­‑Tac-­‑Toe? ¡

10

slide-11
SLIDE 11

CS ¡4730 ¡

The ¡Goal ¡State ¡

  • The ¡objecAve ¡of ¡the ¡decision ¡tree ¡model ¡is ¡to ¡

move ¡from ¡the ¡iniAal ¡game ¡state ¡(root ¡of ¡the ¡ tree) ¡to ¡the ¡goal ¡state ¡of ¡the ¡NPC ¡

– What ¡might ¡a ¡goal ¡state ¡be? ¡

  • When ¡searching ¡through ¡the ¡decision ¡tree ¡

space, ¡which ¡path ¡do ¡we ¡take? ¡

11

slide-12
SLIDE 12

CS ¡4730 ¡

Cost ¡and ¡Reward ¡

  • Every ¡choice ¡has ¡a ¡cost ¡

– Ammo ¡ – Movement ¡ – Time ¡ – Increase ¡vulnerability ¡to ¡aVack ¡

  • Every ¡choice ¡has ¡a ¡reward ¡

– Opportunity ¡to ¡hit ¡PC ¡ – Capture ¡a ¡strategic ¡point ¡ – Gain ¡new ¡resources ¡

12

slide-13
SLIDE 13

CS ¡4730 ¡

Minimax ¡Algorithm ¡

  • Find ¡the ¡path ¡through ¡the ¡decision ¡tree ¡that ¡

yields ¡the ¡best ¡outcome ¡for ¡one ¡player, ¡ assuming ¡the ¡other ¡player ¡always ¡makes ¡a ¡ decision ¡that ¡would ¡lead ¡to ¡the ¡best ¡outcome ¡ for ¡themselves ¡

13

slide-14
SLIDE 14

CS ¡4730 ¡

Naïve ¡Search ¡Algorithms ¡

  • Breadth-­‑First ¡Search ¡

– At ¡each ¡depth, ¡explore ¡every ¡opAon ¡at ¡the ¡next ¡ depth ¡

  • Depth-­‑First ¡Search ¡

– Fully ¡explore ¡one ¡possible ¡path ¡to ¡its ¡“conclusion”, ¡ then ¡backtrack ¡to ¡check ¡other ¡opAons ¡

  • What ¡are ¡the ¡problems ¡with ¡these ¡techniques ¡

in ¡gaming? ¡

14

slide-15
SLIDE 15

CS ¡4730 ¡

Breadth-­‑First ¡Search ¡

  • Expand ¡Root ¡node ¡

– Expand ¡all ¡Root ¡node’s ¡children ¡

  • Expand ¡all ¡Root ¡node’s ¡grandchildren ¡
  • Problem: ¡Memory ¡size ¡

Root Root Child1 Child2 Root Child1 Child2

GChild1 GChild2 GChild3 GChild4

slide-16
SLIDE 16

CS ¡4730 ¡

Uniform ¡Cost ¡Search ¡

  • Modify ¡Breadth-­‑First ¡by ¡expanding ¡cheapest ¡

nodes ¡first ¡

  • Minimize ¡g(n) ¡cost ¡of ¡path ¡so ¡far ¡

Root Child1 Child2

GChild1 9 GChild2 5 GChild3 3 GChild4 8

slide-17
SLIDE 17

CS ¡4730 ¡

Depth ¡First ¡Search ¡

  • Always ¡expand ¡the ¡node ¡that ¡is ¡deepest ¡in ¡the ¡

tree ¡

Root Child1

GChild1 GChild2

Root Child1 Root Child1

GChild1

slide-18
SLIDE 18

CS ¡4730 ¡

Adding ¡a ¡HeurisAc ¡

  • Simple ¡definiAon: ¡a ¡heurisAc ¡is ¡a ¡“mental ¡

shortcut” ¡to ¡ignore ¡non-­‑useful ¡states ¡to ¡limit ¡ the ¡search ¡space ¡and ¡make ¡the ¡decision ¡tree ¡ more ¡reasonable ¡

  • What ¡metrics ¡might ¡we ¡use ¡to ¡determine ¡“the ¡

value” ¡of ¡a ¡potenAal ¡opAon? ¡

  • What ¡metrics ¡might ¡we ¡use ¡to ¡determine ¡“the ¡

cost” ¡of ¡a ¡potenAal ¡opAon? ¡

18

slide-19
SLIDE 19

CS ¡4730 ¡

Adding ¡a ¡HeurisAc ¡

  • CreaAng ¡an ¡AI ¡heurisAc ¡forms ¡the ¡basis ¡of ¡how ¡

the ¡NPCs ¡will ¡behave ¡

– Will ¡they ¡ignore ¡enemies ¡that ¡are ¡farther ¡than ¡X ¡ away? ¡ – Will ¡they ¡avoid ¡water? ¡ – Will ¡they ¡always ¡move ¡in ¡the ¡straightest ¡path ¡to ¡the ¡ PC? ¡

19

slide-20
SLIDE 20

CS ¡4730 ¡

Cheaper ¡Distance ¡First! ¡

20

slide-21
SLIDE 21

CS ¡4730 ¡

Greedy ¡Search ¡

  • Expand ¡the ¡node ¡that ¡yields ¡the ¡minimum ¡cost ¡ ¡

– Expand ¡the ¡node ¡that ¡is ¡closest ¡to ¡target ¡ – Depth ¡first ¡ – Minimize ¡the ¡funcAon ¡h(n) ¡the ¡heurisAc ¡cost ¡ funcAon ¡

slide-22
SLIDE 22

CS ¡4730 ¡

Greedy ¡Search ¡

22

slide-23
SLIDE 23

CS ¡4730 ¡

Greedy ¡Search ¡

23

slide-24
SLIDE 24

CS ¡4730 ¡

Greedy ¡Search ¡

  • Greedy ¡gives ¡us ¡(ojen) ¡a ¡sub-­‑opAmal ¡path, ¡but ¡

it’s ¡really ¡cheap ¡to ¡calculate! ¡

  • How ¡can ¡we ¡improve ¡on ¡this? ¡
  • Add ¡another ¡aspect ¡to ¡the ¡funcAon ¡– ¡the ¡cost ¡
  • f ¡the ¡node ¡+ ¡the ¡heurisAc ¡distance ¡

24

slide-25
SLIDE 25

CS ¡4730 ¡

A* ¡

  • A ¡best-­‑first ¡search ¡(using ¡heurisAcs) ¡to ¡find ¡the ¡

least-­‑cost ¡path ¡from ¡the ¡iniAal ¡state ¡to ¡the ¡goal ¡ state ¡

  • The ¡algorithm ¡follows ¡the ¡path ¡of ¡lowest ¡

expected ¡cost, ¡keeping ¡a ¡priority ¡queue ¡of ¡ alternate ¡path ¡segments ¡along ¡the ¡way ¡

25

slide-26
SLIDE 26

CS ¡4730 ¡

A* ¡Search ¡

  • Minimize ¡sum ¡of ¡costs ¡
  • g(n) ¡+ ¡h(n) ¡

– Cost ¡so ¡far ¡+ ¡heurisAc ¡to ¡goal ¡

  • Guaranteed ¡to ¡work ¡

– If ¡h(n) ¡does ¡not ¡overesBmate ¡cost ¡

  • Examples ¡

– Euclidean ¡distance ¡

slide-27
SLIDE 27

CS ¡4730 ¡

A* ¡

27

slide-28
SLIDE 28

CS ¡4730 ¡

A* ¡

28

slide-29
SLIDE 29

CS ¡4730 ¡

A* ¡

29

slide-30
SLIDE 30

CS ¡4730 ¡

NavigaAon ¡Grid ¡

30

slide-31
SLIDE 31

CS ¡4730 ¡

Pathfinding ¡in ¡“real ¡life” ¡

  • These ¡algorithms ¡work ¡great ¡when ¡the ¡game ¡is ¡

grid ¡based ¡

– Square ¡grid ¡ – Hex ¡grid ¡

  • For ¡more ¡“open” ¡games, ¡like ¡FPSs: ¡

– Path ¡nodes ¡are ¡placed ¡on ¡the ¡map ¡that ¡NPCs ¡can ¡ reach ¡ – NavigaAon ¡mesh ¡layers ¡are ¡added ¡over ¡the ¡terrain ¡ – Ojen ¡done ¡automaAcally ¡in ¡advanced ¡engines ¡

31

slide-32
SLIDE 32

CS ¡4730 ¡

NavigaAon ¡Mesh ¡

  • Instead ¡of ¡using ¡discrete ¡node ¡locaAons, ¡a ¡

node ¡in ¡this ¡instance ¡is ¡a ¡convex ¡polygon ¡

  • Every ¡point ¡inside ¡a ¡valid ¡polygon ¡can ¡be ¡

considered ¡“fair ¡game” ¡to ¡move ¡into ¡

  • NavigaAon ¡meshes ¡can ¡be ¡auto ¡generated ¡by ¡

the ¡engine, ¡so ¡easier ¡to ¡manage ¡than ¡nodes ¡

  • Can ¡also ¡handle ¡different ¡sized ¡NPCs ¡by ¡

checking ¡collisions ¡

32

slide-33
SLIDE 33

CS ¡4730 ¡

NavigaAon ¡Mesh ¡

33

slide-34
SLIDE 34

CS ¡4730 ¡

Groups ¡

  • Groups ¡stay ¡together ¡

– All ¡units ¡move ¡at ¡same ¡speed ¡ – All ¡units ¡follow ¡the ¡same ¡general ¡path ¡ – Units ¡arrive ¡at ¡the ¡same ¡Ame ¡

ObstrucAon ¡ Goal ¡

slide-35
SLIDE 35

CS ¡4730 ¡

Groups ¡

  • Need ¡a ¡hierarchical ¡movement ¡system ¡
  • Group ¡structure ¡

– Manages ¡its ¡own ¡prioriAes ¡ – Resolves ¡its ¡own ¡collisions ¡ – Elects ¡a ¡commander ¡that ¡traces ¡paths, ¡etc ¡

  • Commander ¡can ¡be ¡an ¡explicit ¡game ¡feature ¡
slide-36
SLIDE 36

CS ¡4730 ¡

FormaAons ¡

  • Groups ¡with ¡unit ¡layouts ¡

– Layouts ¡designed ¡in ¡advance ¡

  • AddiAonal ¡States ¡

– Forming ¡ – Formed ¡ – Broken ¡

  • Only ¡formed ¡formaAons ¡can ¡move ¡
slide-37
SLIDE 37

CS ¡4730 ¡

FormaAons ¡

  • Schedule ¡arrival ¡into ¡posiAon ¡

– Start ¡at ¡the ¡middle ¡and ¡work ¡outwards ¡ – Move ¡one ¡unit ¡at ¡a ¡Ame ¡into ¡posiAon ¡ – Pick ¡the ¡next ¡unit ¡with ¡

  • Least ¡collisions ¡
  • Least ¡distance ¡

– Formed ¡units ¡have ¡highest ¡priority ¡

  • Forming ¡units ¡medium ¡priority ¡
  • Unformed ¡units ¡lowest ¡
slide-38
SLIDE 38

CS ¡4730 ¡

FormaAons ¡

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

Not so good… Better…

slide-39
SLIDE 39

CS ¡4730 ¡

FormaAons: ¡Wheeling ¡

  • Only ¡necessary ¡for ¡non-­‑symmetric ¡formaAons ¡

1 2 3 4 5 1 2 3 4 5

Break ¡formaAon ¡here ¡ Stop ¡moAon ¡temporarily ¡ Set ¡re-­‑formaAon ¡point ¡here ¡

slide-40
SLIDE 40

CS ¡4730 ¡

FormaAons: ¡Obstacles ¡

1 2 3 4 5

Scale ¡formaAon ¡layout ¡to ¡ ¡ fit ¡through ¡gaps ¡

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

Subdivide ¡formaAon ¡ ¡ around ¡small ¡obstacles ¡

slide-41
SLIDE 41

CS ¡4730 ¡

FormaAons ¡

  • Adopt ¡a ¡hierarchy ¡of ¡paths ¡to ¡simplify ¡path-­‑

planning ¡problems ¡

  • High-­‑level ¡path ¡considers ¡only ¡large ¡
  • bstacles ¡

– Perhaps ¡at ¡lower ¡resoluAon ¡ – Solves ¡problem ¡of ¡gross ¡formaAon ¡movement ¡ – Paths ¡around ¡major ¡terrain ¡features ¡

slide-42
SLIDE 42

CS ¡4730 ¡

AI ¡That ¡Learns ¡

  • Imagine ¡a ¡player ¡in ¡Madden ¡calls ¡a ¡parAcular ¡

play ¡on ¡offense ¡over ¡and ¡over ¡and ¡over ¡

  • The ¡heurisAc ¡values ¡for ¡certain ¡states ¡should ¡

change ¡to ¡reflect ¡a ¡more ¡opAmal ¡strategy ¡

  • Now, ¡the ¡adjustment ¡of ¡heurisAc ¡values ¡

represents ¡long-­‑term ¡strategy ¡(to ¡a ¡degree) ¡

42

slide-43
SLIDE 43

CS ¡4730 ¡

AI ¡That ¡Evolves ¡

  • Neural ¡networks ¡add ¡in ¡a ¡mutaAon ¡mechanic ¡
  • Bayesian ¡networks ¡can ¡also ¡learn ¡and ¡add ¡

inferences ¡

  • How ¡much ¡processing ¡power ¡can ¡we ¡use ¡for ¡

this? ¡

  • Is ¡it ¡beVer ¡to ¡truly ¡have ¡a ¡“learning” ¡AI, ¡or ¡

should ¡we ¡just ¡adjust ¡some ¡game ¡parameters? ¡

43