Refactor out a drop_magazine function, lots of variable name tweaking
parent
3f7df33cc1
commit
392a655433
|
@ -1,7 +1,7 @@
|
|||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
|
||||
minetest.register_node("rangedweapons:antigun_block", {
|
||||
description = core.colorize(rangedweapons.colors.title_color, "Anti-gun block\n") .. core.colorize(rangedweapons.colors.white, "Prevents people from using guns, in 10 node radius to each side from this block"),
|
||||
description = core.colorize(ranged_weapons.colors.title_color, "Anti-gun block\n") .. core.colorize(ranged_weapons.colors.white, "Prevents people from using guns, in 10 node radius to each side from this block"),
|
||||
tiles = {"rangedweapons_antigun_block.png"},
|
||||
groups = {choppy = 3, oddly_breakable_by_hand = 3},
|
||||
sounds = default.node_sound_wood_defaults(),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
rangedweapons.colors = {
|
||||
ranged_weapons.colors = {
|
||||
white = "#ffffff",
|
||||
turquoise = "35cdff"
|
||||
}
|
||||
|
||||
ranged_weapons.color.title_color = color.turquoise
|
||||
ranged_weapons.colors.title_color = ranged_weapons.colors.turquoise
|
||||
|
|
|
@ -65,10 +65,6 @@ end end
|
|||
|
||||
end
|
||||
|
||||
|
||||
|
||||
--minetest.chat_send_all(u_meta:get_float("rw_cooldown"))
|
||||
|
||||
if u_meta:get_float("rw_cooldown") <= 0 then
|
||||
if player:get_wielded_item():get_definition().loaded_gun ~= nil then
|
||||
local itemstack = player:get_wielded_item()
|
||||
|
@ -86,7 +82,7 @@ if player:get_wielded_item():get_definition().rw_next_reload ~= nil then
|
|||
minetest.sound_play(itemstack:get_definition().load_sound, {player})
|
||||
end
|
||||
gunMeta = itemstack:get_meta()
|
||||
u_meta:set_float("rw_cooldown",gunMeta:get_float("RW_reload_delay"))
|
||||
u_meta:set_float("rw_cooldown",gunMeta:get_float("rw_reload_delay"))
|
||||
itemstack:set_name(player:get_wielded_item():get_definition().rw_next_reload)
|
||||
player:set_wielded_item(itemstack)
|
||||
end
|
||||
|
|
121
init.lua
121
init.lua
|
@ -1,7 +1,11 @@
|
|||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
|
||||
ranged_weapons = {}
|
||||
ranged_weapons.mod_name = "rangedweapons"
|
||||
|
||||
local weapon = dofile(modpath .. "/weapon.lua")
|
||||
|
||||
dofile(modpath .. "/colors.lua")
|
||||
dofile(modpath .. "/settings.lua")
|
||||
dofile(modpath .. "/cooldown_stuff.lua")
|
||||
dofile(modpath .. "/skills.lua")
|
||||
|
@ -13,8 +17,6 @@ dofile(modpath .. "/armor.lua")
|
|||
|
||||
dofile(modpath .. "/blocks/antigun_block.lua")
|
||||
|
||||
rangedweapons = {}
|
||||
rangedweapons.mod_name = "rangedweapons"
|
||||
|
||||
-- Load all weapons
|
||||
for k, v in pairs(weapon.weapons) do
|
||||
|
@ -61,47 +63,39 @@ end
|
|||
rangedweapons_reload_gun = function(itemstack, player)
|
||||
local gun_data = itemstack:get_definition().rw_gun_data
|
||||
|
||||
if gun_data ~= nil then
|
||||
gun_unload_sound = gun_data.gun_unload_sound or ""
|
||||
end
|
||||
|
||||
minetest.sound_play(gun_unload_sound, {player})
|
||||
minetest.sound_play(gun_data.unload_sound, {player})
|
||||
|
||||
local playerMeta = player:get_meta()
|
||||
local gunMeta = itemstack:get_meta()
|
||||
|
||||
gunMeta:set_float("RW_reload_delay", gun_data["reload_delay"])
|
||||
gunMeta:set_float("rw_reload_delay", gun_data.reload_delay)
|
||||
|
||||
playerMeta:set_float("rw_cooldown", gun_data["reload_delay"])
|
||||
playerMeta:set_float("rw_cooldown", gun_data.reload_delay)
|
||||
|
||||
local player_has_ammo = 0
|
||||
local clipSize = 0
|
||||
local reload_ammo = ""
|
||||
|
||||
-- If we have the right type of ammo in our inventory?
|
||||
if gun_data.suitable_ammo ~= nil then
|
||||
-- Get the players inventory and iterate over it
|
||||
local inv = player:get_inventory()
|
||||
for i = 1, inv:get_size("main") do
|
||||
-- For each type of suitable ammo in the inventory,
|
||||
-- if the ammo is the kind we're looking for
|
||||
for _, ammo in pairs(gun_data.suitable_ammo) do
|
||||
if inv:get_stack("main", i):get_name() == ammo[1] then
|
||||
-- Then mark that stack of ammo as what we will use
|
||||
-- to reload
|
||||
reload_ammo = inv:get_stack("main", i)
|
||||
-- And set the clipsize??
|
||||
clipSize = ammo[2]
|
||||
|
||||
player_has_ammo = 1
|
||||
break
|
||||
end
|
||||
end
|
||||
local inv = player:get_inventory()
|
||||
|
||||
if player_has_ammo == 1 then
|
||||
-- TODO: break out into separate function.
|
||||
-- Iterate over player inventory searching for ammo, and mark it
|
||||
-- for use during reload.
|
||||
for i = 1, inv:get_size("main") do
|
||||
for _, ammo in pairs(gun_data.suitable_ammo) do
|
||||
if inv:get_stack("main", i):get_name() == ammo[1] then
|
||||
reload_ammo = inv:get_stack("main", i)
|
||||
clipSize = ammo[2]
|
||||
|
||||
player_has_ammo = 1
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if player_has_ammo == 1 then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if player_has_ammo == 1 then
|
||||
|
@ -111,43 +105,29 @@ rangedweapons_reload_gun = function(itemstack, player)
|
|||
ammo_icon = reload_ammo:get_definition().inventory_image
|
||||
end
|
||||
|
||||
player:hud_change(gunimg, "text", gun_data["texture"]["icon"])
|
||||
player:hud_change(gunimg, "text", gun_data.texture.icon)
|
||||
player:hud_change(ammoimg, "text", ammo_icon)
|
||||
|
||||
local gunMeta = itemstack:get_meta()
|
||||
local ammoCount = gunMeta:get_int("RW_bullets")
|
||||
local ammoName = gunMeta:get_string("RW_ammo_name")
|
||||
local ammoCount = gunMeta:get_int("rw_bullets")
|
||||
local ammoName = gunMeta:get_string("rw_ammo_name")
|
||||
local inv = player:get_inventory()
|
||||
|
||||
inv:add_item("main", ammoName .. " " .. ammoCount)
|
||||
|
||||
if inv:contains_item("main", reload_ammo:get_name() .. " " .. clipSize) then
|
||||
inv:remove_item("main", reload_ammo:get_name() .. " " .. clipSize)
|
||||
gunMeta:set_int("RW_bullets", clipSize)
|
||||
gunMeta:set_int("rw_bullets", clipSize)
|
||||
else
|
||||
gunMeta:set_int("RW_bullets", reload_ammo:get_count())
|
||||
gunMeta:set_int("rw_bullets", reload_ammo:get_count())
|
||||
inv:remove_item("main", reload_ammo:get_name() .. " " .. reload_ammo:get_count())
|
||||
end
|
||||
|
||||
gunMeta:set_string("RW_ammo_name", reload_ammo:get_name())
|
||||
gunMeta:set_string("rw_ammo_name", reload_ammo:get_name())
|
||||
|
||||
player:hud_change(gunammo, "text", gunMeta:get_int("RW_bullets"))
|
||||
player:hud_change(gunammo, "text", gunMeta:get_int("rw_bullets"))
|
||||
|
||||
if gun_data.gun_magazine ~= nil then
|
||||
local pos = player:get_pos()
|
||||
local dir = player:get_look_dir()
|
||||
local yaw = player:get_look_horizontal()
|
||||
if pos and dir and yaw then
|
||||
pos.y = pos.y + 1.4
|
||||
local obj = minetest.add_entity(pos, "rangedweapons:mag")
|
||||
if obj then
|
||||
obj:set_properties({textures = {gun_data.gun_magazine}})
|
||||
obj:set_velocity({x = dir.x * 2, y = dir.y * 2, z = dir.z * 2})
|
||||
obj:set_acceleration({x = 0, y = -5, z = 0})
|
||||
obj:set_rotation({x = 0, y = yaw + math.pi, z = 0})
|
||||
end
|
||||
end
|
||||
end
|
||||
weapon.drop_magazine(player, gun_data)
|
||||
|
||||
if gun_data.gun_unloaded ~= nil then
|
||||
itemstack:set_name(gun_data.gun_unloaded)
|
||||
|
@ -157,13 +137,12 @@ end
|
|||
|
||||
rangedweapons_single_load_gun = function(itemstack, player)
|
||||
local gun_data = itemstack:get_definition().rw_gun_data
|
||||
|
||||
local reload_delay = gun_data["reload_delay"]
|
||||
local reload_delay = gun_data.reload_delay
|
||||
|
||||
local playerMeta = player:get_meta()
|
||||
local gunMeta = itemstack:get_meta()
|
||||
|
||||
gunMeta:set_float("RW_reload_delay", reload_delay)
|
||||
gunMeta:set_float("rw_reload_delay", reload_delay)
|
||||
playerMeta:set_float("rw_cooldown", reload_delay)
|
||||
|
||||
local player_has_ammo = 0
|
||||
|
@ -190,39 +169,33 @@ rangedweapons_single_load_gun = function(itemstack, player)
|
|||
end
|
||||
|
||||
if player_has_ammo == 1 then
|
||||
local gun_icon = "rangedweapons_emergency_gun_icon.png"
|
||||
|
||||
if gun_data.gun_icon ~= nil then
|
||||
gun_icon = gun_data.gun_icon
|
||||
end
|
||||
|
||||
local ammo_icon = "rangedweapons_emergency_ammo_icon.png"
|
||||
|
||||
if reload_ammo:get_definition().inventory_image ~= nil then
|
||||
ammo_icon = reload_ammo:get_definition().inventory_image
|
||||
end
|
||||
|
||||
player:hud_change(gunimg, "text", gun_icon)
|
||||
player:hud_change(gunimg, "text", gun_data.texture.icon)
|
||||
player:hud_change(ammoimg, "text", ammo_icon)
|
||||
|
||||
local gunMeta = itemstack:get_meta()
|
||||
local ammoCount = gunMeta:get_int("RW_bullets")
|
||||
local ammoName = gunMeta:get_string("RW_ammo_name")
|
||||
local ammoCount = gunMeta:get_int("rw_bullets")
|
||||
local ammoName = gunMeta:get_string("rw_ammo_name")
|
||||
local inv = player:get_inventory()
|
||||
|
||||
if ammoName ~= reload_ammo:get_name() then
|
||||
inv:add_item("main", ammoName .. " " .. ammoCount)
|
||||
gunMeta:set_int("RW_bullets", 0)
|
||||
gunMeta:set_int("rw_bullets", 0)
|
||||
end
|
||||
|
||||
if inv:contains_item("main", reload_ammo:get_name()) and gunMeta:get_int("RW_bullets") < clipSize then
|
||||
if inv:contains_item("main", reload_ammo:get_name()) and gunMeta:get_int("rw_bullets") < clipSize then
|
||||
inv:remove_item("main", reload_ammo:get_name())
|
||||
gunMeta:set_int("RW_bullets", gunMeta:get_int("RW_bullets") + 1)
|
||||
gunMeta:set_int("rw_bullets", gunMeta:get_int("rw_bullets") + 1)
|
||||
end
|
||||
|
||||
gunMeta:set_string("RW_ammo_name", reload_ammo:get_name())
|
||||
gunMeta:set_string("rw_ammo_name", reload_ammo:get_name())
|
||||
|
||||
player:hud_change(gunammo, "text", gunMeta:get_int("RW_bullets"))
|
||||
player:hud_change(gunammo, "text", gunMeta:get_int("rw_bullets"))
|
||||
|
||||
if gun_data.gun_unloaded ~= nil then
|
||||
itemstack:set_name(gun_data.gun_unloaded)
|
||||
|
@ -371,21 +344,17 @@ rangedweapons_shoot_gun = function(itemstack, player)
|
|||
local gunMeta = itemstack:get_meta()
|
||||
local playerMeta = player:get_meta()
|
||||
|
||||
if gunMeta:get_int("RW_bullets") > 0 and playerMeta:get_float("rw_cooldown") <= 0 then
|
||||
playerMeta:set_float("rw_cooldown", gun_data["gun_cooldown"])
|
||||
if gunMeta:get_int("rw_bullets") > 0 and playerMeta:get_float("rw_cooldown") <= 0 then
|
||||
playerMeta:set_float("rw_cooldown", gun_data.cooldown)
|
||||
|
||||
player:hud_change(gunammo, "text", gunMeta:get_int("RW_bullets"))
|
||||
player:hud_change(gunammo, "text", gunMeta:get_int("rw_bullets"))
|
||||
|
||||
local gun_icon = "rangedweapons_emergency_gun_icon.png"
|
||||
if gun_data.gun_icon ~= nil then
|
||||
gun_icon = gun_data.gun_icon
|
||||
end
|
||||
player:hud_change(gunimg, "text", gun_icon)
|
||||
player:hud_change(gunimg, "text", gun_data.texture.icon)
|
||||
|
||||
local OnCollision = function()
|
||||
end
|
||||
|
||||
local bulletStack = ItemStack({name = gunMeta:get_string("RW_ammo_name")})
|
||||
local bulletStack = ItemStack({name = gunMeta:get_string("rw_ammo_name")})
|
||||
AmmoCaps = bulletStack:get_definition().RW_ammo_capabilities
|
||||
|
||||
local gun_damage = {fleshy = 1}
|
||||
|
|
75
weapon.lua
75
weapon.lua
|
@ -1,6 +1,4 @@
|
|||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
local constants = dofile(modpath.."/constants.lua")
|
||||
|
||||
local weapon = {}
|
||||
|
||||
weapon.weapons = {
|
||||
|
@ -28,30 +26,30 @@ end
|
|||
|
||||
|
||||
function weapon.get_texture_name(weapon_name, texture_name)
|
||||
return constants.mod_name .. "_" .. weapon_name .. "_" .. texture_name
|
||||
return ranged_weapons.mod_name .. "_" .. weapon_name .. "_" .. texture_name
|
||||
end
|
||||
|
||||
function weapon.generate_description(weapon_data)
|
||||
local description = ""
|
||||
local text = {
|
||||
string.format("%s", weapon_data["name"]),
|
||||
string.format("Damage: %d", weapon_data["damage"]),
|
||||
string.format("Accuracy: %d", weapon_data["accuracy"]).."%",
|
||||
string.format("Velocity: %d", weapon_data["velocity"]),
|
||||
string.format("Knockback: %d", weapon_data["knock_back"]),
|
||||
string.format("Critical chance: %d", weapon_data["critical_chance"]).."%",
|
||||
string.format("Critical efficiency: %d", weapon_data["critical_efficiency"]),
|
||||
string.format("Reload delay: %.2f", weapon_data["reload_delay"]),
|
||||
string.format("Capacity: %d", weapon_data["capacity"]),
|
||||
string.format("Ammunition: %s", weapon_data["ammunition_string"]),
|
||||
string.format("Rate of fire: %.2f", weapon_data["fire_rate"]),
|
||||
string.format("%s", weapon_data.name),
|
||||
string.format("Damage: %d", weapon_data.damage),
|
||||
string.format("Accuracy: %d", weapon_data.accuracy).."%",
|
||||
string.format("Velocity: %d", weapon_data.velocity),
|
||||
string.format("Knockback: %d", weapon_data.knock_back),
|
||||
string.format("Critical chance: %d", weapon_data.critical_chance).."%",
|
||||
string.format("Critical efficiency: %d", weapon_data.critical_efficiency),
|
||||
string.format("Reload delay: %.2f", weapon_data.reload_delay),
|
||||
string.format("Capacity: %d", weapon_data.capacity),
|
||||
string.format("Ammunition: %s", weapon_data.ammunition_string),
|
||||
string.format("Rate of fire: %.2f", weapon_data.fire_rate),
|
||||
}
|
||||
|
||||
for i,v in ipairs(text) do
|
||||
if i == 0 then
|
||||
color_text = core.colorize(rangedweapons.colors.title_color, v)
|
||||
color_text = core.colorize(ranged_weapons.colors.title_color, v)
|
||||
else
|
||||
color_text = core.colorize(rangedweapons.colors.white, v)
|
||||
color_text = core.colorize(ranged_weapons.colors.white, v)
|
||||
end
|
||||
|
||||
description = description .. color_text
|
||||
|
@ -61,19 +59,44 @@ function weapon.generate_description(weapon_data)
|
|||
|
||||
end
|
||||
|
||||
function weapon.eject_shell(itemstack, player, rld_item, rld_time, rldsound, shell)
|
||||
itemstack:set_name(rld_item)
|
||||
local meta = player:get_meta()
|
||||
meta:set_float("rw_cooldown", rld_time)
|
||||
|
||||
local gunMeta = itemstack:get_meta()
|
||||
|
||||
local bulletStack = ItemStack({name = gunMeta:get_string("RW_ammo_name")})
|
||||
|
||||
minetest.sound_play(rldsound, {player})
|
||||
function weapon.drop_magazine(player, gun_data)
|
||||
--TODO: This should probably be abstracted into a more general
|
||||
--function for dropping stuff on the ground and not just magazines.
|
||||
--
|
||||
local pos = player:get_pos()
|
||||
local dir = player:get_look_dir()
|
||||
local yaw = player:get_look_horizontal()
|
||||
|
||||
if pos and dir and yaw then
|
||||
pos.y = pos.y + 1.4
|
||||
|
||||
local obj = minetest.add_entity(pos, "rangedweapons:mag")
|
||||
|
||||
--TODO: Make some sense out of these magic values from the
|
||||
--original codebase:
|
||||
if obj then
|
||||
obj:set_properties({textures = {gun_data.magazine}})
|
||||
obj:set_velocity({x = dir.x * 2, y = dir.y * 2, z = dir.z * 2})
|
||||
obj:set_acceleration({x = 0, y = -5, z = 0})
|
||||
obj:set_rotation({x = 0, y = yaw + math.pi, z = 0})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function weapon.eject_shell(itemstack, player, rld_item, rld_time, rldsound, shell)
|
||||
local meta = player:get_meta()
|
||||
local gun_meta = itemstack:get_meta()
|
||||
local bulletStack = ItemStack({name = gun_meta:get_string("rw_ammo_name")})
|
||||
|
||||
local pos = player:get_pos()
|
||||
local dir = player:get_look_dir()
|
||||
local yaw = player:get_look_horizontal()
|
||||
|
||||
minetest.sound_play(rldsound, {player})
|
||||
itemstack:set_name(rld_item)
|
||||
meta:set_float("rw_cooldown", rld_time)
|
||||
|
||||
if pos and dir and yaw then
|
||||
pos.y = pos.y + 1.6
|
||||
local obj = minetest.add_entity(pos, "rangedweapons:empty_shell")
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
local weapon_type = "handgun"
|
||||
local weapon_name = "beretta"
|
||||
|
||||
local weapon_item_name = rangedweapons.mod_name .. ":" .. weapon_name
|
||||
|
||||
local weapon_item_name = ranged_weapons.mod_name .. ":" .. weapon_name
|
||||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
|
||||
|
||||
local weapon = dofile(modpath.."/weapon.lua")
|
||||
local handgun = dofile(modpath.."/weapons".."/handgun".."/handgun.lua")
|
||||
|
||||
|
@ -17,9 +17,9 @@ local beretta_data = {
|
|||
default = weapon.get_texture_name(weapon_type, weapon_name, "texture.png"),
|
||||
reload = weapon.get_texture_name(weapon_type, weapon_name, "reload.png"),
|
||||
},
|
||||
unloaded = rangedweapons.mod_name .. ":beretta_r",
|
||||
cooling = rangedweapons.mod_name .. ":beretta_rld",
|
||||
fire_sound = rangedweapons.mod_name .. "_beretta",
|
||||
unloaded = ranged_weapons.mod_name .. ":beretta_r",
|
||||
cooling = ranged_weapons.mod_name .. ":beretta_rld",
|
||||
fire_sound = ranged_weapons.mod_name .. "_beretta",
|
||||
}
|
||||
|
||||
beretta_data["suitable_ammo"] = {{"rangedweapons:9mm",beretta_data["capacity"]}}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
|
||||
local weapon = dofile(modpath.."/weapon.lua")
|
||||
local handgun = dofile(modpath.."/weapons".."/handgun".."/handgun.lua")
|
||||
|
||||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
|
||||
local weapon_type = "handgun"
|
||||
local weapon_name = "glock17"
|
||||
|
||||
local weapon_item_name = rangedweapons.mod_name .. ":" .. weapon_name
|
||||
local weapon_item_name = ranged_weapons.mod_name .. ":" .. weapon_name
|
||||
|
||||
-- Attributes for the weapon
|
||||
local glock_data = {
|
||||
|
@ -22,7 +22,7 @@ local glock_data = {
|
|||
wield_scale = {x = 1.2, y = 1.2, z =1.2}
|
||||
}
|
||||
|
||||
glock_data.suitable_ammo = {{rangedweapons.mod_name .. ":" .. "9mm", glock_data.capacity}}
|
||||
glock_data.suitable_ammo = {{ranged_weapons.mod_name .. ":" .. "9mm", glock_data.capacity}}
|
||||
|
||||
-- Populate any unset values with handgun defaults
|
||||
for k,v in pairs(handgun) do
|
||||
|
|
|
@ -10,7 +10,7 @@ local handgun = {
|
|||
magazine = "rangedweapons:handgun_mag_black",
|
||||
accuracy = 94,
|
||||
fire_rate = 0.9,
|
||||
cool_down = 0.4,
|
||||
cooldown = 0.4,
|
||||
reload_delay = 0.5,
|
||||
projectiles = 1,
|
||||
has_shell = 1,
|
||||
|
|
Loading…
Reference in New Issue