-
Notifications
You must be signed in to change notification settings - Fork 6
/
entity.lua
216 lines (201 loc) · 7.1 KB
/
entity.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
mineunit("common/vector")
local ObjectRef = {}
function ObjectRef:set_velocity(value) self._velocity = vector.new(value) end
function ObjectRef:get_acceleration() return self._acceleration end
function ObjectRef:set_acceleration(value) self._acceleration = value end
function ObjectRef:get_yaw() return self._yaw end
function ObjectRef:set_yaw(value) self._yaw = value end
function ObjectRef:get_pitch() return self._pitch end
function ObjectRef:set_pitch(value) self._pitch = value end
function ObjectRef:get_roll() return self._roll end
function ObjectRef:set_roll(value) self._roll = value end
function ObjectRef:get_pos() return table.copy(self._pos) end
function ObjectRef:set_pos(value) self._pos = vector.new(value) end
function ObjectRef:get_velocity() return table.copy(self._velocity) end
function ObjectRef:add_velocity(vel)
-- * `vel` is a vector, e.g. `{x=0.0, y=2.3, z=1.0}`
-- * In comparison to using get_velocity, adding the velocity and then using
-- set_velocity, add_velocity is supposed to avoid synchronization problems.
-- Additionally, players also do not support set_velocity.
-- * If a player:
-- * Does not apply during free_move.
-- * Note that since the player speed is normalized at each move step,
-- increasing e.g. Y velocity beyond what would usually be achieved
-- (see: physics overrides) will cause existing X/Z velocity to be reduced.
-- * Example: `add_velocity({x=0, y=6.5, z=0})` is equivalent to
-- pressing the jump key (assuming default settings)
end
function ObjectRef:move_to(pos, continuous) -- continuous=false
-- * Does an interpolated move for Lua entities for visually smooth transitions.
-- * If `continuous` is true, the Lua entity will not be moved to the current
-- position before starting the interpolated move.
-- * For players this does the same as `set_pos`,`continuous` is ignored.
end
function ObjectRef:punch(puncher, time_from_last_punch, tool_capabilities, direction)
-- * `puncher` = another `ObjectRef`,
-- * `time_from_last_punch` = time since last punch action of the puncher
-- * `direction`: can be `nil`
end
function ObjectRef:right_click(clicker)
--; `clicker` is another `ObjectRef`
end
function ObjectRef:get_hp()
--: returns number of health points
end
function ObjectRef:set_hp(hp, reason)
--: set number of health points
-- * See reason in register_on_player_hpchange
-- * Is limited to the range of 0 ... 65535 (2^16 - 1)
-- * For players: HP are also limited by `hp_max` specified in the player's
-- object properties
end
function ObjectRef:get_inventory()
--: returns an `InvRef` for players, otherwise returns `nil`
end
function ObjectRef:get_wield_list()
--: returns the name of the inventory list the wielded item
-- is in.
end
function ObjectRef:get_wield_index()
--: returns the index of the wielded item
end
function ObjectRef:get_wielded_item()
--: returns an `ItemStack`
end
function ObjectRef:set_wielded_item(item)
--: replaces the wielded item, returns `true` if
-- successful.
end
function ObjectRef:set_armor_groups(t)
-- {group1=rating, group2=rating, ...}
end
function ObjectRef:get_armor_groups()
--: returns a table with the armor group ratings
end
function ObjectRef:set_animation(frame_range, frame_speed, frame_blend, frame_loop)
-- * `frame_range`: table {x=num, y=num}, default: `{x=1, y=1}`
-- * `frame_speed`: number, default: `15.0`
-- * `frame_blend`: number, default: `0.0`
-- * `frame_loop`: boolean, default: `true`
end
function ObjectRef:get_animation()
--: returns `range`, `frame_speed`, `frame_blend` and
-- `frame_loop`.
end
function ObjectRef:set_animation_frame_speed(frame_speed)
-- * `frame_speed`: number, default: `15.0`
end
function ObjectRef:set_attach(parent, bone, position, rotation, forced_visible)
--
-- * `bone`: string. Default is `""`, the root bone
-- * `position`: `{x=num, y=num, z=num}`, relative, default `{x=0, y=0, z=0}`
-- * `rotation`: `{x=num, y=num, z=num}` = Rotation on each axis, in degrees.
-- Default `{x=0, y=0, z=0}`
-- * `forced_visible`: Boolean to control whether the attached entity
-- should appear in first person. Default `false`.
-- * This command may fail silently (do nothing) when it would result
-- in circular attachments.
end
function ObjectRef:get_attach()
--: returns parent, bone, position, rotation, forced_visible,
-- or nil if it isn't attached.
end
function ObjectRef:get_children()
--: returns a list of ObjectRefs that are attached to the
-- object.
end
function ObjectRef:set_detach()
end
function ObjectRef:set_bone_position(bone, position, rotation)
-- * `bone`: string. Default is `""`, the root bone
-- * `position`: `{x=num, y=num, z=num}`, relative, `default {x=0, y=0, z=0}`
-- * `rotation`: `{x=num, y=num, z=num}`, default `{x=0, y=0, z=0}`
end
function ObjectRef:get_bone_position(bone)
--: returns position and rotation of the bone
end
function ObjectRef:set_properties(value) self._properties = value end
function ObjectRef:get_properties() return table.copy(self._properties) end
function ObjectRef:is_player() return true end -- FIXME! This is not actually player, add and test in Player class
function ObjectRef:get_nametag_attributes()
-- * returns a table with the attributes of the nametag of an object
-- * {
-- text = "",
-- color = {a=0..255, r=0..255, g=0..255, b=0..255},
-- bgcolor = {a=0..255, r=0..255, g=0..255, b=0..255},
-- }
end
function ObjectRef:set_nametag_attributes(attributes)
-- * sets the attributes of the nametag of an object
-- * `attributes`:
-- {
-- text = "My Nametag",
-- color = ColorSpec,
-- -- ^ Text color
-- bgcolor = ColorSpec or false,
-- -- ^ Sets background color of nametag
-- -- `false` will cause the background to be set automatically based on user settings
-- -- Default: false
-- }
end
mineunit.export_object(ObjectRef, {
name = "ObjectRef",
constructor = function(self)
local obj = {
_pos = {x=0, y=0, z=0},
_velocity = 0,
_acceleration = 0,
_yaw = 0,
_pitch = 0,
_roll = 0,
_properties = {},
}
setmetatable(obj, ObjectRef)
return obj
end,
})
local LuaEntity = {}
mineunit.export_object(LuaEntity, {
name = "LuaEntity",
constructor = function(self, data)
assert(type(data) == "table")
local obj = {}
obj.object = ObjectRef()
setmetatable(obj, data)
return obj
end,
})
local EntitySAO = {}
local last_unique_id = -1
local function unique_id()
last_unique_id = last_unique_id + 1
return last_unique_id
end
function EntitySAO:get_luaentity()
return self._luaentity
end
mineunit.export_object(EntitySAO, {
name = "EntitySAO",
constructor = function(self, name, staticdata)
assert(type(name) == "string")
assert(core.registered_entities[name])
local obj = {}
obj._luaentity = LuaEntity(core.registered_entities[name])
obj._name = name
obj._staticdata = staticdata
setmetatable(obj, EntitySAO)
return obj
end,
})
local entities = {}
function mineunit:get_entities()
return entities
end
function _G.core.add_entity(pos, name, staticdata)
local entity = EntitySAO(name, staticdata)
if not entities[name] then
entities[name] = {}
end
table.insert(entities[name], entity)
return entity
end