From ee85f7ecede4c3929ec971f1e16c45f64d477641 Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Tue, 2 Sep 2014 00:13:06 +0100 Subject: [PATCH 1/5] /tg/station Atom Pool + AtomPooling of Emitter beams. --- code/game/pooling/atom_pool.dm | 84 ++++++++++++++++++++ code/modules/power/singularity/emitter.dm | 8 +- code/modules/projectiles/projectile.dm | 12 +-- code/modules/projectiles/projectile/beams.dm | 2 + tgstation.dme | 1 + 5 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 code/game/pooling/atom_pool.dm diff --git a/code/game/pooling/atom_pool.dm b/code/game/pooling/atom_pool.dm new file mode 100644 index 00000000000..76804962fd1 --- /dev/null +++ b/code/game/pooling/atom_pool.dm @@ -0,0 +1,84 @@ + +/* +/tg/station13 /atom/movable Pool: +--------------------------------- +By RemieRichards + +Creation/Deletion is laggy, so let's reduce reuse and recycle! + +Locked to /atom/movable and it's subtypes due to Loc being a const var on /atom +being read&write on /movable due to how they... move. + +*/ + +var/global/list/GlobalPool = list() + +//You'll be using this proc 90% of the time. +//It grabs a type from the pool if it can +//And if it can't, it creates one +//The pool is flexible and will expand to fit +//The newley created atom when it eventually +//Goes into the pool + +/proc/PoolOrNew(var/get_type,var/new_loc) + if(!get_type || !new_loc) + return + + var/atom/movable/AM = GetFromPool(get_type,new_loc) + + if(!AM) + if(ispath(get_type)) + AM = new get_type (new_loc) + + if(AM) + return AM + + + +/proc/GetFromPool(var/get_type,var/new_loc) + if(!get_type || !new_loc) + return 0 + + if(isnull(GlobalPool[get_type])) + return 0 + + if(length(GlobalPool[get_type]) == 0) + return 0 + + var/atom/movable/AM = pick_n_take(GlobalPool[get_type]) + if(AM) + AM.ResetVars() + AM.loc = new_loc + return AM + return 0 + + + +/proc/PlaceInPool(var/atom/movable/AM) + if(!istype(AM)) + return + + if(AM in GlobalPool[AM.type]) + return + + AM.ResetVars() + + if(!GlobalPool[AM.type]) + GlobalPool[AM.type] = list() + + GlobalPool[AM.type] += AM + + + +/atom/movable/proc/ResetVars() + var/list/excluded = list("loc", "locs", "parent_type", "vars", "verbs", "type") + + for(var/V in vars) + if(V in excluded) + continue + + vars[V] = initial(vars[V]) + + vars["loc"] = null + + diff --git a/code/modules/power/singularity/emitter.dm b/code/modules/power/singularity/emitter.dm index e1ff0aa2a43..76aa62eda7e 100644 --- a/code/modules/power/singularity/emitter.dm +++ b/code/modules/power/singularity/emitter.dm @@ -119,13 +119,17 @@ else src.fire_delay = rand(20,100) src.shot_number = 0 - var/obj/item/projectile/beam/emitter/A = new /obj/item/projectile/beam/emitter( src.loc ) + + var/obj/item/projectile/beam/emitter/A = PoolOrNew(/obj/item/projectile/beam/emitter,src.loc) + + A.dir = src.dir playsound(src.loc, 'sound/weapons/emitter.ogg', 25, 1) + if(prob(35)) var/datum/effect/effect/system/spark_spread/s = new /datum/effect/effect/system/spark_spread s.set_up(5, 1, src) s.start() - A.dir = src.dir + switch(dir) if(NORTH) A.yo = 20 diff --git a/code/modules/projectiles/projectile.dm b/code/modules/projectiles/projectile.dm index 8d6659ff84a..e5213d7c967 100644 --- a/code/modules/projectiles/projectile.dm +++ b/code/modules/projectiles/projectile.dm @@ -50,10 +50,13 @@ var/forcedodge = 0 + /obj/item/projectile/proc/delete() // Garbage collect the projectiles loc = null + + /obj/item/projectile/proc/on_hit(var/atom/target, var/blocked = 0, var/hit_zone) if(!isliving(target)) return 0 if(isanimal(target)) return 0 @@ -67,8 +70,8 @@ else return 50 //if the projectile doesn't do damage, play its hitsound at 50% volume -/obj/item/projectile/Bump(atom/A as mob|obj|turf|area) +/obj/item/projectile/Bump(atom/A as mob|obj|turf|area) if(A == firer) loc = A.loc return 0 //cannot shoot yourself @@ -120,8 +123,7 @@ permutated.Add(A) return 0 - density = 0 - invisibility = 101 + delete() return 0 return 1 @@ -141,9 +143,9 @@ delete() return kill_count-- - spawn while(src && src.loc) + spawn while(loc) if((!( current ) || loc == current)) - current = locate(min(max(x + xo, 1), world.maxx), min(max(y + yo, 1), world.maxy), z) + current = locate(Clamp(x+xo,1,world.maxx),Clamp(y+yo,1,world.maxy),z) if((x == 1 || x == world.maxx || y == 1 || y == world.maxy)) delete() return diff --git a/code/modules/projectiles/projectile/beams.dm b/code/modules/projectiles/projectile/beams.dm index a64c3291f23..ef6596731cf 100644 --- a/code/modules/projectiles/projectile/beams.dm +++ b/code/modules/projectiles/projectile/beams.dm @@ -54,6 +54,8 @@ icon_state = "emitter" damage = 30 +/obj/item/projectile/beam/emitter/delete() //what projectiles use to set loc = null + PlaceInPool(src) /obj/item/projectile/lasertag name = "laser tag beam" diff --git a/tgstation.dme b/tgstation.dme index d95ef20d0cf..460f53f33af 100644 --- a/tgstation.dme +++ b/tgstation.dme @@ -662,6 +662,7 @@ #include "code\game\objects\structures\transit_tubes\station.dm" #include "code\game\objects\structures\transit_tubes\transit_tube.dm" #include "code\game\objects\structures\transit_tubes\transit_tube_pod.dm" +#include "code\game\pooling\atom_pool.dm" #include "code\game\turfs\simulated.dm" #include "code\game\turfs\turf.dm" #include "code\game\turfs\unsimulated.dm" From 08676747e71cc6e6812e2b5e34d6662650932e34 Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Tue, 2 Sep 2014 00:21:04 +0100 Subject: [PATCH 2/5] typos in the readme section. --- code/game/pooling/atom_pool.dm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/game/pooling/atom_pool.dm b/code/game/pooling/atom_pool.dm index 76804962fd1..5e0379ba594 100644 --- a/code/game/pooling/atom_pool.dm +++ b/code/game/pooling/atom_pool.dm @@ -7,7 +7,7 @@ By RemieRichards Creation/Deletion is laggy, so let's reduce reuse and recycle! Locked to /atom/movable and it's subtypes due to Loc being a const var on /atom -being read&write on /movable due to how they... move. +but being read&write on /movable due to how they... move. */ @@ -17,7 +17,7 @@ var/global/list/GlobalPool = list() //It grabs a type from the pool if it can //And if it can't, it creates one //The pool is flexible and will expand to fit -//The newley created atom when it eventually +//The new created atom when it eventually //Goes into the pool /proc/PoolOrNew(var/get_type,var/new_loc) From a70a1a52ae37f958f0434be5b338a6412f1dfd9d Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Tue, 2 Sep 2014 22:03:35 +0100 Subject: [PATCH 3/5] Support for null loc pool-gets + Pooling Miauw's Say() virtualspeakers --- code/game/machinery/telecomms/broadcaster.dm | 4 ++-- code/game/pooling/atom_pool.dm | 18 +++++++++++++----- .../scripting/Implementations/Telecomms.dm | 6 ++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/code/game/machinery/telecomms/broadcaster.dm b/code/game/machinery/telecomms/broadcaster.dm index 4b97f504628..add0390729f 100644 --- a/code/game/machinery/telecomms/broadcaster.dm +++ b/code/game/machinery/telecomms/broadcaster.dm @@ -213,7 +213,7 @@ var/message_delay = 0 // To make sure restarting the recentmessages list is kept var/list/radios = list() - var/atom/movable/virtualspeaker/virt = new(null) + var/atom/movable/virtualspeaker/virt = PoolOrNew(/atom/movable/virtualspeaker,null) virt.name = name virt.job = job virt.languages = AM.languages @@ -295,7 +295,7 @@ var/message_delay = 0 // To make sure restarting the recentmessages list is kept blackbox.messages += blackbox_msg spawn(50) - qdel(virt) + PlaceInPool(virt) /proc/Broadcast_SimpleMessage(var/source, var/frequency, var/text, var/data, var/mob/M, var/compression, var/level) diff --git a/code/game/pooling/atom_pool.dm b/code/game/pooling/atom_pool.dm index 5e0379ba594..eb74ff12458 100644 --- a/code/game/pooling/atom_pool.dm +++ b/code/game/pooling/atom_pool.dm @@ -21,14 +21,21 @@ var/global/list/GlobalPool = list() //Goes into the pool /proc/PoolOrNew(var/get_type,var/new_loc) - if(!get_type || !new_loc) + if(!get_type) return - var/atom/movable/AM = GetFromPool(get_type,new_loc) + var/atom/movable/AM + if(new_loc) + AM = GetFromPool(get_type,new_loc) + else + AM = GetFromPool(get_type,null) if(!AM) if(ispath(get_type)) - AM = new get_type (new_loc) + if(new_loc) + AM = new get_type (new_loc) + else + AM = new get_type (null) if(AM) return AM @@ -36,7 +43,7 @@ var/global/list/GlobalPool = list() /proc/GetFromPool(var/get_type,var/new_loc) - if(!get_type || !new_loc) + if(!get_type) return 0 if(isnull(GlobalPool[get_type])) @@ -48,7 +55,8 @@ var/global/list/GlobalPool = list() var/atom/movable/AM = pick_n_take(GlobalPool[get_type]) if(AM) AM.ResetVars() - AM.loc = new_loc + if(new_loc) + AM.loc = new_loc return AM return 0 diff --git a/code/modules/scripting/Implementations/Telecomms.dm b/code/modules/scripting/Implementations/Telecomms.dm index ab3b73cdbfa..ad1cd386975 100644 --- a/code/modules/scripting/Implementations/Telecomms.dm +++ b/code/modules/scripting/Implementations/Telecomms.dm @@ -301,10 +301,12 @@ datum/signal if(!job) job = "Unknown" - + //SAY REWRITE RELATED CODE. //This code is a little hacky, but it *should* work. Even though it'll result in a virtual speaker referencing another virtual speaker. vOv - var/atom/movable/virtualspeaker/virt = new(null) + //var/atom/movable/virtualspeaker/virt = new(null) //REMIE POOL + var/atom/movable/virtualspeaker/virt = PoolOrNew(/atom/movable/virtualspeaker,null) + world << "Virtual speaker collected/created from pool |Telecomms.dm|" virt.name = source virt.job = job virt.faketrack = 1 From 0bfd88455643e7cb0680b6d1f38fb9a08ce8ab54 Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Tue, 2 Sep 2014 22:05:03 +0100 Subject: [PATCH 4/5] Debug messages removal. --- code/modules/scripting/Implementations/Telecomms.dm | 2 -- 1 file changed, 2 deletions(-) diff --git a/code/modules/scripting/Implementations/Telecomms.dm b/code/modules/scripting/Implementations/Telecomms.dm index ad1cd386975..2437e485f77 100644 --- a/code/modules/scripting/Implementations/Telecomms.dm +++ b/code/modules/scripting/Implementations/Telecomms.dm @@ -304,9 +304,7 @@ datum/signal //SAY REWRITE RELATED CODE. //This code is a little hacky, but it *should* work. Even though it'll result in a virtual speaker referencing another virtual speaker. vOv - //var/atom/movable/virtualspeaker/virt = new(null) //REMIE POOL var/atom/movable/virtualspeaker/virt = PoolOrNew(/atom/movable/virtualspeaker,null) - world << "Virtual speaker collected/created from pool |Telecomms.dm|" virt.name = source virt.job = job virt.faketrack = 1 From 780bcfad12234788b20a545c663d3519a20f1f7a Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Sat, 6 Sep 2014 07:39:30 +0100 Subject: [PATCH 5/5] Adds a call to New() for initialising vars Cheers Carn! --- code/game/pooling/atom_pool.dm | 1 + 1 file changed, 1 insertion(+) diff --git a/code/game/pooling/atom_pool.dm b/code/game/pooling/atom_pool.dm index eb74ff12458..ca6f8f19341 100644 --- a/code/game/pooling/atom_pool.dm +++ b/code/game/pooling/atom_pool.dm @@ -55,6 +55,7 @@ var/global/list/GlobalPool = list() var/atom/movable/AM = pick_n_take(GlobalPool[get_type]) if(AM) AM.ResetVars() + AM.New() if(new_loc) AM.loc = new_loc return AM