[Avida-cvs] [avida-svn] r876 - in development: Avida.xcodeproj source/actions source/event
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Sat Aug 19 14:29:37 PDT 2006
Author: brysonda
Date: 2006-08-19 17:29:36 -0400 (Sat, 19 Aug 2006)
New Revision: 876
Modified:
development/Avida.xcodeproj/project.pbxproj
development/source/actions/EnvironmentActions.cc
development/source/event/cEventManager.cc
Log:
Transition various resource events into the actions framework.
Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj 2006-08-17 18:02:14 UTC (rev 875)
+++ development/Avida.xcodeproj/project.pbxproj 2006-08-19 21:29:36 UTC (rev 876)
@@ -834,7 +834,7 @@
70DCAC77097AF730002F8733 /* key_chart */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = key_chart; sourceTree = "<group>"; };
70DCAC78097AF730002F8733 /* viewer.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = viewer.cc; sourceTree = "<group>"; };
70DCAC9B097AF7C0002F8733 /* primitive.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = primitive.cc; sourceTree = "<group>"; };
- 70DCAD1C097AF7CC002F8733 /* avida-s */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = "avida-s"; sourceTree = BUILT_PRODUCTS_DIR; };
+ 70DCAD1C097AF7CC002F8733 /* avida-s */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "avida-s"; sourceTree = BUILT_PRODUCTS_DIR; };
70DCAD1F097AF81A002F8733 /* AvidaScript.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = AvidaScript.h; sourceTree = "<group>"; };
70DCAD20097AF81A002F8733 /* cLexer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cLexer.h; sourceTree = "<group>"; };
70DCAD21097AF81A002F8733 /* cLexer.l */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.lex; path = cLexer.l; sourceTree = "<group>"; };
Modified: development/source/actions/EnvironmentActions.cc
===================================================================
--- development/source/actions/EnvironmentActions.cc 2006-08-17 18:02:14 UTC (rev 875)
+++ development/source/actions/EnvironmentActions.cc 2006-08-19 21:29:36 UTC (rev 876)
@@ -11,8 +11,140 @@
#include "cAction.h"
#include "cActionLibrary.h"
+#include "cEnvironment.h"
+#include "cPopulation.h"
+#include "cResource.h"
+#include "cStats.h"
+#include "cWorld.h"
+class cActionInjectResource : public cAction
+{
+private:
+ cString m_res_name;
+ double m_res_count;
+
+public:
+ cActionInjectResource(cWorld* world, const cString& args) : cAction(world, args), m_res_name(""), m_res_count(0.0)
+ {
+ cString largs(args);
+ if (largs.GetSize()) m_res_name = largs.PopWord();
+ if (largs.GetSize()) m_res_count = largs.PopWord().AsDouble();
+ }
+ const cString GetDescription() { return "InjectResource <string res_name> <double res_count>"; }
+
+ void Process(cAvidaContext& ctx)
+ {
+ cResource* res = m_world->GetEnvironment().GetResourceLib().GetResource(m_res_name);
+ if (res != NULL) m_world->GetPopulation().UpdateResource(res->GetID(), m_res_count);
+ }
+};
+
+
+/*
+ Inject (add) a specified amount of a specified resource, scaled by
+ the current average merit divided by the average time slice.
+*/
+class cActionInjectScaledResource : public cAction
+{
+private:
+ cString m_res_name;
+ double m_res_count;
+
+public:
+ cActionInjectScaledResource(cWorld* world, const cString& args) : cAction(world, args), m_res_name(""), m_res_count(0.0)
+ {
+ cString largs(args);
+ if (largs.GetSize()) m_res_name = largs.PopWord();
+ if (largs.GetSize()) m_res_count = largs.PopWord().AsDouble();
+ }
+
+ const cString GetDescription() { return "InjectScaledResource <string res_name> <double res_count>"; }
+
+ void Process(cAvidaContext& ctx)
+ {
+ double ave_merit = m_world->GetStats().SumMerit().Average();
+ if (ave_merit <= 0.0) ave_merit = 1.0; // make sure that we don't get NAN's or negative numbers
+ ave_merit /= m_world->GetConfig().AVE_TIME_SLICE.Get();
+
+ cResource* res = m_world->GetEnvironment().GetResourceLib().GetResource(m_res_name);
+ if (res != NULL) m_world->GetPopulation().UpdateResource(res->GetID(), (m_res_count / ave_merit));
+ }
+};
+
+
+/*
+ Removes a specified percentage of a specified resource, scaled by
+ the current average merit divided by the average time slice.
+ */
+class cActionOutflowScaledResource : public cAction
+{
+private:
+ cString m_res_name;
+ double m_res_percent;
+
+public:
+ cActionOutflowScaledResource(cWorld* world, const cString& args) : cAction(world, args), m_res_name(""), m_res_percent(0.0)
+ {
+ cString largs(args);
+ if (largs.GetSize()) m_res_name = largs.PopWord();
+ if (largs.GetSize()) m_res_percent = largs.PopWord().AsDouble();
+ }
+
+ const cString GetDescription() { return "OutflowScaledResource <string res_name> <double res_count>"; }
+
+ void Process(cAvidaContext& ctx)
+ {
+ double ave_merit = m_world->GetStats().SumMerit().Average();
+ if (ave_merit <= 0.0) ave_merit = 1.0; // make sure that we don't get NAN's or negative numbers
+ ave_merit /= m_world->GetConfig().AVE_TIME_SLICE.Get();
+
+ cResource* res = m_world->GetEnvironment().GetResourceLib().GetResource(m_res_name);
+
+ double res_level = m_world->GetPopulation().GetResource(res->GetID());
+ double scaled_perc = 1 / (1 + ave_merit * (1 - m_res_percent) / m_res_percent);
+ res_level -= res_level * scaled_perc;
+
+ if (res != NULL) m_world->GetPopulation().UpdateResource(res->GetID(), res_level);
+ }
+};
+
+
+class cActionSetResource : public cAction
+{
+private:
+ cString m_res_name;
+ double m_res_count;
+
+public:
+ cActionSetResource(cWorld* world, const cString& args) : cAction(world, args), m_res_name(""), m_res_count(0.0)
+ {
+ cString largs(args);
+ if (largs.GetSize()) m_res_name = largs.PopWord();
+ if (largs.GetSize()) m_res_count = largs.PopWord().AsDouble();
+ }
+
+ const cString GetDescription() { return "SetResource <string res_name> <double res_count>"; }
+
+ void Process(cAvidaContext& ctx)
+ {
+ cResource* res = m_world->GetEnvironment().GetResourceLib().GetResource(m_res_name);
+ if (res != NULL) m_world->GetPopulation().SetResource(res->GetID(), m_res_count);
+ }
+};
+
+
+
void RegisterEnvironmentActions(cActionLibrary* action_lib)
{
+ action_lib->Register<cActionInjectResource>("InjectResource");
+ action_lib->Register<cActionInjectScaledResource>("InjectScaledResource");
+ action_lib->Register<cActionOutflowScaledResource>("OutflowScaledResource");
+ action_lib->Register<cActionSetResource>("SetResource");
+
+ // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
+ action_lib->Register<cActionInjectResource>("inject_resource");
+ action_lib->Register<cActionInjectScaledResource>("inject_scaled_resource");
+ action_lib->Register<cActionOutflowScaledResource>("outflow_scaled_resource");
+ action_lib->Register<cActionSetResource>("set_resource");
}
Modified: development/source/event/cEventManager.cc
===================================================================
--- development/source/event/cEventManager.cc 2006-08-17 18:02:14 UTC (rev 875)
+++ development/source/event/cEventManager.cc 2006-08-19 21:29:36 UTC (rev 876)
@@ -525,148 +525,9 @@
}
};
-///// inject_resource /////
-/**
-* Inject (add) a specified amount of a specified resource.
- **/
-class cEvent_inject_resource : public cEvent {
-private:
- cString res_name;
- double res_count;
-public:
- const cString GetName() const { return "inject_resource"; }
- const cString GetDescription() const { return "inject_resource <cString res_name> <double res_count>"; }
-
- void Configure(cWorld* world, const cString& in_args)
- {
- m_world = world;
- m_args = in_args;
- cString args(in_args);
- res_name = args.PopWord();
- res_count = args.PopWord().AsDouble();
- }
- ///// inject_resource /////
- void Process(){
- cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
- int res_id = res_lib.GetResource(res_name)->GetID();
- m_world->GetPopulation().UpdateResource(res_id, res_count);
- }
-};
-
-///// set_resource /////
-
-/**
-* Set the resource amount to a specific level
- **/
-
-
-class cEvent_set_resource : public cEvent {
-private:
- cString res_name;
- double res_count;
-public:
- const cString GetName() const { return "set_resource"; }
- const cString GetDescription() const { return "set_resource <cString res_name> <double res_count>"; }
-
- void Configure(cWorld* world, const cString& in_args)
- {
- m_world = world;
- m_args = in_args;
- cString args(in_args);
- res_name = args.PopWord();
- res_count = args.PopWord().AsDouble();
- }
- ///// set_resource /////
- void Process(){
- cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
- cResource * found_resource = res_lib.GetResource(res_name);
- if (found_resource != NULL) {
- m_world->GetPopulation().SetResource(found_resource->GetID(), res_count);
- }
- }
-};
-
-///// inject_scaled_resource /////
-
-/**
-* Inject (add) a specified amount of a specified resource, scaled by
- * the current average merit divided by the average time slice.
- **/
-
-
-class cEvent_inject_scaled_resource : public cEvent {
-private:
- cString res_name;
- double res_count;
-public:
- const cString GetName() const { return "inject_scaled_resource"; }
- const cString GetDescription() const { return "inject_scaled_resource <cString res_name> <double res_count>"; }
-
- void Configure(cWorld* world, const cString& in_args)
- {
- m_world = world;
- m_args = in_args;
- cString args(in_args);
- res_name = args.PopWord();
- res_count = args.PopWord().AsDouble();
- }
- ///// inject_scaled_resource /////
- void Process(){
- double ave_merit = m_world->GetStats().SumMerit().Average();
- if ( ave_merit <= 0 )
- ave_merit = 1; // make sure that we don't get NAN's or negative numbers
- ave_merit /= m_world->GetConfig().AVE_TIME_SLICE.Get();
- cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
- int res_id = res_lib.GetResource(res_name)->GetID();
- m_world->GetPopulation().UpdateResource(res_id, res_count/ave_merit);
- }
-};
-
-
-///// outflow_scaled_resource /////
-
-/**
-* Removes a specified percentage of a specified resource, scaled by
- * the current average merit divided by the average time slice.
- **/
-class cEvent_outflow_scaled_resource : public cEvent {
-private:
- cString res_name;
- double res_perc;
-public:
- const cString GetName() const { return "outflow_scaled_resource"; }
- const cString GetDescription() const { return "outflow_scaled_resource <cString res_name> <double res_perc>"; }
-
- void Configure(cWorld* world, const cString& in_args)
- {
- m_world = world;
- m_args = in_args;
- cString args(in_args);
- res_name = args.PopWord();
- res_perc = args.PopWord().AsDouble();
- }
- void Process()
- {
- double ave_merit = m_world->GetStats().SumMerit().Average();
- if ( ave_merit <= 0 )
- ave_merit = 1; // make sure that we don't get NAN's or negative numbers
- ave_merit /= m_world->GetConfig().AVE_TIME_SLICE.Get();
- cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
- int res_id = res_lib.GetResource(res_name)->GetID();
- double res_level = m_world->GetPopulation().GetResource(res_id);
- // a quick calculation shows that this formula guarantees that
- // the equilibrium level when resource is not used is independent
- // of the average merit
- double scaled_perc = 1/(1+ave_merit*(1-res_perc)/res_perc);
- res_level -= res_level*scaled_perc;
- m_world->GetPopulation().SetResource(res_id, res_level);
- }
-};
-
-
///// set_reaction_value /////
/**
@@ -791,10 +652,6 @@
REGISTER(disconnect_cells);
// Environment events --> Environment Action
- REGISTER(inject_resource);
- REGISTER(set_resource);
- REGISTER(inject_scaled_resource);
- REGISTER(outflow_scaled_resource);
REGISTER(set_reaction_value);
REGISTER(set_reaction_value_mult);
REGISTER(set_reaction_inst);
More information about the Avida-cvs
mailing list