|
| 1 | +/proc/instantiate_serialized_data(load_z, requestor, list/instance_map, entries_decay_at, entry_decay_weight) |
| 2 | + |
| 3 | + var/list/nested_instances = list() |
| 4 | + var/list/instanced_areas = list() |
| 5 | + var/list/created_data = list() |
| 6 | + |
| 7 | + LAZYINITLIST(instance_map) |
| 8 | + |
| 9 | + to_world_log("Finalising load of [length(instance_map)] instance\s for level '[requestor]'.") |
| 10 | + for(var/uid in instance_map) |
| 11 | + |
| 12 | + var/list/instance_data = instance_map[uid] |
| 13 | + try |
| 14 | + |
| 15 | + var/raw_load_path = instance_data[nameof(/datum::type)] |
| 16 | + var/load_path = ispath(raw_load_path, /datum) ? raw_load_path : text2path(raw_load_path) |
| 17 | + if(!ispath(load_path, /datum)) |
| 18 | + error("[requestor]: attempted to load persistent instance with invalid or non-/datum type '[raw_load_path]'") |
| 19 | + continue |
| 20 | + |
| 21 | + var/datum/created_instance |
| 22 | + |
| 23 | + // Instance is a /datum. |
| 24 | + // Just pass the data in and assume the datum type knows what to do with it. |
| 25 | + if(!ispath(load_path, /atom) && ispath(load_path, /datum)) |
| 26 | + created_instance = new load_path(instance_data) |
| 27 | + created_data += created_instance |
| 28 | + else |
| 29 | + var/list/spawn_data = instance_data[nameof(/atom/movable::loc)] |
| 30 | + if(spawn_data) |
| 31 | + |
| 32 | + if(isnull(spawn_data) || length(spawn_data) < 3) |
| 33 | + error("[requestor]: attempted to load persistent instance with malformed loc.") |
| 34 | + continue |
| 35 | + |
| 36 | + // Instance has a world coordinate. |
| 37 | + if(islist(spawn_data)) |
| 38 | + var/turf/spawn_loc = locate(spawn_data[1], spawn_data[2], isnull(load_z) ? spawn_data[3] : load_z) |
| 39 | + if(!istype(spawn_loc)) |
| 40 | + error("[requestor]: attempted to load persistent instance but could not find spawn loc.") |
| 41 | + continue |
| 42 | + if(ispath(load_path, /turf)) |
| 43 | + if(spawn_loc.type == load_path) |
| 44 | + created_instance = spawn_loc |
| 45 | + else |
| 46 | + created_instance = spawn_loc.ChangeTurf(load_path) |
| 47 | + |
| 48 | + // TODO: Areas will need bespoke handling for non-subtype-related persistence (blueprint renaming etc). |
| 49 | + else if(ispath(load_path, /area)) |
| 50 | + var/area/area = instanced_areas[load_path] |
| 51 | + if(!area) |
| 52 | + area = new load_path(null) |
| 53 | + instanced_areas[load_path] = area |
| 54 | + ChangeArea(spawn_loc, area) |
| 55 | + |
| 56 | + else if(ispath(load_path, /atom)) |
| 57 | + created_instance = new load_path(spawn_loc) |
| 58 | + spawn_loc._contents_were_modified = TRUE // ensure |
| 59 | + else |
| 60 | + error("[requestor]: attempted to instantiate unimplemented path '[load_path]'.") |
| 61 | + continue |
| 62 | + |
| 63 | + // Instance is inside another instance; implies/requires /atom/movable |
| 64 | + else if(istext(spawn_data)) |
| 65 | + if(!ispath(load_path, /atom/movable)) |
| 66 | + error("[requestor]: tried to spawn non-movable [load_path] inside an instance.") |
| 67 | + continue |
| 68 | + created_instance = new load_path |
| 69 | + nested_instances[created_instance] = spawn_data |
| 70 | + |
| 71 | + else |
| 72 | + error("[requestor]: attempted to load persistent instance with malformed loc.") |
| 73 | + continue |
| 74 | + |
| 75 | + else |
| 76 | + // Should we just go ahead and do this to create atoms in nullspace? |
| 77 | + // Would we ever want to track an atom in nullspace via level persistence? |
| 78 | + error("[requestor]: attempted to load non-/datum persistent instance with no spawn loc.") |
| 79 | + |
| 80 | + if(istype(created_instance)) |
| 81 | + LAZYSET(., uid, created_instance) |
| 82 | + if(isatom(created_instance)) |
| 83 | + var/atom/atom = created_instance |
| 84 | + atom.__deserialization_payload = instance_data |
| 85 | + SSatoms.deserialized_atoms[uid] = atom |
| 86 | + if(!isnull(entries_decay_at) && !isnull(entry_decay_weight)) |
| 87 | + created_instance.HandlePersistentDecay(entries_decay_at, entry_decay_weight) |
| 88 | + |
| 89 | + catch(var/exception/E) |
| 90 | + log_error("Exception during persistent instance load - [islist(instance_data) ? json_encode(instance_data) : "no instance data"]: [EXCEPTION_TEXT(E)]") |
| 91 | + |
| 92 | + // Atoms use SSatoms for this, datums don't go through SSatoms so need to do it here. |
| 93 | + for(var/datum/instance in created_data) |
| 94 | + instance.DeserializePostInit(.) |
| 95 | + |
| 96 | + // Resolve any loc references to instances. |
| 97 | + for(var/atom/movable/atom as anything in nested_instances) |
| 98 | + var/nested_atom_id = nested_instances[atom] |
| 99 | + var/atom/nested_atom = .[nested_atom_id] |
| 100 | + if(!istype(nested_atom)) |
| 101 | + error("[requestor]: could not resolve instance ref [nested_atom_id] to instance.") |
| 102 | + continue |
| 103 | + atom.forceMove(nested_atom) |
| 104 | + nested_atom.contents_were_modified() |
| 105 | + |
| 106 | + // Now that everything is loaded and placed, clear out anything that should not be present on the turfs we've loaded. |
| 107 | + for(var/uid in SSatoms.deserialized_atoms) |
| 108 | + var/turf/turf = SSatoms.deserialized_atoms[uid] |
| 109 | + if(!istype(turf)) |
| 110 | + continue |
| 111 | + for(var/atom/thing in turf) |
| 112 | + if(!thing.simulated) |
| 113 | + continue |
| 114 | + if(!isnull(thing.__deserialization_payload)) |
| 115 | + continue |
| 116 | + qdel(thing) |
| 117 | + |
| 118 | + to_world_log("[requestor] loaded [length(.)] persistent instance\s.") |
| 119 | + |
| 120 | +/proc/apply_serde_message_decay(_message, _age, _decay_weight, _decay_at) |
| 121 | + var/static/list/decayed_chars = list(".",",","-","'","\\","/","\"",":",";") |
| 122 | + if(_age < _decay_at || isnull(_message)) |
| 123 | + return _message |
| 124 | + . = "" |
| 125 | + for(var/i = 1 to length(_message)) |
| 126 | + var/char = copytext(_message, i, i + 1) |
| 127 | + if(prob(round(_age * _decay_weight))) |
| 128 | + if(prob(99)) |
| 129 | + . += pick(decayed_chars) |
| 130 | + else |
| 131 | + . += char |
0 commit comments