Jump to content

[idea] model for NPC AI


s_f

Recommended Posts

---

TL;DR:  imagined model for ai-related stuff meant to drive NPC (mods).  Should be of interest only for mod developers related to NPC stuff.

---

 

a (wip) simple conceptial model for NPC ai (for potential NPC mods) dumped for ideas exchange.

 

Pls note that it is just a first attempt made over a short period of time by a relatively new player who does not understand the whole game system that well yet.  So take it with a pinch of salt if something is not applicable (will appreciate feedback on those though) as it is mostly just a dump of ideas without much follow-up on applicability and feasibility.

 

------------------------------------------------------------

1. model Entity stats - Needs, Character (preferences), Attribute (skills and psuedo-skills).  Basically made up of what is already in-game, plus whatever that is required as extra. May potentially have a custom-need framework that allows 3rd party mods to add their own derived needs (needs that are determined by existing stats/needs/context)

 

------------------------------------------------------------
2. model Entity memory - long term memory, including group/faction shared memory, short term memory e.g. seeing the party leader use a particular door/window/sheet rope (so potentially will use it when following), interaction memory (talked to this guy, that guy gave me something etc, did some farming work etc)
Basically split into 2 types:

a. static memory which are always persistent data used by the engine. e.g. remembering where the base is, or which faction/party the entity is in
b. historical events (long term ones which are important ones that are kept, short term ones which are removed after some time. e.g.daily/weekly/monthly)
c. general knowledge (see point 6 Helper modules - knowledge)

 

------------------------------------------------------------
3. 2-tier mode-based AI - I'm not expert in ai, but this seems to be the easiest way to implement a more "responsive" AI, but having basic "modes" which assess the needs, availability of means to satisfy the needs, then decide what to do based on the weight, modified by character (preferences).  

Each mode basically provides a list of pre-determined "goals" to satisfy some needs, and routines to list out the "means" of sastifying those needs along with the desirability (usually based on how effective the means is, modified by entity's character)  The weights may also be modified indirectly by moodlet (e.g. a "suicide" goal may have a positive weight for very negative "depression"(need) which usually will not be considered at all unless depression is really, really high.  But say some happenings (memory) recently caused a "depressed" moodlet which seriously increase the depression for a time.  If not rectified quickly, the NPC entity may suddenly find that "suicide" goal very attractive, and will look for "means" to achieve it)


Above this, there is a "mode-selector" which changes the mode based on "urgency".  A mode of higher urgency always takes precedence, and only when the mode of higher urgency reaches "satisfied", will the mode-selector "drop down" to the modes of lower urgency and so on (based on mode-specific evaluation functions)

Note that this just the basic model for the individual AI. The actual weights and actions available depends on the entity context.  e.g. if alone, an NPC entity will have to be more autonomous, compared to NPC in a group.  What is different though is just the possible tasks available in each context and weights for selecting.  The structure of the ai model doesn't really change (though it is possible to insert/remove/swap the modes)

the "modes", "goals" and "means" can potentially be modified/extended by other mods (in addition to the core set provided)

E.g. of modes in order of "urgency".

 

a. Fight or Flee - basically in the face of immediate danger (that can harm/kill the entity within a short period of time if unattended).  Danger can be hostile enemies, but also things like hazards like fire, or conditions like potentially fatal unjuries.  This simple mode basically only results in two kinds of outcome: whether to fight and contain the danger (kill/drive away hostiles, extinguish fires, treat injuries) , or to flee from the danger (only those applicable like enemies and hazards. you cannot escape from your wounds).  A sub-group of this is to "fight" for another entity of relevance (e.g. an NPC doctor in a party treating serious wounds for party members once he is no longer in immediate danger)

It will become "satisfied" when there is no longer immediate danger around. 

 

b. Short-term Survival - longer term than immediate danger, but will threaten the Entity's existence if not attended.  Basically attending to current needs like treating injuries, food, water, rest, heat ,hygiene

 

c. Long-term Survival - different from short-term survival, this mode tries to to longer-term (predicted) needs, often by producing/acquiring/stocking up the corresponding kinds of supplies. e.g. scavenging/foraging/farming/water-purification

 

d. Idle - when the above modes are satisfied or stalled (cannot be fulfilled currently), will switch to this ladst mode which is filled by all other possible tasks, which are selected based on the entity's context and character.  This mods is always "active" as the lowest priority mode.
- skilling : reading skill books/exercise
- equipment maintenance : swapping/repairing/resupply (ammo/food/water)
- leisure : reading, watching tv, listening to radio, talking, playing games, rest

 

Each mode will have an "activation" function and a "satisfaction" function.  when activated, the mode will become active, and if it is of higher priority/urgency than current mode, mode-selector will switch to the mode immediately (and perform task selection and take action).  When satisfied, the mode will become inactive, and mode-selector will choose next mode with highest priority.

 

Extensions to this (by 3rd party mods) can be: adding new "goals" and "means" to an existing mode, or adding new modes with a different priority/urgency which will automatically be considered by the mode-selector.

 

------------------------------------------------------------
4. Missions, Tasks and Actions (note: not directly related to any in-game code classes of similar names)

These are states and classes which is sort of orthogonal to the 2-tier mode-based AI.
in my simplistic definition:

 

a. actions - the lowest level is simply that. an action.  it doesn't have a goal and the result is not handled within the action (but by the context that performs the action).  It just performs (or attempts to at least). Actions should be close to what the game can perform at the lowest level (most of the time, one-to-one with in-game Action class).  e.g. pickup/equip/drop/use/unequip an item (which must exists, already be nearby/in inventory and accessible), rack/reload firearms, perform interaction with fixed item/tile (must already be in range)

 

b. task - may consist of one or more actions, with evaluation of the outcome (whether success or fail, and any task-specific sub-results).  Tasks can potentially be hierarchical (main task spawning sub-tasks). A task MUST eventually complete. E.g. for  tasks: find something to eat (which can spawn sub tasks of looking for food, and actually moving there and then eating it), it must either end with succesfully eating something, or failed if it cannot be achieved.

 

c. mission - an on-going context with set goal(s). It will have a component that generates tasks from the goal and actual context (e.g. environment, entity abilties) to attempt to fulfil that goal. It will not end by itself, and will simply loop to generate more tasks when the current tasks are completed or interrupted (or idle if there are no tasks generated).  Instead, it will prob be started and stopped from outside (e.g. AI mode-selector, or player actions/orders).  E.g. of missions: fight mission (idles until all hostiles dead or interrupted), flee mission (idles when no more immediate danger, or no possibility of escape left), maintain-needs (tries to survey entity need and generate tasks to fulfil those needs otherwise idles).  (Note that if a mission is idling, it will prob satisfy the correpsonding ai mode to change modes for other behavior modes)


By combining missions, tasks, actions with the 2-tier mode AI, hopefully, we can achieve a basic ai model that is flexible enough to model a wide-enough range of behavior.
It will also become possible for developers to collaborate in a more organized manner, by breaking down the work specifically.  

E.g. some developers can concentrate on improving/perfecting some basic tasks, which can be used by other developers who design "missions", which in-turn can be aseembled into "modes" that can be executed by the mode-selector during run-time.

 

------------------------------------------------------------
5. Entity interaction model (currently just conversation)

 

Basically a simplistic model for NPC interaction with other NPCs or the player.  

Currently, I list down the following simple kinds:

 

a. flavor speech - these does nothing consequential, but just displays speech from the NPC which does not require response.  e.g. battle-cry when fighting, or random mumbling to self

 

b. casual interaction - these are non-commital interaction requests where response is not necessary, but another NPC may potentially repond.  e.g. greeting (other NPC may greet back if the greeting itself is not a reponse already, or just ignore it), chat (talks about random stuff, can pick from events in "Memory", response can be approval/disapproval/neutral, and then if the NPC is interested, may "counter" with another chat from his/her own Memory)  When the addressed party is the player, may need some kind of non-intrusive conversation response interface which will quickly fade away (assume no response) if not addressed within a short period of time.

 

c. important interaction - these are commital interaction request where the addressed party must respond (where usually one of the response will be to reject the interaction).  E.g. asking for food/water/supplies, or asking for help with injury.  When the addressed party is the player, may use similar response intrerface as casual interaction but with longer timeouts, which assumes "rejection" if not addressed within the time. 

 

d. notification - these are meant to send notifications to NPC(s) nearby with no expectations of response. e.g. "Hostiles spotted!" (which can caused friendly NPCs nearby to switch AI-mode to fight-and-flee faster, without having to actually see the hostile themselves). Or "(resource) spotted" when scavenging, such that nearby NPC with some needs (like food/water) can potentially come and pick it up.

 

Prob still need more work to build this up into an implementable model. Each kind of interaction is seen as a "protocol" which is provided as "base-class/template" that can potentially be extended by 3rd party modders and still be integratable into the system.  

 

Each "protocol" can also define its weights wrt needs and character, so that they can potentially be integrated in to the AI model (used in tasks) (e.g. instead of hard-coding interactions to fulfil some tasks, the task can dynamically examine all interactions including those added by 3rd-party, and make them available to the NPC entities)

 

For each kind of interaction, there can also be a speech-pattern model which can selects an exact speech pattern based on the NPC attributes and character (e.g. a gentle, elderly male ex-cook, vs a young, hot-tempered female student).  Ideally, speech patterns can be freely extended by 3-rd party mods.

 

------------------------------------------------------------

6. Helper modules

 

These are more general modules which I think would be useful. The reason for listing them separately is because they may potentially be used (re-used) by many tasks/missions, so instead of each task/mission developer coding the functions in their own creation, provide a common library so that tasks/missions can be developed and used more easily.

 

- Survey - I imagine that this module will consists of functions which surveys the in-game environment objectively (no preferences/heuristics, which should be done subjectively on the results).  E.g. look for all containers with some inventory item within a specified region (list of rectangular bounds), or list of all potential entry points to a specific regions

 

- Assess - often using the results from Survey module, this module will add priorities/weightages by analyzing those results with some supplied conditions/weights.  E.g. prioritize the potential entrances to a region based on security (hostile), hazard (fire/glass), ability (e.g. strength to climb sheeet-rope, climb over fence, key to unlock a door < allow NPC to carry keys and automatically lock/unlock doors)

 

- Schedule - allowing NPCs to make long term plans by making schedule for pre-planned tasks or make a time-table for regular tasks.  Provides (in-game)date-time-based functions in addition to a "table" that can be indexed/sorted by (in-game) date-time.

 

- Plan  - companion to schedule. Should include functions that can predict or allocate how long a task will last (potentially allow decision to interrupt a task to go to next task in schedule, or to cancel next task). Prob uses Survey/Assess a lot too.

 

- Knowledge - basically a tuple-based model of relating custom concepts, so that it may become possible to "teach" NPCs new concepts and use them dynamically in AI instead of being hard-coded.
e.g. instead of hard-coding "food" related code, instead define the "hunger" concept (based on the in-game need) and relate it to "food", and then relate it to the possible sources of food including the related tasks.  So in the future, by just defining new relations to existing concepts, the knowledge-base can be used to figure out how to satisfy certain goals without explicit coding.

Very far-fetched and may not be feasible (computationally speaking), but this is the foundations of "general" ai ;)  Prob not feasible to be used as basis for all tasks, but once the basic tasks that can satisfy gameplay are up, can potentially try to do "knowledge-based tasks" which makes use of the knowledge model to figure out what to do dynamically.

 

May have more which I forgot atm, (which is why it is good to dump ideas once in a while), but I think that is already a lot to write (and read).

 

If you have read the whole post until here, thank you for your time, and if you have ideas, comments, suggestions, pls add them to this post for further discussions.

until next time ;)

 

Link to comment
Share on other sites

Adding an example:

 

Example: possible emergent behavior possibly achievable from above model:


Scenario: Survivors before meeting zombies.
- Hungry ones will try to find and eat food
- Tired ones will rest (sit on ground, or nearby chairs)
- Inhured ones will try to treat/bandage.


Scenario: Survivors met with zombies.
- All previous actions (from bnefore meeting zombies) are interrupted.
- Cowardly ones may simply run away, while others will try to fight back while the odds are good.
- But when the odds turns bad (more zombies approach), they may all try to run away, especially after their allies started running.
- When they are cornered and have no way else to run, some whose personality "never gives up" will continue fighting until they die.
- Those not so strong mentally may simply breakdown and do nothing or panicked and do panicky things while the zombies pull them down to feast on them
- Others may decide to end their lives, ranging from a bullet to the under chin, or for the more vengeful type, throw a hand grenade at his feet.

 

------------------------------------------------------------

Possible common 2-tier ai mode structure  that drives all the above behavior:


- Mode(T1): Fight or Flee or Giveup - active when there is immediate danger.
  - Mission(T2):Fight, goal:all hostiles dead
    - Task:discreet attack (not to draw attention from zombies further away, using quiet melee or silent weapons)
    - Task:all-out attack(just use best weapon and any other means necessary)
  - Mission(T2):Flee, goal:no hostiles near
    - Task:Flee to: (tries to flee to a spot or in a direction)
    - Task:Flee: (tries to flee based on concentration of danger)
  - Mission(T2):Giveup, goal:die
    - Task:Do nothing
    - Task:Panic (do random stuff/scream/throw/drop items)
    - Task:Suicide (choose a means available)
    - Task:Vengeful end (choose a means that may also hurt nearby hostiles)

- Mode(T1): Survival - active when a need is high enough
  - Mission(T2):Tend to Injuries
    - Task: Tries to ask ally for help
    - Task: Tries to treat it
    - Task: Tries to find necessary items
    - Task: Tries to ask for necessary items
  - Mission(T2):Tend to Hunger
    - Task: Find and Eat food from inventory
    - Task: Find and Eat food from surrounding
    - Task: Ask allies for food
  - etc


Basically, each NPC ai selects an activated mode based on priority.

 

The Mode will continuously (periodically) evaluates the current context and sets/changes the Mission (potentially interrupting current mission)

 

An active Mission will loop to examine the options available to fulfil the goal (i.e. Tasks) and picks the most attractive one (based on objective factors like NPC status. item availability and environment and subjective reasons like NPC preferences and personality) as well as odds of success (e.g. if previous attempts failed, then the Task becomes less attractive)

 

At the same time the Mode-Selector will also (periodically) checks if the current Mode is no longer applicable, or if another higher priority Mode is activated and may switch the Mode where necessary (interrupting all current missions and tasks as a result).


 

Link to comment
Share on other sites

  • 4 weeks later...

Steam Link They work pretty great all things considered! Absolutely genius as far as interactions with the world go (coding involved is insane), currently requires a submod to fully function. Their logic is a bit funky but is easily adjustable for the most part. (and brings a real apocalyptic feel to the start) Would love to work on or test if you get anything else going :lol:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...