]> git.lizzy.rs Git - dragonfireclient.git/blob - src/server/luaentity_sao.h
Handle lua entity HP changes correctly (like punches)
[dragonfireclient.git] / src / server / luaentity_sao.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013-2020 Minetest core developers & community
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #pragma once
22
23 #include "unit_sao.h"
24
25 class LuaEntitySAO : public UnitSAO
26 {
27 public:
28         LuaEntitySAO() = delete;
29         // Used by the environment to load SAO
30         LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &data);
31         // Used by the Lua API
32         LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &name,
33                         const std::string &state) :
34                         UnitSAO(env, pos),
35                         m_init_name(name), m_init_state(state)
36         {
37         }
38         ~LuaEntitySAO();
39
40         ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_LUAENTITY; }
41         ActiveObjectType getSendType() const { return ACTIVEOBJECT_TYPE_GENERIC; }
42         virtual void addedToEnvironment(u32 dtime_s);
43         void step(float dtime, bool send_recommended);
44         std::string getClientInitializationData(u16 protocol_version);
45
46         bool isStaticAllowed() const { return m_prop.static_save; }
47         bool shouldUnload() const { return true; }
48         void getStaticData(std::string *result) const;
49
50         u32 punch(v3f dir, const ToolCapabilities *toolcap = nullptr,
51                         ServerActiveObject *puncher = nullptr,
52                         float time_from_last_punch = 1000000.0f,
53                         u16 initial_wear = 0);
54
55         void rightClick(ServerActiveObject *clicker);
56
57         void setPos(const v3f &pos);
58         void moveTo(v3f pos, bool continuous);
59         float getMinimumSavedMovement();
60
61         std::string getDescription();
62
63         void setHP(s32 hp, const PlayerHPChangeReason &reason);
64         u16 getHP() const;
65
66         /* LuaEntitySAO-specific */
67         void setVelocity(v3f velocity);
68         void addVelocity(v3f velocity) { m_velocity += velocity; }
69         v3f getVelocity();
70         void setAcceleration(v3f acceleration);
71         v3f getAcceleration();
72
73         void setTextureMod(const std::string &mod);
74         std::string getTextureMod() const;
75         void setSprite(v2s16 p, int num_frames, float framelength,
76                         bool select_horiz_by_yawpitch);
77         std::string getName();
78         bool getCollisionBox(aabb3f *toset) const;
79         bool getSelectionBox(aabb3f *toset) const;
80         bool collideWithObjects() const;
81
82 protected:
83         void dispatchScriptDeactivate();
84         virtual void onMarkedForDeactivation() { dispatchScriptDeactivate(); }
85         virtual void onMarkedForRemoval() { dispatchScriptDeactivate(); }
86
87 private:
88         std::string getPropertyPacket();
89         void sendPosition(bool do_interpolate, bool is_movement_end);
90         std::string generateSetTextureModCommand() const;
91         static std::string generateSetSpriteCommand(v2s16 p, u16 num_frames,
92                         f32 framelength, bool select_horiz_by_yawpitch);
93
94         std::string m_init_name;
95         std::string m_init_state;
96         bool m_registered = false;
97
98         v3f m_velocity;
99         v3f m_acceleration;
100
101         v3f m_last_sent_position;
102         v3f m_last_sent_velocity;
103         v3f m_last_sent_rotation;
104         float m_last_sent_position_timer = 0.0f;
105         float m_last_sent_move_precision = 0.0f;
106         std::string m_current_texture_modifier = "";
107 };