1
0
mirror of https://github.com/tacigar/maidroid.git synced 2024-11-09 11:40:28 +01:00
maidroid/modules/farming_module.lua

136 lines
4.5 KiB
Lua
Raw Normal View History

2016-05-31 06:26:54 +02:00
------------------------------------------------------------
-- Copyright (c) 2016 tacigar
-- https://github.com/tacigar/maidroid
------------------------------------------------------------
local _aux = maidroid.modules._aux
local state = {walk = 0, punch = 1, plant = 2}
2016-06-02 06:18:58 +02:00
-- find max size of each plants
2016-05-31 06:26:54 +02:00
local target_plants_list = {}
minetest.after(0, function()
local max = {}
for name, node in pairs(minetest.registered_nodes) do
if minetest.get_item_group(name, "plant") > 0 then
local s, i = string.match(name, "(.+)_(%d+)")
if max[s] == nil or max[s] < i then max[s] = i end
end
end
for s, i in pairs(max) do
table.insert(target_plants_list, s.."_"..i)
end
end)
local max_punch_time = 20
local max_plant_time = 15
2016-06-02 06:18:58 +02:00
-- check the maidroid has seed items
2016-05-31 06:26:54 +02:00
local function has_seed_item(self)
local inv = maidroid._aux.get_maidroid_inventory(self)
local stacks = inv:get_list("main")
for _, stack in ipairs(stacks) do
local item_name = stack:get_name()
if minetest.get_item_group(item_name, "seed") > 0 then
return true
end
end
return false
end
maidroid.register_module("maidroid:farming_module", {
description = "Maidroid Module : Farming",
inventory_image = "maidroid_farming_module.png",
2016-06-02 06:18:58 +02:00
2016-05-31 06:26:54 +02:00
initialize = function(self)
self.object:set_animation(maidroid.animations.walk, 15, 0)
self.object:setacceleration{x = 0, y = -10, z = 0}
self.state = state.walk
self.preposition = self.object:getpos()
self.time_count = 0
_aux.change_dir(self)
end,
2016-06-02 06:18:58 +02:00
2016-05-31 06:26:54 +02:00
finalize = function(self)
self.state = nil
self.preposition = nil
self.time_count = nil
self.object:setvelocity{x = 0, y = 0, z = 0}
end,
2016-06-02 06:18:58 +02:00
2016-05-31 06:26:54 +02:00
on_step = function(self, dtime)
local pos = self.object:getpos()
local rpos = vector.round(pos)
local yaw = self.object:getyaw()
local forward_vec = _aux.get_forward(yaw)
local forward_vec2 = _aux.get_round_forward(forward_vec)
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})
2016-06-02 06:18:58 +02:00
2016-05-31 06:26:54 +02:00
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
self.object:set_animation(maidroid.animations.mine, 15, 0)
self.object:setvelocity{x = 0, y = 0, z = 0}
elseif pos.x == self.preposition.x or pos.z == self.preposition.z then
_aux.change_dir(self)
elseif forward_node.name == "air"
2016-06-01 13:51:15 +02:00
and minetest.get_item_group(minetest.get_node(forward_under_pos).name, "wet") > 0
2016-05-31 06:26:54 +02:00
and has_seed_item(self) then
self.state = state.plant
self.object:set_animation(maidroid.animations.mine, 15, 0)
self.object:setvelocity{x = 0, y = 0, z = 0}
end
2016-06-02 06:18:58 +02:00
-- pickup droped seed items
2016-06-01 04:15:14 +02:00
_aux.pickup_item(self, 1.5, function(itemstring)
return minetest.get_item_group(itemstring, "seed") > 0
end)
2016-06-02 06:18:58 +02:00
2016-05-31 06:26:54 +02:00
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
2016-06-01 13:51:15 +02:00
minetest.remove_node(forward_pos)
2016-05-31 06:26:54 +02:00
local inv = minetest.get_inventory{type = "detached", name = self.invname}
local stacks = minetest.get_node_drops(forward_node.name)
for _, stack in ipairs(stacks) do
local leftover = inv:add_item("main", stack)
minetest.add_item(forward_pos, leftover)
end
end
self.state = state.walk
self.object:set_animation(maidroid.animations.walk, 15, 0)
self.time_count = 0
_aux.change_dir(self)
else
self.time_count = self.time_count + 1
end
2016-06-02 06:18:58 +02:00
2016-05-31 06:26:54 +02:00
elseif self.state == state.plant then
if self.time_count >= max_plant_time then
if forward_node.name == "air" and minetest.get_item_group(
minetest.get_node(forward_under_pos).name, "soil") > 0 then
local inv = minetest.get_inventory{type = "detached", name = self.invname}
local stacks = inv:get_list("main")
for idx, stack in ipairs(stacks) do
local item_name = stack:get_name()
if minetest.get_item_group(item_name, "seed") > 0 then
minetest.add_node(forward_pos, {name = item_name, param2 = 1})
stack:take_item(1)
inv:set_stack("main", idx, stack)
break
end
end
end
self.state = state.walk
self.object:set_animation(maidroid.animations.walk, 15, 0)
self.time_count = 0
_aux.change_dir(self)
else
self.time_count = self.time_count + 1
end
end
self.preposition = pos
return
end
})