Esempio n. 1
0
        //private TBlackboard _defaultBlackboard;

        public static TreeRuntime Create <TBlackboard>(TreeDef tree, int capacity, TBlackboard defaultBlackboard)
            where TBlackboard : struct, IComponentData
        {
            var rt = new TreeRuntime
            {
                Def      = tree,
                Capacity = capacity,
            };

            rt.NodeData        = new NativeArray <byte>(tree.Nodes.Sum(n => n.DataSize) * capacity, Allocator.Persistent, NativeArrayOptions.ClearMemory);
            rt.StateData       = new NativeArray <NodeState>(tree.Nodes.Length * capacity, Allocator.Persistent, NativeArrayOptions.ClearMemory);
            rt.NodeDataOffset  = new NativeArray <int>(tree.Nodes.Length, Allocator.Persistent);
            rt._allocHandle    = GCHandle.Alloc(new AllocState());
            rt._blackboardType = typeof(TBlackboard);
            //rt._defaultBlackboard = defaultBlackboard;
            int off = 0;

            for (int i = 0; i < tree.Nodes.Length; ++i)
            {
                rt.NodeDataOffset[i] = off;
                off += tree.Nodes[i].DataSize * capacity;
            }

            return(rt);
        }
Esempio n. 2
0
        public static TreeDef Compile(NodeBuilder root)
        {
            var layers = new List <int>();

            var open     = new List <NodeBuilder>();
            var nodes    = new INode[root.TreeCount];
            var children = new TreeDef.Span[root.TreeCount];

            open.Add(root);
            int nodeIdx = 0;

            while (open.Count > 0)
            {
                layers.Add(open.Count);

                var layerNodes = open.ToArray();
                open.Clear();

                var childPos = nodeIdx + layerNodes.Length;

                foreach (var n in layerNodes)
                {
                    nodes[nodeIdx]    = n.Node;
                    children[nodeIdx] = new TreeDef.Span(n.ChildCount > 0 ? childPos : -1, n.ChildCount);

                    ++nodeIdx;

                    if (n.ChildCount > 0)
                    {
                        childPos += n.ChildCount;
                        open.AddRange(n.Children);
                    }
                }
            }

            var bt = new TreeDef
            {
                Nodes    = nodes,
                Children = children,
            };

            return(bt);
        }
Esempio n. 3
0
        public unsafe void Update(float dt, int nodeIndex, TreeDef treeDef, NativeSlice <NodeState> state, NativeSlice <byte> data)
        {
            Profiler.BeginSample("TNode::Update");
            var arr = data.SliceConvert <T>();

            for (int i = 0; i < arr.Length; ++i)
            {
                var h = new NodeStateHandle(nodeIndex, state.Slice(i * treeDef.Nodes.Length, treeDef.Nodes.Length), treeDef);
                var v = arr[i];

                if (h.State == NodeState.Activating)
                {
                    h.State = Activate(h, ref v);
                }

                if (h.State == NodeState.Running)
                {
                    h.State = Update(dt, h, ref v);
                    arr[i]  = v;
                }
            }
            Profiler.EndSample();
        }
Esempio n. 4
0
        public void Update(float dt, int nodeIndex, TreeDef treeDef, NativeSlice <NodeState> state, NativeSlice <byte> data)
        {
            int nStates = state.Length / treeDef.Nodes.Length;

            for (int i = 0; i < nStates; ++i)
            {
                var h = new NodeStateHandle(nodeIndex, state.Slice(i * treeDef.Nodes.Length, treeDef.Nodes.Length), treeDef);

                if (!h.State.Active())
                {
                    continue;
                }

                if (h.State == NodeState.Activating)
                {
                    h.State = Activate(h);
                }

                if (h.State == NodeState.Running)
                {
                    h.State = Update(dt, h);
                }
            }
        }
Esempio n. 5
0
 public Job(TreeDef def, INode node, ExecutionParams execParams)
 {
     Def    = def;
     Node   = node;
     Params = execParams;
 }
Esempio n. 6
0
 public NodeStateHandle(int nodeIndex, NativeSlice <NodeState> treeState, TreeDef def)
 {
     NodeIndex = nodeIndex;
     TreeState = treeState;
     Def       = def;
 }