public bool TryGet(int nodeIdx, int potentialLayerMask, PotentialFilter filter, float currentTime, out float potential) { var hashcode = Entry.GetHashCode(nodeIdx, potentialLayerMask, filter); potential = 0f; var hit = false; IList <Entry> cachedSamples; if (potentialCache.TryGet(hashcode, out cachedSamples)) { for (int i = 0; i < cachedSamples.Count; i++) { var cs = cachedSamples[i]; if (cs.node == nodeIdx && cs.layerMask == potentialLayerMask && cs.filter == filter) { if (currentTime - cs.time >= lifetime) { break; } potential = cs.potential; hit = true; break; } } } return(hit); }
public Entry(int node, int layerMask, PotentialFilter filter, float time, float potential) { this.node = node; this.layerMask = layerMask; this.filter = filter; this.time = time; this.potential = potential; }
public void Set(int nodeIdx, int potentialLayerMask, PotentialFilter filter, float currentTime, float potential) { var hashcode = Entry.GetHashCode(nodeIdx, potentialLayerMask, filter); var item = new Entry(nodeIdx, potentialLayerMask, filter, currentTime, potential); potentialCache.Remove(hashcode, item); potentialCache.Add(hashcode, item); }
public static int GetHashCode(int nodeIdx, int potentialLayerMask, PotentialFilter filter) { var hash = 17; hash = hash * 23 + nodeIdx.GetHashCode(); hash = hash * 23 + potentialLayerMask.GetHashCode(); if (filter != null) { hash = hash * 23 + filter.GetHashCode(); } return(hash); }
private void AddNextStrongest() { if (this.isWorking) { return; } this.isWorking = true; if (this.targetNode == null) { this.targetNode = this.PotentialField.GetClosestNode(this.tx.position); } // Disable the target node potential first so that it won't affect the choice of the next node. It will remain // disabled if no node with a higher potential is found. if (this.targetNodePotential.enabled) { if (this.targetNodeSource == null) { this.targetNodeSource = this.source.AddNodeSource(this.targetNodePotential.sourceKey, this.targetNodePotential.layers.value, this.targetNodePotential.potential); this.targetNodeSource.Calculator = this.targetNodePotential.calculator as IPotentialCalculator; } this.targetNodeSource.Enabled = false; } if (this.filter == null) { var go = this.gameObject; this.filter = (source) => (GameObject)source.Context != go || !ignoreSourceKeys.Contains(source.SourceKey); } var samples = this.PotentialField.SamplePotential(this.potentialSampler, this.targetNode, this.layerMask, this.filter); this.OnNextStrongestSearchFinished(samples); samples.ReturnToPool(); }
public float SamplePotential(SimpleGraphFieldNodeRef node, IList <PotentialFieldNodeSource> sources, PotentialFilter filter, Func <float, float, float> combiner) { var potential = 0f; for (int k = 0; k < sources.Count; k++) { var source = sources[k]; if (source.Enabled && (filter == null || filter(source))) { var sourcePotential = 0f; if (node == source.Node) { sourcePotential = source.Potential; } else if (source.Calculator != null) { sourcePotential = source.Calculator.GetPotential(node.Position, source.Node.Position, source.Potential); } potential = combiner(potential, sourcePotential); } } return(potential); }
public void ScheduleSamplePotential(IPotentialSampleStrategy strategy, IFieldNodeRef startNode, int layerMask, PotentialFilter filter, NodeSamplerDelegate callback) { throw new System.NotImplementedException(); }
public IList <NodeSample> SamplePotential(IPotentialSampleStrategy strategy, IFieldNodeRef startNode, int layerMask, PotentialFilter filter) { return(strategy.SampleNodes(this, startNode, layerMask, filter)); }
public IList <NodeSample> SampleNodes(IPotentialFieldSystem potentialField, IFieldNodeRef startNode, int layerMask, PotentialFilter filter) { var neighbours = potentialField.GetNeighbours(startNode); neighbours.Insert(0, startNode); var sampleRequest = new SampleRequest(); sampleRequest.potentialLayerMask = layerMask; sampleRequest.filter = filter; sampleRequest.nodes = neighbours; var potentials = potentialField.SamplePotential(sampleRequest); var maxP = potentials[0]; var maxN = 0; for (int i = 1; i < potentials.Count; i++) { var p2 = potentials[i]; if (p2 > maxP) { maxN = i; maxP = p2; } } potentials.ReturnToPool(); var results = ListPool <NodeSample> .Take(); results.Add(new NodeSample(neighbours[maxN], maxP)); neighbours.ReturnToPool(); return(results); }