summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2022-06-20 22:41:49 +0200
committerTor Andersson <tor@ccxvii.net>2022-11-17 12:53:18 +0100
commita9e07b9ec0a5e5f01406d9426e965181f27c12cd (patch)
tree76144e27ba312d4e2a391265ac34b5238eb4b0fe
parentc456686185da9fc5f4cb24a211e2e9623362e855 (diff)
downloadhammer-of-the-scots-a9e07b9ec0a5e5f01406d9426e965181f27c12cd.tar.gz
Don't use JSON for undo state.
-rw-r--r--rules.js52
1 files changed, 42 insertions, 10 deletions
diff --git a/rules.js b/rules.js
index 481f857..5ed6034 100644
--- a/rules.js
+++ b/rules.js
@@ -109,25 +109,54 @@ function remove_from_array(array, item) {
array.splice(i, 1)
}
-function clear_undo() {
- game.undo = []
+function deep_copy(original) {
+ if (Array.isArray(original)) {
+ let n = original.length
+ let copy = new Array(n)
+ for (let i = 0; i < n; ++i) {
+ let v = original[i]
+ if (typeof v === "object" && v !== null)
+ copy[i] = deep_copy(v)
+ else
+ copy[i] = v
+ }
+ return copy
+ } else {
+ let copy = {}
+ for (let i in original) {
+ let v = original[i]
+ if (typeof v === "object" && v !== null)
+ copy[i] = deep_copy(v)
+ else
+ copy[i] = v
+ }
+ return copy
+ }
}
function push_undo() {
- game.undo.push(JSON.stringify(game, (k,v) => {
- if (k === 'undo') return 0
- if (k === 'log') return v.length
- return v
- }))
+ let copy = {}
+ for (let k in game) {
+ let v = game[k]
+ if (k === "undo") continue
+ else if (k === "log") v = v.length
+ else if (typeof v === "object" && v !== null) v = deep_copy(v)
+ copy[k] = v
+ }
+ game.undo.push(copy)
}
function pop_undo() {
- let undo = game.undo
let save_log = game.log
- game = JSON.parse(undo.pop())
- game.undo = undo
+ let save_undo = game.undo
+ game = save_undo.pop()
save_log.length = game.log
game.log = save_log
+ game.undo = save_undo
+}
+
+function clear_undo() {
+ game.undo = []
}
function gen_action_undo(view) {
@@ -2890,6 +2919,9 @@ function make_battle_view() {
exports.setup = function (seed, scenario, options) {
game = {
seed: seed,
+ log: [],
+ undo: [],
+
attacker: {},
border_limit: {},
last_used: {},