Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert: lazy ammo init #2815

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions code/modules/projectiles/ammunition.dm
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
var/fall_sounds = list('sound/weapons/guns/casingfall1.ogg','sound/weapons/guns/casingfall2.ogg','sound/weapons/guns/casingfall3.ogg')



/obj/item/ammo_magazine/proc/create_initial_contents()
if(contents_initialized || !initial_ammo || !ammo_type)
return
for(var/i in 1 to initial_ammo)
stored_ammo += new ammo_type(src)

/obj/item/ammo_magazine/proc/get_stored_ammo_count()
. = length(stored_ammo)
if(!contents_initialized)
. += initial_ammo


/obj/item/ammo_casing/Initialize()
if(ispath(projectile_type))
BB = new projectile_type(src)
Expand Down Expand Up @@ -139,11 +126,6 @@
var/list/icon_keys = list() //keys
var/list/ammo_states = list() //values

/// Determines whether or not we wait until the first time our contents are gotten to initialize contents. May lead to icon bugs if not handled delicately.
var/lazyload_contents = TRUE
/// Whether or not our contents have been initialized or not, used in lazyloaded contents.
var/contents_initialized = FALSE


/obj/item/ammo_magazine/box
w_class = ITEM_SIZE_NORMAL
Expand All @@ -157,8 +139,9 @@
if(isnull(initial_ammo))
initial_ammo = max_ammo

if(!lazyload_contents)
create_initial_contents()
if(initial_ammo)
for(var/i in 1 to initial_ammo)
stored_ammo += new ammo_type(src)
if(caliber)
LAZYINSERT(labels, caliber, 1)
if(LAZYLEN(labels))
Expand All @@ -172,7 +155,7 @@
if(C.caliber != caliber)
to_chat(user, SPAN_WARNING("\The [C] does not fit into \the [src]."))
return TRUE
if(get_stored_ammo_count() >= max_ammo)
if(length(stored_ammo) >= max_ammo)
to_chat(user, SPAN_WARNING("\The [src] is full!"))
return TRUE
if(!user.unEquip(C, src))
Expand All @@ -185,7 +168,6 @@


/obj/item/ammo_magazine/attack_self(mob/user)
create_initial_contents()
if(!length(stored_ammo))
to_chat(user, SPAN_NOTICE("[src] is already empty!"))
return
Expand All @@ -199,7 +181,6 @@

/obj/item/ammo_magazine/attack_hand(mob/user)
if(user.get_inactive_hand() == src)
create_initial_contents()
if(!length(stored_ammo))
to_chat(user, SPAN_NOTICE("[src] is already empty!"))
else
Expand Down Expand Up @@ -233,21 +214,19 @@
icon_state = initial(icon_state)

if(multiple_sprites)
//find the lowest key greater than or equal to our ammo count
//find the lowest key greater than or equal to length(stored_ammo)
var/new_state = null
var/self_ammo_count = get_stored_ammo_count()
for(var/idx in 1 to length(icon_keys))
var/icon_ammo_count = icon_keys[idx]
if (icon_ammo_count >= self_ammo_count)
var/ammo_count = icon_keys[idx]
if (ammo_count >= length(stored_ammo))
new_state = ammo_states[idx]
break
icon_state = (new_state)? new_state : initial(icon_state)


/obj/item/ammo_magazine/examine(mob/user)
. = ..()
var/self_ammo_count = get_stored_ammo_count()
to_chat(user, "There [(self_ammo_count == 1)? "is" : "are"] [self_ammo_count] round\s left!")
to_chat(user, "There [(length(stored_ammo) == 1)? "is" : "are"] [length(stored_ammo)] round\s left!")


//magazine icon state caching
Expand Down
1 change: 0 additions & 1 deletion code/modules/projectiles/ammunition/boxes.dm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@

/obj/item/ammo_magazine/shotholder/attack_hand(mob/user)
if((user.a_intent == I_HURT) && (length(stored_ammo)))
create_initial_contents()
var/obj/item/ammo_casing/C = stored_ammo[length(stored_ammo)]
stored_ammo-=C
user.put_in_hands(C)
Expand Down
20 changes: 7 additions & 13 deletions code/modules/projectiles/guns/projectile.dm
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,10 @@
chambered = loaded[1] //load next casing.
if(handle_casings != HOLD_CASINGS)
loaded -= chambered
else if(ammo_magazine)
if(!ammo_magazine.contents_initialized && ammo_magazine.initial_ammo > 0)
chambered = new ammo_magazine.ammo_type(src)
if(handle_casings == HOLD_CASINGS)
ammo_magazine.stored_ammo += chambered
ammo_magazine.initial_ammo--
else if(ammo_magazine && length(ammo_magazine.stored_ammo))
chambered = ammo_magazine.stored_ammo[length(ammo_magazine.stored_ammo)]
if(handle_casings != HOLD_CASINGS)
ammo_magazine.stored_ammo -= chambered
else if(ammo_magazine && length(ammo_magazine.stored_ammo))
chambered = ammo_magazine.stored_ammo[length(ammo_magazine.stored_ammo)]
if(handle_casings != HOLD_CASINGS)
ammo_magazine.stored_ammo -= chambered

if (chambered)
return chambered.BB
Expand Down Expand Up @@ -321,7 +315,7 @@

/obj/item/gun/projectile/afterattack(atom/A, mob/living/user)
..()
if(auto_eject && ammo_magazine && !ammo_magazine.get_stored_ammo_count())
if(auto_eject && ammo_magazine && ammo_magazine.stored_ammo && !length(ammo_magazine.stored_ammo))
ammo_magazine.dropInto(user.loc)
user.visible_message(
"[ammo_magazine] falls out and clatters on the floor!",
Expand All @@ -348,8 +342,8 @@
var/bullets = 0
if(loaded)
bullets += length(loaded)
if(ammo_magazine)
bullets += ammo_magazine.get_stored_ammo_count()
if(ammo_magazine && ammo_magazine.stored_ammo)
bullets += length(ammo_magazine.stored_ammo)
if(chambered)
bullets += 1
return bullets
Expand Down
4 changes: 2 additions & 2 deletions code/modules/projectiles/guns/projectile/automatic.dm
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
/obj/item/gun/projectile/automatic/bullpup_rifle/on_update_icon()
..()
if(ammo_magazine)
if(length(ammo_magazine.get_stored_ammo_count()))
if(length(ammo_magazine.stored_ammo))
icon_state = "carbine-loaded"
else
icon_state = "carbine-empty"
Expand Down Expand Up @@ -351,7 +351,7 @@
/obj/item/gun/projectile/automatic/l6_saw/on_update_icon()
..()
if(istype(ammo_magazine, /obj/item/ammo_magazine/box))
icon_state = "l6[cover_open ? "open" : "closed"][round(length(ammo_magazine.get_stored_ammo_count()), 10)]"
icon_state = "l6[cover_open ? "open" : "closed"][round(length(ammo_magazine.stored_ammo), 10)]"
item_state = "l6[cover_open ? "open" : "closed"]"
wielded_item_state = "l6[cover_open ? "open" : "closed"]-wielded"
else if(ammo_magazine)
Expand Down
10 changes: 5 additions & 5 deletions code/modules/projectiles/guns/projectile/dartgun.dm
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
icon_state = "dartgun-empty"
return 1

if(!ammo_magazine.get_stored_ammo_count() || length(ammo_magazine.get_stored_ammo_count()))
if(!ammo_magazine.stored_ammo || length(ammo_magazine.stored_ammo))
icon_state = "dartgun-0"
else if(length(ammo_magazine.get_stored_ammo_count()) > 5)
else if(length(ammo_magazine.stored_ammo) > 5)
icon_state = "dartgun-5"
else
icon_state = "dartgun-[length(ammo_magazine.get_stored_ammo_count())]"
icon_state = "dartgun-[length(ammo_magazine.stored_ammo)]"
return 1

/obj/item/gun/projectile/dartgun/consume_next_projectile()
Expand Down Expand Up @@ -166,8 +166,8 @@
dat += " \[<A href='?src=\ref[src];eject=[i]'>Eject</A>\]<br>"

if(ammo_magazine)
if(ammo_magazine.stored_ammo && length(ammo_magazine.get_stored_ammo_count()))
dat += "The dart cartridge has [length(ammo_magazine.get_stored_ammo_count())] shots remaining."
if(ammo_magazine.stored_ammo && length(ammo_magazine.stored_ammo))
dat += "The dart cartridge has [length(ammo_magazine.stored_ammo)] shots remaining."
else
dat += SPAN_COLOR("red", "The dart cartridge is empty!")
dat += " \[<A href='?src=\ref[src];eject_cart=1'>Eject</A>\]<br>"
Expand Down