added option for wear to represent ammo initially, fixed a but with meta ammo spawning

This commit is contained in:
FatalErr42O 2024-03-17 21:40:56 -07:00
parent 33bc4da625
commit 1930beb338
5 changed files with 23 additions and 16 deletions

View File

@ -25,21 +25,21 @@ end
function Guns4d.ammo.initialize_mag_data(itemstack, meta)
meta = meta or itemstack:get_meta()
meta:set_string("guns4d_loaded_bullets", minetest.serialize({}))
if meta:get_int("guns4d_spawn_with_ammo") > 0 then
bullets = {
[def.accepted_bullets[1]]=meta:get_int("guns4d_spawn_with_ammo")
}
meta:set_int("guns4d_spawn_with_ammo", 0)
else
bullets = minetest.deserialize(meta:get_string("guns4d_loaded_bullets"))
end
Guns4d.ammo.update_mag(nil, itemstack, meta)
return itemstack
end
function Guns4d.ammo.update_mag(def, itemstack, meta)
def = def or Guns4d.ammo.registered_magazines[itemstack:get_name()]
meta = meta or itemstack:get_meta()
local bullets
if meta:get_int("guns4d_spawn_with_ammo") > 0 then
bullets = {
[def.accepted_bullets[1]]=meta:get_int("guns4d_spawn_with_ammo")
}
meta:set_int("guns4d_spawn_with_ammo")
else
bullets = minetest.deserialize(meta:get_string("guns4d_loaded_bullets"))
end
local bullets = minetest.deserialize(meta:get_string("guns4d_loaded_bullets"))
local count = 0
for i, v in pairs(bullets) do
count = count + v

View File

@ -9,23 +9,27 @@ Ammo_handler = Instantiatable_class:inherit({
local gun = def.gun
def.ammo = {}
if gun.properties.ammo then
if meta:get_int("guns4d_spawn_with_ammo") > 0 then
local bullets = meta:get_int("guns4d_spawn_with_ammo")
local spawn_with = meta:get_int("guns4d_spawn_with_ammo")
if (meta:get_string("guns4d_loaded_bullets") == "") and ((spawn_with > 0) or (Guns4d.config.interpret_initial_wear_as_ammo))then
local bullets = (spawn_with > 0 and spawn_with) or (1-(def.gun.itemstack:get_wear()/65535))
if gun.properties.ammo.magazine_only then
def.ammo.loaded_mag = gun.properties.ammo.accepted_magazines[1]
local magname = gun.properties.ammo.accepted_magazines[1]
bullets = math.floor(Guns4d.ammo.registered_magazines[magname].capacity*bullets)
def.ammo.loaded_mag = magname
def.ammo.loaded_bullets = {
[Guns4d.registered_magazines[gun.properties.ammo.accepted_magazines[1]].accepted_bullets[1]] = bullets
[Guns4d.ammo.registered_magazines[magname].accepted_bullets[1]] = bullets
}
else
def.ammo.loaded_mag = "empty"
def.ammo.loaded_bullets = gun.properties.accepted_bullets[1]
end
def.ammo.total_bullets = bullets
meta:set_int("guns4d_spawn_with_ammo")
gun.itemstack:set_wear(0)
meta:set_int("guns4d_spawn_with_ammo", 0)
def:update_meta()
else
if meta:get_string("guns4d_loaded_bullets") == "" then
def.ammo.loaded_mag = gun.properties.ammo.comes_with or "empty"
def.ammo.loaded_mag = gun.properties.ammo.initial_mag or "empty"
def.ammo.next_bullet = "empty"
def.ammo.total_bullets = 0
def.ammo.loaded_bullets = {}

View File

@ -118,7 +118,8 @@ local gun_default = {
ammo = { --used by ammo_handler
magazine_only = false,
accepted_bullets = {},
accepted_magazines = {}
accepted_magazines = {},
initial_mag = "empty"
},
visuals = {
--mesh

View File

@ -0,0 +1 @@
interpret_initial_wear_as_ammo = true

View File

@ -13,6 +13,7 @@ Guns4d.config = {
control_held_toggle_threshold = 0,
empty_symbol = "0e",
infinite_ammo_priv = "guns4d_infinite_ammo",
interpret_initial_wear_as_ammo = false
--`["official_content.replace_ads_with_bloom"] = false,
--`["official_content.uses_magazines"] = true
}