Explosion Optimizations

Implements a trange that internally checks only turfs and thus less
intensive.

Conflicts:
	code/game/objects/explosion.dm
This commit is contained in:
ChuckTheSheep
2014-01-04 19:29:17 -05:00
committed by Mloc-Argent
parent dfdeed7434
commit 533600eaa5
2 changed files with 31 additions and 40 deletions

View File

@@ -7,6 +7,19 @@
else return dy + (0.5*dx) else return dy + (0.5*dx)
proc/trange(var/Dist=0,var/turf/Center=null)//alternative to range (ONLY processes turfs and thus less intensive)
if(Center==null) return
//var/x1=((Center.x-Dist)<1 ? 1 : Center.x-Dist)
//var/y1=((Center.y-Dist)<1 ? 1 : Center.y-Dist)
//var/x2=((Center.x+Dist)>world.maxx ? world.maxx : Center.x+Dist)
//var/y2=((Center.y+Dist)>world.maxy ? world.maxy : Center.y+Dist)
var/turf/x1y1 = locate(((Center.x-Dist)<1 ? 1 : Center.x-Dist),((Center.y-Dist)<1 ? 1 : Center.y-Dist),Center.z)
var/turf/x2y2 = locate(((Center.x+Dist)>world.maxx ? world.maxx : Center.x+Dist),((Center.y+Dist)>world.maxy ? world.maxy : Center.y+Dist),Center.z)
return block(x1y1,x2y2)
proc/explosion(turf/epicenter, devastation_range, heavy_impact_range, light_impact_range, flash_range, adminlog = 1) proc/explosion(turf/epicenter, devastation_range, heavy_impact_range, light_impact_range, flash_range, adminlog = 1)
src = null //so we don't abort once src is deleted src = null //so we don't abort once src is deleted
spawn(0) spawn(0)
@@ -48,7 +61,7 @@ proc/explosion(turf/epicenter, devastation_range, heavy_impact_range, light_impa
var/y0 = epicenter.y var/y0 = epicenter.y
var/z0 = epicenter.z var/z0 = epicenter.z
for(var/turf/T in range(epicenter, max(devastation_range, heavy_impact_range, light_impact_range))) for(var/turf/T in trange(epicenter, max(devastation_range, heavy_impact_range, light_impact_range)))
var/dist = cheap_pythag(T.x - x0,T.y - y0) var/dist = cheap_pythag(T.x - x0,T.y - y0)
if(dist < devastation_range) dist = 1 if(dist < devastation_range) dist = 1
@@ -85,4 +98,4 @@ proc/explosion(turf/epicenter, devastation_range, heavy_impact_range, light_impa
proc/secondaryexplosion(turf/epicenter, range) proc/secondaryexplosion(turf/epicenter, range)
for(var/turf/tile in range(range, epicenter)) for(var/turf/tile in range(range, epicenter))
tile.ex_act(2) tile.ex_act(2)

View File

@@ -6,31 +6,12 @@
/obj /obj
var/explosion_resistance var/explosion_resistance
/datum/explosion_turf
var/turf/turf //The turf which will get ex_act called on it
var/max_power //The largest amount of power the turf sustained
New()
..()
max_power = 0
proc/save_power_if_larger(power) var/list/explosion_turfs = list()
if(power > max_power)
max_power = power
return 1
return 0
var/list/datum/explosion_turf/explosion_turfs = list()
var/explosion_in_progress = 0 var/explosion_in_progress = 0
proc/get_explosion_turf(var/turf/T)
for( var/datum/explosion_turf/ET in explosion_turfs )
if( T == ET.turf )
return ET
var/datum/explosion_turf/ET = new()
ET.turf = T
explosion_turfs += ET
return ET
proc/explosion_rec(turf/epicenter, power) proc/explosion_rec(turf/epicenter, power)
@@ -52,9 +33,8 @@ proc/explosion_rec(turf/epicenter, power)
explosion_in_progress = 1 explosion_in_progress = 1
explosion_turfs = list() explosion_turfs = list()
var/datum/explosion_turf/ETE = get_explosion_turf()
ETE.turf = epicenter explosion_turfs[epicenter] = power
ETE.max_power = power
//This steap handles the gathering of turfs which will be ex_act() -ed in the next step. It also ensures each turf gets the maximum possible amount of power dealt to it. //This steap handles the gathering of turfs which will be ex_act() -ed in the next step. It also ensures each turf gets the maximum possible amount of power dealt to it.
for(var/direction in cardinal) for(var/direction in cardinal)
@@ -62,22 +42,21 @@ proc/explosion_rec(turf/epicenter, power)
T.explosion_spread(power - epicenter.explosion_resistance, direction) T.explosion_spread(power - epicenter.explosion_resistance, direction)
//This step applies the ex_act effects for the explosion, as planned in the previous step. //This step applies the ex_act effects for the explosion, as planned in the previous step.
for( var/datum/explosion_turf/ET in explosion_turfs ) for(var/turf/T in explosion_turfs)
if(ET.max_power <= 0) continue if(explosion_turfs[T] <= 0) continue
if(!ET.turf) continue if(!T) continue
//Wow severity looks confusing to calculate... Fret not, I didn't leave you with any additional instructions or help. (just kidding, see the line under the calculation) //Wow severity looks confusing to calculate... Fret not, I didn't leave you with any additional instructions or help. (just kidding, see the line under the calculation)
var/severity = 4 - round(max(min( 3, ((ET.max_power - ET.turf.explosion_resistance) / (max(3,(power/3)))) ) ,1), 1) var/severity = 4 - round(max(min( 3, ((explosion_turfs[T] - T.explosion_resistance) / (max(3,(power/3)))) ) ,1), 1) //sanity effective power on tile divided by either 3 or one third the total explosion power
//sanity effective power on tile divided by either 3 or one third the total explosion power
// One third because there are three power levels and I // One third because there are three power levels and I
// want each one to take up a third of the crater // want each one to take up a third of the crater
var/x = ET.turf.x var/x = T.x
var/y = ET.turf.y var/y = T.y
var/z = ET.turf.z var/z = T.z
ET.turf.ex_act(severity) T.ex_act(severity)
if(!ET.turf) if(!T)
ET.turf = locate(x,y,z) T = locate(x,y,z)
for( var/atom/A in ET.turf ) for(var/atom/A in T)
A.ex_act(severity) A.ex_act(severity)
explosion_in_progress = 0 explosion_in_progress = 0
@@ -123,10 +102,9 @@ proc/explosion_rec(turf/epicenter, power)
new/obj/effect/debugging/marker(src) new/obj/effect/debugging/marker(src)
*/ */
var/datum/explosion_turf/ET = get_explosion_turf(src) if(explosion_turfs[src] >= power)
if(ET.max_power >= power)
return //The turf already sustained and spread a power greated than what we are dealing with. No point spreading again. return //The turf already sustained and spread a power greated than what we are dealing with. No point spreading again.
ET.max_power = power explosion_turfs[src] = power
var/spread_power = power - src.explosion_resistance //This is the amount of power that will be spread to the tile in the direction of the blast var/spread_power = power - src.explosion_resistance //This is the amount of power that will be spread to the tile in the direction of the blast
var/side_spread_power = power - 2 * src.explosion_resistance //This is the amount of power that will be spread to the side tiles var/side_spread_power = power - 2 * src.explosion_resistance //This is the amount of power that will be spread to the side tiles