From fc36f32e9769fc11c062f3a62d8291eea5a15591 Mon Sep 17 00:00:00 2001 From: John Cote Date: Mon, 30 Dec 2019 18:20:32 -0500 Subject: [PATCH] ai: fix some crashes --- otp/otpbase/PythonUtil.py | 6 +++++- toontown/building/DistributedBuildingMgrAI.py | 8 ++++---- toontown/building/SuitPlannerInteriorAI.py | 6 +++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/otp/otpbase/PythonUtil.py b/otp/otpbase/PythonUtil.py index df7c74c..c7e2775 100644 --- a/otp/otpbase/PythonUtil.py +++ b/otp/otpbase/PythonUtil.py @@ -1,7 +1,7 @@ import builtins import sys -__all__ = ['enumerate', 'nonRepeatingRandomList', 'describeException', 'pdir', 'choice'] +__all__ = ['enumerate', 'nonRepeatingRandomList', 'describeException', 'pdir', 'choice', 'cmp'] if not hasattr(builtins, 'enumerate'): def enumerate(L): @@ -152,7 +152,11 @@ def isClient(): return False return True +def cmp(a, b): + return (a > b) - (a < b) + builtins.pdir = pdir builtins.isClient = isClient builtins.choice = choice +builtins.cmp = cmp diff --git a/toontown/building/DistributedBuildingMgrAI.py b/toontown/building/DistributedBuildingMgrAI.py index 9a69532..7e0fc82 100644 --- a/toontown/building/DistributedBuildingMgrAI.py +++ b/toontown/building/DistributedBuildingMgrAI.py @@ -253,7 +253,7 @@ class DistributedBuildingMgrAI: if os.path.exists(working): os.remove(working) os.rename(fileName, working) - file = open(working, 'w') + file = open(working, 'wb') file.seek(0, 2) self.saveTo(file, block) file.close() @@ -267,7 +267,7 @@ class DistributedBuildingMgrAI: backup = fileName + self.backupExtension if os.path.exists(fileName): os.rename(fileName, backup) - file = open(fileName, 'w') + file = open(fileName, 'wb') file.seek(0) self.saveTo(file) file.close() @@ -291,12 +291,12 @@ class DistributedBuildingMgrAI: def load(self): fileName = self.getFileName() try: - file = open(fileName + self.backupExtension, 'r') + file = open(fileName + self.backupExtension, 'rb') if os.path.exists(fileName): os.remove(fileName) except IOError: try: - file = open(fileName, 'r') + file = open(fileName, 'rb') except IOError: return {} diff --git a/toontown/building/SuitPlannerInteriorAI.py b/toontown/building/SuitPlannerInteriorAI.py index ab2f6a3..982827b 100644 --- a/toontown/building/SuitPlannerInteriorAI.py +++ b/toontown/building/SuitPlannerInteriorAI.py @@ -1,5 +1,5 @@ from otp.ai.AIBaseGlobal import * -import random +import random, functools from toontown.suit import SuitDNA from direct.directnotify import DirectNotifyGlobal from toontown.suit import DistributedSuitAI @@ -31,7 +31,7 @@ class SuitPlannerInteriorAI: for currChance in range(num): joinChances.append(random.randint(1, 100)) - joinChances.sort(cmp) + joinChances.sort(key=functools.cmp_to_key(cmp)) return joinChances def _genSuitInfos(self, numFloors, bldgLevel, bldgTrack): @@ -120,7 +120,7 @@ class SuitPlannerInteriorAI: bossLvlRange = bldgInfo[SuitBuildingGlobals.SUIT_BLDG_INFO_BOSS_LVLS] newLvl = random.randint(bossLvlRange[0], bossLvlRange[1]) lvlList.append(newLvl) - lvlList.sort(cmp) + lvlList.sort(key=functools.cmp_to_key(cmp)) self.notify.debug('LevelList: ' + repr(lvlList)) return lvlList