lundi 25 mai 2020

Beat them all (UE 4)

Presentation

On this project use with unreal Engine 4 , the idea is to reproduce a beat them all game like Devil May Cry (DMC).

Note this project is not finished, and I have no intention to make a real game. It's just a demo

Actions 

- Attack enemy, base on the timing of the input, theirs is different combo. On this project if the player input : attack, attack, wait the end of animation, then attack
.Player actor will perfom 2 more attack (like in DMC)
- Locking target and move around. Player can change target lock
- Player can fire projectile (UE 4 Logo) if a enemy is lock , projectile go direclty on target
- If the play walk during an certains amount of time, it's will run (Like in DMC 5)
- Player can jump and attack
- Charge attack (DMC) :  if the player input front + attack , player actor rush to the target if lock and attack
- Launch Attack (DMC) : if the player press back + attack, player will perform that launch enemy upward
- If the player does'nt touch anything , the player actor will perform a relax animation

Lock Action

Relax Action

Attack action

Run Action

Behavious Tree (BT) with Finite State Machine (FSM)

We can combine Behavious Tree and Finite State Machine. Each Leafs of the Behavious Tree are Finite State Machine system. That way we can get all advantages of this 2 AI techniques.

I use this paper A Character Decision-Making System for FINAL FANTASY XV by Combining Behavior Trees and State Machines by  Youichiro Miyake, Youji Shirakami, Kazuya Shimokawa,
Kousuke Namiki, Tomoki Komatsu, Joudan Tatsuhiro,Prasert Prasertvithyakarn, and Takanori Yokoyama(http://www.gameaipro.com/GameAIPro3/GameAIPro3_Chapter11_A_Character_Decision-Making_System_for_FINAL_FANTASY_XV_by_Combining_Behavior_Trees_and_State_Machines.pdf)use in Final Fantasy 15 , I didn't implement the tool but get the idea

Behavior Tree is an classical one and the FSM is based on this paper A Reusable, Light-Weight
Finite-State Machine by David Grez Graham (http://www.gameaipro.com/GameAIPro3/GameAIPro3_Chapter12_A_Reusable_Light-Weight_Finite-State_Machine.pdf) use on this game Drawn to Life: The Next Chapter.

On this sample project, Actor lives in a house and 3 neend : sleep, eat, wash (and play video game if he have nothing to do), it's like the Sims game.
Every time a leaft of the BT is selected, we goes in a FSM to accomplish actions by instance eat means take a meal, put it on oven and eat or for sleeping it's means switch of the light , sleep n and switch on the light when the actor sleeps enough



Score Behavious Tree (Goal Oriented Behaviors Score)

Behavious Tree actions are selected depend some conditions.One of the weakness is player can anticipate the action if he knows how to trigger this behavious
and on the developpement we need to add more and more condition when we are checking condtions

On this paper : Simulating Behavior Tree, A Behavior Tree/ Planner Hybrid Approach by Daniel Hilburn(http://www.gameaipro.com/GameAIPro/GameAIPro_Chapter08_Simulating_Behavior_Trees.pdf)Use the in Jedi Academy Kinect.The idea is to make "simulation" of each action possible and select the best one. On my side, I add a score system to know which are the best actions

On this example, it's a shooter game. The AI Actor have to select the best actions depend on data:

Data are :

- Weapon : Handgun, Shotgun, Sniper . Weapon are more or less effective depend on the distance (Sniper is good for far target, where shotgun is good for close combat)
- Health
- Cover
- Health pack distance

Actions :

- Attach the player
- Go Cover or take health pack
- Go near or far the target to use more effectively weapons

With this method, user can add/remove conditions and can tweak more easier than a "classic" BT

Bot attack player with a sniper

Bot go near the player to use shotgun

Finite State Machine (FSM) & Behavior Tree (BT)

Presentation

In video games, two of the most AI algorithm use are Finite State Machine (FSM)
and Behavious Tree (BT). They are easy to implement and can have good result

FSM

FSM is the easier AI algorithm to create. Each behavious are state so it's easy
to create new behavious. A lot of document is already made on this subject
and it's use on commercial engine like Unreal Engine (Animation use state machine)

On this example project. Actor moves to eat or sleep depend of this state,
if its ok for eat or sleep . He goes to work



Behavious Tree

Behavious tree is the 2nd AI Algorithm, as the name suggests, it's a tree algorithm.
It's go on each branch and see if the condition is met, depend on the node (selector or sequence)
behavious leaf is selected

On this example. The Actor patrol on the waypoint path, random gold are spawn on the map,
so actor can pick gold.. If the actor see the player, he will pursuit him to catch the player

With selector, BT can give priorities on actor actions, on this example
the most importants actions are : Chase the player, Pick gold then patrol



Space Invaders (C++/OpenGL)

Presentation

Here is Space invaders in C++/OpenGL. The main objective is of course to have a working game but more important the goal of this project is to develops somes features use in modern video game

Smart Pointers

A lot of object like aliens or player rockets are dynamically allocated , so on the heap. The difficulity of variable allocate on the heap is to manage the lifetime on the variable : When to allocate or when to delete, a small forgot of deallocation can mean a memory leak that will become bigger with time

With C++ 11, the language got smart pointers. Smart pointer are wrapper of a pointer variables which automatically manager lifetime of a pointer.
This project use unique_ptr (uniquer ownership) and shared_ptr( multiple ownership for an entity) 

Event System

Entity objects have to communicate between them . A player rocket touch an alien entity or alien collides with player by example. To avoid that player class  use enemy data, an event manager is useful to simulate interaction between objects.

Like an observer design pattern, player and alien subscribe as a listener on an event manager. When an "event" (rocket collision) is trigger, an event is create and add to the event manager which can after contact listener (in this case alien and rocket classes) to manager the event.

Pool Memory

Allocate object on the heap during runtime it's a bad thing due time to get memory adressy ,this can have negative performance of the game.
One solution is to allocated a finite number of rockets and aliens but this will limit the numbers of each entity

The pool memory is one of the solution. We can allocate a big chunk of memory and then divide this big chunk with defined block of memory. 
When a rocket entity is need, we pull block memory from the memory chunk, and when the rocket is destroy we give back this block of memory to the memory pool.
Because each blocks of memory of the memory pool have the same size and we use 2 pointer to know where are "free" or "use" memory , their is no "hole" of memory during
allocation and deallocation of memory.

With a pool memory, we can use dynamic memory but without the overhead of calling the memory manager. We allocate only one time the memory : at the beginning of the game

Dynamic Game Loop

Gameloop is the core of the game. Each Entity (input,player,physics,enemy) is call in a defined order. But sometime we need an dynamic ordering instead of a static
order, by example we don't need to call some entity or we wish to change the order of the gameloop

On this project, I create a linkedlist of entity that way we can add or remove entities need for the gameloop

Images