Advertisement
cr34m3

KlotskiPuzzleMBMA

Mar 9th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.25 KB | None | 0 0
  1. --[[ KlotskiPuzzle.lua
  2.     Klotski-style sliding puzzle for MBMA.
  3. --]]
  4.  
  5. solution = {{9, 0, 0,10},
  6.             {9,11,11,10},
  7.             {8,11,11, 6},
  8.             {8, 7, 7, 3},
  9.             {1, 5, 4, 2}}
  10. --initialise board (double index)
  11. board = {{9,11,11,10},
  12.          {9,11,11,10},
  13.          {8, 7, 7, 6},
  14.          {8, 5, 4, 3},
  15.          {1, 0, 0, 2}}
  16.  
  17. -- initialise pieces
  18. --          {     1         2       3      4         5    }
  19. -- tbl[x] = {CharObject, letter, height, width, TLposition}
  20. tblPieces = {}
  21. tblPieces[1]  = {getObject("Characters[KlotskiA]"), "A", 1, 1, 51}
  22. tblPieces[2]  = {getObject("Characters[KlotskiB]"), "B", 1, 1, 54}
  23. tblPieces[3]  = {getObject("Characters[KlotskiC]"), "C", 1, 1, 44}
  24. tblPieces[4]  = {getObject("Characters[KlotskiD]"), "D", 1, 1, 43}
  25. tblPieces[5]  = {getObject("Characters[KlotskiE]"), "E", 1, 1, 42}
  26. tblPieces[6]  = {getObject("Characters[KlotskiF]"), "F", 1, 1, 34}
  27. tblPieces[7]  = {getObject("Characters[KlotskiG]"), "G", 1, 2, 32}
  28. tblPieces[8]  = {getObject("Characters[KlotskiH]"), "H", 2, 1, 31}
  29. tblPieces[9]  = {getObject("Characters[KlotskiI]"), "I", 2, 1, 11}
  30. tblPieces[10] = {getObject("Characters[KlotskiJ]"), "J", 2, 1, 14}
  31. tblPieces[11] = {getObject("Characters[KlotskiK]"), "K", 2, 2, 12}
  32.  
  33. solved = getObject("Conditions[KlotskiPuzzleSolved]")
  34. MoreSteps = getObject("Conditions[AnotherStep]")
  35.  
  36. function ResetPuzzle()
  37.     ResetOutfits()
  38.     board = {{9,11,11,10},
  39.              {9,11,11,10},
  40.              {8, 7, 7, 6},
  41.              {8, 5, 4, 3},
  42.              {1, 0, 0, 2}}
  43.     tblPieces[1][5]  = 51
  44.     tblPieces[2][5]  = 54
  45.     tblPieces[3][5]  = 44
  46.     tblPieces[4][5]  = 43
  47.     tblPieces[5][5]  = 42
  48.     tblPieces[6][5]  = 34
  49.     tblPieces[7][5]  = 32
  50.     tblPieces[8][5]  = 31
  51.     tblPieces[9][5]  = 11
  52.     tblPieces[10][5] = 14
  53.     tblPieces[11][5] = 12
  54. end
  55.  
  56. function CheckSolution()
  57.     tmpsolved = false
  58.     runsolved = true
  59.     for r = 1,5 do
  60.         for c = 1,4 do
  61.             tmpsolved = board[r][c] == solution[r][c]
  62.             runsolved = runsolved and tmpsolved
  63.         end
  64.     end
  65.     if runsolved then
  66.         solved:setValue(VConditionValue, true)
  67.     end
  68. end
  69.  
  70. function ResetOutfits()
  71.     for x = 1,11 do
  72.         tblPieces[x][1]:setValue(VCharacterCurrentOutfit,
  73.           getObject("Characters[Klotski"..tblPieces[x][2].."].CharacterOutfits[NotSelected]"))
  74.     end
  75. end
  76.  
  77. function row(index)
  78.     return math.floor(index/10)
  79. end
  80.  
  81. function col(index)
  82.     return index % 10
  83. end
  84.  
  85. function blocksize(blockno)
  86.     bsizec = tblPieces[blockno][4]
  87.     bsizer = tblPieces[blockno][3]
  88.     return bsizer,bsizec
  89. end
  90. --OnBlockClick - select block, deselect other, selected block = current character (all in VS)
  91.  
  92. function OnSpaceClick(S)
  93.     curN,curpos = GetBlockInfo()
  94.     xtrans,ytrans = GetMoveDir(curpos,S,curN)
  95.     movable = DoMoves(xtrans,ytrans,curN,curpos)
  96.     MoreSteps:setValue(VConditionValue, movable)
  97. end
  98.  
  99. function GetBlockInfo()
  100.     CurBlock = game:getLink(VGameCurrentCharacter)
  101.     curN = getObject("Characters["..CurBlock:getName().."].CharacterValues[BlockN]"):getInt(VValueInt)
  102.     tlpos = tblPieces[curN][5]
  103.     return curN,tlpos
  104. end
  105.  
  106. function GetMoveDir(from,to,curN)
  107.     bsizer,bsizec = blocksize(curN)
  108.     xtrans = col(to)-col(from) --raw
  109.     ytrans = row(to)-row(from) --raw
  110.     if xtrans > 0 then
  111.         xtrans = xtrans-(bsizec-1) --fixed for size
  112.     end
  113.     if ytrans > 0 then
  114.         ytrans = ytrans-(bsizer-1) --fixed for size
  115.     end
  116.     return xtrans, ytrans
  117. end
  118.  
  119. function DoMoves(xtm,ytm,curN,curpos)
  120. --check space to determin first move
  121.     ready = false
  122.     dox = 0
  123.     doy = 0
  124.     bsizer,bsizec = blocksize(curN)
  125.   --check x
  126.     if xtm > 0 then
  127.         if bsizec == 1 and bsizer == 1 then
  128.             ready = board[row(curpos)][col(curpos)+1] == 0
  129.         elseif bsizec == 2 and bsizer == 1 then
  130.             ready = board[row(curpos)][col(curpos)+2] == 0
  131.         elseif bsizec == 1 and bsizer == 2 then
  132.             ready = board[row(curpos)][col(curpos)+1] == 0 and
  133.                     board[row(curpos)+1][col(curpos)+1] == 0
  134.         elseif bsizec == 2 and bsizer == 2 then
  135.             ready = board[row(curpos)][col(curpos)+2] == 0 and
  136.                     board[row(curpos)+1][col(curpos)+2] == 0
  137.         end
  138.     elseif xtm < 0 then
  139.         if (bsizec == 1 or bsizec == 2) and bsizer == 1 then
  140.             ready = board[row(curpos)][col(curpos)-1] == 0
  141.         elseif (bsizec == 1 or bsizec == 2) and bsizer == 2 then
  142.             ready = board[row(curpos)][col(curpos)-1] == 0 and
  143.                     board[row(curpos)+1][col(curpos)-1] == 0
  144.         end
  145.     end
  146.     if ready then dox = xtm/math.abs(xtm) end --do a single x move (with direction)
  147.   --check y
  148.     if not ready and ytm > 0 then
  149.         if bsizec == 1 and bsizer == 1 then
  150.             ready = board[row(curpos)+1][col(curpos)] == 0
  151.         elseif bsizec == 2 and bsizer == 1 then
  152.             ready = board[row(curpos)+1][col(curpos)] == 0 and
  153.                     board[row(curpos)+1][col(curpos)+1] == 0
  154.         elseif bsizec == 1 and bsizer == 2 then
  155.             ready = board[row(curpos)+2][col(curpos)] == 0
  156.         elseif bsizec == 2 and bsizer == 2 then
  157.             ready = board[row(curpos)+2][col(curpos)] == 0 and
  158.                     board[row(curpos)+2][col(curpos)+1] == 0
  159.         end
  160.     elseif not ready and ytm < 0 then
  161.         if bsizec == 1 and (bsizer == 1 or bsizer == 2) then
  162.             ready = board[row(curpos)-1][col(curpos)] == 0
  163.         elseif bsizec == 2 and (bsizer == 1 or bsizer == 2) then
  164.             ready = board[row(curpos)-1][col(curpos)] == 0 and
  165.                     board[row(curpos)-1][col(curpos)+1] == 0
  166.         end
  167.     end
  168.     if ready and dox == 0 then doy = ytm/math.abs(ytm) end --do a single y move
  169. --single move
  170.     if ready then
  171.         newpos = curpos+(10*doy)+(dox)
  172.         nowrunning = startAction(actionnumber(newpos))
  173.         updateboard(curN,newpos)
  174.     end
  175.     return ready
  176. end
  177.  
  178. function actionnumber(index)
  179.     return getObject("Scenes[PUZ_KlotskiBox].SceneActions[MoveTo"..index.."]")
  180. end
  181.  
  182. function updateboard(curN,newpos)
  183.     --update piece TLposition
  184.     tblPieces[curN][5] = newpos
  185.     --update board layout
  186.     ----'remove' moved block
  187.     for r = 1,5 do
  188.         for c = 1,4 do
  189.             if board[r][c] == curN then
  190.                 board[r][c] = 0
  191.             end
  192.         end
  193.     end
  194.     ----'add' moved block
  195.     if     bsizec == 1 and bsizer == 1 then
  196.         board[row(newpos)][col(newpos)] = curN
  197.     elseif bsizec == 2 and bsizer == 1 then
  198.         board[row(newpos)][col(newpos)] = curN
  199.         board[row(newpos)][col(newpos)+1] = curN
  200.     elseif bsizec == 1 and bsizer == 2 then
  201.         board[row(newpos)][col(newpos)] = curN
  202.         board[row(newpos)+1][col(newpos)] = curN
  203.     elseif bsizec == 2 and bsizer == 2 then
  204.         board[row(newpos)][col(newpos)] = curN
  205.         board[row(newpos)+1][col(newpos)] = curN
  206.         board[row(newpos)][col(newpos)+1] = curN
  207.         board[row(newpos)+1][col(newpos)+1] = curN
  208.     end
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement