This commit is contained in:
tacigar 2016-06-02 13:18:58 +09:00
parent 04ef881e7f
commit cc60e9ae17
8 changed files with 41 additions and 30 deletions

View File

@ -5,7 +5,7 @@
maidroid._aux = {}
-- maidroidのインベントリを得る
-- get inventory of the maidroid
function maidroid._aux.get_maidroid_inventory(self)
return minetest.get_inventory{
type = "detached",
@ -13,5 +13,3 @@ function maidroid._aux.get_maidroid_inventory(self)
}
end

16
api.lua
View File

@ -5,7 +5,7 @@
local _aux = maidroid._aux
-- インベントリにユニークな値を与えるための補助関数
-- aux function to generate serialnumber for inventories
local gen_inv_serialnumber = (function ()
local serialnumber = 0
return function ()
@ -19,10 +19,9 @@ local main_invname = "main"
local module_invsize = 1
local module_invname = "module"
-- モジュール名と定義を対応づけるテーブル
-- key = modulename, value = moduledef
maidroid.registered_modules = {}
-- 新しいmoduleを登録する関数
function maidroid.register_module(module_name, def)
maidroid.registered_modules[module_name] = def
minetest.register_craftitem(module_name, {
@ -32,7 +31,7 @@ function maidroid.register_module(module_name, def)
})
end
-- アニメーションの区切りフレーム
-- animation frame
maidroid.animations = {
stand = {x = 0, y = 79},
lay = {x = 162, y = 166},
@ -42,7 +41,6 @@ maidroid.animations = {
sit = {x = 81, y = 160},
}
-- 新しいmaidroidを登録する関数
function maidroid.register_maidroid(product_name, def)
minetest.register_entity(product_name, {
hp_max = def.hp_max or 1,
@ -57,6 +55,7 @@ function maidroid.register_maidroid(product_name, def)
makes_footstep_sound = true,
module = nil,
invname = "",
on_activate = function(self, staticdata)
self.invname = "maidroid"..tostring(gen_inv_serialnumber())
local inv = minetest.create_detached_inventory(self.invname, {
@ -107,11 +106,14 @@ function maidroid.register_maidroid(product_name, def)
if self.module then self.module.initialize(self)
else self.object:setvelocity{x = 0, y = 0, z = 0} end
end,
on_step = function(self, dtime)
if self.module then self.module.on_step(self, dtime) end
end,
on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
end,
on_rightclick = function(self, clicker)
local formspec = "size[8,9]"
.."list[detached:"..self.invname..";"..main_invname..";0,0;4,4;]"
@ -121,6 +123,7 @@ function maidroid.register_maidroid(product_name, def)
.."list[current_player;"..main_invname..";0,6.2;8,3;8]"
minetest.show_formspec(clicker:get_player_name(), self.invname, formspec)
end,
get_staticdata = function(self)
local inv = _aux.get_maidroid_inventory(self)
local staticdata = {}
@ -139,7 +142,8 @@ function maidroid.register_maidroid(product_name, def)
return minetest.serialize(staticdata)
end,
})
-- スポーンエッグを登録
-- register spawn egg
minetest.register_craftitem(product_name.."_spawn_egg", {
description = def.description.." Swapn Egg",
inventory_image = def.inventory_image,

View File

@ -3,10 +3,9 @@
-- https://github.com/tacigar/maidroid
------------------------------------------------------------
-- いい感じにモジュール化出来ないので名前空間に隠す
maidroid.modules._aux = {}
-- 向いている方向と速度ベクトルを変える
-- change direction and velocity vector
function maidroid.modules._aux.change_dir(self)
local rnd = function() return math.random(0, 5) * 2 - 5 end
local dir = {x = rnd(), y = 0, z = rnd()}
@ -15,12 +14,12 @@ function maidroid.modules._aux.change_dir(self)
self.object:setyaw(math.atan2(vel.z, vel.x) + math.pi / 2)
end
-- yawから向いている方向ベクトルを得る
-- get direction vector by yaw
function maidroid.modules._aux.get_forward(yaw)
return { x = math.sin(yaw), y = 0.0, z = -math.cos(yaw) }
end
-- 方向ベクトルを丸める
-- round direction vector
function maidroid.modules._aux.get_round_forward(forward)
local rforward = { x = 0, y = 0, z = 0}
if math.abs((forward.x / (math.abs(forward.x) + math.abs(forward.z)))) > 0.5 then
@ -34,17 +33,15 @@ function maidroid.modules._aux.get_round_forward(forward)
return rforward
end
-- 真下の位置を返すだけ
function maidroid.modules._aux.get_under_pos(vec)
return { x = vec.x, y = vec.y - 1, z = vec.z }
end
-- 真上の位置を返すだけ
function maidroid.modules._aux.get_upper_pos(vec)
return { x = vec.x, y = vec.y + 1, z = vec.z }
end
-- 落ちているアイテムを拾う
-- pickup droped items
function maidroid.modules._aux.pickup_item(self, radius, target_pred)
local pos = self.object:getpos()
local pred = target_pred or (function(itemstring) return true end)

View File

@ -7,22 +7,24 @@ local util = maidroid.util
local _aux = maidroid.modules._aux
local state = { idle = 0, chase = 1}
local view_of_range = 7 -- 発見可能距離
local stop_of_range = 2 -- 止まる距離
local view_of_range = 7
local stop_of_range = 2
-- プレーヤを追跡するだけのモジュール
maidroid.register_module("maidroid:chasing_player_module", {
description = "Maidroid Module : Chasing Player",
inventory_image = "maidroid_chasing_player_module.png",
initialize = function(self)
self.state = state.idle
self.object:setacceleration{x = 0, y = -10, z = 0}
self.object:setvelocity{x = 0, y = 0, z = 0}
end,
finalize = function(self)
self.state = nil
self.object:setvelocity{x = 0, y = 0, z = 0}
end,
on_step = function(self, dtime)
local pos = self.object:getpos()
local all_objects = minetest.get_objects_inside_radius(pos, view_of_range)
@ -53,6 +55,7 @@ maidroid.register_module("maidroid:chasing_player_module", {
end
local yaw = math.atan2(dir.z, dir.x) + math.pi/2
self.object:setyaw(yaw)
-- jump process
if vel.y == 0 and self.state == state.chase then
local rdir = vector.round(dir)

View File

@ -3,7 +3,6 @@
-- https://github.com/tacigar/maidroid
------------------------------------------------------------
-- 何もしないモジュール
maidroid.register_module("maidroid:empty_module", {
description = "Maidroid Module : Empty Module",
inventory_image = "maidroid_empty_module.png",

View File

@ -6,7 +6,7 @@
local _aux = maidroid.modules._aux
local state = {walk = 0, punch = 1, plant = 2}
-- 各植物ノードの種類の最大番号を探し出す
-- find max size of each plants
local target_plants_list = {}
minetest.after(0, function()
local max = {}
@ -24,7 +24,7 @@ end)
local max_punch_time = 20
local max_plant_time = 15
-- 種を持っているか否かを確認する
-- check the maidroid has seed items
local function has_seed_item(self)
local inv = maidroid._aux.get_maidroid_inventory(self)
local stacks = inv:get_list("main")
@ -37,10 +37,10 @@ local function has_seed_item(self)
return false
end
-- 農業を行うモジュール
maidroid.register_module("maidroid:farming_module", {
description = "Maidroid Module : Farming",
inventory_image = "maidroid_farming_module.png",
initialize = function(self)
self.object:set_animation(maidroid.animations.walk, 15, 0)
self.object:setacceleration{x = 0, y = -10, z = 0}
@ -49,12 +49,14 @@ maidroid.register_module("maidroid:farming_module", {
self.time_count = 0
_aux.change_dir(self)
end,
finalize = function(self)
self.state = nil
self.preposition = nil
self.time_count = nil
self.object:setvelocity{x = 0, y = 0, z = 0}
end,
on_step = function(self, dtime)
local pos = self.object:getpos()
local rpos = vector.round(pos)
@ -64,6 +66,7 @@ maidroid.register_module("maidroid:farming_module", {
local forward_pos = vector.add(rpos, forward_vec2)
local forward_node = minetest.get_node(forward_pos)
local forward_under_pos = vector.subtract(forward_pos, {x = 0, y = 1, z = 0})
if self.state == state.walk then -- searching plants or spaces
if maidroid.util.table_find_value(target_plants_list, forward_node.name) then
self.state = state.punch
@ -78,10 +81,11 @@ maidroid.register_module("maidroid:farming_module", {
self.object:set_animation(maidroid.animations.mine, 15, 0)
self.object:setvelocity{x = 0, y = 0, z = 0}
end
-- 種を広い集める
-- pickup droped seed items
_aux.pickup_item(self, 1.5, function(itemstring)
return minetest.get_item_group(itemstring, "seed") > 0
end)
elseif self.state == state.punch then
if self.time_count >= max_punch_time then
if maidroid.util.table_find_value(target_plants_list, forward_node.name) then
@ -100,6 +104,7 @@ maidroid.register_module("maidroid:farming_module", {
else
self.time_count = self.time_count + 1
end
elseif self.state == state.plant then
if self.time_count >= max_plant_time then
if forward_node.name == "air" and minetest.get_item_group(

View File

@ -12,7 +12,7 @@ local max_plant_time = 20
local target_tree_list = { "default:tree" }
local target_sapling_list = { "default:sapling" }
-- punchを始める必要があるか否かを調べる
-- check the maidroid need to start to punch
local function check_punch_flag(forward_pos)
local forward_upper_pos = util.table_shallow_copy(forward_pos)
while true do
@ -25,7 +25,7 @@ local function check_punch_flag(forward_pos)
return false, nil, nil
end
-- 苗木を持っているかを調べる
-- check the maidroid has sapling items
local function has_sapling_item(self)
local inv = maidroid._aux.get_maidroid_inventory(self)
local stacks = inv:get_list("main")
@ -38,10 +38,10 @@ local function has_sapling_item(self)
return false
end
-- 木こりモジュールを登録する
maidroid.register_module("maidroid:lumberjack", {
description = "Maidroid Module : Lumberjack",
inventory_image = "maidroid_lumberjack_module.png",
initialize = function(self)
self.state = state.walk
self.time_count = 0
@ -50,12 +50,14 @@ maidroid.register_module("maidroid:lumberjack", {
self.preposition = self.object:getpos()
_aux.change_dir(self)
end,
finalize = function(self)
self.state = nil
self.time_count = nil
self.preposition = nil
self.object:setvelocity{x = 0, y = 0, z = 0}
end,
on_step = function(self, dtime)
local pos = self.object:getpos()
local rpos = vector.round(pos)
@ -66,6 +68,7 @@ maidroid.register_module("maidroid:lumberjack", {
local forward_node = minetest.get_node(forward_pos)
local forward_under_pos = _aux.get_under_pos(forward_pos)
local forward_under_node = minetest.get_node(forward_under_pos)
if self.state == state.walk then
if check_punch_flag(forward_pos) then -- punch tree node
self.state = state.punch
@ -80,10 +83,11 @@ maidroid.register_module("maidroid:lumberjack", {
self.object:set_animation(maidroid.animations.mine, 15, 0)
self.object:setvelocity{x = 0, y = 0, z = 0}
end
-- 苗木を拾い集める
-- pickup sapling items
_aux.pickup_item(self, 1.5, function(itemstring)
return util.table_find_value(target_sapling_list, itemstring)
end)
elseif self.state == state.punch then
if self.time_count >= max_punch_time then
local punch_flag, forward_upper_pos, forward_upper_node
@ -107,6 +111,7 @@ maidroid.register_module("maidroid:lumberjack", {
else
self.time_count = self.time_count + 1
end
elseif self.state == state.plant then
if self.time_count > max_plant_time then
if forward_node.name == "air"

View File

@ -5,7 +5,7 @@
maidroid.util = {}
-- ある値を持つキーが存在するかを調べる
-- check that the table has the value
function maidroid.util.table_find_value(tbl, value)
for k, v in ipairs(tbl) do
if v == value then return true, k end
@ -13,7 +13,7 @@ function maidroid.util.table_find_value(tbl, value)
return false, nil
end
-- テーブルの浅いコピー
-- table shallow copy
function maidroid.util.table_shallow_copy(source)
local copy = {}
for key, value in pairs(source) do