Skip to content

Commit 3b9f99e

Browse files
committedOct 18, 2015
ABMs: Make catch-up behaviour optional
Default is true for backwards compatibility Update lua_api.txt
1 parent 2364449 commit 3b9f99e

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed
 

Diff for: ‎doc/lua_api.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -3063,10 +3063,14 @@ Definition tables
30633063
{
30643064
-- In the following two fields, also group:groupname will work.
30653065
nodenames = {"default:lava_source"},
3066-
neighbors = {"default:water_source", "default:water_flowing"}, -- (any of these)
3067-
-- ^ If left out or empty, any neighbor will do
3068-
interval = 1.0, -- (operation interval)
3069-
chance = 1, -- (chance of trigger is 1.0/this)
3066+
neighbors = {"default:water_source", "default:water_flowing"}, -- Any of these --[[
3067+
^ If left out or empty, any neighbor will do ]]
3068+
interval = 1.0, -- Operation interval in seconds
3069+
chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this
3070+
catch_up = true, -- If true, catch-up behaviour is enabled --[[
3071+
^ The chance value is temporarily reduced when returning to
3072+
an area to simulate time lost by the area being unattended.
3073+
^ Note chance value can often be reduced to 1 ]]
30703074
action = func(pos, node, active_object_count, active_object_count_wider),
30713075
}
30723076

Diff for: ‎src/environment.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,21 @@ class ABMHandler
577577
i->timer -= trigger_interval;
578578
actual_interval = trigger_interval;
579579
}
580-
float intervals = actual_interval / trigger_interval;
581-
if(intervals == 0)
582-
continue;
583580
float chance = abm->getTriggerChance();
584581
if(chance == 0)
585582
chance = 1;
586583
ActiveABM aabm;
587584
aabm.abm = abm;
588-
aabm.chance = chance / intervals;
589-
if(aabm.chance == 0)
590-
aabm.chance = 1;
585+
if(abm->getSimpleCatchUp()) {
586+
float intervals = actual_interval / trigger_interval;
587+
if(intervals == 0)
588+
continue;
589+
aabm.chance = chance / intervals;
590+
if(aabm.chance == 0)
591+
aabm.chance = 1;
592+
} else {
593+
aabm.chance = chance;
594+
}
591595
// Trigger neighbors
592596
std::set<std::string> required_neighbors_s
593597
= abm->getRequiredNeighbors();

Diff for: ‎src/environment.h

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class ActiveBlockModifier
155155
virtual float getTriggerInterval() = 0;
156156
// Random chance of (1 / return value), 0 is disallowed
157157
virtual u32 getTriggerChance() = 0;
158+
// Whether to modify chance to simulate time lost by an unnattended block
159+
virtual bool getSimpleCatchUp() = 0;
158160
// This is called usually at interval for 1/chance of the nodes
159161
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
160162
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,

Diff for: ‎src/script/cpp_api/s_env.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
143143
int trigger_chance = 50;
144144
getintfield(L, current_abm, "chance", trigger_chance);
145145

146-
LuaABM *abm = new LuaABM(L, id, trigger_contents,
147-
required_neighbors, trigger_interval, trigger_chance);
146+
bool simple_catch_up = true;
147+
getboolfield(L, current_abm, "catch_up", simple_catch_up);
148+
149+
LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
150+
trigger_interval, trigger_chance, simple_catch_up);
148151

149152
env->addActiveBlockModifier(abm);
150153

Diff for: ‎src/script/lua_api/l_env.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,18 @@ class LuaABM : public ActiveBlockModifier
184184
std::set<std::string> m_required_neighbors;
185185
float m_trigger_interval;
186186
u32 m_trigger_chance;
187+
bool m_simple_catch_up;
187188
public:
188189
LuaABM(lua_State *L, int id,
189190
const std::set<std::string> &trigger_contents,
190191
const std::set<std::string> &required_neighbors,
191-
float trigger_interval, u32 trigger_chance):
192+
float trigger_interval, u32 trigger_chance, bool simple_catch_up):
192193
m_id(id),
193194
m_trigger_contents(trigger_contents),
194195
m_required_neighbors(required_neighbors),
195196
m_trigger_interval(trigger_interval),
196-
m_trigger_chance(trigger_chance)
197+
m_trigger_chance(trigger_chance),
198+
m_simple_catch_up(simple_catch_up)
197199
{
198200
}
199201
virtual std::set<std::string> getTriggerContents()
@@ -212,6 +214,10 @@ class LuaABM : public ActiveBlockModifier
212214
{
213215
return m_trigger_chance;
214216
}
217+
virtual bool getSimpleCatchUp()
218+
{
219+
return m_simple_catch_up;
220+
}
215221
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
216222
u32 active_object_count, u32 active_object_count_wider);
217223
};

0 commit comments

Comments
 (0)
Please sign in to comment.