Skip to content

Commit

Permalink
ports Nebula's ammo magazines lazy-initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustKisik committed Oct 9, 2024
1 parent 2107620 commit 49454e1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
37 changes: 29 additions & 8 deletions code/modules/projectiles/ammunition.dm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
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 @@ -126,6 +139,11 @@
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 @@ -139,9 +157,8 @@
if(isnull(initial_ammo))
initial_ammo = max_ammo

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


/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 @@ -181,6 +199,7 @@

/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 @@ -214,19 +233,21 @@
icon_state = initial(icon_state)

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


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


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

/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: 13 additions & 7 deletions code/modules/projectiles/guns/projectile.dm
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@
chambered = loaded[1] //load next casing.
if(handle_casings != HOLD_CASINGS)
loaded -= 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
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.stored_ammo.len)
chambered = ammo_magazine.stored_ammo[ammo_magazine.stored_ammo.len]
if(handle_casings != HOLD_CASINGS)
ammo_magazine.stored_ammo -= chambered

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

/obj/item/gun/projectile/afterattack(atom/A, mob/living/user)
..()
if(auto_eject && ammo_magazine && ammo_magazine.stored_ammo && !length(ammo_magazine.stored_ammo))
if(auto_eject && ammo_magazine && !ammo_magazine.get_stored_ammo_count())
ammo_magazine.dropInto(user.loc)
user.visible_message(
"[ammo_magazine] falls out and clatters on the floor!",
Expand All @@ -342,8 +348,8 @@
var/bullets = 0
if(loaded)
bullets += length(loaded)
if(ammo_magazine && ammo_magazine.stored_ammo)
bullets += length(ammo_magazine.stored_ammo)
if(ammo_magazine)
bullets += ammo_magazine.get_stored_ammo_count()
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.stored_ammo))
if(length(ammo_magazine.get_stored_ammo_count()))
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.stored_ammo), 10)]"
icon_state = "l6[cover_open ? "open" : "closed"][round(length(ammo_magazine.get_stored_ammo_count()), 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.stored_ammo || length(ammo_magazine.stored_ammo))
if(!ammo_magazine.get_stored_ammo_count() || length(ammo_magazine.get_stored_ammo_count()))
icon_state = "dartgun-0"
else if(length(ammo_magazine.stored_ammo) > 5)
else if(length(ammo_magazine.get_stored_ammo_count()) > 5)
icon_state = "dartgun-5"
else
icon_state = "dartgun-[length(ammo_magazine.stored_ammo)]"
icon_state = "dartgun-[length(ammo_magazine.get_stored_ammo_count())]"
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.stored_ammo))
dat += "The dart cartridge has [length(ammo_magazine.stored_ammo)] shots remaining."
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."
else
dat += SPAN_COLOR("red", "The dart cartridge is empty!")
dat += " \[<A href='?src=\ref[src];eject_cart=1'>Eject</A>\]<br>"
Expand Down

0 comments on commit 49454e1

Please sign in to comment.