From de0135b3c68522789212e23e0357fa552105b87a Mon Sep 17 00:00:00 2001
From: Martin Haye <m1@snyder-haye.com>
Date: Fri, 24 Jun 2016 08:46:40 -0700
Subject: [PATCH] Fix to not ask about loading unless an initial save game is
 present. Avoid double-locking the heap area.

---
 Platform/Apple/virtual/src/plasma/diskops.pla | 36 ++++++++++++++-----
 .../Apple/virtual/src/plasma/gameloop.pla     | 11 ++++--
 2 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/Platform/Apple/virtual/src/plasma/diskops.pla b/Platform/Apple/virtual/src/plasma/diskops.pla
index 45503e89..9bef9daf 100644
--- a/Platform/Apple/virtual/src/plasma/diskops.pla
+++ b/Platform/Apple/virtual/src/plasma/diskops.pla
@@ -258,10 +258,35 @@ def _loadGame()
 end
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-def _newOrLoadGame()
+def newGame()
   word playersModule, globalScriptsModule
+  initHeap(0) // initially empty heap
+  global = getGlobals()
+  playersModule = mmgr(QUEUE_LOAD, MODULE_GEN_PLAYERS<<8 | RES_TYPE_MODULE)
+  globalScriptsModule = mmgr(QUEUE_LOAD, MODULE_GEN_GLOBAL_SCRIPTS<<8 | RES_TYPE_MODULE)
+  mmgr(FINISH_LOAD, LEAVE_OPEN)
+  playersModule()=>makeInitialParty()
+  globalScriptsModule()=>sc_newGame()
+end
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+def gameExists()
+  open_filename = @game1_filename
+  open_buffer = $5000
+  if callMLI(MLI_OPEN, @open_params) > 0; return FALSE; fin
+  close_fileref = open_fileref
+  guaranteeMLI(MLI_CLOSE, @close_params)
+  return TRUE
+end
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+def _newOrLoadGame()
   byte key
 
+  if !gameExists()
+    newGame(); return 1
+  fin
+
   home()
   ^$c053
   ^$25 = 20
@@ -272,14 +297,7 @@ def _newOrLoadGame()
     ^$c052
     if key > $60; key = key - $20; fin
     if key == 'N'
-      initHeap(0) // initially empty heap
-      global = getGlobals()
-      playersModule = mmgr(QUEUE_LOAD, MODULE_GEN_PLAYERS<<8 | RES_TYPE_MODULE)
-      globalScriptsModule = mmgr(QUEUE_LOAD, MODULE_GEN_GLOBAL_SCRIPTS<<8 | RES_TYPE_MODULE)
-      mmgr(FINISH_LOAD, LEAVE_OPEN)
-      playersModule()=>makeInitialParty()
-      globalScriptsModule()=>sc_newGame()
-      return 1
+      newGame(); return 1
     elsif key == 'L' and loadInternal()
       return 0
     fin
diff --git a/Platform/Apple/virtual/src/plasma/gameloop.pla b/Platform/Apple/virtual/src/plasma/gameloop.pla
index 21f01353..0adacdf6 100644
--- a/Platform/Apple/virtual/src/plasma/gameloop.pla
+++ b/Platform/Apple/virtual/src/plasma/gameloop.pla
@@ -72,6 +72,7 @@ word triggerTbl
 
 word cmdTbl[96]   // ASCII $00..$5F
 byte frameLoaded = 0
+byte heapLocked = FALSE
 
 // Queue setMap / teleport, since otherwise script might be replaced while executing
 byte q_mapIs3D = 0
@@ -2062,9 +2063,13 @@ end
 def _initHeap(loadedSize)
   byte i
 
-  mmgr(SET_MEM_TARGET, HEAP_BOTTOM)
-  mmgr(REQUEST_MEMORY, HEAP_SIZE)
-  mmgr(LOCK_MEMORY,    HEAP_BOTTOM)
+  if !heapLocked
+    mmgr(SET_MEM_TARGET, HEAP_BOTTOM)
+    mmgr(REQUEST_MEMORY, HEAP_SIZE)
+    mmgr(LOCK_MEMORY,    HEAP_BOTTOM)
+    heapLocked = TRUE
+  fin
+
   if loadedSize <> 0
     mmgr(SET_MEM_TARGET, HEAP_BOTTOM + loadedSize)
   fin