Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12: Implementing Actor Behavior (“AI”)

Similar presentations


Presentation on theme: "Chapter 12: Implementing Actor Behavior (“AI”)"— Presentation transcript:

1 Chapter 12: Implementing Actor Behavior (“AI”)

2 Initial Questions What is Artificial Intelligence?
What is the purpose of Artificial Intelligence in games?

3 Concerning Terminology:
This Chapter will deal with an aspect of game design that is commonly called “Artificial Intelligence”. As this term is inherently vague and frequently misused, we will instead be using a more Greenfoot-specific term: Actor Behavior For the purposes of this Chapter, Actor Behavior will refer to any algorithmically generated (i.e. Non-Player) Behavior exhibited by an Actor within a scenario.

4 Importance of Actor Behavior
Any game that implements moving (or otherwise non-static) Actors needs to find a way to actually create that Behavior. Greenfoot is not providing any kind of “default” or “dummy” Behavior. The specific implementation of Actor Behavior can have a significant influence on how well a game is able to achieve its goals.

5 Implementing Actor Behavior
We will examine 4 kinds of Actor Behavior: Deterministic Behavior “Optimal” or Greedy Behavior Random Behavior Constrained Random Behavior

6 Implementing Actor Behavior
We will be using a modified version of the classroomScrolling scenario for this chapter. Specifically, we will focus on the Boss-class and how we can implement its behavior.

7 Implementing Actor Behavior
What is the goal of the Boss “Character”? What is our goal when designing the Boss Behavior?

8 Deterministic Behavior
Deterministic Behavior refers to a class of Behavior where every individual action (and thus the observed Behavior in general) is predetermined and can be predicted. Strictly Deterministic Behavior is not influenced by context (such as the movement or presence of other Actor objects) or randomness.

9 Examples of Deterministic Behavior
Do not take any actions

10 Examples of Deterministic Behavior
Move in a straight line

11 Examples of Deterministic Behavior
Move one cell up, then one cell forward, then one cell forward, then one cell down

12 Implementing Deterministic Behavior
Design a non-trivial Deterministic Behavior for the Boss class. You can take the following actions: a) Fire a Bullet b) Change the Boss´s x-Coordinate

13 Implementing Deterministic Behavior
Deterministic Behavior for the Boss class: Constantly move left or right. If you reach the end of the screen turn the other way. Fire every 20 act cycles.

14 Implementing Deterministic Behavior
public void behavior() { if (fireDelay == 20) { fire(); fireDelay = 0; } fireDelay++; if(getX() == 5) { direction = 1; if(getX() == getWorld().getWidth()-5) { direction = -1; setLocation(getX()+direction, getY());

15 Deterministic Behavior
(Strictly) Deterministic Behavior is relatively easy to implement and can be useful for certain tasks (such as obstacle avoidance). However, it can also appear predictable, insufficiently dynamic, non-reactive and “lifeless”. Note: Sufficiently complex deterministic behavior can appear to be random (or at least non-predictable).

16 Deterministic Behavior
While the initial placement is random, this (too) is Deterministic Behavior

17 Greedy Behavior Greedy Behavior refers to a class of Behavior where every action is “locally optimal”. I.e. during each “action-cycle” (usually one call to the act() method) the Actor always takes the action that most directly advances its goal. Unless the scenario itself is static, Greedy Behavior is necessarily influenced by context (such as the movement of other Actor objects).

18 Examples of Greedy Behavior
Shortest Path (“Travelling Salesman”): What would be a “locally optimal” move?

19 Examples of Greedy Behavior
Always travel to the next closest unvisited point:

20 Implementing Greedy Behavior
Design a possible Greedy Behavior for the Boss class. What is the goal the Boss is trying to achieve? Considering this goal, what would be an “optimal” action?

21 Implementing Greedy Behavior
Greedy Behavior for the Boss class: Move to the same x-coordinate as the Rocket. Fire every act cycle.

22 Implementing Greedy Behavior
public void behavior() { fire(); List<Rocket> rockets = getWorld().getObjects(Rocket.class); for(Rocket a: rockets) setLocation(a.getX(), getY()); } The Boss fires constantly.

23 Implementing Greedy Behavior
public void behavior() { fire(); List<Rocket> rockets = getWorld().getObjects(Rocket.class); for(Rocket a: rockets) setLocation(a.getX(), getY()); } The Boss tracks the movement of the Rocket.

24 Greedy Behavior Greedy Behavior is comparatively easy to implement, assuming a clear goal can be identified and the number of possible actions is relatively limited. However, Greedy Behavior can be simplistic, frustrating for the player and generally not “fun” to compete against. Also note that Greedy Behavior does not necessarily result in the most efficient Behavior.

25 Random Behavior Random Behavior refers to a class of Behavior where actions are executed or not executed depending exclusively on the result of a random operation. The exact nature of the random operation is irrelevant, assuming it is sufficiently random.

26 ? ? ? Implementing Random Behavior Random Behavior for the Boss class:
Move left with 50% probability. If you are not moving left, move right. Fire with 50% probability each act cycle. ?

27 Implementing Random Behavior
public void behavior() { int random = Greenfoot.getRandomNumber(2); if (random == 0) { setLocation(getX()+2, getY()); } else { setLocation(getX()-2, getY()); random = Greenfoot.getRandomNumber(2); if(random == 0) { fire(); Movement is random and not constrainted by previous actions. Every act cycle can change where the Boss is moving.

28 Random Behavior Unconstrained random behavior can appear to be either exceedingly erratic or insufficiently dynamic. Especially unmodified random movement (“coin-flip”) is problematic. While including an element of randomness in Actor Behavior is usually desirable, it needs to be “constrained”.

29 Constrained Random Behavior
Constrained Random Behavior is still based on a “Seed” of randomness, meaning it is generally non-deterministic. However, the randomness is modified by various constraints that impose restrictions on how random the Actor behaves. We can think of Constrained Random Behavior as “unpredictable behavior within predictable parameters”.

30 Examples of Constrained Random Behavior
Start at a random position (within a fixed quadrant)

31 Examples of Constrained Random Behavior
Spawn with a randomized movement Vector (with a direction component between 75 and 104 degrees)

32 Implementing Constrained Random Behavior
What Constraints could we impose on the Boss character to improve his behavior?

33 Implementing Constrained Random Behavior
We will impose the following constraints on our Boss Behavior: There is a high chance that no Bullet is fired each turn (no “coin-flip”) If the Boss is within 50 pixels of a border, it move in the opposite direction Once the Boss has started moving in a certain direction, it stays committed to that movement for at least 20 act cycles

34 Implementing Constrained Random Behavior
private int movementTimeDelay = 20; private int movementTime = 0; private int direction = 0; public void behavior() { int random = Greenfoot.getRandomNumber(20); if(random == 9) { Bullet bullet = new Bullet (new Vector(0,0), 90); getWorld().addObject (bullet, getX(), getY()+40); bullet.move (); } movementTime++; setLocation(getX()+direction, getY()); A Bullet is only fired 5% of the time.

35 Implementing Constrained Random Behavior
if(movementTime > movementTimeDelay) { if (getX() > 350) { direction = -1; movementTime = 0; } else if (getX() < 50) { direction = +1; else if (random < 5) { else if (random > 14) { Movement stays consistent for at least 20 act cycles.

36 Implementing Constrained Random Behavior
if(movementTime > movementTimeDelay) { if (getX() > 350) { direction = -1; movementTime = 0; } else if (getX() < 50) { direction = +1; else if (random < 5) { else if (random > 14) { Evading borders is prioritized over general movement.

37 Constrained Random Behavior
Constrained Random Behavior allows us to implement Behavior that is both relatively unpredictable for the player and “designed”. This means that we can influence difficulty, style of challenge, and other factors. It can be considered a combination of Deterministic Behavior and Random Behavior. In fact, any complex Actor Behavior will most likely contain elements of all 4 Behavior classes.

38 The PacMan Game

39 The PacMan Game There are 4 Non-Player Actors in this game:
Ghosts of different colors that move around the board. While each Ghost has the same goal in theory (Eat PacMan), they all exhibit unique Behavior.

40 The Orange Ghost The Orange Ghost exhibits Deterministic Behavior.
It moves around the central Box on a fixed path, ignoring PacMan and only eating him if their paths happen to cross.

41 The Orange Ghost

42 The Orange Ghost public void ghostBehavior() { if (direction == WEST) { setLocation(getX()-3,getY()); if (null != getOneIntersectingObject(Boundary.class)) setLocation(getX()+3,getY()); setDirection(SOUTH); } else if (direction == EAST) setDirection(NORTH); …. The Ghost changes direction whenever a Boundary is hit.

43 The Orange Ghost public void ghostBehavior() { if (direction == WEST) { setLocation(getX()-3,getY()); if (null != getOneIntersectingObject(Boundary.class)) setLocation(getX()+3,getY()); setDirection(SOUTH); } else if (direction == EAST) setDirection(NORTH); …. The direction it chooses is fixed and depends entirely on its previous direction.

44 The Pink Ghost The Pink Ghost exhibits (Heavily) Constrained Random Behavior. He moves on predetermined paths between the "teleport" points on the left/right side of the level. If it has reached one of the "teleport" points, it starts to move towards a "teleport" point on the other side of the board. The decision between the two possible points is made at random.

45 The Pink Ghost

46 The Pink Ghost

47 The Pink Ghost The predetermined paths are stored in a two-dimensional array as a sequence of direction-changes: int[][] sequence = { {WEST, SOUTH, WEST, NORTH, WEST, NORTH, WEST, NORTH, WEST}, {WEST, SOUTH, WEST, NORTH, WEST, NORTH, EAST, NORTH, WEST, SOUTH, WEST}, {EAST, SOUTH, EAST, NORTH, EAST}, {EAST, NORTH, WEST, NORTH, EAST, NORTH, EAST, NORTH, EAST, SOUTH, WEST, SOUTH, EAST, SOUTH, EAST}, … int path = 2; int index = 0; This path connects the Right-Down teleport point to the Left-Down teleport point.

48 The Pink Ghost The predetermined paths are stored in a two-dimensional array as a sequence of direction-changes: int[][] sequence = { {WEST, SOUTH, WEST, NORTH, WEST, NORTH, WEST, NORTH, WEST}, {WEST, SOUTH, WEST, NORTH, WEST, NORTH, EAST, NORTH, WEST, SOUTH, WEST}, {EAST, SOUTH, EAST, NORTH, EAST}, {EAST, NORTH, WEST, NORTH, EAST, NORTH, EAST, NORTH, EAST, SOUTH, WEST, SOUTH, EAST, SOUTH, EAST}, … int path = 2; int index = 0; The path the Ghost is currently on, as well as the position within this path, are stored in two integer variables.

49 The Pink Ghost public void ghostBehavior() { getNewPath(); if (direction == WEST) { setLocation(getX()-movement,getY()); if (null != getOneIntersectingObject(Boundary.class)) setLocation(getX()+movement,getY()); setDirection(sequence[path][index]); index++; } …. If the Ghost collides with a Boundary, it changes direction. The new direction is determined by the next value stored in the array.

50 The Pink Ghost public void getNewPath() { if(getX() < 5) { index = 0; int random = Greenfoot.getRandomNumber(2); if(getY() < 298) path = 4+random; else path = 2+random; setDirection(sequence[path][index]); } if(getX() > getWorld().getWidth()-5) { path = 6+random; path = 0+random; If a screen border has been reached, the Ghost chooses between the two possible new paths via „coin-flip“.

51 The Blue Ghost The Blue Ghost exhibits Greedy Behavior.
It tries to get to PacMan on the shortest path possible. However, this means that he is unable to get around Boundaries.

52 The Blue Ghost

53 The Blue Ghost

54 The Red Ghost The Red Ghost exhibits (Lightly) Constrained Random Behavior. He continues on his current path until he hits a Boundary and then chooses a new direction at random. However, he is made more dangerous by his ability to use teleport points.


Download ppt "Chapter 12: Implementing Actor Behavior (“AI”)"

Similar presentations


Ads by Google