maidroid/maidroid/api.lua

427 lines
13 KiB
Lua
Raw Normal View History

2016-09-13 03:59:18 +02:00
------------------------------------------------------------
2016-09-13 10:38:30 +02:00
-- Copyright (c) 2016 tacigar. All rights reserved.
2016-09-13 03:59:18 +02:00
-- https://github.com/tacigar/maidroid
------------------------------------------------------------
-- maidroid.animation_frames represents the animation frame data
-- of "models/maidroid.b3d".
maidroid.animation_frames = {
STAND = {x = 0, y = 79},
SIT = {x = 81, y = 160},
LAY = {x = 162, y = 166},
WALK = {x = 168, y = 187},
MINE = {x = 189, y = 198},
WALK_MINE = {x = 200, y = 219},
}
2016-09-13 05:44:26 +02:00
-- maidroid.registered_maidroids represents a table that contains
-- definitions of maidroid registered by maidroid.register_maidroid.
maidroid.registered_maidroids = {}
-- maidroid.registered_cores represents a table that contains
-- definitions of core registered by maidroid.register_core.
maidroid.registered_cores = {}
-- maidroid.is_core reports whether a item is a core item by the name.
function maidroid.is_core(item_name)
if maidroid.registered_cores[item_name] then
return true
end
return false
end
2016-09-13 09:09:44 +02:00
---------------------------------------------------------------------
-- maidroid.maidroid represents a table that contains common methods
-- for maidroid object.
2016-09-13 09:55:29 +02:00
-- this table must be contains by a metatable.__index of maidroid self tables.
-- minetest.register_entity set initial properties as a metatable.__index, so
-- this table's methods must be put there.
2016-09-13 09:09:44 +02:00
maidroid.maidroid = {}
-- maidroid.maidroid.get_inventory returns a inventory of a maidroid.
function maidroid.maidroid.get_inventory(self)
2016-09-13 03:59:18 +02:00
return minetest.get_inventory {
type = "detached",
name = self.inventory_name,
}
end
2016-09-13 09:09:44 +02:00
-- maidroid.maidroid.get_core_name returns a name of a maidroid's current core.
function maidroid.maidroid.get_core_name(self)
return self.core_name
2016-09-13 05:44:26 +02:00
end
2016-09-13 09:09:44 +02:00
-- maidroid.maidroid.get_core returns a maidroid's current core definition.
function maidroid.maidroid.get_core(self)
local name = self:get_core_name(self)
2016-09-13 05:44:26 +02:00
if name ~= "" then
return maidroid.registered_cores[name]
end
return nil
end
2016-09-13 12:02:53 +02:00
-- maidroid.maidroid.get_nearest_player returns a player object who
-- is the nearest to the maidroid.
function maidroid.maidroid.get_nearest_player(self, range_distance)
local player, min_distance = nil, range_distance
local position = self.object:getpos()
local all_objects = minetest.get_objects_inside_radius(position, range_distance)
for _, object in pairs(all_objects) do
if object:is_player() then
local player_position = object:getpos()
local distance = vector.distance(position, player_position)
if distance < min_distance then
2016-09-15 15:01:28 +02:00
min_distance = distance
2016-09-13 12:02:53 +02:00
player = object
end
end
end
return player
end
-- maidroid.maidroid.get_front_node returns a node that exists in front of the maidroid.
function maidroid.maidroid.get_front_node(self)
2016-09-13 15:33:02 +02:00
local direction = self:get_look_direction()
2016-09-13 15:51:47 +02:00
if math.abs(direction.x) >= 0.5 then
if direction.x > 0 then direction.x = 1 else direction.x = -1 end
else
direction.x = 0
end
2016-09-13 15:51:47 +02:00
if math.abs(direction.z) >= 0.5 then
if direction.z > 0 then direction.z = 1 else direction.z = -1 end
else
direction.z = 0
end
local front = vector.add(vector.round(self.object:getpos()), direction)
return minetest.get_node(front)
end
2016-09-13 15:33:02 +02:00
-- maidroid.maidroid.get_look_direction returns a normalized vector that is
-- the maidroid's looking direction.
function maidroid.maidroid.get_look_direction(self)
local yaw = self.object:getyaw()
return vector.normalize{x = math.sin(yaw), y = 0.0, z = -math.cos(yaw)}
end
-- maidroid.maidroid.set_animation sets the maidroid's animation.
-- this method is wrapper for self.object:set_animation.
function maidroid.maidroid.set_animation(self, frame)
self.object:set_animation(frame, 15, 0)
end
-- maidroid.maidroid.set_yaw_by_direction sets the maidroid's yaw
-- by a direction vector.
function maidroid.maidroid.set_yaw_by_direction(self, direction)
self.object:setyaw(math.atan2(direction.z, direction.x) + math.pi / 2)
end
2016-09-13 09:09:44 +02:00
---------------------------------------------------------------------
-- maidroid.manufacturing_data represents a table that contains manufacturing data.
-- this table's keys are product names, and values are manufacturing numbers
-- that has been already manufactured.
maidroid.manufacturing_data = (function()
local file_name = minetest.get_worldpath() .. "/manufacturing_data"
minetest.register_on_shutdown(function()
local file = io.open(file_name, "w")
file:write(minetest.serialize(maidroid.manufacturing_data))
file:close()
end)
2016-09-13 10:38:30 +02:00
local file = io.open(file_name, "r")
if file ~= nil then
local data = file:read("*a")
file:close()
return minetest.deserialize(data)
end
return {}
end) ()
---------------------------------------------------------------------
2016-09-17 10:29:31 +02:00
local formspec_opened_selves = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "maidroid:gui" then return end
local self = formspec_opened_selves[player]
if not self then return end
if fields.name then
if fields.name == "" then
self.nametag = self.inventory_name
else
self.nametag = fields.name
end
self.object:set_nametag_attributes{
text = self.nametag
}
end
end)
-- maidroid.register_core registers a definition of a new core.
function maidroid.register_core(core_name, def)
maidroid.registered_cores[core_name] = def
minetest.register_craftitem(core_name, {
stack_max = 1,
description = def.description,
inventory_image = def.inventory_image,
})
end
2016-09-13 03:59:18 +02:00
-- maidroid.register_maidroid registers a definition of a new maidroid.
function maidroid.register_maidroid(product_name, def)
-- initialize manufacturing number of a new maidroid.
2016-09-13 10:38:30 +02:00
if maidroid.manufacturing_data[product_name] == nil then
maidroid.manufacturing_data[product_name] = 0
end
2016-09-13 03:59:18 +02:00
2016-09-16 15:37:18 +02:00
local function update_infotext(self)
if self.core_name ~= "" then
local infotext = ""
if self.pause then
infotext = infotext .. "this maidroid is paused\n"
else
infotext = infotext .. "this maidroid is active\n"
end
infotext = infotext .. "[Core] : " .. self.core_name
self.object:set_properties{infotext = infotext}
return
end
self.object:set_properties{infotext = "this maidroid is inactive"}
end
2016-09-13 03:59:18 +02:00
-- create_inventory creates a new inventory, and returns it.
local function create_inventory(self)
2016-09-17 10:29:31 +02:00
self.inventory_name = self.product_name .. "_" .. tostring(self.manufacturing_number)
2016-09-13 03:59:18 +02:00
local inventory = minetest.create_detached_inventory(self.inventory_name, {
on_put = function(inv, listname, index, stack, player)
2016-09-13 05:44:26 +02:00
if listname == "core" then
local core_name = stack:get_name()
2016-09-13 15:11:23 +02:00
local core = maidroid.registered_cores[core_name]
2016-09-13 12:02:53 +02:00
core.on_start(self)
2016-09-13 05:44:26 +02:00
self.core_name = core_name
2016-09-16 15:37:18 +02:00
update_infotext(self)
2016-09-13 05:44:26 +02:00
end
2016-09-13 03:59:18 +02:00
end,
allow_put = function(inv, listname, index, stack, player)
2016-09-13 05:44:26 +02:00
-- only cores can put to a core inventory.
if listname == "main" then
return stack:get_count()
elseif listname == "core" and maidroid.is_core(stack:get_name()) then
return stack:get_count()
end
return 0
2016-09-13 03:59:18 +02:00
end,
on_take = function(inv, listname, index, stack, player)
2016-09-13 05:44:26 +02:00
if listname == "core" then
2016-09-13 15:11:23 +02:00
local core = maidroid.registered_cores[self.core_name]
2016-09-13 05:44:26 +02:00
self.core_name = ""
2016-09-13 12:02:53 +02:00
core.on_stop(self)
2016-09-16 15:37:18 +02:00
update_infotext(self)
2016-09-13 05:44:26 +02:00
end
2016-09-13 03:59:18 +02:00
end,
})
inventory:set_size("main", 16)
inventory:set_size("core", 1)
return inventory
end
-- create_formspec_string returns a string that represents a formspec definition.
local function create_formspec_string(self)
2016-09-17 10:29:31 +02:00
local nametag = self.object:get_nametag_attributes().text
2016-09-13 03:59:18 +02:00
return "size[8,9]"
2016-09-17 10:29:31 +02:00
.. default.gui_bg
.. default.gui_bg_img
.. default.gui_slots
2016-09-13 03:59:18 +02:00
.. "list[detached:"..self.inventory_name..";main;0,0;4,4;]"
2016-09-17 10:29:31 +02:00
.. "label[5.5,1;core]"
.. "list[detached:"..self.inventory_name..";core;5.5,1.5;1,1;]"
2016-09-13 03:59:18 +02:00
.. "list[current_player;main;0,5;8,1;]"
.. "list[current_player;main;0,6.2;8,3;8]"
2016-09-17 10:29:31 +02:00
.. "button[7,0.25;1,0.875;apply_name;Apply]"
.. "field[4.5,0.5;2.75,1;name;name;" .. nametag .. "]"
2016-09-13 03:59:18 +02:00
end
-- on_activate is a callback function that is called when the object is created or recreated.
local function on_activate(self, staticdata)
2016-09-13 09:55:29 +02:00
-- parse the staticdata, and compose a inventory.
2016-09-13 03:59:18 +02:00
if staticdata == "" then
self.product_name = product_name
self.manufacturing_number = maidroid.manufacturing_data[product_name]
maidroid.manufacturing_data[product_name] = maidroid.manufacturing_data[product_name] + 1
2016-09-13 03:59:18 +02:00
create_inventory(self)
2016-09-17 10:50:15 +02:00
self.nametag = self.inventory_name
2016-09-13 03:59:18 +02:00
else
-- if static data is not empty string, this object has beed already created.
local data = minetest.deserialize(staticdata)
self.product_name = data["product_name"]
self.manufacturing_number = data["manufacturing_number"]
2016-09-17 10:29:31 +02:00
self.nametag = data["nametag"]
2016-09-13 03:59:18 +02:00
local inventory = create_inventory(self)
2016-09-13 09:21:33 +02:00
local core_name = data["inventory"]["core"]
local items = data["inventory"]["main"]
if core_name ~= "" then -- set a core
core_stack = ItemStack(core_name)
core_stack:set_count(1)
inventory:add_item("core", core_stack)
self.core_name = core_name
2016-09-13 03:59:18 +02:00
end
2016-09-13 09:21:33 +02:00
for _, item in ipairs(items) do -- set items
local item_stack = ItemStack(item["name"])
item_stack:set_count(item["count"])
inventory:add_item("main", item_stack)
end
2016-09-13 03:59:18 +02:00
end
2016-09-16 15:37:18 +02:00
update_infotext(self)
2016-09-13 15:33:02 +02:00
2016-09-17 10:29:31 +02:00
self.object:set_nametag_attributes{
text = self.nametag
}
2016-09-13 15:33:02 +02:00
local core = self:get_core()
if core ~= nil then
core.on_start(self)
else
self.object:setvelocity{x = 0, y = 0, z = 0}
self.object:setacceleration{x = 0, y = -10, z = 0}
end
2016-09-13 03:59:18 +02:00
end
-- get_staticdata is a callback function that is called when the object is destroyed.
local function get_staticdata(self)
2016-09-13 09:55:29 +02:00
local inventory = self:get_inventory()
2016-09-13 03:59:18 +02:00
local data = {
["product_name"] = self.product_name,
["manufacturing_number"] = self.manufacturing_number,
2016-09-17 10:29:31 +02:00
["nametag"] = self.nametag,
2016-09-13 03:59:18 +02:00
["inventory"] = {
["main"] = {},
2016-09-13 05:44:26 +02:00
["core"] = self.core_name,
2016-09-13 03:59:18 +02:00
},
}
for _, item in ipairs(inventory:get_list("main")) do
local count = item:get_count()
local itemname = item:get_name()
if count ~= 0 then
local itemdata = {count = count, name = itemname}
table.insert(data["inventory"]["main"], itemdata)
end
end
return minetest.serialize(data)
end
2016-09-13 05:44:26 +02:00
-- on_step is a callback function that is called every delta times.
local function on_step(self, dtime)
2016-09-13 05:44:26 +02:00
if (not self.pause) and self.core_name ~= "" then
2016-09-13 15:11:23 +02:00
local core = maidroid.registered_cores[self.core_name]
2016-09-13 05:44:26 +02:00
core.on_step(self, dtime)
end
end
-- on_rightclick is a callback function that is called when a player right-click them.
local function on_rightclick(self, clicker)
2016-09-17 10:29:31 +02:00
formspec_opened_selves[clicker] = self
2016-09-13 05:44:26 +02:00
minetest.show_formspec(
clicker:get_player_name(),
2016-09-17 10:29:31 +02:00
"maidroid:gui",
create_formspec_string(self)
2016-09-13 05:44:26 +02:00
)
end
-- on_punch is a callback function that is called when a player punch then.
local function on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
2016-09-13 05:44:26 +02:00
if self.pause == true then
self.pause = false
if self.core_name ~= "" then
2016-09-13 15:11:23 +02:00
local core = maidroid.registered_cores[self.core_name]
2016-09-13 05:44:26 +02:00
core.on_pause(self)
end
else
self.pause = true
if self.core_name ~= "" then
2016-09-13 15:11:23 +02:00
local core = maidroid.registered_cores[self.core_name]
2016-09-13 05:44:26 +02:00
core.on_resume(self)
end
end
end
2016-09-13 03:59:18 +02:00
-- register a definition of a new maidroid.
minetest.register_entity(product_name, {
2016-09-13 05:44:26 +02:00
-- basic initial properties
hp_max = def.hp_max,
weight = def.weight,
mesh = def.mesh,
textures = def.textures,
physical = true,
visual = "mesh",
visual_size = {x = 10, y = 10},
collisionbox = {-0.25, -0.5, -0.25, 0.25, 1.05, 0.25},
is_visible = true,
makes_footstep_sound = true,
automatic_face_movement_dir = 90.0,
2016-09-16 15:37:18 +02:00
infotext = "",
2016-09-17 10:29:31 +02:00
nametag = "",
2016-09-13 05:44:26 +02:00
-- extra initial properties
pause = false,
product_name = "",
manufacturing_number = -1,
core_name = "",
2016-09-13 05:44:26 +02:00
-- callback methods.
on_activate = on_activate,
on_step = on_step,
on_rightclick = on_rightclick,
on_punch = on_punch,
get_staticdata = get_staticdata,
2016-09-13 09:55:29 +02:00
-- extra methods.
get_inventory = maidroid.maidroid.get_inventory,
get_core = maidroid.maidroid.get_core,
get_core_name = maidroid.maidroid.get_core_name,
get_nearest_player = maidroid.maidroid.get_nearest_player,
get_front_node = maidroid.maidroid.get_front_node,
get_look_direction = maidroid.maidroid.get_look_direction,
set_animation = maidroid.maidroid.set_animation,
set_yaw_by_direction = maidroid.maidroid.set_yaw_by_direction,
2016-09-13 03:59:18 +02:00
})
2016-09-13 09:09:44 +02:00
2016-09-13 10:38:30 +02:00
-- register a spawner for debugging maidroid mods.
2016-09-15 03:59:12 +02:00
minetest.register_craftitem(product_name .. "_spawner", {
description = product_name .. " spawner",
inventory_image = def.inventory_image,
stack_max = 1,
on_use = function(item_stack, user, pointed_thing)
if pointed_thing.above ~= nil then
minetest.add_entity(pointed_thing.above, product_name)
return itemstack
end
return nil
end,
})
2016-09-13 03:59:18 +02:00
end