Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Globals
- ##################################################
- var screenLines = 14
- #var screenLines = 20
- string gArrayPrefix = "aRR"
- array memoryKeyPrefix = "c07_"
- array memoryKeyPeopleSorted = memoryKeyPrefix + "people_sorted"
- array memoryKeyPeople = memoryKeyPrefix + "people"
- array memoryKeyAllRooms = memoryKeyPrefix + "all_rooms"
- array memoryKeyAboutToConnect = memoryKeyPrefix + "a2c"
- array memoryKeyMakingSlurpSelection = memoryKeyPrefix + "mss"
- array gPeopleAsKeys = []
- array gAllRooms = []
- array memoryKeyInvalidHashes = memoryKeyPrefix + "stored_hashes4"
- array gStoredHashKeys = []
- number cUnStored = 0
- number cStoredConnectable = 1
- number cStoredNotConnectable = 2
- number cStoredInvalid = 3
- # Program
- ##################################################
- ClearText()
- loop
- Main()
- end
- # Control Functions
- ##################################################
- void Main()
- CheckPreviousRunForErrors()
- var actions = ["Find person", "Slurp"]
- MergeSortString(actions)
- string action = ScreenSelect(screenLines, LeadingText("Choose action"), [], actions)
- if action == "Find person"
- FindPerson()
- else if action == "Slurp"
- PickSlurp()
- end
- end
- void CheckPreviousRunForErrors()
- if (HasAboutToConnect())
- # Clear lower priority resume flag
- ClearMakingSlurpSelection()
- array previousRun = GetAboutToConnect()
- string room = previousRun[0]
- string item = previousRun[1]
- number hash = previousRun[2]
- array title = []
- Append(title, "Detected previous run failed.")
- Append(title, " Room: " + room)
- Append(title, " Item: " + item)
- Append(title, "")
- Append(title, "Would you like to ignore item from now on?")
- Append(title, DupString(Longest(title), "-"))
- string ignoreAndContinueText = "Resume Slurp(), ignoring item"
- string forgetAndContinueText = "Resume Slurp(), retrying item"
- string ignoreText = "Restart, ignoring item"
- string forgetText = "Restart"
- var actions = [ignoreAndContinueText, forgetAndContinueText, ignoreText, forgetText]
- string action = ScreenSelect(screenLines, title, [], actions)
- ClearAboutToConnect()
- if action == ignoreText
- StoreRoomItem(room, hash, cStoredInvalid)
- else if action == ignoreAndContinueText
- StoreRoomItem(room, hash, cStoredInvalid)
- SlurpToRoom(room)
- else if action == forgetAndContinueText
- SlurpToRoom(room)
- end
- else if (WasMakingSlurpSelection())
- string room = SlurpSelectionRoom()
- ClearMakingSlurpSelection()
- Print("Resuming previous slurp selection...")
- Sleep(3)
- SlurpToRoom(room)
- end
- end
- var GetPeople(bool sortRequired)
- bool sorted = HasMemory(memoryKeyPeopleSorted)
- array people = []
- if HasMemoryArray(memoryKeyPeople)
- people = LoadMemoryArray(memoryKeyPeople)
- else
- var s = PersonServer()
- Print("Retrieving people...")
- people = s.GetPeople()
- SaveMemoryArray(memoryKeyPeople, people)
- Print("People retrieved...")
- DisconnectAll()
- end
- if sortRequired == True && sorted == False
- Print("Sorting people(" + Count(people) + ")...")
- MergeSortString(people)
- SaveMemoryArray(memoryKeyPeople, people)
- SaveMemory(memoryKeyPeopleSorted, "y")
- end
- return people
- end
- array GetPeopleAsKeys()
- if (Count(gPeopleAsKeys) == 0)
- var people = GetPeople(False)
- loop person in people
- gPeopleAsKeys[person] = 1
- end
- end
- return gPeopleAsKeys
- end
- var PersonServer()
- # GetPeople :: [String]
- # GetPosition :: String -> String
- # "ROOMNAME, coordinate: (X,Y)
- # GetAction :: PersonName -> String
- # GetRoom :: PersonName -> String
- # GetThingsInRoom :: RoomName -> [String]
- # GetAllRooms :: [String]
- return Connect("PoliceOfficeInterior_MinistryOfficeWorkstationComputer_1")
- end
- string GetPosition(string person)
- var s = PersonServer()
- Print("Retrieving position...")
- string position = s.GetPosition(person)
- Print("Position retrieved...")
- DisconnectAll()
- return position
- end
- string GetAction(string person)
- var s = PersonServer()
- Print("Retrieving action...")
- string action = s.GetAction(person)
- Print("Action retrieved...")
- DisconnectAll()
- return action
- end
- string GetRoom(string person)
- var s = PersonServer()
- Print("Retrieving room...")
- string room = s.GetRoom(person)
- Print("Room retrieved...")
- DisconnectAll()
- return room
- end
- array GetAllRooms()
- if (Count(gAllRooms) == 0)
- if HasMemoryArray(memoryKeyAllRooms)
- gAllRooms = LoadMemoryArray(memoryKeyAllRooms)
- else
- var s = PersonServer()
- Print("Getting rooms...")
- gAllRooms = s.GetAllRooms()
- Print("Rooms retrieved...")
- DisconnectAll()
- SaveMemoryArray(memoryKeyAllRooms, gAllRooms)
- end
- end
- return gAllRooms
- end
- array GetThingsInRoom(string room)
- var s = PersonServer()
- array things = s.GetThingsInRoom(room)
- DisconnectAll()
- return things
- end
- void FindPerson()
- array people = GetPeople(True)
- string person = ScreenSelect(screenLines, LeadingText("People"), ["q"], people)
- if (person == "q")
- return
- end
- array title = []
- array positionInfo = ParsePosition(GetPosition(person))
- Append(title, "Person: " + person)
- Append(title, " Room: " + positionInfo[0])
- Append(title, " Coord: " + positionInfo[1] + ", " + positionInfo[2])
- Append(title, " Action: " + GetAction(person))
- Append(title, DupString(Longest(title), "-"))
- array actions = ["Restart", "Slurp to same room"]
- string action = ScreenSelect(screenLines, title, [], actions)
- if action == "Slurp to same room"
- SlurpToRoom(GetRoom(person))
- end
- end
- bool IsAllowedRoom(string room)
- if (IsSuffixOf("_inventory", room))
- return False
- end
- return True
- end
- void PickSlurp()
- array areas = ["BarYvonne", "BurrowsApartments", "BurrowsNorth", "Casino", "DorisGardens", "Factory", "FinanceArea", "Foley", "HarborEast", "HarborNorth", "HarborSouth", "HarborWest", "Hotel", "Internet", "Mines", "Ministry", "PixiesApartment", "Plaza", "PoliceOffice", "PoorDesolateBuilding1", "ShoeStore", "SodaStorage", "TownHall", "YulianApartment"]
- string area = ScreenSelect(screenLines, LeadingText("Choose area"), ["q"], areas)
- if (area == "q")
- return
- end
- array rooms = GetAllRooms()
- array selectRooms = []
- loop room in rooms
- if IsPrefixOf(area, room) && IsAllowedRoom(room)
- Append(selectRooms, room)
- end
- end
- string selectedRoom = selectRooms[0]
- if(Count(selectRooms) > 1)
- Print("Sorting rooms(" + Count(selectRooms) + ")...")
- MergeSortString(selectRooms)
- selectedRoom = ScreenSelect(screenLines, LeadingText("Choose room"), [], selectRooms)
- end
- SlurpToRoom(selectedRoom)
- end
- string MakeNicePrefix(string prefix, string str)
- return StripPrefix(":::", StripPrefix("____", StripPrefix(prefix, str)))
- end
- void SlurpToRoom(string selectedRoom)
- # Wrap with resume as CPUs have tendency to lock up.
- MakingSlurpSelection(selectedRoom)
- number cDisplayLength = 27
- ClearText()
- Print("Room: " + selectedRoom)
- Print("Retrieving items in room...")
- array things = GetThingsInRoom(selectedRoom)
- array peopleAsKeys = GetPeopleAsKeys()
- Print("Culling non-connectable items. (" + Count(things) + ")")
- array connectablesByPrefix = []
- array thingsByPrefix = []
- loop thing in things
- number thingHash = CreateRoomItemHash(thing)
- number storedHashValue = GetStoredRoomItem(selectedRoom, thingHash)
- if (storedHashValue == cStoredConnectable)
- connectablesByPrefix[Shorten(cDisplayLength, "*" + MakeNicePrefix(selectedRoom, thing))] = thing
- else if (storedHashValue == cStoredNotConnectable)
- thingsByPrefix[Shorten(cDisplayLength, MakeNicePrefix(selectedRoom, thing))] = thing
- else if (storedHashValue != cStoredInvalid)
- if (HasIndex(peopleAsKeys, thing) == False && IsDangerous(thing) == False)
- ClearText()
- Print("Checking item:")
- Print(" " + thing)
- Print("")
- DisconnectAll()
- # Some items cause the program to crash. Skip them next time if so.
- AboutToConnect(selectedRoom, thing, thingHash)
- var c = Connect(thing)
- if (Count(GetConnections()) > 0)
- bool hasNameFunc = c.HasFunction("Name") || c.HasFunction("GetName")
- if (c.HasFunction("Connect") && hasNameFunc)
- connectablesByPrefix[Shorten(cDisplayLength, "*" + MakeNicePrefix(selectedRoom, thing))] = thing
- StoreRoomItem(selectedRoom, thingHash, cStoredConnectable)
- else if IsIgnorable(thing) == False
- thingsByPrefix[Shorten(cDisplayLength, MakeNicePrefix(selectedRoom, thing))] = thing
- StoreRoomItem(selectedRoom, thingHash, cStoredNotConnectable)
- else
- # Store hash so we skip it next time
- StoreRoomItem(selectedRoom, thingHash, cStoredInvalid)
- end
- end
- # Program didn't crash :)
- ClearAboutToConnect()
- end
- end
- end
- DisconnectAll()
- array thingsByPrefixKeys = GetIndexes(thingsByPrefix)
- array connectablesByPrefixKeys = GetIndexes(connectablesByPrefix)
- if Count(thingsByPrefixKeys) > 150
- Print("Skipping sort due to too many items! (" + Count(thingsByPrefixKeys)+ ")")
- else
- Print("Sorting things(" + Count(thingsByPrefixKeys) + ")...")
- MergeSortString(thingsByPrefixKeys)
- end
- MergeSortString(connectablesByPrefixKeys)
- ArrayAppend(connectablesByPrefixKeys, thingsByPrefixKeys)
- string selectedPrefix = ScreenSelect(screenLines, LeadingText("Choose thing in " + selectedRoom), ["q"], connectablesByPrefixKeys)
- ClearMakingSlurpSelection()
- if (selectedPrefix == "q")
- return
- end
- string selectedThing
- if (HasIndex(thingsByPrefix, selectedPrefix))
- selectedThing = thingsByPrefix[selectedPrefix]
- else
- selectedThing = connectablesByPrefix[selectedPrefix]
- end
- DisconnectAll()
- var server = Connect(selectedThing)
- if server.HasFunction("Connect")
- server.Connect(Name())
- end
- Slurp()
- end
- string ScreenSelect(number height, array niceLines, array extraOptions, array values)
- number niceLinesLength = Count(niceLines)
- string selection
- number currentPage = 0
- array pages = PartitionBy((height - niceLinesLength) - 1, values)
- number pagesLength = Count(pages)
- if (pagesLength == 0)
- Input("Nothing to select!")
- return ""
- end
- loop
- bool firstPage = currentPage == 0
- bool lastPage = currentPage == pagesLength - 1
- ClearText()
- loop line in niceLines
- Print(line)
- end
- array byLineNumber = []
- var lineNumber = 1
- loop line in pages[currentPage]
- byLineNumber[lineNumber+""] = line
- Print(lineNumber + ": " + line)
- lineNumber += 1
- end
- selection = Input(FormatSelectionText(currentPage, pagesLength, extraOptions))
- if (Elem(selection, extraOptions))
- return selection
- else if (lastPage == True && selection == "")
- currentPage = 0
- else if ((selection == "" || selection == "n") && lastPage == False)
- currentPage += 1
- else if (firstPage == False && selection == "p")
- currentPage -= 1
- else if (HasIndex(byLineNumber, selection))
- return byLineNumber[selection]
- end
- end
- end
- string FormatSelectionText(number currentPage, number pages, array extraOptions)
- bool firstPage = currentPage == 0
- bool lastPage = currentPage == pages - 1
- string pageDetail = "(" + (currentPage+1) + "/" + pages + ")"
- string ret = ""
- if (pages == 1)
- ret = "Selection"
- else if (firstPage)
- ret = pageDetail + " #/n"
- else if (lastPage)
- ret = pageDetail + " #/p"
- else
- ret = pageDetail + " #/n/p"
- end
- loop item in extraOptions
- ret += "/" + item
- end
- return ret + ": "
- end
- bool IsDangerous(string targetC)
- string target = ToLowerCase(targetC)
- if (Contains(target, "diskette"))
- return true
- else if (Contains(target, "memory"))
- return true
- else
- return False
- end
- end
- bool IsIgnorable(string targetC)
- string target = ToLowerCase(targetC)
- if (Contains(target, "beer"))
- return true
- else if (Contains(target, "glass"))
- return true
- else if (Contains(target, "trash"))
- return true
- else if (Contains(target, "seat"))
- return true
- else if (Contains(target, "toilet"))
- return true
- else if (Contains(target, "cigar"))
- return true
- else if (Contains(target, "wellspringsoda"))
- return true
- else
- return False
- end
- end
- number CreateRoomItemHash(string str)
- return Hash(str, 0, 27, 591749)
- end
- # Helper Functions
- ##################################################
- void PartitionBy(number partionSize, array values)
- array ret = []
- array current = []
- number currentSize = 0
- loop val in values
- currentSize = currentSize + 1
- Append(current, val)
- if(currentSize == partionSize)
- Append(ret, current)
- current = []
- currentSize = 0
- end
- end
- # Get last partition if not empty
- if(Count(current) > 0)
- Append(ret, current)
- end
- return ret
- end
- var ByIndex(array arr, number index)
- number count = 0
- loop x in arr
- if(count == index)
- return x
- end
- count = count + 1
- end
- end
- # String Helper Functions
- ##################################################
- bool Contains(string target, string sub)
- number targetLen = Count(target)
- number subLen = Count(sub)
- if (targetLen < subLen)
- return False
- end
- number endPos = targetLen - subLen
- loop pos from 0 to endPos
- loop subPos from 0 to (subLen - 1)
- if target[pos+subPos] != sub[subPos]
- break
- end
- if subPos == (subLen - 1)
- return True
- end
- end
- end
- return False
- end
- string Shorten(number limit, string str)
- string ret
- number count = 0
- loop c in str
- if count >= limit
- break
- end
- ret += c
- count += 1
- end
- return ret
- end
- string StripPrefix(string prefix, string str)
- var pos = 0
- loop c in prefix
- if c != str[pos]
- break
- end
- pos += 1
- end
- string ret = ""
- loop i in Range(pos, Count(str) - 1)
- ret += str[i]
- end
- return ret
- end
- number CompareString(string a_, string b_)
- string a = ToLowerCase(a_)
- string b = ToLowerCase(b_)
- var aLen = Count(a)
- var bLen = Count(b)
- var pos = 0
- loop
- if pos == aLen && pos == bLen
- return 0
- else if pos == aLen
- return -1
- else if pos == bLen
- return 1
- else
- var diff = CharToInt(a[pos]) - CharToInt(b[pos])
- if diff != 0
- return diff
- else
- pos += 1
- end
- end
- end
- end
- bool IsUpperCase(var c)
- return CharToInt(c) >= -32 && CharToInt(c) <= -7
- end
- string ToLowerCase(var text)
- string res = ""
- loop c in text
- if IsUpperCase(c)
- res += IntToChar(CharToInt(c) + 32)
- else
- res += c
- end
- end
- return res
- end
- string DupString(number times, string str)
- string ret = ""
- loop i from 1 to times
- ret = ret + str
- end
- return ret
- end
- array LeadingText(string title)
- return [title, DupString(Count(title), "-")]
- end
- number Longest(array arr)
- var count = 0
- loop str in arr
- var strCount = Count(str)
- if strCount > count
- count = strCount
- end
- end
- return count
- end
- bool IsPrefixOf(string prefix, string str)
- var prefixSize = Count(prefix)
- var strSize = Count(str)
- if strSize < prefixSize
- return False
- end
- var i = 0
- loop c in prefix
- if c != str[i]
- return False
- end
- i += 1
- end
- return True
- end
- bool IsSuffixOf(string suffix, string str)
- var suffixSize = Count(suffix)
- var strSize = Count(str)
- if strSize < suffixSize
- return False
- end
- var difference = strSize - suffixSize
- var i = 0
- loop c in suffix
- if c != str[i+difference]
- return False
- end
- i += 1
- end
- return True
- end
- array ParsePosition(string position)
- number rule = 1
- string working = ""
- string room = "Unknown"
- number x = -1
- number y = -1
- loop c in position
- if rule == 1
- if c == ":"
- rule = 2
- end
- else if rule == 2
- rule = 3
- else if rule == 3
- if c == " " or c == ","
- rule = 4
- room = working
- working = ""
- else
- working = working + c
- end
- else if rule == 4
- if c == "("
- rule = 5
- end
- else if rule == 5
- if c == ","
- rule = 6
- x = working
- working = ""
- else
- working = working + c
- end
- else if rule == 6
- if c == ")"
- rule = 7
- x = working
- working = ""
- else
- working = working + c
- end
- end
- end
- return [room,x,y]
- end
- # To prevent conversion to float maintain a*p < 2^31 - 50
- number Hash(string str, number initialVal, number a, number p)
- number h = initialVal
- number fix = 0 - CharToInt("1")
- loop c in str
- h = Mod((h*a) + CharToInt(c) + fix, p)
- end
- return h
- end
- # Function to test if values for a hash are perfect for the array
- void TestHash(array strs, number initialVal, number n, number p)
- array dups = []
- array hashSet = []
- number count = 0
- loop str in strs
- if (HasIndex(dups, str) == False)
- dups[str] = 1
- Print("Hashing: " + str)
- var hash = Hash(str, initialVal, n, p)
- Print("Got hash: " + hash)
- if (HasIndex(hashSet, hash))
- Print("Failure!")
- Print(count + " of " + Count(strs))
- return
- end
- hashSet[hash] = 1
- count += 1
- end
- end
- Print("Success!")
- end
- # Array Helper Functions
- ##################################################
- void ArrayAppend(array target, array to_append)
- loop val in to_append
- Append(target, val)
- end
- end
- bool Elem(var item, array arr)
- loop val in arr
- if val == item
- return True
- end
- end
- return False
- end
- # Sort Functions
- ##################################################
- void MergeSortString(array arr)
- array work = []
- if Count(arr) > 0
- TopDownSplitMergeString(arr, 0, Count(arr), work)
- end
- end
- void TopDownSplitMergeString(array a, number iBegin, number iEnd, array b)
- if(iEnd - iBegin < 2)
- return
- end
- number iMiddle = Int((iEnd + iBegin) / 2)
- TopDownSplitMergeString(a, iBegin, iMiddle, b)
- TopDownSplitMergeString(a, iMiddle, iEnd, b)
- TopDownMergeString(a, iBegin, iMiddle, iEnd, b)
- CopyArray(b, iBegin, iEnd, a)
- end
- void TopDownMergeString(array a, number iBegin, number iMiddle, number iEnd, array b)
- var i0 = iBegin
- var i1 = iMiddle
- loop j from iBegin to (iEnd - 1)
- # Why split this up? "if" parameters are broken when this was written
- var postCheck = i1 >= iEnd;
- if postCheck == False
- postCheck = CompareString(a[i0], a[i1]) < 1
- end
- if (i0 < iMiddle && postCheck)
- b[j] = a[i0]
- i0 += 1
- else
- b[j] = a[i1]
- i1 += 1
- end
- end
- end
- void CopyArray(array b, number iBegin, number iEnd, array a)
- loop k in Range(iBegin, iEnd - 1)
- a[k] = b[k]
- end
- end
- # Memory Functions
- ##################################################
- bool HasMemoryArray(string key)
- return HasMemory(gArrayPrefix + key)
- end
- void SaveMemoryArray(string key, array arr)
- string serialized = ""
- var i = 0
- loop val in arr
- if (i == 0)
- serialized = val
- else
- serialized = serialized + "|" + val
- end
- i += 1
- end
- return SaveMemory(gArrayPrefix + key, serialized)
- end
- array LoadMemoryArray(string key)
- string serialized = LoadMemory(gArrayPrefix + key)
- array ret = []
- string current = ""
- loop c in serialized
- if c == "|"
- Append(ret, current)
- current = ""
- else
- current = current + c
- end
- end
- # Get last
- Append(ret, current)
- return ret
- end
- string StoredRoomItemsMemoryKey(string room)
- return memoryKeyInvalidHashes + "_" + room
- end
- array GetStoredRoomItems(string room)
- if (HasIndex(gStoredHashKeys, room) == False)
- gStoredHashKeys[room] = []
- array currentRoom = gStoredHashKeys[room]
- string memoryKey = StoredRoomItemsMemoryKey(room)
- if HasMemory(memoryKey)
- string serialized = LoadMemory(memoryKey)
- string tmp = ""
- number currentHash
- loop c in serialized
- if (c == "|")
- currentHash = tmp
- tmp = ""
- else if (c == "&")
- number currentValue = tmp
- tmp = ""
- currentRoom[currentHash] = currentValue
- else
- tmp += c
- end
- end
- end
- end
- return gStoredHashKeys[room]
- end
- void StoreRoomItem(string room, number hash_, number value)
- number hash = Int(hash_)
- GetStoredRoomItems(room)
- array currentRoom = gStoredHashKeys[room]
- currentRoom[hash] = value
- string serialized = ""
- loop h in GetIndexes(currentRoom)
- serialized += (h + "|" + currentRoom[h] + "&")
- end
- SaveMemory(StoredRoomItemsMemoryKey(room), serialized)
- end
- # For use with CreateRoomItemHash(string str)
- number GetStoredRoomItem(string room, number hash_)
- number hash = Int(hash_)
- array storedHashes = GetStoredRoomItems(room)
- if (HasIndex(storedHashes, hash) == False)
- return cUnStored
- else
- return storedHashes[hash]
- end
- end
- void AboutToConnect(string room, string item, number hash)
- SaveMemory(memoryKeyAboutToConnect, room + "|" + item + "|" + hash)
- end
- void ClearAboutToConnect()
- EraseMemory(memoryKeyAboutToConnect)
- end
- bool HasAboutToConnect()
- return HasMemory(memoryKeyAboutToConnect)
- end
- array GetAboutToConnect()
- string serialized = LoadMemory(memoryKeyAboutToConnect)
- string room = ""
- string item = ""
- number hash = 0
- string tmp = ""
- number state = 0
- loop c in serialized
- if (c == "|")
- if (state == 0)
- room = tmp
- else if (state == 1)
- item = tmp
- end
- tmp = ""
- state += 1
- else
- tmp += c
- end
- end
- hash = tmp
- return [room, item, hash]
- end
- void MakingSlurpSelection(string room)
- SaveMemory(memoryKeyMakingSlurpSelection, room)
- end
- void ClearMakingSlurpSelection()
- EraseMemory(memoryKeyMakingSlurpSelection)
- end
- bool WasMakingSlurpSelection()
- return HasMemory(memoryKeyMakingSlurpSelection)
- end
- array SlurpSelectionRoom()
- return LoadMemory(memoryKeyMakingSlurpSelection)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement