spacecannon/cannon.lua

201 lines
4.8 KiB
Lua
Raw Normal View History

2018-04-23 16:04:35 +02:00
2018-11-16 08:35:51 +01:00
local cable_entry = "^technic_cable_connection_overlay.png"
2018-11-16 08:05:42 +01:00
2018-11-15 14:15:10 +01:00
local register_spacecannon = function(def)
2018-04-24 09:37:53 +02:00
2018-11-15 14:15:10 +01:00
local entity_texture = "energycube_" .. def.color .. ".png"
2018-04-24 09:37:53 +02:00
2018-11-15 14:15:10 +01:00
minetest.register_entity("spacecannon:energycube_" .. def.color, {
2018-04-24 09:37:53 +02:00
initial_properties = {
visual = "cube",
visual_size = {x=0.25, y=0.25},
textures = {entity_texture,entity_texture,entity_texture,entity_texture,entity_texture,entity_texture},
collisionbox = {-0.25,-0.25,-0.25, 0.25,0.25,0.25},
2018-04-24 13:42:55 +02:00
physical = false
2018-04-24 09:37:53 +02:00
},
2018-04-24 13:42:55 +02:00
timer = 0,
2018-11-15 14:15:10 +01:00
static_save = false,
2018-04-24 09:37:53 +02:00
on_step = function(self, dtime)
2018-04-24 13:42:55 +02:00
self.timer = self.timer + dtime
2018-04-24 09:37:53 +02:00
local pos = self.object:getpos()
2018-04-24 13:42:55 +02:00
if self.timer > 0.5 then
-- add sparks along the way
minetest.add_particlespawner({
amount = 5,
time = 0.5,
minpos = pos,
maxpos = pos,
minvel = {x = -2, y = -2, z = -2},
maxvel = {x = 2, y = 2, z = 2},
minacc = {x = -3, y = -3, z = -3},
maxacc = {x = 3, y = 3, z = 3},
minexptime = 1,
maxexptime = 2.5,
minsize = 0.5,
maxsize = 0.75,
texture = "spacecannon_spark.png",
glow = 5
})
self.timer = 0
end
2018-04-24 09:37:53 +02:00
local node = minetest.get_node(pos)
2018-11-15 14:40:14 +01:00
if node.name == "air" or node.name == "vacuum:vacuum" then
2018-11-16 11:19:44 +01:00
local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 1)
2018-11-15 14:40:14 +01:00
local collided = false
2018-04-24 09:37:53 +02:00
for k, obj in pairs(objs) do
2018-11-15 14:40:14 +01:00
if obj:get_luaentity() ~= nil and obj:get_luaentity().name ~= self.name then
collided = true
obj:punch(self.object, 1.0, {
full_punch_interval=1.0,
damage_groups={fleshy=def.range*2},
}, nil)
2018-04-24 13:54:36 +02:00
end
2018-04-24 08:55:09 +02:00
end
2018-11-15 14:40:14 +01:00
if collided then
2019-07-29 13:13:46 +02:00
spacecannon.destroy(pos, def.range, def.intensity)
2018-11-15 14:40:14 +01:00
self.object:remove()
end
elseif node.name ~= "air" and node.name ~= "vacuum:vacuum" then
2018-04-24 09:37:53 +02:00
-- collision
2019-07-29 13:13:46 +02:00
spacecannon.destroy(pos, def.range, def.intensity)
2018-04-24 09:37:53 +02:00
self.object:remove()
2018-04-24 08:55:09 +02:00
end
2018-04-24 09:37:53 +02:00
end,
on_activate = function(self, staticdata)
2019-07-29 13:13:46 +02:00
minetest.after(def.timeout,
function(me)
me.object:remove()
2018-04-24 09:37:53 +02:00
end,
self)
2018-04-24 08:55:09 +02:00
end
2018-04-23 16:04:35 +02:00
2018-04-24 09:37:53 +02:00
})
2018-04-24 08:55:09 +02:00
2018-04-23 16:04:35 +02:00
2018-11-15 14:15:10 +01:00
minetest.register_node("spacecannon:cannon_" .. def.color, {
description = "Spacecannon (" .. def.desc .. ")",
2018-04-24 09:37:53 +02:00
-- top, bottom
2018-11-15 14:15:10 +01:00
tiles = {
2018-11-16 08:35:51 +01:00
"cannon_blank.png" .. cable_entry,
2018-11-15 14:15:10 +01:00
"cannon_front_" .. def.color .. ".png",
2018-11-16 08:35:51 +01:00
"cannon_blank.png" .. cable_entry,
"cannon_blank.png" .. cable_entry,
"cannon_blank.png" .. cable_entry,
"cannon_blank.png" .. cable_entry
2018-11-15 14:15:10 +01:00
},
2018-04-24 09:37:53 +02:00
groups = {cracky=3,oddly_breakable_by_hand=3,technic_machine = 1, technic_hv = 1},
2018-11-15 14:15:10 +01:00
drop = "spacecannon:cannon_" .. def.color,
2018-04-24 09:37:53 +02:00
sounds = default.node_sound_glass_defaults(),
paramtype2 = "facedir",
2018-11-15 14:15:10 +01:00
legacy_facedir_simple = true,
2018-04-23 16:04:35 +02:00
2018-04-24 09:37:53 +02:00
mesecons = {effector = {
action_on = function (pos, node)
2018-11-15 14:15:10 +01:00
spacecannon.fire(pos, def.color, def.speed, def.range)
2018-04-24 09:37:53 +02:00
end
}},
2018-04-23 16:04:35 +02:00
2018-04-24 09:37:53 +02:00
connects_to = {"group:technic_hv_cable"},
connect_sides = {"bottom", "top", "left", "right", "front", "back"},
2018-04-23 16:04:35 +02:00
2018-04-24 09:37:53 +02:00
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_int("powerstorage", 0)
2018-11-15 14:40:14 +01:00
meta:set_int("HV_EU_input", 0)
meta:set_int("HV_EU_demand", 0)
2018-04-24 11:24:41 +02:00
spacecannon.update_formspec(meta)
2018-04-24 09:37:53 +02:00
end,
technic_run = function(pos, node)
local meta = minetest.get_meta(pos)
local eu_input = meta:get_int("HV_EU_input")
local demand = meta:get_int("HV_EU_demand")
local store = meta:get_int("powerstorage")
meta:set_string("infotext", "Power: " .. eu_input .. "/" .. demand .. " Store: " .. store)
2018-11-15 14:15:10 +01:00
if store < spacecannon.config.powerstorage * def.range then
2018-04-24 09:37:53 +02:00
-- charge
meta:set_int("HV_EU_demand", spacecannon.config.powerrequirement)
store = store + eu_input
meta:set_int("powerstorage", store)
else
-- charged
meta:set_int("HV_EU_demand", 0)
end
2018-04-24 11:24:41 +02:00
end,
on_receive_fields = function(pos, formname, fields, sender)
if fields.fire then
2018-11-15 14:15:10 +01:00
spacecannon.fire(pos, def.color, def.speed, def.range)
2018-04-24 11:24:41 +02:00
end
2018-04-23 16:04:35 +02:00
end
2018-04-24 11:24:41 +02:00
2018-04-24 09:37:53 +02:00
})
2018-11-15 14:40:14 +01:00
technic.register_machine("HV", "spacecannon:cannon_" .. def.color, technic.receiver)
2018-04-23 16:04:35 +02:00
2018-04-24 09:37:53 +02:00
minetest.register_craft({
2018-11-15 14:15:10 +01:00
output = 'spacecannon:cannon_' .. def.color,
2018-04-24 09:37:53 +02:00
recipe = {
2018-11-15 14:15:10 +01:00
{'', 'default:steelblock', ''},
{ def.ingredient, def.ingredient, def.ingredient},
{'', 'default:steelblock', ''}
2018-04-24 09:37:53 +02:00
}
})
2018-04-23 16:04:35 +02:00
end
2018-11-15 14:15:10 +01:00
register_spacecannon({
color = "green",
range = 1,
2019-07-29 13:13:46 +02:00
intensity = 1,
2018-11-15 14:15:10 +01:00
timeout = 8,
speed = 10,
desc = "fast,low damage",
ingredient = "default:mese_block"
})
register_spacecannon({
color = "yellow",
range = 3,
2019-07-29 13:13:46 +02:00
intensity = 2,
2018-11-15 14:15:10 +01:00
timeout = 8,
speed = 5,
desc = "medium speed, medium damage",
ingredient = "spacecannon:cannon_green"
})
register_spacecannon({
color = "red",
range = 5,
2019-07-29 13:13:46 +02:00
intensity = 4,
2018-11-15 14:15:10 +01:00
timeout = 15,
speed = 3,
desc = "slow, heavy damage",
ingredient = "spacecannon:cannon_yellow"
})
2018-04-24 09:37:53 +02:00
2018-04-23 16:04:35 +02:00