Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "KeyCodeSolved" "InternalState0""KeyCodeFailed" "InternalState1""ReadyForScramble" "InternalState2""ConnectKey1" "InternalState3""ConnectKey2" "InternalState4""ConnectKey3" "InternalState5""ConnectKey4" "InternalState6""ConnectKey5" "InternalState7""ConnectKey6" "InternalState8""ConnectKey7" "InternalState9""ConnectKey8" "InternalState10""ConnectTextField1" "InternalState11""ConnectTextField2" "InternalState12""ConnectTextField3" "InternalState13""ConnectTextField4" "InternalState14""ConnectCodeField1" "InternalState15""ConnectCodeField2" "InternalState16""ConnectCodeField3" "InternalState17""ConnectCodeField4" "InternalState18""TestForXRay" "InternalState19" "KeyPressed1" "InternalMessage0""KeyPressed2" "InternalMessage1""KeyPressed3" "InternalMessage2""KeyPressed4" "InternalMessage3""KeyPressed5" "InternalMessage4""KeyPressed6" "InternalMessage5""KeyPressed7" "InternalMessage6""KeyPressed8" "InternalMessage7""KeyCodeCancelled" "InternalMessage10""GenerateKeyCode" "InternalMessage11""RandomizeKeys" "InternalMessage12""PrintDebugInfo" "InternalMessage13""PlayerInXRay" "InternalMessage14""PlayerNotInXRay" "InternalMessage15"
- -- this code is run when the script is loaded since its outside all functions
- -- allows you to initialize things
- DEBUG_LUA_PIRATEXRAYPAD = FALSE
- MAX_KEYS_IN_SEQUENCE = 4
- NUMBER_OF_KEYS = 8
- -- gKeyCodeSequence has an extra buffer 0 to be read as the final Enter (5th entry) needed in a code of 4 keys
- gKeyCodeSequence = { 0, 0, 0, 0, 0 }
- -- gKeysPressedList tracks which keys have been pressed on the pad. 1 = UNPRESSED, 0 = PRESSED (its set up this way so RandomWeightedChance can be used on the list)
- gKeysPressedList = { 1, 1, 1, 1, 1, 1, 1, 1 }
- -- gKeyValuesList tracks the mapping of displayed keys to the actual key it represents. Starts out unscrambled.
- gKeyValuesList = { 1, 2, 3, 4, 5, 6, 7, 8 }
- -- which entry of the code sequence we're at
- gCurrentCodeIndex = 1
- -- true until an incorrect key is pressed
- gIsEnteredCodeCorrect = 1
- -- variable used to track what key the player pressed, so we can go outside the LUA object, test for X-Ray with PlayerProxy, then come back in from the Proxy
- -- and check the actual button pressed
- gCurrentKeyPressed = 0
- -- list of connected text fields for displaying pressed keys
- gConnectedTextFieldObjIDs = { 0, 0, 0, 0 }
- -- list of connected Key MultiModelActors, for swithcing out the look of keys during randomization
- gConnectedKeyActorObjIDs = { 0, 0, 0, 0, 0, 0, 0, 0 }
- -- list of connected text fields for displaying code needed
- gConnectedCodeFieldObjIDs = { 0, 0, 0, 0 }
- -- Message functions
- -- Message functions will be called with the matching name as the message with
- -- "On" prefixed. As below, this object takes Start, Stop, Reset and ResetAndStart
- -- messages.
- --
- -- A message function parameter is a table containing three elements:
- -- msg.state is a string indicating the source state that sent the message
- -- msg.sender is a string identifer for the object that sent the message
- -- msg.originator is a string identify for the object that originator this
- -- chain of messages
- -- ********************************************* DEBUG FUNCTIONS *********************************************************
- function printKeysPressed()
- if (DEBUG_LUA_PIRATEXRAYPAD) then
- Print("KeysPressed : 1 2 3 4 5 6 7 8 \n")
- Print("KeysPressed :")
- for i = 1, NUMBER_OF_KEYS, 1 do
- Print(" " .. gKeysPressedList[i] .."")
- end
- Print("\n")
- end
- end
- function printKeyValues()
- if (DEBUG_LUA_PIRATEXRAYPAD) then
- Print("KeyValues : 1 2 3 4 5 6 7 8 \n")
- Print("KeyValues :")
- for i = 1, NUMBER_OF_KEYS, 1 do
- Print(" " .. gKeyValuesList[i] .."")
- end
- Print("\n")
- end
- end
- function printDebugInfo()
- printKeysPressed()
- printKeyValues()
- end
- -- ********************************************* MATH FUNCTIONS *********************************************************
- function round(num, idp)
- return tonumber(string.format("%." .. (idp or 0) .. "f", num))
- end
- -- ********************************************* FUNCTIONS FOR HANDLING MULTIACTOROBJECT SUPPORT *********************************************************
- function convertTextFieldToConnectionName(textFieldToConvert)
- -- This function gives us the InternalState that we'll look for on connected MultiModelActors to figure out which object corresponds to which
- -- text field, in order from left to right.
- if(textFieldToConvert == 1) then
- return ("InternalState11")
- elseif(textFieldToConvert == 2) then
- return ("InternalState12")
- elseif(textFieldToConvert == 3) then
- return ("InternalState13")
- elseif(textFieldToConvert == 4) then
- return ("InternalState14")
- end
- end
- function convertKeyToMessage(keyToConvert)
- -- This function converts a Key (1-8) to the corresponding InternalMessage that must be sent to a connected MultiModelActor to tell it to display the
- -- CMDL corresponding to that number. 9 Is used for the Pressed State on the actual keys
- -- This is used both for the Display Area as well as the 8 keys you can press, when they get randomized.
- if(keyToConvert == 1) then
- return("InternalMessage3")
- elseif(keyToConvert == 2) then
- return("InternalMessage4")
- elseif(keyToConvert == 3) then
- return("InternalMessage5")
- elseif(keyToConvert == 4) then
- return("InternalMessage6")
- elseif(keyToConvert == 5) then
- return("InternalMessage7")
- elseif(keyToConvert == 6) then
- return("InternalMessage8")
- elseif(keyToConvert == 7) then
- return("InternalMessage9")
- elseif(keyToConvert == 8) then
- return("InternalMessage10")
- elseif(keyToConvert == 9) then
- return("InternalMessage11")
- else
- -- must be "enter/nothing"
- return("InternalMessage2")
- end
- end
- function getConnectedMultiModelActors(msg)
- -- This function populates our three ObjID lists (gConnectedTextFieldObjIDs, gConnectedCodeFieldObjIDs, and gConnectedKeyActorObjIDs) with the connected
- -- MultiObjectActors that correspond to their members. Whenever we need to update a display field, a code field, or a key actor with a specific button image,
- -- we now have simple lists to index for the ObjID to send the appropriate message to
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print("Checking connections for Objects...\n")
- end
- local connections = GetObjectConnectionList()
- for i,connection in ipairs( connections ) do
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print(" Connection[" .. connection.state .. "] --> [" .. connection.message .. "]\n" )
- end
- for i2,connectedObjID in ipairs( connection.objectids ) do
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print(" ConnectedID :" .. connectedObjID .. "\n")
- end
- if(connection.state == "InternalState3") then -- "ConnectKey1"
- gConnectedKeyActorObjIDs[1] = connectedObjID
- elseif(connection.state == "InternalState4") then -- "ConnectKey2"
- gConnectedKeyActorObjIDs[2] = connectedObjID
- elseif(connection.state == "InternalState5") then -- "ConnectKey3"
- gConnectedKeyActorObjIDs[3] = connectedObjID
- elseif(connection.state == "InternalState6") then -- "ConnectKey4"
- gConnectedKeyActorObjIDs[4] = connectedObjID
- elseif(connection.state == "InternalState7") then -- "ConnectKey5"
- gConnectedKeyActorObjIDs[5] = connectedObjID
- elseif(connection.state == "InternalState8") then -- "ConnectKey6"
- gConnectedKeyActorObjIDs[6] = connectedObjID
- elseif(connection.state == "InternalState9") then -- "ConnectKey7"
- gConnectedKeyActorObjIDs[7] = connectedObjID
- elseif(connection.state == "InternalState10") then -- "ConnectKey8"
- gConnectedKeyActorObjIDs[8] = connectedObjID
- elseif(connection.state == "InternalState11") then -- "ConnectTextField1"
- gConnectedTextFieldObjIDs[1] = connectedObjID
- elseif(connection.state == "InternalState12") then -- "ConnectTextField2"
- gConnectedTextFieldObjIDs[2] = connectedObjID
- elseif(connection.state == "InternalState13") then -- "ConnectTextField3"
- gConnectedTextFieldObjIDs[3] = connectedObjID
- elseif(connection.state == "InternalState14") then -- "ConnectTextField4"
- gConnectedTextFieldObjIDs[4] = connectedObjID
- elseif(connection.state == "InternalState15") then -- "ConnectCodeField1"
- gConnectedCodeFieldObjIDs[1] = connectedObjID
- elseif(connection.state == "InternalState16") then -- "ConnectCodeField2"
- gConnectedCodeFieldObjIDs[2] = connectedObjID
- elseif(connection.state == "InternalState17") then -- "ConnectCodeField3"
- gConnectedCodeFieldObjIDs[3] = connectedObjID
- elseif(connection.state == "InternalState18") then -- "ConnectCodeField4"
- gConnectedCodeFieldObjIDs[4] = connectedObjID
- end
- end
- end
- end
- function displayKeyPressed(keyPressed)
- -- This function receives a keyPressed (1-9, since this doesn't get sent by Enter), and converts it to a message string (messageToSend)
- -- The message string (messageToSend) is one of 10 messages that a MultiModeActor can receive. We've connected a MultiModeActor for each
- -- Text Field corresponding to an index in the sequence.
- -- The message string is sent to the Object ID of the corresponding Text Field for the current key index we're on.
- -- i.e. "We pressed the number 2 for our 3rd number. Convert 2 into "InternalMessage4", the message that will switch our Text Field MultiModelActor to the
- -- "2" image. We then look through our connected objects list and find the ObjID for the 3rd display TextField. Send that ObjID the "InternalMessage4"
- -- message so it will switch its image to the number 2.
- local messageToSend = convertKeyToMessage(keyPressed)
- local receiverObjID = gConnectedTextFieldObjIDs[gCurrentCodeIndex]
- if(receiverObjID == 0) then
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("WARNING! No connected actor for TextField" .. gCurrentCodeIndex .."\n")
- end
- return
- end
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("Sending message ".. messageToSend .." to receiverObjID " .. receiverObjID .." \n")
- end
- SendEventMessage("Activate", messageToSend, receiverObjID)
- end
- function setKeyActorModels(includePressedKeys)
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("setKeyActorModels! includePressedKeys : " .. includePressedKeys .." \n")
- end
- -- This function runs through the gKeyValuesList to get the current value of a key (what it's been randomized to) and then sends the appropriate message to
- -- the associated KeyActor, to force the DisplayModel for that value.
- for i = 1, NUMBER_OF_KEYS, 1 do
- -- if we've decided not to include pressed keys, then check to see if the current key we're looking at is pressed, and return out if true.
- if((includePressedKeys == 0) and (gKeysPressedList[i] == 0)) then
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("gKeysPressedList[" .. i .. "] == 0 and includePressedKeys was 0. Skipping setActorModel for this key. \n")
- end
- else
- local receiverObjID = gConnectedKeyActorObjIDs[i]
- if(receiverObjID == 0) then
- if (DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("WARNING! No connected actor for KeyActor" .. i .."\n")
- end
- return
- end
- local keyToDisplay = gKeyValuesList[i]
- local messageToSend = convertKeyToMessage(keyToDisplay)
- if (DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("key ".. i .." | ToDisplay ".. keyToDisplay .." | Sending message ".. messageToSend .." to receiverObjID " .. receiverObjID .." \n")
- end
- SendEventMessage("Activate", messageToSend, receiverObjID)
- end
- end
- end
- function setCodeDisplayModels()
- -- this function sets up the DisplayModels for each CodeDisplay multimodel actor - effectively resetting the display to the current code chosen
- for i = 1, MAX_KEYS_IN_SEQUENCE, 1 do
- local receiverObjID = gConnectedCodeFieldObjIDs[i]
- if(receiverObjID == 0) then
- if (DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("WARNING! No connected actor for CodeDisplay" .. i .."\n")
- end
- return
- end
- local keyToDisplay = gKeyCodeSequence[i]
- local messageToSend = convertKeyToMessage(keyToDisplay)
- SendEventMessage("Activate", messageToSend, receiverObjID)
- end
- end
- -- ********************************************* RANDOMIZING KEYS *********************************************************
- function randomizeKeyValues()
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("Randomizing Keys! \n")
- end
- printDebugInfo()
- -- This function randomizes the keys by remapping the value for each key. I.e. you may press key 1, but it's acting like it's key 6
- local chosenKey
- for i = 1, NUMBER_OF_KEYS-1, 1 do
- -- make sure we're not trying to remap a key that's already been pressed and locked
- -- Print ("Remapping Key " .. i .." : ")
- if(gKeysPressedList[i] == 1) then
- -- return a random index from the list of available keys that haven't been pressed (pressed keys are set to 0, so they can't be rolled)
- -- Print ("Key Unpressed, choosing random remap \n")
- chosenKey = RandomRange(1, NUMBER_OF_KEYS)
- chosenKey = round(chosenKey, 0)
- local needKeyReplacement = 1
- while needKeyReplacement do
- -- Print("trying chosenKey ["..chosenKey.."]\n")
- if(gKeysPressedList[chosenKey] == 1) then
- -- Print("gKeysPressedList["..chosenKey.."] is available, swapping with keyValue ["..i.."]\n")
- break
- else
- chosenKey = chosenKey + 1
- if(chosenKey > NUMBER_OF_KEYS) then
- chosenKey = 1
- end
- end
- end
- -- swap the current key's value with the chosen key's value
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("Remapping : Key["..i.."] Value["..gKeyValuesList[i].."] to Value["..gKeyValuesList[chosenKey].."]\n")
- Print ("Swapping : Key["..chosenKey.."] Value["..gKeyValuesList[chosenKey].."] to Value["..gKeyValuesList[i].."]\n")
- end
- local valueToSwap = gKeyValuesList[i]
- gKeyValuesList[i] = gKeyValuesList[chosenKey]
- gKeyValuesList[chosenKey] = valueToSwap
- else
- if(DEBUG_LUA_PIRATEXRAYPAD) then
- Print ("gKeysPressedList["..i.."] is pressed, skipping! \n")
- end
- end
- end
- printDebugInfo()
- -- once we've randomized, set the key actors' models to match their randomized values. Don't include pressed keys
- setKeyActorModels(0)
- end
- -- ********************************************* KEY CODE CREATION / SETUP *********************************************************
- function generateRandomKeyCode()
- -- This function (shock!) generates a random Key Code that the player must enter into the key code interaction.
- local i
- local availableKeys = { 1, 1, 1, 1, 1, 1, 1, 1 }
- if ( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print (" Generating Key : ")
- end
- for i = 1, MAX_KEYS_IN_SEQUENCE, 1 do
- local chosenKey = RandomWeightedChoice(availableKeys)
- gKeyCodeSequence[i] = chosenKey
- availableKeys[chosenKey] = 0
- if ( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print (" " .. chosenKey .. " ")
- end
- end
- if ( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print (" \n")
- end
- -- set the code display up with the correct visuals for our new code
- setCodeDisplayModels()
- end
- function clearKeyCode()
- if ( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print (" Clearing Key Code! \n")
- end
- gIsEnteredCodeCorrect = 1
- gCurrentCodeIndex = 1
- for i = 1, NUMBER_OF_KEYS, 1 do
- gKeysPressedList[i] = 1
- end
- end
- -- ********************************************* CHECKING INPUT VS. CODE *********************************************************
- function checkKeyCodeCompletion()
- if ( gIsEnteredCodeCorrect > 0 ) then
- SendEvent( "KeyCodeSolved" )
- else
- SendEvent( "KeyCodeFailed" )
- end
- end
- --[[ **********OLD KEY CODE CHECK***********
- function checkKeyPressedAgainstCode(keyPressed)
- -- map the key pressed to its current value in the gKeyValuesList. This is the scrambled list representing what each key maps to at that moment
- local remappedKeyPressed = gKeyValuesList[keyPressed]
- -- get the current key needed in the sequence
- local currentKeyNeeded = gKeyCodeSequence[gCurrentCodeIndex]
- displayKeyPressed(remappedKeyPressed)
- if ( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print (" CurrentKeyNeeded : " .. currentKeyNeeded .. " / User Pressed : " .. remappedKeyPressed .. " \n")
- end
- if( currentKeyNeeded ~= remappedKeyPressed ) then
- gIsEnteredCodeCorrect = 0
- SendEvent( "InvalidKeyPressed" )
- end
- -- set the key pressed to the "pressed image"
- local receiverObjID = gConnectedKeyActorObjIDs[keyPressed]
- local messageToSend = convertKeyToMessage(9)
- SendEventMessage("Activate", messageToSend, receiverObjID)
- -- set the passed key as being pressed, then randomize the values of all keys
- gKeysPressedList[keyPressed] = 0
- -- increment to the next index in the key code sequence
- gCurrentCodeIndex = gCurrentCodeIndex+1
- -- check if we just entered the last number needed
- if(gCurrentCodeIndex > MAX_KEYS_IN_SEQUENCE) then
- checkKeyCodeCompletion()
- else
- SendEvent("ReadyForScramble")
- end
- end
- ]]--
- -- ********** NEW KEY CODE CHECK *********
- function checkKeyPressedAgainstCode(isInXRay)
- -- get the current key needed in the sequence
- local currentKeyNeeded = gKeyCodeSequence[gCurrentCodeIndex]
- local keyPressed = gCurrentKeyPressed
- -- set the passed key as being pressed
- gKeysPressedList[keyPressed] = 0
- -- map the key pressed to its current value in the gKeyValuesList. This is the scrambled list representing what each key maps to at that moment
- local keyPressedValue = gKeyValuesList[keyPressed]
- if ( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print (" CurrentKeyNeeded : " .. currentKeyNeeded .. " / User Pressed : " .. keyPressedValue .. " \n")
- end
- -- If player is not in X-Ray visor, check if they pressed the current key value needed. If so, swap the key's value with another unpressed key's value
- if(isInXRay == 0) then
- if (keyPressedValue == currentKeyNeeded) then
- if ( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print("Not In X-Ray and pressed correct key. Swapping with another key value! \n")
- end
- local chosenKey = RandomRange(1, NUMBER_OF_KEYS)
- chosenKey = round(chosenKey, 0)
- local needKeyReplacement = 1
- while needKeyReplacement do
- -- Print("trying chosenKey ["..chosenKey.."]\n")
- if(gKeysPressedList[chosenKey] == 1) then
- -- Swap the newly chosen value to our keyPressedValue, then swap the two values in the ValuesList
- -- Print("gKeysPressedList["..chosenKey.."] is available, swapping its value with currentKeyPressed Value ["..valueToSwap.."] \n")
- keyPressedValue = gKeyValuesList[chosenKey]
- gKeyValuesList[chosenKey] = gKeyValuesList[keyPressed]
- gKeyValuesList[keyPressed] = keyPressedValue
- break
- else
- chosenKey = chosenKey + 1
- if(chosenKey > NUMBER_OF_KEYS) then
- chosenKey = 1
- end
- end
- end
- else
- if( DEBUG_LUA_PIRATEXRAYPAD ) then
- Print("Not In X-Ray but did not press correct value! Let it drop through!\n")
- end
- end
- end
- -- show the value pressed on the appropariate Display TextField
- displayKeyPressed(keyPressedValue)
- -- if the player didn't press the key with the value needed, send the InvalidKeyPressed Event and mark that they've put in a wrong value
- if( currentKeyNeeded ~= keyPressedValue ) then
- gIsEnteredCodeCorrect = 0
- SendEvent( "InvalidKeyPressed" )
- end
- -- set the key pressed to the "pressed image"
- local receiverObjID = gConnectedKeyActorObjIDs[keyPressed]
- local messageToSend = convertKeyToMessage(9)
- SendEventMessage("Activate", messageToSend, receiverObjID)
- -- increment to the next index in the key code sequence
- gCurrentCodeIndex = gCurrentCodeIndex+1
- -- check if we just entered the last number needed
- if(gCurrentCodeIndex > MAX_KEYS_IN_SEQUENCE) then
- checkKeyCodeCompletion()
- else
- SendEvent("ReadyForScramble")
- end
- end
- -- ********************************************* X-RAY HANDLING **********************************************************
- function OnPlayerInXRay(msg)
- checkKeyPressedAgainstCode(1)
- end
- function OnPlayerNotInXRay(msg)
- checkKeyPressedAgainstCode(0)
- end
- -- ********************************************* MESSAGE AND LOAD EVENTS *********************************************************
- function OnKeyPressed1(msg)
- gCurrentKeyPressed = 1
- SendEvent("TestForXRay")
- end
- function OnKeyPressed2(msg)
- gCurrentKeyPressed = 2
- SendEvent("TestForXRay")
- end
- function OnKeyPressed3(msg)
- gCurrentKeyPressed = 3
- SendEvent("TestForXRay")
- end
- function OnKeyPressed4(msg)
- gCurrentKeyPressed = 4
- SendEvent("TestForXRay")
- end
- function OnKeyPressed5(msg)
- gCurrentKeyPressed = 5
- SendEvent("TestForXRay")
- end
- function OnKeyPressed6(msg)
- gCurrentKeyPressed = 6
- SendEvent("TestForXRay")
- end
- function OnKeyPressed7(msg)
- gCurrentKeyPressed = 7
- SendEvent("TestForXRay")
- end
- function OnKeyPressed8(msg)
- gCurrentKeyPressed = 8
- SendEvent("TestForXRay")
- end
- function OnKeyCodeCancelled(msg)
- clearKeyCode()
- end
- function OnGenerateKeyCode(msg)
- clearKeyCode()
- generateRandomKeyCode()
- randomizeKeyValues()
- end
- function OnRandomizeKeys(msg)
- randomizeKeyValues()
- end
- function OnPrintDebugInfo(msg)
- printDebugInfo()
- end
- function OnAreaLoaded(msg)
- getConnectedMultiModelActors(msg)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement