root/src/BRAINSFramework/Agent.cs

2023
50
            }
50
            }
51
        }
51
        }
52
52
53
        public Dictionary<uint, int> Labels;
54
53
        /// <summary>
55
        /// <summary>
54
        /// Gets the cells the agent is in
56
        /// Gets the cells the agent is in
55
        /// </summary>
57
        /// </summary>
...
...
60
        /// The position of the Actor
62
        /// The position of the Actor
61
        /// </summary>
63
        /// </summary>
62
        public Vector2 Position { get; set; }
64
        public Vector2 Position { get; set; }
65
        
63
        /// <summary>
66
        /// <summary>
64
        /// Gets or sets the previous position of the agent
67
        /// Gets or sets the previous position of the agent
65
        /// </summary>
68
        /// </summary>
...
...
141
            AddFeeler(.5f, -.5f, Radius + 32);
144
            AddFeeler(.5f, -.5f, Radius + 32);
142
145
143
            Name = CreateUniqueName();
146
            Name = CreateUniqueName();
147
            Labels = new Dictionary<uint, int>();
144
        }
148
        }
145
149
146
        private string CreateUniqueName()
150
        private string CreateUniqueName()
...
...
153
            Feelers.Add(new Feeler(new Vector2(x, y),length,this));
157
            Feelers.Add(new Feeler(new Vector2(x, y),length,this));
154
        }
158
        }
155
159
160
        public virtual void UpdatePerception(GameTime gameTime)
161
        {
162
            //Tag nearby
163
        }
164
156
        /// <summary>
165
        /// <summary>
157
        /// Update the agent every frame. 
166
        /// Update the agent every frame. 
158
        /// </summary>
167
        /// </summary>
...
...
193
            // - for each component calculate row and col
202
            // - for each component calculate row and col
194
203
195
            Vector2 vDistanceFromMapCenter = this.Position;
204
            Vector2 vDistanceFromMapCenter = this.Position;
196
            Grid map = ParentWorld.Map.ClusterGrid.Grids[0];
205
            Grid map = ParentWorld.Map.ClusterGrid.GetGridAtPosition(Position);
206
            if (map == null)
207
                return;
197
            float _hstep = (map.Width/ ((float)map.Cols));
208
            float _hstep = (map.Width/ ((float)map.Cols));
198
            float _vstep = (map.Height/ ((float)map.Rows));
209
            float _vstep = (map.Height/ ((float)map.Rows));
199
            float xComponent = (vDistanceFromMapCenter.X / _hstep) ;
210
            float xComponent = (vDistanceFromMapCenter.X / _hstep) ;
200
            float yComponent = (vDistanceFromMapCenter.Y / _vstep);
211
            float yComponent = (vDistanceFromMapCenter.Y / _vstep);
201
212
202
            xComponent = xComponent - 0.5f;
213
           // xComponent = xComponent - 0.5f;
203
            yComponent = yComponent - 0.5f;
214
            //yComponent = yComponent - 0.5f;
204
215
205
            //debugX = (int)xComponent;
216
            //debugX = (int)xComponent;
206
            //debugY = (int)yComponent;
217
            //debugY = (int)yComponent;
...
...
229
                Console.Write("");
240
                Console.Write("");
230
            }
241
            }
231
        }
242
        }
243
        
232
        /// <summary>
244
        /// <summary>
233
        /// Loads a Behavior from a file
245
        /// Loads a Behavior from a file
234
        /// </summary>
246
        /// </summary>
...
...
240
            BehaviorNode _rootNode = (BehaviorNode)_ser.Deserialize(_reader);
252
            BehaviorNode _rootNode = (BehaviorNode)_ser.Deserialize(_reader);
241
            _reader.Close();
253
            _reader.Close();
242
254
243
            BehaviorBase _behavior = CreateBehaviorInstance(_rootNode.Parameters[0].Value);
255
            CompositeBehavior _behavior = CreateBehaviorInstance(_rootNode.Parameters[0].Value);
244
            
256
            
245
            SetupBehavior(_behavior, _rootNode);
257
            SetupBehavior(_behavior, _rootNode);
246
            RootBehavior = _behavior;
258
            RootBehavior = _behavior;
...
...
263
                    else
275
                    else
264
                        prop.SetValue(behavior, param.Value, null);
276
                        prop.SetValue(behavior, param.Value, null);
265
                }
277
                }
266
                parentBehavior.SubBehaviors.Add(behavior);
278
                if(parentBehavior is ISubBehaviorHolder)
279
                    ((ISubBehaviorHolder)parentBehavior).SubBehaviors.Add(behavior);
267
280
268
                SetupBehavior(behavior, item);
281
                SetupBehavior(behavior, item);
269
            }
282
            }
270
        }
283
        }
271
284
272
        private BehaviorBase CreateBehaviorInstance(string typeName)
285
        private CompositeBehavior CreateBehaviorInstance(string typeName)
273
        {
286
        {
274
            foreach (var item in ParentWorld.Engine.Assemblies)
287
            foreach (var item in ParentWorld.Engine.Assemblies)
275
            {
288
            {
276
                BehaviorBase behavior =(BehaviorBase)item.CreateInstance(typeName);
289
                CompositeBehavior behavior =(CompositeBehavior)item.CreateInstance(typeName);
277
                if (behavior != null)
290
                if (behavior != null)
278
                    return behavior;
291
                    return behavior;
279
            }
292
            }
280
            return null;
293
            return null;
281
        }
294
        }
282
295
283
        public virtual byte TypeToCost(int type)
296
        /// <summary>
297
        /// Gets the cost of a grid cell type for use with pathfinding.
298
        /// </summary>
299
        /// <param name="type">The Type value from a GridCell</param>
300
        /// <returns>The cost of the cell</returns>
301
        /// <remarks>Override this method for greater flexibility in pathfinding</remarks>
302
        public virtual byte GetCostOfCellType(int type)
284
        {
303
        {
285
            if (type == 0)
304
            if (type == 0)
286
                return 99;
305
                return 99;
287
            else
306
            else
288
                return (byte)type;
307
                return (byte)type;
289
        }
308
        }
309
310
        public void SetLabel(uint labelid, int labelvalue)
311
        {
312
            if (!Labels.Keys.Contains(labelid))
313
                Labels.Add(labelid, labelvalue);
314
            else
315
                Labels[labelid] = labelvalue;
316
        }
317
318
        public int GetLabel(uint labelid)
319
        {
320
            if (!Labels.Keys.Contains(labelid))
321
                return 0;
322
            else
323
                return Labels[labelid];
324
        }
290
    }
325
    }
291
}
326
}