AnimatLab  2
Test
Stomach.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Node.h"
13 #include "IPhysicsMovableItem.h"
14 #include "IPhysicsBody.h"
15 #include "BoundingBox.h"
16 #include "MovableItem.h"
17 #include "BodyPart.h"
18 #include "Joint.h"
19 #include "ReceptiveField.h"
20 #include "ContactSensor.h"
21 #include "RigidBody.h"
22 #include "Stomach.h"
23 #include "Structure.h"
24 #include "Organism.h"
25 #include "ActivatedItem.h"
26 #include "ActivatedItemMgr.h"
27 #include "DataChartMgr.h"
28 #include "ExternalStimuliMgr.h"
29 #include "KeyFrame.h"
30 #include "SimulationRecorder.h"
31 #include "OdorType.h"
32 #include "Odor.h"
33 #include "Light.h"
34 #include "LightManager.h"
35 #include "Simulator.h"
36 
37 namespace AnimatSim
38 {
39  namespace Environment
40  {
41  namespace Bodies
42  {
50 {
51  m_bKilled = false;
52  m_fltReportAlive = 1;
53  m_fltMaxEnergyLevel = 100000;
54  m_fltEnergyLevel = 2000;
60 }
61 
69 {
70  try
71  {
72  }
73  catch(...)
74  {Std_TraceMsg(0, "Caught Error in desctructor of Stomach\r\n", "", -1, false, true);}
75 }
76 
86 
99 void Stomach::EnergyLevel(float fltVal)
100 {
101  if(fltVal > m_fltMaxEnergyLevel)
103  else if(fltVal < 0)
104  m_fltEnergyLevel = 0;
105  else
106  m_fltEnergyLevel = fltVal;
107 
108  //If the sim is running then we do not set the init level. Only set it if changed while the sim is not running.
109  if(!m_lpSim->SimRunning())
111 }
112 
113 void Stomach::AddEnergy(float fltVal)
114 {
115  float fltEnergy = m_fltEnergyLevel + fltVal;
116  EnergyLevel(fltEnergy);
117 }
118 
128 
138 void Stomach::ConsumptionRate(float fltVal)
139 {
140  Std_IsAboveMin((float) 0, fltVal, true, "ConsumptionRate", true);
141  m_fltConsumptionRate = fltVal;
142 }
143 
153 
163 void Stomach::BaseConsumptionRate(float fltVal)
164 {
165  Std_IsAboveMin((float) 0, fltVal, true, "BaseConsumptionRate", true);
166  m_fltBaseConsumptionRate = fltVal;
167 }
168 
178 
188 void Stomach::MaxEnergyLevel(float fltVal)
189 {
190  Std_IsAboveMin((float) 0, fltVal, true, "MaxEnergyLevel");
191  m_fltMaxEnergyLevel = fltVal;
192 }
193 
203 
212 void Stomach::KillOrganism(bool bVal) {m_bKillOrganism = bVal;}
213 
214 
215 // There are no parts or joints to create for muscle attachment points.
217 {
218 }
219 
221 {
223 
227  m_bKilled = false;
228  m_fltReportAlive = 1;
229 }
230 
232 {
235 
236  //We have the replenish rate in Quantity/s, but we need it in Quantity/timeslice. Lets recalculate it here.
238 
240 
241  if(m_fltEnergyLevel < 0)
242  m_fltEnergyLevel = 0;
243 
245  {
246  Organism *lpOrganism = dynamic_cast<Organism *>(m_lpStructure);
247  if(!lpOrganism)
248  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Organism");
249 
250  lpOrganism->Kill(true);
251  m_bKilled = true;
252  m_fltReportAlive = 0;
253  }
254 }
255 
256 bool Stomach::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
257 {
258  std::string strType = Std_CheckString(strDataType);
259 
260  if(RigidBody::SetData(strType, strValue, false))
261  return true;
262 
263  if(strType == "KILLORGANISM")
264  {
265  KillOrganism(Std_ToBool(strValue));
266  return true;
267  }
268 
269  if(strType == "ENERGYLEVEL")
270  {
271  EnergyLevel((float) atof(strValue.c_str()));
272  return true;
273  }
274 
275  if(strType == "MAXENERGYLEVEL")
276  {
277  MaxEnergyLevel((float) atof(strValue.c_str()));
278  return true;
279  }
280 
281  if(strType == "BASECONSUMPTIONRATE")
282  {
283  BaseConsumptionRate((float) atof(strValue.c_str()));
284  return true;
285  }
286 
287  //If it was not one of those above then we have a problem.
288  if(bThrowError)
289  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
290 
291  return false;
292 }
293 
294 void Stomach::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
295 {
296  RigidBody::QueryProperties(aryProperties);
297 
298  aryProperties.Add(new TypeProperty("ConsumptionRate", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
299  aryProperties.Add(new TypeProperty("ConsumptionForStep", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
300  aryProperties.Add(new TypeProperty("AdapterConsumptionRate", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
301  aryProperties.Add(new TypeProperty("Alive", AnimatPropertyType::Boolean, AnimatPropertyDirection::Get));
302 
303  aryProperties.Add(new TypeProperty("KillOrganism", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
304  aryProperties.Add(new TypeProperty("EnergyLevel", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
305  aryProperties.Add(new TypeProperty("MaxEnergyLevel", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
306  aryProperties.Add(new TypeProperty("BaseConsumptionRate", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
307 }
308 
309 float *Stomach::GetDataPointer(const std::string &strDataType)
310 {
311  std::string strType = Std_CheckString(strDataType);
312 
313  if(strType == "ENERGYLEVEL")
314  return &m_fltEnergyLevel;
315 
316  if(strType == "CONSUMPTIONRATE")
317  return &m_fltConsumptionRate;
318 
319  if(strType == "CONSUMPTIONFORSTEP")
320  return &m_fltConsumptionForStep;
321 
322  if(strType == "ADAPTERCONSUMPTIONRATE")
324 
325  if(strType == "ALIVE")
326  return &m_fltReportAlive;
327 
328  return RigidBody::GetDataPointer(strDataType);
329 }
330 
331 void Stomach::AddExternalNodeInput(int iTargetDataType, float fltInput)
332 {
333  m_fltAdapterConsumptionRate += fltInput;
336 }
337 
338 void Stomach::Load(CStdXml &oXml)
339 {
340  RigidBody::Load(oXml);
341 
342  oXml.IntoElem(); //Into RigidBody Element
343 
344  MaxEnergyLevel(oXml.GetChildFloat("MaxEnergyLevel", m_fltMaxEnergyLevel));
345  EnergyLevel(oXml.GetChildFloat("EnergyLevel", m_fltEnergyLevel));
346  BaseConsumptionRate(oXml.GetChildFloat("BaseConsumptionRate", m_fltBaseConsumptionRate));
347  KillOrganism(oXml.GetChildBool("KillOrganism", true));
348 
349  oXml.OutOfElem(); //OutOf RigidBody Element
350 }
351 
352  } //Joints
353  } //Environment
354 } //AnimatSim
float m_fltEnergyLevel
The current energy level.
Definition: Stomach.h:37
Base class file for all Animat simulation objects.
virtual void PhysicsTimeStep(float fltVal)
Sets the integration time step for the physics engine.
Definition: Simulator.cpp:1126
Declares the simulation recorder class.
float m_fltBaseConsumptionRate
The base consumption rate. This is the standard, constant rate of consumption.
Definition: Stomach.h:47
Root namespace for the base simulation library for AnimatLab.
Stomach()
Default constructor.
Definition: Stomach.cpp:49
float m_fltConsumptionForStep
The consumption for the current simulation step. This is the consumption rate times the time step siz...
Definition: Stomach.h:50
Declares the body part class.
virtual float BaseConsumptionRate()
Gets the base consumption rate.
Definition: Stomach.cpp:152
virtual bool KillOrganism()
Gets whether to kill the organism if energy level reaches zero.
Definition: Stomach.cpp:202
bool m_bKillOrganism
If this is true then if the energy level reaches zero then the organism is killed.
Definition: Stomach.h:53
float m_fltConsumptionRate
The current consumption rate. This is calculated by adding m_fltAdapterConsumptionRate and m_fltBaseC...
Definition: Stomach.h:40
bool m_bKilled
Set to true if the organism is killed.
Definition: Stomach.h:56
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: RigidBody.cpp:1473
virtual ~Stomach()
Destructor.
Definition: Stomach.cpp:68
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
Declares the key frame class.
virtual void AddExternalNodeInput(int iTargetDataType, float fltInput)
Adds an external node input.
Definition: Stomach.cpp:331
Declares the joint class.
Declares the organism class.
virtual bool SimRunning()
Gets whether the simulation is running.
Definition: Simulator.cpp:673
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
Declares a light object.
Declares the activated item class.
Declares the stomach class.
Declares a light manager object.
Declares the bounding box class.
virtual float ConsumptionRate()
Gets the current consumption rate.
Definition: Stomach.cpp:127
A standard xml manipulation class.
Definition: StdXml.h:19
virtual float EnergyLevel()
Gets the energy level.
Definition: Stomach.cpp:85
virtual float MaxEnergyLevel()
Gets the maximum energy level.
Definition: Stomach.cpp:177
float m_fltReportAlive
Used to report in GetDataPointer if the organism was killed.
Definition: Stomach.h:59
Declares the node class.
virtual void CreateParts()
Allows the rigid body to create its parts using the chosen physics engine.
Definition: Stomach.cpp:216
bool Std_ToBool(int iVal)
Converts a value toa bool.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
Definition: StdXml.cpp:699
A dynamic organism that is controlled by a neural network.
Definition: Organism.h:31
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Stomach.cpp:220
Declares the data chart manager class.
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Declares the structure class.
Declares the odor type class.
virtual void Kill(bool bState=true)
Kills.
Definition: Organism.cpp:95
Declares the odor class.
Declares the simulator class.
Declares the activated item manager class.
float m_fltInitEnergyLevel
The starting energy level.
Definition: Stomach.h:34
Declares the contact sensor class.
Declares the external stimuli manager class.
virtual void StepSimulation()
Step the simulation for this object.
Definition: Stomach.cpp:231
Declares the receptive field class.
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617