From 6f7b74445c6ae19ac44493a89dd14482825d346c Mon Sep 17 00:00:00 2001 From: tacigar Date: Tue, 13 Sep 2016 17:49:35 +0900 Subject: [PATCH 1/7] [UPDATE] First commit --- maidroid_core_basic/depends.txt | 1 + maidroid_core_basic/init.lua | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 maidroid_core_basic/depends.txt create mode 100644 maidroid_core_basic/init.lua diff --git a/maidroid_core_basic/depends.txt b/maidroid_core_basic/depends.txt new file mode 100644 index 0000000..2c64b95 --- /dev/null +++ b/maidroid_core_basic/depends.txt @@ -0,0 +1 @@ +maidroid diff --git a/maidroid_core_basic/init.lua b/maidroid_core_basic/init.lua new file mode 100644 index 0000000..be168a4 --- /dev/null +++ b/maidroid_core_basic/init.lua @@ -0,0 +1,11 @@ +------------------------------------------------------------ +-- Copyright (c) 2016 tacigar. All rights reserved. +-- https://github.com/tacigar/maidroid +------------------------------------------------------------ + +maidroid_core_basic = {} + +maidroid_core_basic.modname = "maidroid_core_basic" +maidroid_core_basic.modpath = minetest.get_modpath(maidroid_core_basic.modname) + +dofile(maidroid.modpath .. "/register.lua") From 3210635e8880239b427a76654991cc5b726a8620 Mon Sep 17 00:00:00 2001 From: tacigar Date: Tue, 13 Sep 2016 19:02:53 +0900 Subject: [PATCH 2/7] [ADD] Add get_nearest_player method --- maidroid/api.lua | 25 +++++++++++++++++++++++-- maidroid_core_basic/register.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 maidroid_core_basic/register.lua diff --git a/maidroid/api.lua b/maidroid/api.lua index ef93199..9bf5d71 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -61,6 +61,26 @@ function maidroid.maidroid.get_core(self) return nil end +-- 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 + player = object + end + end + end + return player +end + --------------------------------------------------------------------- -- maidroid.manufacturing_data represents a table that contains manufacturing data. @@ -112,7 +132,7 @@ function maidroid.register_maidroid(product_name, def) if listname == "core" then local core_name = stack:get_name() local core = registered_cores[core_name] - core.initialize(self) + core.on_start(self) self.core_name = core_name end end, @@ -131,7 +151,7 @@ function maidroid.register_maidroid(product_name, def) if listname == "core" then local core = registered_cores[self.core_name] self.core_name = "" - core.finalize(self) + core.on_stop(self) end end, }) @@ -277,6 +297,7 @@ function maidroid.register_maidroid(product_name, def) 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, }) -- register a spawner for debugging maidroid mods. diff --git a/maidroid_core_basic/register.lua b/maidroid_core_basic/register.lua new file mode 100644 index 0000000..ff43d00 --- /dev/null +++ b/maidroid_core_basic/register.lua @@ -0,0 +1,26 @@ +------------------------------------------------------------ +-- Copyright (c) 2016 tacigar. All rights reserved. +-- https://github.com/tacigar/maidroid +------------------------------------------------------------ + +local function on_start(self) + +end + +local function on_stop(self) + +end + +local function on_step(self, dtime) + +end + +maidroid.register_core("maidroid_core_basic:core_basic", { + description = "maidroid core : basic", + inventory_image = "maidroid_core_basic.png", + on_start = on_start, + on_stop = on_stop, + on_resume = on_start, + on_pause = on_stop, + on_step = on_step, +}) From 55b0af6189d81eb7dc57602e312c9231c84d85a8 Mon Sep 17 00:00:00 2001 From: tacigar Date: Tue, 13 Sep 2016 20:03:15 +0900 Subject: [PATCH 3/7] [ADD] Add something, commit before debugging --- maidroid/api.lua | 37 +++++++++++++++++++++++++++ maidroid_core_basic/register.lua | 44 ++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index 9bf5d71..da89ac2 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -81,6 +81,40 @@ function maidroid.maidroid.get_nearest_player(self, range_distance) 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) + local direction = self.object:get_look_dir() + direction.y = 0 + direction = vector.normalize(direction) + + if direction.x >= 0.5 then + if direction.x > 0 then direction.x = 1 else direction.x = -1 end + else + direction.x = 0 + end + + if 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 + +-- 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 + --------------------------------------------------------------------- -- maidroid.manufacturing_data represents a table that contains manufacturing data. @@ -298,6 +332,9 @@ function maidroid.register_maidroid(product_name, def) 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, + set_animation = maidroid.maidroid.set_animation, + set_yaw_by_direction = maidroid.maidroid.set_yaw_by_direction, }) -- register a spawner for debugging maidroid mods. diff --git a/maidroid_core_basic/register.lua b/maidroid_core_basic/register.lua index ff43d00..21c1a58 100644 --- a/maidroid_core_basic/register.lua +++ b/maidroid_core_basic/register.lua @@ -3,16 +3,56 @@ -- https://github.com/tacigar/maidroid ------------------------------------------------------------ -local function on_start(self) +local state = { + IDLE = 0, + ACCOMPANY = 1, +} +local function on_start(self) + self.state = state.IDLE + self.object:setacceleration{x = 0, y = -10, z = 0} + self.object:setvelocity{x = 0, y = 0, z = 0} end local function on_stop(self) - + self.state = nil + self.object:setvelocty{x = 0, y = 0, z = 0} end local function on_step(self, dtime) + local player = self:get_nearest_player(10) + if player == nil then + self:set_animation(maidroid.animation_frames.STAND) + return + end + local position = self.object:getpos() + local player_position = player:getpos() + local direction = vector.subtract(player_position, position) + local velocity = self.object:getvelocity() + + if vector.length(dir) < 3 then + if self.state == state.ACCOMPANY then + self:set_animation(maidroid.animation_frames.STAND) + self.state = state.idle + self.object:setvelocity{x = 0, y = velocity.y, z = 0} + end + else + if self.state == state.IDLE then + self:set_animation(maidroid.animation_frames.WALK) + self.state = state.ACCOMPANY + end + self.object:setvelocity{x = direction.x, y = velocity.y, z = direction.z} -- TODO + end + self:set_yaw_by_direction(direction) + + -- if maidroid is stoped by obstacle, the maidroid must jump. + if velocity == 0 and self.state = state.ACCOMPANY then + local front_node = self:get_front_node() + if front_node.name ~= "air" then + self.object:setvelocity{x = direction.x, y = 3, z = direction.z} + end + end end maidroid.register_core("maidroid_core_basic:core_basic", { From b1e45725a36f972ab0c343aa7cf7fe7a375f9c7f Mon Sep 17 00:00:00 2001 From: tacigar Date: Tue, 13 Sep 2016 22:11:23 +0900 Subject: [PATCH 4/7] [UPDATE] Update something --- maidroid/api.lua | 13 ++++++++----- maidroid_core_basic/init.lua | 2 +- maidroid_core_basic/register.lua | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index da89ac2..e4b9570 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -3,6 +3,8 @@ -- https://github.com/tacigar/maidroid ------------------------------------------------------------ +maidroid.debug_mode = true + -- maidroid.animation_frames represents the animation frame data -- of "models/maidroid.b3d". maidroid.animation_frames = { @@ -165,7 +167,7 @@ function maidroid.register_maidroid(product_name, def) on_put = function(inv, listname, index, stack, player) if listname == "core" then local core_name = stack:get_name() - local core = registered_cores[core_name] + local core = maidroid.registered_cores[core_name] core.on_start(self) self.core_name = core_name end @@ -178,12 +180,13 @@ function maidroid.register_maidroid(product_name, def) elseif listname == "core" and maidroid.is_core(stack:get_name()) then return stack:get_count() end + print("KOKO", listname, maidroid.is_core(stack:get_name())) return 0 end, on_take = function(inv, listname, index, stack, player) if listname == "core" then - local core = registered_cores[self.core_name] + local core = maidroid.registered_cores[self.core_name] self.core_name = "" core.on_stop(self) end @@ -268,7 +271,7 @@ function maidroid.register_maidroid(product_name, def) -- on_step is a callback function that is called every delta times. function on_step(self, dtime) if (not self.pause) and self.core_name ~= "" then - local core = registered_cores[self.core_name] + local core = maidroid.registered_cores[self.core_name] core.on_step(self, dtime) end end @@ -288,13 +291,13 @@ function maidroid.register_maidroid(product_name, def) if self.pause == true then self.pause = false if self.core_name ~= "" then - local core = registered_cores[self.core_name] + local core = maidroid.registered_cores[self.core_name] core.on_pause(self) end else self.pause = true if self.core_name ~= "" then - local core = registered_cores[self.core_name] + local core = maidroid.registered_cores[self.core_name] core.on_resume(self) end end diff --git a/maidroid_core_basic/init.lua b/maidroid_core_basic/init.lua index be168a4..574c59d 100644 --- a/maidroid_core_basic/init.lua +++ b/maidroid_core_basic/init.lua @@ -8,4 +8,4 @@ maidroid_core_basic = {} maidroid_core_basic.modname = "maidroid_core_basic" maidroid_core_basic.modpath = minetest.get_modpath(maidroid_core_basic.modname) -dofile(maidroid.modpath .. "/register.lua") +dofile(maidroid_core_basic.modpath .. "/register.lua") diff --git a/maidroid_core_basic/register.lua b/maidroid_core_basic/register.lua index 21c1a58..f63b460 100644 --- a/maidroid_core_basic/register.lua +++ b/maidroid_core_basic/register.lua @@ -31,10 +31,10 @@ local function on_step(self, dtime) local direction = vector.subtract(player_position, position) local velocity = self.object:getvelocity() - if vector.length(dir) < 3 then + if vector.length(direction) < 3 then if self.state == state.ACCOMPANY then self:set_animation(maidroid.animation_frames.STAND) - self.state = state.idle + self.state = state.IDLE self.object:setvelocity{x = 0, y = velocity.y, z = 0} end else @@ -47,7 +47,7 @@ local function on_step(self, dtime) self:set_yaw_by_direction(direction) -- if maidroid is stoped by obstacle, the maidroid must jump. - if velocity == 0 and self.state = state.ACCOMPANY then + if velocity == 0 and self.state == state.ACCOMPANY then local front_node = self:get_front_node() if front_node.name ~= "air" then self.object:setvelocity{x = direction.x, y = 3, z = direction.z} From e2bf995a74f4ec44c3a1554fd8525b696b8c23c5 Mon Sep 17 00:00:00 2001 From: tacigar Date: Tue, 13 Sep 2016 22:33:02 +0900 Subject: [PATCH 5/7] [FIX] Fix bugs --- maidroid/api.lua | 23 +++++++++++++++++------ maidroid_core_basic/register.lua | 5 +++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index e4b9570..fa4f11b 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -85,9 +85,7 @@ end -- maidroid.maidroid.get_front_node returns a node that exists in front of the maidroid. function maidroid.maidroid.get_front_node(self) - local direction = self.object:get_look_dir() - direction.y = 0 - direction = vector.normalize(direction) + local direction = self:get_look_direction() if direction.x >= 0.5 then if direction.x > 0 then direction.x = 1 else direction.x = -1 end @@ -105,6 +103,13 @@ function maidroid.maidroid.get_front_node(self) return minetest.get_node(front) end +-- 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) @@ -180,7 +185,6 @@ function maidroid.register_maidroid(product_name, def) elseif listname == "core" and maidroid.is_core(stack:get_name()) then return stack:get_count() end - print("KOKO", listname, maidroid.is_core(stack:get_name())) return 0 end, @@ -214,7 +218,6 @@ function maidroid.register_maidroid(product_name, def) if staticdata == "" then self.product_name = product_name self.manufacturing_number = maidroid.manufacturing_data[product_name] - print(self.manufacturing_number, "KOKO") maidroid.manufacturing_data[product_name] = maidroid.manufacturing_data[product_name] + 1 create_inventory(self) else @@ -243,6 +246,14 @@ function maidroid.register_maidroid(product_name, def) end self.formspec_string = create_formspec_string(self) + + 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 end -- get_staticdata is a callback function that is called when the object is destroyed. @@ -287,7 +298,6 @@ function maidroid.register_maidroid(product_name, def) -- on_punch is a callback function that is called when a player punch then. function on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir) - print(self.product_name .. tostring(self.manufacturing_number)) if self.pause == true then self.pause = false if self.core_name ~= "" then @@ -336,6 +346,7 @@ function maidroid.register_maidroid(product_name, def) 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, }) diff --git a/maidroid_core_basic/register.lua b/maidroid_core_basic/register.lua index f63b460..62a1367 100644 --- a/maidroid_core_basic/register.lua +++ b/maidroid_core_basic/register.lua @@ -47,10 +47,11 @@ local function on_step(self, dtime) self:set_yaw_by_direction(direction) -- if maidroid is stoped by obstacle, the maidroid must jump. - if velocity == 0 and self.state == state.ACCOMPANY then + if velocity.y == 0 and self.state == state.ACCOMPANY then local front_node = self:get_front_node() + print(front_node.name) if front_node.name ~= "air" then - self.object:setvelocity{x = direction.x, y = 3, z = direction.z} + self.object:setvelocity{x = direction.x, y = 5, z = direction.z} end end end From 409001d7e3ce2407390a9cea548507d835e74937 Mon Sep 17 00:00:00 2001 From: tacigar Date: Tue, 13 Sep 2016 22:51:47 +0900 Subject: [PATCH 6/7] [UPDATE] Update maidroid collisionbox --- maidroid/api.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index fa4f11b..8c80d3c 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -86,14 +86,14 @@ end -- maidroid.maidroid.get_front_node returns a node that exists in front of the maidroid. function maidroid.maidroid.get_front_node(self) local direction = self:get_look_direction() - - if direction.x >= 0.5 then + print(direction.x, direction.z) + 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 - if direction.z >= 0.5 then + 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 @@ -324,6 +324,7 @@ function maidroid.register_maidroid(product_name, def) 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, From 6b5c57bfda816b94520dd17af51ee18ad703fc49 Mon Sep 17 00:00:00 2001 From: tacigar Date: Tue, 13 Sep 2016 22:54:17 +0900 Subject: [PATCH 7/7] [DELETE] Delete print debug --- maidroid/api.lua | 1 - maidroid_core_basic/register.lua | 1 - 2 files changed, 2 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index 8c80d3c..9813bd5 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -86,7 +86,6 @@ end -- maidroid.maidroid.get_front_node returns a node that exists in front of the maidroid. function maidroid.maidroid.get_front_node(self) local direction = self:get_look_direction() - print(direction.x, direction.z) if math.abs(direction.x) >= 0.5 then if direction.x > 0 then direction.x = 1 else direction.x = -1 end else diff --git a/maidroid_core_basic/register.lua b/maidroid_core_basic/register.lua index 62a1367..e48099a 100644 --- a/maidroid_core_basic/register.lua +++ b/maidroid_core_basic/register.lua @@ -49,7 +49,6 @@ local function on_step(self, dtime) -- if maidroid is stoped by obstacle, the maidroid must jump. if velocity.y == 0 and self.state == state.ACCOMPANY then local front_node = self:get_front_node() - print(front_node.name) if front_node.name ~= "air" then self.object:setvelocity{x = direction.x, y = 5, z = direction.z} end