AI Model Wiki

    Skip to main content
    22 min
    Lesson 2 of 4

    Intelligent NPC Behavior

    From the course AI for Game Development

    Create believable NPC behaviors using modern AI techniques beyond traditional behavior trees.

    LLM-Powered NPCs

    Conversational NPCs

    class IntelligentNPC:
        def __init__(self, character_profile):
            self.profile = character_profile
            self.memory = ConversationMemory()
            self.world_state = WorldStateAccess()
            
        def respond(self, player_input):
            # Build context for LLM
            context = f"""
            Character: {self.profile.name}
            Personality: {self.profile.personality}
            Current location: {self.world_state.current_location}
            Player relationship: {self.profile.get_relationship('player')}
            Recent events: {self.world_state.recent_events}
            
            Conversation history:
            {self.memory.get_recent(5)}
            
            Player says: {player_input}
            
            Respond in character. Include emotional cues in brackets.
            """
            
            response = llm.generate(context, max_tokens=150)
            self.memory.add(player_input, response)
            
            return self.parse_response(response)
    

    Emergent Behaviors

    Goal-Oriented Action Planning (GOAP)

    class GOAPAgent:
        def __init__(self, actions, goals):
            self.actions = actions  # Available actions with preconditions/effects
            self.goals = goals      # Priority-ordered goals
            
        def plan(self, world_state):
            for goal in sorted(self.goals, key=lambda g: g.priority, reverse=True):
                if self.is_satisfied(goal, world_state):
                    continue
                    
                # A* search through action space
                plan = self.search_plan(world_state, goal)
                if plan:
                    return plan
            
            return IdleAction()
        
        def search_plan(self, start_state, goal):
            # Find sequence of actions to satisfy goal
            open_set = [(0, start_state, [])]
            
            while open_set:
                cost, state, plan = heapq.heappop(open_set)
                
                if self.is_satisfied(goal, state):
                    return plan
                
                for action in self.get_valid_actions(state):
                    new_state = self.apply_action(action, state)
                    new_cost = cost + action.cost
                    heapq.heappush(open_set, (new_cost, new_state, plan + [action]))
            
            return None
    

    Utility AI

    Utility-Based Decision Making:
    
    For each possible action:
    ├── Calculate utility scores across multiple factors
    ├── hunger_utility = curve(npc.hunger, 0, 100)
    ├── safety_utility = curve(threat_level, inverse=True)  
    ├── social_utility = curve(time_since_conversation)
    └── Select action with highest combined utility
    
    Advantages:
    ├── Smooth behavior transitions
    ├── Easy to tune with curves
    ├── Emergent decision-making
    └── Handles complex tradeoffs
    

    Key Takeaways

    • •LLM-powered NPCs maintain memory, personality, and react to world state dynamically
    • •GOAP enables emergent behavior by planning action sequences to achieve goals
    • •Utility AI creates smooth behavior transitions with tunable utility curves
    • •Combining techniques (LLM dialogue + GOAP actions) creates believable characters
    0 of 4 lessons marked complete

    Progress is kept in this browser only.