Files
Bubberstation/code/modules/mapping/ruins.dm
LemonInTheDark d34fa4c642 Macro optimizes SSmapping saving 50% (#69632)
* 'optimizes' space transitions by like 0.06 seconds, makes them easier to read tho, so that's an upside

* ''''optimizes'''' parsed map loading

I'm honestly not sure how big a difference this makes, looked like small
percentage points if anything
It's a bit more internally concistent at least, which is nice. Also I
understand the system now.

I'd like to think it helped but I think this is kinda a "do you think
it's easier to read" sort of situation. if it did help it was by the
skin of its teeth

* Saves 0.6 seconds off loading meta and lavaland's map files

This is just a lot of micro stuff.
1: Bound checks don't need to be inside for loops, we can instead bound the iteration counts
2: TGM and DMM are parsed differently. in dmm a grid_set is one z level,
   in tgm it's one collumn. Realizing this allows you to skip copytexts and
   other such silly in the tgm implemenentation, saving a good bit of time
3: Min/max bounds do not need to be checked inside for loops, and can
   instead be handled outside of them, because we know the order of x
   and y iteration. This saves 0.2 seconds

I may or may not have made the code harder to read, if so let me know
and I'll check it over.

* Micro ops key caching significantly. Fixes macros bug

inserting \ into a dmm with no valid target would just less then loop
the string. Dumb

Anyway, optimizations. I save a LOT of time by not needing to call
find_next_delimiter_position for every entry and var set. (like maybe 0.5
seconds, not totally sure)
I save this by using splittext, which is significantly faster. this
would cause parsing issues if you could embed \n into dmms, but you
can't, so I'm safe.

Lemme see uh, lots of little things, stuff that's suboptimal or could be
done cheaper. Some "hey you and I both know a \" is 2 chars long sort of
stuff

I removed trim_text because the quote trimming was never actually used,
and the space trimming was slower then using the code in trim. I also
micro'd trim to save a bit of time. this saves another maybe 0.5.

Few other things, I think that's the main of it. Gives me the fuzzy
feelings

* Saves 50% of build_coordinate's time

Micro optimizing go brrrrr
I made turf_blacklist an assoc list rather then just a normal one, so
lookups are O(log n) instead of O(n). Also it's faster for the base case
of loading mostly space.

Instead of toggling the map loader right before and right after New()
calls, we toggle at the start of mapload, and disable then reenable if
we check tick. This saves like 0.3 seconds

Rather then tracking an area cache ourselves, and needing to pass it
around, we use a locally static list to reference the global list of
area -> type. This is much faster, if slightly fragile.

Rather then checking for a null turf at every line, we do it at the
start of the proc and not after. Faster this way, tho it can in theory
drop area vvs.

Avoids calling world.preloader_setup unless we actually have a unique
set of attributes. We use another static list to make this comparison
cheap. This saves another 0.3

Rather then checking for area paths in the turf logic, or vis versa, we
assume we are creating the type implied by the index we're reading off.
So only the last type entry will be loaded like a turf, etc.
This is slightly unsafe but saves a good bit of time, and will properly
error on fucked maps.

Also, rather then using a datum to hold preloader vars, we use 2 global
variables. This is faster.

This marks the end of my optimizations for direct maploading. I've
reduced the cost of loading a map by more then 50% now. Get owned.

* Adds a define for maploading tick check

* makes shuttles load again, removes some of the hard limits I had on the reader for profiling

* Macro ops cave generation

Cave generation was insanely more expensive then it had any right to be.
Maybe 0.5 seconds was saved off not doing a range(12) for EVERY SPAWNED
MOB.
0.14 was saved off using expanded weighted lists (A new idea of mine)
This is useful because I can take a weighted list, and condense it into
weight * path count. This is more memory heavy, and costs more to
create, but is so much faster then the proc.

I also added a naive implementation of gcd to make this a bit less bad.
It's not great, but it'll do for this usecase.

Oh and I changed some ChangeTurfs into New()s. I'm still not entirely
sure what the core difference between the two is, but it seems to work
fine.
I believe it's safe because the turf below us hasn't init'd yet, there's
nothing to take from them. It's like 3 seconds faster too so I'll be sad
when it turns out I'm being dumb

* Micros river spawning

This uses the same sort of concepts as the last change, mostly New being
preferable to ChangeTurf at this level of code.
This bit isn't nearly as detailed as the last few, I honestly got a bit
tired. It's still like 0.4 seconds saved tho

* Micros ruin loading

Turns out it saves time if you don't check area type for every tile on a
ruin. Not a whole ton faster, like 0.03, but faster.

Saves even more time (0.1) to not iterate all your ruin's turfs 3 times
to clear away lavaland mobs, when you're IN SPACE who wrote this.

Oh it also saves time to only pull your turf list once, rather then 3
times
2022-09-22 15:34:10 -07:00

187 lines
6.9 KiB
Plaintext

/datum/map_template/ruin/proc/try_to_place(z, list/allowed_areas_typecache, turf/forced_turf, clear_below)
var/sanity = forced_turf ? 1 : PLACEMENT_TRIES
if(SSmapping.level_trait(z,ZTRAIT_ISOLATED_RUINS))
return place_on_isolated_level(z)
while(sanity > 0)
sanity--
var/width_border = TRANSITIONEDGE + SPACERUIN_MAP_EDGE_PAD + round(width / 2)
var/height_border = TRANSITIONEDGE + SPACERUIN_MAP_EDGE_PAD + round(height / 2)
var/turf/central_turf = forced_turf ? forced_turf : locate(rand(width_border, world.maxx - width_border), rand(height_border, world.maxy - height_border), z)
var/valid = TRUE
var/list/affected_turfs = get_affected_turfs(central_turf,1)
var/list/affected_areas = list()
for(var/turf/check in affected_turfs)
// Use assoc lists to move this out, it's easier that way
if(check.turf_flags & NO_RUINS)
valid = FALSE // set to false before we check
break
var/area/new_area = get_area(check)
affected_areas[new_area] = TRUE
// This is faster yes. Only BARELY but it is faster
for(var/area/affct_area as anything in affected_areas)
if(!allowed_areas_typecache[affct_area.type])
valid = FALSE
break
if(!valid)
continue
testing("Ruin \"[name]\" placed at ([central_turf.x], [central_turf.y], [central_turf.z])")
if(clear_below)
var/list/static/clear_below_typecache = typecacheof(list(
/obj/structure/spawner,
/mob/living/simple_animal,
/obj/structure/flora
))
for(var/turf/T as anything in affected_turfs)
for(var/atom/thing as anything in T)
if(clear_below_typecache[thing.type])
qdel(thing)
load(central_turf,centered = TRUE)
loaded++
for(var/turf/T in affected_turfs)
T.turf_flags |= NO_RUINS
new /obj/effect/landmark/ruin(central_turf, src)
return central_turf
/datum/map_template/ruin/proc/place_on_isolated_level(z)
var/datum/turf_reservation/reservation = SSmapping.RequestBlockReservation(width, height, z) //Make the new level creation work with different traits.
if(!reservation)
return
var/turf/placement = locate(reservation.bottom_left_coords[1],reservation.bottom_left_coords[2],reservation.bottom_left_coords[3])
load(placement)
loaded++
for(var/turf/T in get_affected_turfs(placement))
T.turf_flags |= NO_RUINS
var/turf/center = locate(placement.x + round(width/2),placement.y + round(height/2),placement.z)
new /obj/effect/landmark/ruin(center, src)
return center
/proc/seedRuins(list/z_levels = null, budget = 0, whitelist = list(/area/space), list/potentialRuins, clear_below = FALSE)
if(!z_levels || !z_levels.len)
WARNING("No Z levels provided - Not generating ruins")
return
var/list/whitelist_typecache = typecacheof(whitelist)
for(var/zl in z_levels)
var/turf/T = locate(1, 1, zl)
if(!T)
WARNING("Z level [zl] does not exist - Not generating ruins")
return
var/list/ruins = potentialRuins.Copy()
var/list/forced_ruins = list() //These go first on the z level associated (same random one by default) or if the assoc value is a turf to the specified turf.
var/list/ruins_available = list() //we can try these in the current pass
//Set up the starting ruin list
for(var/key in ruins)
var/datum/map_template/ruin/R = ruins[key]
if(R.cost > budget) //Why would you do that
continue
if(R.always_place)
forced_ruins[R] = -1
if(R.unpickable)
continue
ruins_available[R] = R.placement_weight
while(budget > 0 && (ruins_available.len || forced_ruins.len))
var/datum/map_template/ruin/current_pick
var/forced = FALSE
var/forced_z //If set we won't pick z level and use this one instead.
var/forced_turf //If set we place the ruin centered on the given turf
if(forced_ruins.len) //We have something we need to load right now, so just pick it
for(var/ruin in forced_ruins)
current_pick = ruin
if(isturf(forced_ruins[ruin]))
var/turf/T = forced_ruins[ruin]
forced_z = T.z //In case of chained ruins
forced_turf = T
else if(forced_ruins[ruin] > 0) //Load into designated z
forced_z = forced_ruins[ruin]
forced = TRUE
break
else //Otherwise just pick random one
current_pick = pick_weight(ruins_available)
var/placement_tries = forced_turf ? 1 : PLACEMENT_TRIES //Only try once if we target specific turf
var/failed_to_place = TRUE
var/target_z = 0
var/turf/placed_turf //Where the ruin ended up if we succeeded
outer:
while(placement_tries > 0)
placement_tries--
target_z = pick(z_levels)
if(forced_z)
target_z = forced_z
if(current_pick.always_spawn_with) //If the ruin has part below, make sure that z exists.
for(var/v in current_pick.always_spawn_with)
if(current_pick.always_spawn_with[v] == PLACE_BELOW)
var/turf/T = locate(1,1,target_z)
if(!SSmapping.get_turf_below(T))
if(forced_z)
continue outer
else
break outer
placed_turf = current_pick.try_to_place(target_z,whitelist_typecache,forced_turf,clear_below)
if(!placed_turf)
continue
else
failed_to_place = FALSE
break
//That's done remove from priority even if it failed
if(forced)
//TODO : handle forced ruins with multiple variants
forced_ruins -= current_pick
forced = FALSE
if(failed_to_place)
for(var/datum/map_template/ruin/R in ruins_available)
if(R.id == current_pick.id)
ruins_available -= R
log_world("Failed to place [current_pick.name] ruin.")
else
budget -= current_pick.cost
if(!current_pick.allow_duplicates)
for(var/datum/map_template/ruin/R in ruins_available)
if(R.id == current_pick.id)
ruins_available -= R
if(current_pick.never_spawn_with)
for(var/blacklisted_type in current_pick.never_spawn_with)
for(var/possible_exclusion in ruins_available)
if(istype(possible_exclusion,blacklisted_type))
ruins_available -= possible_exclusion
if(current_pick.always_spawn_with)
for(var/v in current_pick.always_spawn_with)
for(var/ruin_name in SSmapping.ruins_templates) //Because we might want to add space templates as linked of lava templates.
var/datum/map_template/ruin/linked = SSmapping.ruins_templates[ruin_name] //why are these assoc, very annoying.
if(istype(linked,v))
switch(current_pick.always_spawn_with[v])
if(PLACE_SAME_Z)
forced_ruins[linked] = target_z //I guess you might want a chain somehow
if(PLACE_LAVA_RUIN)
forced_ruins[linked] = pick(SSmapping.levels_by_trait(ZTRAIT_LAVA_RUINS))
if(PLACE_SPACE_RUIN)
forced_ruins[linked] = pick(SSmapping.levels_by_trait(ZTRAIT_SPACE_RUINS))
if(PLACE_DEFAULT)
forced_ruins[linked] = -1
if(PLACE_BELOW)
forced_ruins[linked] = SSmapping.get_turf_below(placed_turf)
if(PLACE_ISOLATED)
forced_ruins[linked] = SSmapping.get_isolated_ruin_z()
//Update the available list
for(var/datum/map_template/ruin/R in ruins_available)
if(R.cost > budget)
ruins_available -= R
log_world("Ruin loader finished with [budget] left to spend.")