From 33bc4da625e39aaabc9955e3726cec1e386e1039 Mon Sep 17 00:00:00 2001 From: FatalErr42O <58855799+FatalError42O@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:43:21 -0700 Subject: [PATCH] added support for spawning preloaded ammo using a single meta element --- TODO.txt | 1 + ammo_api.lua | 27 ++++++++++++++++++++++++++- classes/Ammo_handler.lua | 34 +++++++++++++++++++++++++--------- classes/Gun.lua | 26 ++++++++++++++++++++++++-- 4 files changed, 76 insertions(+), 12 deletions(-) diff --git a/TODO.txt b/TODO.txt index accdf01..33f0911 100644 --- a/TODO.txt +++ b/TODO.txt @@ -41,6 +41,7 @@ ( ) AR15 ( ) Berreta m12 (smg) +( ) remove total_bullets as a meta property. ( ) `textures` should be in visuals!!! ( ) `inventory_image_magless` should be in visuals!!! ( ) leave round in chamber if reloading the same type of Ammunition diff --git a/ammo_api.lua b/ammo_api.lua index 24b222f..5699309 100644 --- a/ammo_api.lua +++ b/ammo_api.lua @@ -31,7 +31,15 @@ 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 = minetest.deserialize(meta:get_string("guns4d_loaded_bullets")) + 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 count = 0 for i, v in pairs(bullets) do count = count + v @@ -186,3 +194,20 @@ function Guns4d.ammo.register_magazine(def) --register the actual recipe to add ammo to a mag end +function Guns4d.ammo.magazine(magname) + local mag = ItemStack() +end + +function Guns4d.ammo.magazine_of_gun(gunname, full, string) + local gprops = Guns4d.gun.registered[gunname].properties + local magname = gprops.ammo.accepted_magazines[1] + assert(magname, "magazines are not accepted") + local mag = ItemStack(magname) + local meta = mag:get_meta() + local new_ammo_table = {} + if full then + new_ammo_table[gprops.ammo.accepted_bullets[1]] = Guns4d.ammo.registered_magazines[magname].capacity + end + meta:set_string("guns4d_loaded_bullets", minetest.serialize(new_ammo_table)) + return (string and mag:to_string()) and mag +end diff --git a/classes/Ammo_handler.lua b/classes/Ammo_handler.lua index 7e396aa..0a9760a 100644 --- a/classes/Ammo_handler.lua +++ b/classes/Ammo_handler.lua @@ -9,17 +9,33 @@ Ammo_handler = Instantiatable_class:inherit({ local gun = def.gun def.ammo = {} if gun.properties.ammo then - if meta:get_string("guns4d_loaded_bullets") == "" then - def.ammo.loaded_mag = gun.properties.ammo.comes_with or "empty" - def.ammo.next_bullet = "empty" - def.ammo.total_bullets = 0 - def.ammo.loaded_bullets = {} + if meta:get_int("guns4d_spawn_with_ammo") > 0 then + local bullets = meta:get_int("guns4d_spawn_with_ammo") + if gun.properties.ammo.magazine_only then + def.ammo.loaded_mag = gun.properties.ammo.accepted_magazines[1] + def.ammo.loaded_bullets = { + [Guns4d.registered_magazines[gun.properties.ammo.accepted_magazines[1]].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") def:update_meta() else - def.ammo.loaded_mag = meta:get_string("guns4d_loaded_mag") - def.ammo.loaded_bullets = minetest.deserialize(meta:get_string("guns4d_loaded_bullets")) - def.ammo.total_bullets = meta:get_int("guns4d_total_bullets") - def.ammo.next_bullet = meta:get_string("guns4d_next_bullet") + if meta:get_string("guns4d_loaded_bullets") == "" then + def.ammo.loaded_mag = gun.properties.ammo.comes_with or "empty" + def.ammo.next_bullet = "empty" + def.ammo.total_bullets = 0 + def.ammo.loaded_bullets = {} + def:update_meta() + else + def.ammo.loaded_mag = meta:get_string("guns4d_loaded_mag") + def.ammo.loaded_bullets = minetest.deserialize(meta:get_string("guns4d_loaded_bullets")) + def.ammo.total_bullets = meta:get_int("guns4d_total_bullets") --TODO: REMOVE TOTAL_BULLETS AS A META + def.ammo.next_bullet = meta:get_string("guns4d_next_bullet") + end end end end diff --git a/classes/Gun.lua b/classes/Gun.lua index bd15dd6..6c9837c 100644 --- a/classes/Gun.lua +++ b/classes/Gun.lua @@ -29,6 +29,20 @@ local gun_default = { burst = "inventory_overlay_burst.png", safe = "inventory_overlay_safe.png" }, + bloom = { + base_aiming = 0, --amount of bloom at a full rest while aiming down sights (if possible) + base_hip = 0, --amount of bloom at rest while not aiming down sights. + recoil = { + decay = 1, --decay rate + amount = 0, + ratio = 0, --ratio of x to y + }, + walking = { + decay = 1, + amount = 0, + ratio = 0, + } + }, infinite_inventory_overlay = "inventory_overlay_inf_ammo.png", recoil = { --used by update_recoil() velocity_correction_factor = { --velocity correction factor is currently very broken. @@ -162,6 +176,13 @@ local gun_default = { } }, }, + --[[ + ammo = { + accepted_magazines = {}, + accepted_bullets = {}, + magazine_only = false + } + ]] initial_vertical_rotation = -60, --inventory_image --inventory_image_empty @@ -192,10 +213,11 @@ local gun_default = { player_axial = Vec.new(), }, }, - animation_rotation = vector.new(), spread = { - + recoil = vector.new(), + walking = vector.new() }, + animation_rotation = vector.new(), --[[total_offset_rotation = { --can't be in offsets, as they're added automatically. gun_axial = Vec.new(), player_axial = Vec.new(),