(was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
(was > 0.5f && m_view_bobbing_anim <= 0.5f));
if(step) {
- MtEvent *e = new SimpleTriggerEvent("ViewBobbingStep");
- m_client->event()->put(e);
+ m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
}
}
}
- if (m_digging_button != -1)
- {
- f32 offset = dtime * 3.5;
+ if (m_digging_button != -1) {
+ f32 offset = dtime * 3.5f;
float m_digging_anim_was = m_digging_anim;
m_digging_anim += offset;
if (m_digging_anim >= 1)
float lim = 0.15;
if(m_digging_anim_was < lim && m_digging_anim >= lim)
{
- if(m_digging_button == 0)
- {
- MtEvent *e = new SimpleTriggerEvent("CameraPunchLeft");
- m_client->event()->put(e);
+ if (m_digging_button == 0) {
+ m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
} else if(m_digging_button == 1) {
- MtEvent *e = new SimpleTriggerEvent("CameraPunchRight");
- m_client->event()->put(e);
+ m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
}
}
}
ICraftDefManager* getCraftDefManager() override;
ITextureSource* getTextureSource();
virtual IShaderSource* getShaderSource();
- IShaderSource *shsrc() { return getShaderSource(); }
u16 allocateUnknownNodeId(const std::string &name) override;
virtual ISoundManager* getSoundManager();
- MtEventManager* getEventManager() override;
+ MtEventManager* getEventManager();
virtual ParticleManager* getParticleManager();
bool checkLocalPrivilege(const std::string &priv)
{ return checkPrivilege(priv); }
--- /dev/null
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+
+#include "event.h"
+#include <list>
+#include <map>
+
+class EventManager : public MtEventManager
+{
+ static void receiverReceive(MtEvent *e, void *data)
+ {
+ MtEventReceiver *r = (MtEventReceiver *)data;
+ r->onEvent(e);
+ }
+ struct FuncSpec
+ {
+ event_receive_func f;
+ void *d;
+ FuncSpec(event_receive_func f, void *d) : f(f), d(d) {}
+ };
+
+ struct Dest
+ {
+ std::list<FuncSpec> funcs{};
+ };
+ std::map<MtEvent::Type, Dest> m_dest{};
+
+public:
+ ~EventManager() override = default;
+
+ void put(MtEvent *e) override
+ {
+ std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(e->getType());
+ if (i != m_dest.end()) {
+ std::list<FuncSpec> &funcs = i->second.funcs;
+ for (FuncSpec &func : funcs) {
+ (*(func.f))(e, func.d);
+ }
+ }
+ delete e;
+ }
+ void reg(MtEvent::Type type, event_receive_func f, void *data) override
+ {
+ std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(type);
+ if (i != m_dest.end()) {
+ i->second.funcs.emplace_back(f, data);
+ } else {
+ Dest dest;
+ dest.funcs.emplace_back(f, data);
+ m_dest[type] = dest;
+ }
+ }
+ void dereg(MtEvent::Type type, event_receive_func f, void *data) override
+ {
+ std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(type);
+ if (i != m_dest.end()) {
+ std::list<FuncSpec> &funcs = i->second.funcs;
+ auto j = funcs.begin();
+ while (j != funcs.end()) {
+ bool remove = (j->f == f && (!data || j->d == data));
+ if (remove)
+ funcs.erase(j++);
+ else
+ ++j;
+ }
+ }
+ }
+};
get(m_map->getNodeNoEx(info.node_p));
// Determine fall damage multiplier
int addp = itemgroup_get(f.groups, "fall_damage_add_percent");
- pre_factor = 1.0 + (float)addp/100.0;
+ pre_factor = 1.0f + (float)addp / 100.0f;
}
float speed = pre_factor * speed_diff.getLength();
if (speed > tolerance && !player_immortal) {
u8 damage = (u8)MYMIN(damage_f + 0.5, 255);
if (damage != 0) {
damageLocalPlayer(damage, true);
- MtEvent *e = new SimpleTriggerEvent("PlayerFallingDamage");
- m_client->event()->put(e);
+ m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_FALLING_DAMAGE));
}
}
}
#pragma once
+#import "irrlichttypes.h"
+
class MtEvent
{
public:
- virtual ~MtEvent() = default;
- //virtual MtEvent* clone(){ return new IEvent; }
- virtual const char* getType() const = 0;
-
- MtEvent* checkIs(const std::string &type)
+ enum Type : u8
{
- if(type == getType())
- return this;
- return NULL;
- }
+ VIEW_BOBBING_STEP = 0,
+ CAMERA_PUNCH_LEFT,
+ CAMERA_PUNCH_RIGHT,
+ PLAYER_FALLING_DAMAGE,
+ PLAYER_DAMAGE,
+ NODE_DUG,
+ PLAYER_JUMP,
+ PLAYER_REGAIN_GROUND,
+ TYPE_MAX,
+ };
+
+ virtual ~MtEvent() = default;
+ virtual Type getType() const = 0;
};
// An event with no parameters and customizable name
-class SimpleTriggerEvent: public MtEvent
+class SimpleTriggerEvent : public MtEvent
{
- const char *type;
+ Type type;
+
public:
- SimpleTriggerEvent(const char *type):
- type(type)
- {}
- const char* getType() const
- {return type;}
+ SimpleTriggerEvent(Type type) : type(type) {}
+ Type getType() const override { return type; }
};
class MtEventReceiver
public:
virtual ~MtEventManager() = default;
virtual void put(MtEvent *e) = 0;
- virtual void reg(const char *type, event_receive_func f, void *data) = 0;
+ virtual void reg(MtEvent::Type type, event_receive_func f, void *data) = 0;
// If data==NULL, every occurence of f is deregistered.
- virtual void dereg(const char *type, event_receive_func f, void *data) = 0;
- virtual void reg(MtEventReceiver *r, const char *type) = 0;
- virtual void dereg(MtEventReceiver *r, const char *type) = 0;
+ virtual void dereg(MtEvent::Type type, event_receive_func f, void *data) = 0;
};
+++ /dev/null
-/*
-Minetest
-Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU Lesser General Public License as published by
-the Free Software Foundation; either version 2.1 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Lesser General Public License for more details.
-
-You should have received a copy of the GNU Lesser General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
-
-#pragma once
-
-#include "event.h"
-#include <list>
-#include <map>
-
-class EventManager: public MtEventManager
-{
- static void receiverReceive(MtEvent *e, void *data)
- {
- MtEventReceiver *r = (MtEventReceiver*)data;
- r->onEvent(e);
- }
- struct FuncSpec{
- event_receive_func f;
- void *d;
- FuncSpec(event_receive_func f, void *d):
- f(f), d(d)
- {}
- };
- struct Dest{
- std::list<FuncSpec> funcs;
- };
- std::map<std::string, Dest> m_dest;
-
-public:
- ~EventManager() = default;
-
- void put(MtEvent *e)
- {
- std::map<std::string, Dest>::iterator i = m_dest.find(e->getType());
- if(i != m_dest.end()){
- std::list<FuncSpec> &funcs = i->second.funcs;
- for (FuncSpec &func : funcs) {
- (*(func.f))(e, func.d);
- }
- }
- delete e;
- }
- void reg(const char *type, event_receive_func f, void *data)
- {
- std::map<std::string, Dest>::iterator i = m_dest.find(type);
- if(i != m_dest.end()){
- i->second.funcs.emplace_back(f, data);
- } else{
- std::list<FuncSpec> funcs;
- Dest dest;
- dest.funcs.emplace_back(f, data);
- m_dest[type] = dest;
- }
- }
- void dereg(const char *type, event_receive_func f, void *data)
- {
- if(type != NULL){
- std::map<std::string, Dest>::iterator i = m_dest.find(type);
- if(i != m_dest.end()){
- std::list<FuncSpec> &funcs = i->second.funcs;
- std::list<FuncSpec>::iterator j = funcs.begin();
- while(j != funcs.end()){
- bool remove = (j->f == f && (!data || j->d == data));
- if(remove)
- funcs.erase(j++);
- else
- ++j;
- }
- }
- } else{
- for (auto &dest : m_dest) {
- std::list<FuncSpec> &funcs = dest.second.funcs;
- std::list<FuncSpec>::iterator j = funcs.begin();
- while(j != funcs.end()){
- bool remove = (j->f == f && (!data || j->d == data));
- if(remove)
- funcs.erase(j++);
- else
- ++j;
- }
- }
- }
- }
- void reg(MtEventReceiver *r, const char *type)
- {
- reg(type, EventManager::receiverReceive, r);
- }
- void dereg(MtEventReceiver *r, const char *type)
- {
- dereg(type, EventManager::receiverReceive, r);
- }
-};
#include "clouds.h"
#include "config.h"
#include "content_cao.h"
-#include "event_manager.h"
+#include "client/event_manager.h"
#include "fontengine.h"
#include "itemdef.h"
#include "log.h"
p(p),
n(n)
{}
- const char *getType() const
+ MtEvent::Type getType() const
{
- return "NodeDug";
+ return MtEvent::NODE_DUG;
}
};
void registerReceiver(MtEventManager *mgr)
{
- mgr->reg("ViewBobbingStep", SoundMaker::viewBobbingStep, this);
- mgr->reg("PlayerRegainGround", SoundMaker::playerRegainGround, this);
- mgr->reg("PlayerJump", SoundMaker::playerJump, this);
- mgr->reg("CameraPunchLeft", SoundMaker::cameraPunchLeft, this);
- mgr->reg("CameraPunchRight", SoundMaker::cameraPunchRight, this);
- mgr->reg("NodeDug", SoundMaker::nodeDug, this);
- mgr->reg("PlayerDamage", SoundMaker::playerDamage, this);
- mgr->reg("PlayerFallingDamage", SoundMaker::playerFallingDamage, this);
+ mgr->reg(MtEvent::VIEW_BOBBING_STEP, SoundMaker::viewBobbingStep, this);
+ mgr->reg(MtEvent::PLAYER_REGAIN_GROUND, SoundMaker::playerRegainGround, this);
+ mgr->reg(MtEvent::PLAYER_JUMP, SoundMaker::playerJump, this);
+ mgr->reg(MtEvent::CAMERA_PUNCH_LEFT, SoundMaker::cameraPunchLeft, this);
+ mgr->reg(MtEvent::CAMERA_PUNCH_RIGHT, SoundMaker::cameraPunchRight, this);
+ mgr->reg(MtEvent::NODE_DUG, SoundMaker::nodeDug, this);
+ mgr->reg(MtEvent::PLAYER_DAMAGE, SoundMaker::playerDamage, this);
+ mgr->reg(MtEvent::PLAYER_FALLING_DAMAGE, SoundMaker::playerFallingDamage, this);
}
void step(float dtime)
}
runData.damage_flash += 95.0 + 3.2 * event->player_damage.amount;
- runData.damage_flash = MYMIN(runData.damage_flash, 127.0);
+ runData.damage_flash = MYMIN(runData.damage_flash, 127.0f);
LocalPlayer *player = client->getEnv().getLocalPlayer();
player->hurt_tilt_timer = 1.5;
player->hurt_tilt_strength =
- rangelim(event->player_damage.amount / 4, 1.0, 4.0);
+ rangelim(event->player_damage.amount / 4, 1.0f, 4.0f);
- client->event()->put(new SimpleTriggerEvent("PlayerDamage"));
+ client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_DAMAGE));
}
void Game::handleClientEvent_PlayerForceMove(ClientEvent *event, CameraOrientation *cam)
// Send event to trigger sound
- MtEvent *e = new NodeDugEvent(nodepos, wasnode);
- client->event()->put(e);
+ client->getEventManager()->put(new NodeDugEvent(nodepos, wasnode));
}
if (runData.dig_time_complete < 100000.0) {
class ICraftDefManager;
class ITextureSource;
class IShaderSource;
-class MtEventManager;
class IRollbackManager;
class EmergeManager;
class Camera;
// Used for keeping track of names/ids of unknown nodes
virtual u16 allocateUnknownNodeId(const std::string &name)=0;
- virtual MtEventManager* getEventManager()=0;
-
// Only usable on the server, and NOT thread-safe. It is usable from the
// environment thread.
virtual IRollbackManager* getRollbackManager() { return NULL; }
IItemDefManager *idef() { return getItemDefManager(); }
const NodeDefManager *ndef() { return getNodeDefManager(); }
ICraftDefManager *cdef() { return getCraftDefManager(); }
-
- MtEventManager *event() { return getEventManager(); }
IRollbackManager *rollback() { return getRollbackManager(); }
virtual const std::vector<ModSpec> &getMods() const = 0;
*/
if(!result.standing_on_object && !touching_ground_was && touching_ground) {
- MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
- m_client->event()->put(e);
+ m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
// Set camera impact value to be used for view bobbing
camera_impact = getSpeed().Y * -1;
if(speedJ.Y >= -0.5 * BS) {
speedJ.Y = movement_speed_jump * physics_override_jump;
setSpeed(speedJ);
-
- MtEvent *e = new SimpleTriggerEvent("PlayerJump");
- m_client->event()->put(e);
+ m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_JUMP));
}
}
else if(in_liquid)
}
if (!result.standing_on_object && !touching_ground_was && touching_ground) {
- MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
- m_client->event()->put(e);
+ m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
// Set camera impact value to be used for view bobbing
camera_impact = getSpeed().Y * -1;
}
#include "content_nodemeta.h"
#include "content_sao.h"
#include "mods.h"
-#include "event_manager.h"
#include "modchannels.h"
#include "serverlist.h"
#include "util/string.h"
m_itemdef(createItemDefManager()),
m_nodedef(createNodeDefManager()),
m_craftdef(createCraftDefManager()),
- m_event(new EventManager()),
m_uptime(0),
m_clients(m_con),
m_admin_chat(iface),
delete m_env;
delete m_rollback;
delete m_banmanager;
- delete m_event;
delete m_itemdef;
delete m_nodedef;
delete m_craftdef;
return m_nodedef->allocateDummy(name);
}
-MtEventManager *Server::getEventManager()
-{
- return m_event;
-}
-
IWritableItemDefManager *Server::getWritableItemDefManager()
{
return m_itemdef;
virtual const NodeDefManager* getNodeDefManager();
virtual ICraftDefManager* getCraftDefManager();
virtual u16 allocateUnknownNodeId(const std::string &name);
- virtual MtEventManager* getEventManager();
IRollbackManager *getRollbackManager() { return m_rollback; }
virtual EmergeManager *getEmergeManager() { return m_emerge; }
PARENT_SCOPE)
set (UNITTEST_CLIENT_SRCS
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_eventmanager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_gameui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp
PARENT_SCOPE)
ITextureSource *getTextureSource() { return m_texturesrc; }
IShaderSource *getShaderSource() { return m_shadersrc; }
ISoundManager *getSoundManager() { return m_soundmgr; }
- MtEventManager *getEventManager() { return m_eventmgr; }
scene::ISceneManager *getSceneManager() { return m_scenemgr; }
IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
EmergeManager *getEmergeManager() { return m_emergemgr; }
ITextureSource *m_texturesrc = nullptr;
IShaderSource *m_shadersrc = nullptr;
ISoundManager *m_soundmgr = nullptr;
- MtEventManager *m_eventmgr = nullptr;
scene::ISceneManager *m_scenemgr = nullptr;
IRollbackManager *m_rollbackmgr = nullptr;
EmergeManager *m_emergemgr = nullptr;
--- /dev/null
+/*
+Minetest
+Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include <unordered_map>
+#include "test.h"
+#include "client/event_manager.h"
+
+class TestEventManager : public TestBase
+{
+public:
+ TestEventManager() { TestManager::registerTestModule(this); }
+ const char *getName() override { return "TestEventManager"; }
+
+ void runTests(IGameDef *gamedef) override;
+
+ void testRegister();
+ void testDeregister();
+ void testRealEvent();
+ void testRealEventAfterDereg();
+};
+
+// EventManager test class
+class EventManagerTest : public EventManager
+{
+public:
+ static void eventTest(MtEvent *e, void *data)
+ {
+ UASSERT(e->getType() >= 0);
+ UASSERT(e->getType() < MtEvent::TYPE_MAX);
+ EventManagerTest *emt = (EventManagerTest *)data;
+ emt->m_test_value = e->getType();
+ }
+
+ u64 getTestValue() const { return m_test_value; }
+ void resetValue() { m_test_value = 0; }
+
+private:
+ u64 m_test_value = 0;
+};
+
+static TestEventManager g_test_instance;
+
+void TestEventManager::runTests(IGameDef *gamedef)
+{
+ TEST(testRegister);
+ TEST(testDeregister);
+ TEST(testRealEvent);
+ TEST(testRealEventAfterDereg);
+}
+
+void TestEventManager::testRegister()
+{
+ EventManager ev;
+ ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
+ ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
+}
+
+void TestEventManager::testDeregister()
+{
+ EventManager ev;
+ ev.dereg(MtEvent::NODE_DUG, nullptr, nullptr);
+ ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
+ ev.dereg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
+}
+
+void TestEventManager::testRealEvent()
+{
+ EventManager ev;
+ std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
+ ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
+
+ // Put event & verify event value
+ ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
+ UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
+}
+
+void TestEventManager::testRealEventAfterDereg()
+{
+ EventManager ev;
+ std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
+ ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
+
+ // Put event & verify event value
+ ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
+ UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
+
+ // Reset internal value
+ emt->resetValue();
+
+ // Remove the registered event
+ ev.dereg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
+
+ // Push the new event & ensure we target the default value
+ ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
+ UASSERT(emt->getTestValue() == 0);
+}
\ No newline at end of file
src/emerge.cpp
src/emerge.h
src/environment.cpp
-src/event.h
-src/event_manager.h
src/exceptions.h
src/face_position_cache.cpp
src/face_position_cache.h