Auto generate quests - monsters not spawned

Announcements, support questions, and discussion for Quest Machine.
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Auto generate quests - monsters not spawned

Post by Tony Li »

Here's a method to determine if an EntityType is a monster:

Code: Select all

public EntityType monsterType; // Assign in inspector.

private bool IsMonster(EntityType entityType)
{
    if (entityType == monsterType) return true;
    foreach (var parent in entityType.parents)
    {
        if (IsMonster(parent)) return true;
    }
    return false;
}

Here's a more complete example with proper error checking for nulls and looping (e.g., monster is parent of wolf, but wolf is parent of monster). If you add the script to a Quest Generator Entity and assign your Monster type, when it generates a world model for quests it will report which entities are monsters.

Code: Select all

using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.QuestMachine;

public class TestGenerator : MonoBehaviour
{
    public EntityType monsterType;

    private void Start()
    {
        GetComponent<QuestGeneratorEntity>().updateWorldModel += OnUpdateWorldModel;
    }

    private void OnUpdateWorldModel(WorldModel worldModel)
    {
        foreach (var fact in worldModel.facts)
        {
            if (IsMonster(fact.entityType))
            {
                Debug.Log("MONSTER: " + fact.count + " " + fact.entityType.name + " in " + fact.domainType.name);
            }
        }
    }

    private bool IsMonster(EntityType entityType)
    {
        var checkedEntities = new List<EntityType>();
        return IsMonsterRecursive(entityType, checkedEntities);
    }

    private bool IsMonsterRecursive(EntityType entityType, List<EntityType> checkedEntities)
    {
        if (entityType == null || checkedEntities.Contains(entityType)) return false;
        checkedEntities.Add(entityType);
        if (entityType == monsterType) return true;
        foreach (var parent in entityType.parents)
        {
            if (IsMonsterRecursive(parent, checkedEntities)) return true;
        }
        return false;
    }
}
dlevel
Posts: 168
Joined: Wed Nov 16, 2016 6:17 pm

Re: Auto generate quests - monsters not spawned

Post by dlevel »

you rock.
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Auto generate quests - monsters not spawned

Post by Tony Li »

Glad to help! :-)
Post Reply