-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
523 lines (453 loc) · 11.8 KB
/
api.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
--- Mob Bridge API
--
-- @topic api
local register
for _, lib in ipairs(mobb.libs) do
mobb.log("action", "checking for mob library " .. lib .. " ...")
if core.get_modpath(lib) then
local init_register, err = loadfile(mobb.modpath .. "/register/" .. lib .. ".lua")
-- file not existing just means we don't support that lib
if err and err:find("No such file or directory$") then
err = nil
end
if err then
error(err)
end
if init_register then
mobb.log("action", "using mob library " .. lib)
register = init_register()
break
end
end
end
if not register then
mobb.register = function(name)
local err_msg = "compatible mob library not available, cannot register"
if type(name) == "string" then
err_msg = err_msg .. " \"" .. name .. "\""
end
mobb.log("error", err_msg)
end
return false, "compatible mob library not found, install one of the following: "
.. table.concat(mobb.libs.default:split("."), ", ")
end
local check = {
format_prefix = function(self, prefix, append)
local formatted = ""
if prefix ~= "" then
formatted = prefix
end
if append then
if formatted ~= "" then
formatted = formatted .. "."
end
formatted = formatted .. append
end
if formatted ~= "" then
formatted = "(" .. formatted .. ") "
end
return formatted
end,
-- @return typematch, required, fields
type = function(self, base, subject)
local typematch
if base == nil then
return false, false
end
if subject == nil then
typematch = true -- set to `true` so doesn't fail if field is not required
else
local sub_type = type(subject)
if type(base) == "string" then
typematch = sub_type == base
else
local allowed_types = {}
if type(base[1]) == "string" then
allowed_types[base[1]] = true
elseif type(base[1]) == "table" then
for k, v in pairs(base[1]) do
if type(k) == "number" and type(v) == "string" then
allowed_types[v] = true
else
allowed_types[k] = v
end
end
end
typematch = allowed_types[sub_type] == true
end
end
return typematch, base.required == true
end,
-- @return typematch, err
fields = function(self, base, def, prefix)
prefix = prefix or ""
local err
if def == nil then
err = "definition required"
elseif type(def) ~= "table" then
err = "definition must be a table"
end
if err then
if prefix == "" then
err = "mob " .. err
end
return false, self:format_prefix(prefix) .. err
end
-- check for unknown fields
for field in pairs(def) do
if base[field] == nil then
return false, self:format_prefix(prefix) .. "unrecognized field: " .. field
end
end
for field, base_def in pairs(base) do
local value = def[field]
local typematch, required = self:type(base_def, value)
if required and value == nil then
return false, self:format_prefix(prefix) .. "missing required field: " .. field
end
if value ~= nil and not typematch then
return false, self:format_prefix(prefix, field) .. "field wrong type"
end
if type(def[field]) == "table" and type(base_def.fields) == "table" then
local next_field = field
if prefix ~= "" then
next_field = prefix .. "." .. next_field
end
if value == nil then
-- make sure parent field exists
value = {}
end
local ret, err = self:fields(base_def.fields, value, next_field)
if not ret then
return false, err
end
-- update definition
def[field] = value
end
if value ~= nil and type(base_def.check_value) == "function" then
local ret, err = base_def.check_value(value)
if not ret then
return false, err
end
end
-- check for default value
if type(base_def) == "table" then
if base_def.default ~= nil and base_def.default ~= "nil" then
if def[field] == nil then
def[field] = base_def.default
end
end
end
-- check for injections
if type(base_def) == "table" and type(base_def.inject) == "function" then
if def[field] == nil then
def[field] = {}
end
if type(def[field]) == "table" then
local new_keys = base_def.inject()
for k, v in pairs(new_keys) do
def[field][k] = v
end
else
mobb.log("warning", "cannot inject into non-table field \"" .. k .. "\"")
end
end
end
return true
end,
}
--- Physical definition.
--
-- @table PhysicalDef
-- @tfield table collisionbox
-- @tfield[opt] table selectionbox
-- @tfield[opt] number rotation
local physical_def = {
collisionbox = {
"table",
check_value = function(value)
if #value < 6 then
return false, "(physical.collisionbox) requires 6 values"
end
return true
end,
},
selectionbox = {
"table",
check_value = function(value)
if #value < 6 then
return false, "(physical.selectionbox) requires 6 values"
end
return true
end,
},
rotation = "number",
}
--- Visual definition.
--
-- @table VisualDef
-- @tfield[opt] string type
-- @tfield[opt] string mesh
-- @tfield string table
-- @tfield[opt] table size
local visual_def = {
type = "string",
mesh = "string",
textures = {
"table",
required=true,
check_value = function(value)
if #value < 1 then
return false, "(visual.textures) table cannot be empty"
end
return true
end,
},
size = {
"table",
fields={{x="number", required=true}, {y="number", required=true}},
},
}
--- Item drop definition.
--
-- @table DropDef
-- @tfield string name
-- @tfield[opt] int min (default: 1)
-- @tfield[opt] int max (default: 1)
-- @tfield[opt] number chance Probability of drop between 0.0 to 1.0 (default: 1)
local drop_def = {
name = {"string", required=true},
min = "number",
max = "number",
chance = {
"number",
check_value = function(value)
local ret, err = true
if value > 1 then
ret, err = false, "(drops.chance) must not be more than 1.0"
elseif value < 0 then
ret, err = false, "(drops.chance) must not be less than 0"
end
return ret, err
end,
},
}
--- Spawn definition.
--
-- @table SpawnDef
-- @tfield table nodes
-- @tfield[opt] int interval
-- @tfield[opt] int chance
-- @tfield[opt] table light_range
-- @tfield[opt] table height_range
-- @tfield[opt] int active_object_count
local spawn_def = {
nodes = "table",
interval = "number",
chance = "number",
light_range = {
"table",
fields = {min={"number", required=true}, max={"number", required=true}},
default = {min=0, max=14},
},
height_range = {
"table",
fields = {min={"number", required=true}, max={"number", required=true}},
default = {min=-31000, max=31000},
},
active_object_count = "number",
}
--- Combat definition.
--
-- @table CombatDef
-- @tfield[opt] int damage
-- @tfield[opt] int radius
local combat_def = {
damage = "number",
radius = "number",
}
--- Individual mode definition.
--
-- @table ModeDef
-- @tfield number chance Value ranging between 0.0-1.0.
local mode_def = {
chance = {
"number",
check_value = function(value)
if value > 1 then
return false, "(mode.chance) can not be greater than 1.0"
elseif value < 0 then
return false, "(mode.chance) can not be less than 0"
end
return true
end,
},
}
--- Modes definition.
--
-- @table ModesDef
-- @tfield ModeDef idle
-- @tfield ModeDef walk
local modes_def = {
idle = {"table"},
walk = {"table"},
}
--- Behavior definition.
--
-- @table BehaviorDef
-- @tfield bool hostile
-- @tfield[opt] bool knockback
-- @tfield[opt] CombatDef combat
-- @tfield table speed Fields can be "walk" & "run"
-- @tfield[opt] table search Fields: radius (number), target (string)
-- @tfield[opt] ModesDef modes List of mode definitions.
-- @tfield[opt] number step_height
-- @tfield[opt] number jump_height
-- @tfield[opt] bool sneaky (default: false)
local behavior_def = {
hostile = {"boolean", required=true},
knockback = {"boolean", default=true},
combat = {"table", fields=combat_def},
speed = {
"table",
fields = {walk="number", run="number"},
inject = function()
local _call = function(self, t)
local value
if t == "walk" or t == "run" then
local value = self[t]
if value == nil then
-- default speed for walk & run
value = 1
end
end
return value
end
return {["get"]=_call}
end,
},
search = {
"table",
fields = {radius="number", target="string"},
inject = function()
return {["get"]=function(self, t) return self[t] end}
end,
},
modes = {"table", fields=modes_def},
step_height = "number",
jump_height = "number",
follow = {{"string", "table"}},
sneaky = {"boolean", default=false},
}
--- Sounds definition.
--
-- @table SoundsDef
-- @tfield[opt] string random Sound played at random intervals.
-- @tfield[opt] string death Sound played when mob dies.
-- @tfield[opt] string war_cry Sound played while mob is attacking.
-- @tfield[opt] string attack Sound played when mob inflicts damage on target entity.
-- @tfield[opt] string damage Sound played when mob receives damage.
local sounds_def = {
random = "string",
death = "string",
war_cry = "string",
attack = "string",
damage = "string",
}
--- Animation definition.
--
-- @table AnimationDef
-- @tfield int start
-- @tfield int stop
-- @tfield[opt] int speed
-- @tfield[opt] bool loop
-- @tfield[opt] bool rotate
-- @tfield[opt] number duration
-- @usage
-- animation = {
-- idle = {start=0, stop=80, speed=15},
-- death = {start=81, stop=101, speed=28, loop=false, rotate=false, duration=2.12},
-- }
local animation_def = {}
for _, ani in ipairs({"idle", "walk", "run", "attack", "death"}) do
animation_def[ani] = {
"table",
--required = true, -- FIXME: not all animation types should be required
fields = {
start = {"number", required=true},
stop = {"number", required=true},
speed = "number",
loop = "boolean",
duration = "number",
},
}
if ani == "death" then
animation_def[ani].fields.rotate = {{"boolean", "number"}}
end
end
--- Mob definition.
--
-- @table MobDef
-- @tfield[opt] string nametag
-- @field hp Can be `int` or `table` ({min=<value>, max=<value>}).
-- @tfield PhysicalDef physical Physical definition.
-- @tfield VisualDef visual Visual definition.
-- @tfield[opt] table drops List of item drop definitions (`DropDef`).
-- @tfield[opt] SpawnDef spawn Spawning definition.
-- @tfield[opt] BehaviorDef behavior FIXME: should this be required?
-- @tfield[opt] SoundsDef sounds
-- @tfield[opt] AnimationDef animation
-- @tfield[opt] table mobs_fields See: [Mobs Redo API](https://notabug.org/TenPlus1/mobs_redo/src/master/api.txt)
local mob_def = {
nametag = "string",
hp = {
{"number", "table"},
required = true,
fields = {min={"number", required=true}, max={"number", required=true}},
},
physical = {"table", required=true, fields=physical_def},
visual = {"table", required=true, fields=visual_def},
drops = {
"table",
check_value = function(value)
local idx = 1
for _, d in ipairs(value) do
local ret, err = check:fields(drop_def, d, "drops["..idx.."]")
if not ret then
return false, err
end
idx = idx + 1
end
return true
end,
},
spawn = {"table", fields=spawn_def},
behavior = {
"table",
required=true,
fields=behavior_def,
},
sounds = {"table", fields=sounds_def},
animation = {"table", fields=animation_def},
mobs_fields = "table",
}
--- Registers a mob.
--
-- @tparam string name Mob technical name.
-- @tparam MobDef def Mob definition.
mobb.register = function(name, def)
if name == nil then
error("registration failed: mob name required")
elseif type(name) ~= "string" then
error("registration failed: mob name must be a string")
end
local ret, err = check:fields(mob_def, def)
if not ret then
error("\"" .. name .. "\" registration failed: " .. err)
end
def.behavior.combat = def.behavior.combat or {}
def.modes = def.modes or {}
def.mobs_fields = def.mobs_fields or {}
return register(name, def)
end
return true