38
The Greedy Wall Building algorithm Rami Khouri

The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Embed Size (px)

Citation preview

Page 1: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

The Greedy Wall Building algorithm

Rami Khouri

Page 2: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Ideal definition of Wall

• Either keeps valuable assets in, or enemies out…mostly keep enemies out

• Impassable, or slows down opponents in vulnerable positions– Every wall segment has two unique neighbors– Contains an interior protected area– All interior areas are connected through a

path

Page 3: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

These are not walls

Exceptions: Some games will connect blocks on diagonal spaces together, While other games use gates to allow subdivision of inner area. The greedy algorithm is NOT concerned with creating an inner wall.

Page 4: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Greedy Algorithm

• A plan that takes steps for the best immediate gain…do not plan ahead into the future moves

• Dijkstra’s Shortest Path is an example of Greedy algorithm that gives optimal results

• In the case of building a wall, Greedy algorithm does not provide optimal results, but good enough for use.

Page 5: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Basic Premise of Greed…

• We will try to “push” the walls outward by expanding at the edges

• Each move is to expand a wall to incorporate one, and only one new space.

• Moves cannot break the definition of wall as seen earlier – we are always protected

Page 6: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,
Page 7: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Problem with This Paradigm

• To push the wall outward, we must have a case for every possible wall configuration.

• 68 cases if tiles are squares, much more if tiles are hexagonal, more if their octagonal

• Special cases if the wall is adjacent to a natural barrier.– Thinking of the problem in terms of pushing

the wall is complex

Page 8: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Change the paradigm

• Instead of pushing the walls, expand the inner area

• Represent the tiles as vertices, and connections between tiles as edges– Now we can have as many edges as we want

coming out of a node– We can represent indestructible natural

barriers as the absence of an vertex/edge

Page 9: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

So how do we apply the algorithm?

• Keep three lists: – List of all nodes & edges, graph– Closed list: all nodes in the interior space of the wall– Open list: all nodes that are candidates for

expansion, in practice this is the perimeter of the enclosed area, i.e. spaces that the wall is occupying

Closed

Open

Graph

Page 10: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

List OpenList, ClosedList, Graph

WallBuilder (Node Start, Criteria Accept){

While (OpenList != empty && Accept != criteria){ if ( bestChoice.Cost <= affordable){

remove bestChoice() from openList

add bestChoice to closedList

for each Neighbor of BestChoice(){if (not in closedList or openList)

{add to openList/Build wall on it}}

}}

Page 11: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

How to pick the best choice?

• Pick the move with the highest value and lowest cost. In this case, Value is always 1, so lets worry only about the cost:– Cost of a move is how many wall

constructions it would take to seal it from the outside: This is the number of nodes it connects to that are not in the open or closed list

Lets call this w(x); for any node x

Page 12: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Demo1

• Run Demo1

Page 13: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

So now we have

• Cost(n) = w(n);

• But now lets extend it to take into account that we want to wall in closer nodes first

• d(n) = value based on distance, – closer = lower

• c is a constant higher than max d(n)

• Cost(n) = c * w(n) + d(n) what is the effect of this formula in English

Page 14: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Lets extend

• Cost(n) = 0 if d(n) < minimum distance

• Cost(n) = infinity > maximum distance

• Cost(n) = c * w(n) + d(n)

• Natural barriers have a cost of zero if toughness => walls

Page 15: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Demo2

• Run Demo 2

Page 16: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

What happens here?

Page 17: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Gates

• Draw a path to areas of interest (resources or enemy strongholds)

• On each path place a gate.

• Gates within a certain distance of each other can be combined

Page 18: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,
Page 19: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Hierarchy in Strategy Games

Division of tasks

Maps

Message Sending

Page 20: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Demo 3

Page 21: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Without subdivision, movements such as “Human Wave Attacks” are simple:

All soldiers: Attack Point (X,Y)”

But more complex commands, such as pincer movement, flanking..etc are much harder to implement:

Soldier 1 “I am closest to east side, but are there already enough people on that side? Let me ask each soldier and make sure they don’t have too many….etc”

Page 22: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Realistic SolutionArmy

Corps Corps

DivisionDivision

Corps

Division

BrigadeBrigade Brigade

BattalionBattalionBattalion

CompanyCompanyCompany

PlatoonPlatoon Platoon

SquadSquad Squad

SoldierSoldierSoldier

Page 23: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Using a realistic solution, we can divide troops into subgroups…troops, squads, platoons…etc. Creating a level of abstraction between the player and individual troops, a concept often used in OOP programming

Squad A attack left side

Abstracts:

soldier 00001 move left, then attack from left

……

soldier 00008 move left, then attack from left

Page 24: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Levels of Hierarchy

• Unit AI:– Soldier, Tank, Helicopter…etc– Short distance path finding– Report events to Squad– Accept Attack & Move commands

• Some freedom is given in how to execute move and attack (touched on later)

Page 25: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Element Map: view of unit

• Unit sees individual elements of the map• i.e. soldier will see

– A tree– A rock– A destroyed tank– An enemy sniper

• Soldier know how to select their own targets and engage, and to find strategic locations within a small area when told to attack or support

Page 26: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Squad

• Interpret commands from higher level (platoon) and send to individual soldiers

• Set checkpoints for individual soldiers to follow, higher level path finding

• Maintain reasonable formation, track each unit

• Receive feedback from units, and move them back up to platoon move and attack to units. Backup units in need of help

Page 27: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Tile Map: Squad view

• A Squad can see its own individual troops in detail, but abstracts enemy troop data i.e. enemy attack & Defend

• Squad abstracts attributes of terrain, i.e. mobility rating, defensive rating, connection to other tiles Defense: 2

Offense:3Mobility: 4Cover:5

Defense: 2Offense:5Mobility: 7Cover:2

Defense: 1Offense:1Mobility: 10Cover:0

Defense: 0Offense:0Mobility: 10Cover:1

Page 28: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Platoon

• Interpret commands from higher level and translate them to squad instructions

• Platoon movement is handled by setting checkpoints for each squad in a path to destination – let squad handle smaller pathfinding

• Keep track of squads, interpret messages i.e. squad Strength, encounter enemy, losing fight…etc. Coordinate backup and formation

• Pass information up to higher levels (Engaging enemy, need backup, need reinforcement…etc)

Page 29: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Tile Map: Platoon view

• See individual squads of self

• Abstract enemy values

• Abstracts attributes of terrain,

• i.e. mobility rating, defensive rating.

• Connection to other tiles is most important

Basically a continuation of map abstraction from unit to squad, to Platoon

Defense: 2Offense:3Mobility: 4Cover:5

Defense: 2Offense:5Mobility: 7Cover:2

Defense: 1Offense:1Mobility: 10Cover:0

Defense: 0Offense:0Mobility: 10Cover:1

Artillery: 14Speed: 10 Small Arms: 7

Page 30: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Computer Player AI

• Civilization building– Economics– Technology

• Building Military

• Set policy towards neighbors– Offensive/Defensive/Alliance

Page 31: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

CPAI map

• Highest level map.

• See strength and weakness of enemy

• See Resources within an area

• Only pathfinding necessary is “does a path exist”

Page 33: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

Gold:104Offense: 19Oil:78 Defense: 21Disposition: Friendly

Gold:67 Offense: 15Oil:44 Defense: 17Disposition: Hostile

Gold:104 Offense: 19Oil:78 Defense: 21

Page 34: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,

SQUADPLATOONComputer Player UNIT

Defend Path

Defend Path (middle)

Defend Location

Ready

Ready

ReadyEnemy spotted

All Units AttackEngaging enemy

Back up squad engage

Engaging enemy

Hold Platoon formation

Page 35: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,
Page 36: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,
Page 37: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,
Page 38: The Greedy Wall Building algorithm Rami Khouri. Ideal definition of Wall Either keeps valuable assets in, or enemies out…mostly keep enemies out Impassable,