How can I make a combination lock using multiple digits?

From OHRRPGCE-Wiki
Jump to: navigation, search

There is no build-in combination lock feature. As such, there is no easy way to make a combination lock.

The Hard Way[edit]

However, there is a hard way to do it. You can write a plotscript that implements a combination lock.

global variable(1, answer)
global variable(2, attempt)

plotscript, test combolock, begin
  variable(finished)
  answer := random(000,999)
  attempt := random(000,999)
  show text box (1) # tells you the answer before you start
  wait for text box
  while(not(finished)) do, begin
    attempt := combolock(0, 3, attempt)
    if(attempt == answer) then, begin
      show text box (2) # combo correct
      finished := true
    end,else,begin
      show text box (3) # incorrect, try again
    end
    wait for text box
  end
end

script, combolock, ID=0, digits=4, starting value=0, center=0, position x=160, position y=110, begin
  variable(cur,i,ptr,n)
  ptr := 1
  cur := starting value
  clear string(ID)
  append ascii(ID, 32)
  for(i, 1, digits) do, begin
    append ascii(ID, 32)
    append ascii(ID, 32)
  end
  if(center)
    then(center string at(ID, position x, position y))
    else(show string at(ID, string X(ID), string Y(ID)))
  variable(finished)
  while(not(finished)), do, begin
    clear string (ID)
    append ascii (ID, 32)
    for(i, digits--1, 0, -1) do, begin
      append ascii (ID, 48 + getdigit(i, cur))
      append ascii (ID, 32)
    end
    replace char(ID,ptr*2--1,60)
    replace char(ID,ptr*2+1,62)
    wait(1)
    if(key is pressed(key:ENTER)
       or,key is pressed(key:SPACE)
       or,key is pressed(joy:button1)) then, begin
      finished := 1
    end
    if(keyval(key:left)>>1, and, ptr>>1) then(ptr -= 1)
    if(keyval(key:right)>>1, and, ptr<<digits) then(ptr += 1)
    if(keyval(key:up)>>1), then, begin
      n := getdigit(digits--ptr, cur)
      n -= 1
      if(n << 0) then(n := 9)
      cur := setdigit(digits--ptr, cur, n)
    end
    if(keyval(key:down)>>1), then, begin
      n := getdigit(digits--ptr, cur)
      n += 1
      if(n >> 9) then(n := 0)
      cur := setdigit(digits--ptr, cur, n)
    end
  end
  return(cur)
end

script, getdigit, digit=0, number=0, begin
  # return a single digit from a number, starting at the end
  return ((number / 10 ^ digit), mod, 10)
end

script, setdigit, digit=0, number=0, newdigit=0, begin
  # set a single digit in a number, startin at the end
  variable(place, olddigit)
  place := 10 ^ digit
  olddigit := getdigit(digit, number)
  number -= olddigit * place
  number += newdigit * place
  return(number)
end