Advertisement
dalvorsn

Untitled

Apr 26th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.28 KB | None | 0 0
  1. --[[
  2. ###################################
  3. ##         Chess System          ##
  4. ##      Developed by Dalvo       ##
  5. ##       Date: 22/04/2012        ##
  6. ##   Tested on TFS 0.4_rev3884   ##
  7. ## Obs.: Need modifiqued sources ##
  8. ###################################
  9. Falta ainda:
  10. - verificações de chequev[done] e cheque-mate
  11. - verificações de passant
  12. - iniciar partida
  13.  -- invite por talk
  14.  -- criação das peças no tabuleiro caso aceite e teleport dos players para area de xadrez
  15.  -- limitar a um player por vez
  16.  -- limitar a uma jogada por vez de cada player
  17.  -- impedir que mova o rei para posição de cheque
  18.  -- função para "resetar" a partida
  19.  -- colocar opção de tempo por jogada, caso o tempo esgote, eliminar jogador e terminar a partida
  20.  -- impedir que players subam no tabuleiro, apenas peças podem estar em cima do mesmo
  21.  -- fazer hook da torre com o rei
  22.  
  23. Posiveis alterações posteriores a criação do sistema completo:
  24.  - criação de cachê afim de reduzir o gasto e memoria e tempo de execução
  25.  
  26.  
  27. ]]
  28. -- const
  29. INITIAL_POS = {x=90, y = 139, z = 7}
  30. XADREZ_STORAGE = 759593
  31. Xadrez = {}
  32. function Xadrez:new(cid)
  33.     local attr = {cid = cid}
  34.     return setmetatable(attr, {__index = self})
  35. end
  36.  
  37. function Xadrez:hasTileSend(pos, namefind)
  38.     if((getTileInfo(pos).itemid == 407 or getTileInfo(pos).itemid == 9146) and not (isCreature(getTopCreature(pos).uid) and getCreatureName(getTopCreature(pos).uid):find(namefind)))then
  39.         return true
  40.     end
  41.     return false
  42. end
  43.  
  44. function Xadrez:getDirsToPos(fromPos, toPos)
  45.     local dirs, fakeFromPos, fakeToPos = {}, {}, {}
  46.     fakeFromPos, fakeToPos = {x = fromPos.x, y=fromPos.y, z=fromPos.z}, {x = toPos.x, y = toPos.y, z= toPos.z}
  47.     while (not self:tableCompare(fakeFromPos, fakeToPos)) do
  48.         local dir = getDirectionTo(fakeFromPos, fakeToPos)
  49.         table.insert(dirs, dir)
  50.         local newPos = getPositionByDirection(fakeFromPos, dir, 1)
  51.         fakeFromPos.x, fakeFromPos.y, fakeFromPos.z = newPos.x, newPos.y, newPos.z
  52.     end
  53.     return dirs
  54. end
  55.  
  56. function Xadrez:moveDirs(uid, listDir, delayStep)
  57.     for i, dir in pairs(listDir) do
  58.         addEvent(function()
  59.                 if(isCreature(uid))then
  60.                     doMoveCreature(uid, dir)
  61.                 end
  62.             end, i*delayStep)
  63.     end
  64. end
  65.  
  66. function Xadrez:tableCompare(tab, tab2)
  67.     bool = true
  68.     for index,value in pairs(tab) do
  69.         if not(value == tab2[index])then
  70.             bool = false
  71.         end
  72.     end
  73.     return bool
  74. end
  75.  
  76. function Xadrez:isFirstPosPawn(pos, color)
  77.     local fakePos = {}
  78.     fakePos.x, fakePos.y, fakePos.z = pos.x,pos.y, pos.z
  79.     fakePos.y = (color == "black") and (fakePos.y + 2) or (fakePos.y - 2)
  80.     return ((getTileInfo(fakePos).itemid == 407 or getTileInfo(fakePos).itemid == 9146) == false)
  81. end
  82.  
  83. function Xadrez:isPiece(name)
  84.     local pieceType = name:sub(7)
  85.     if(isPlayer(getCreatureByName(name)))then return false end
  86.     return isInArray({"king", "pawn", "horse", "tower", "queen", "bishop"}, pieceType)
  87. end
  88. function Xadrez:getPathPos(uid_piece)
  89.     local piece_moves =
  90.     {
  91.         ["king"] = {distMove=1, dirs = {0,1,2,3,4,5,6,7}},
  92.         ["queen"] = {distMove=8, dirs = {0,1,2,3,4,5,6,7}},
  93.         ["bishop"] = {distMove=8, dirs = {4,5,6,7}},
  94.         ["horse"] = {posits = {{x = -1, y = -2}, {x = 1, y = -2}, {x = 2, y = 1}, {x = 2, y = -1},{x = -2, y = 1}, {x = -2, y = -1}, {x = -1, y = 2}, {x = 1, y = 2}}},
  95.         ["tower"] = {distMove=8, dirs = {0,1,2,3}},
  96.         ["pawn"] = 0 -- ignore this
  97.     }
  98.     if not(isCreature(uid_piece))then return error("[Chess System] Creature Not Found.") end
  99.     local positions, piecename = {}, getCreatureName(uid_piece)
  100.     local pieceType, pieceColor = piecename:sub(7):lower(), piecename:sub(1,5):lower()
  101.  
  102.     if(piece_moves[pieceType])then
  103.         if(isInArray({"king", "queen", "tower", "bishop"}, pieceType))then
  104.             for _, dir in pairs(piece_moves[pieceType].dirs)do
  105.                 for dist = 1, piece_moves[pieceType].distMove do
  106.                     local pos = getPositionByDirection(getThingPos(uid_piece), dir, dist)
  107.                     if(self:hasTileSend(pos, pieceColor))then
  108.                         table.insert(positions, pos)
  109.                     end
  110.                     local uid_pos = getTopCreature(pos).uid
  111.                     if(isCreature(uid_pos) or (not self:hasTileSend(pos, pieceColor)))then break end
  112.                 end
  113.             end
  114.         elseif(pieceType == "horse")then
  115.             for _, mod_ in pairs(piece_moves[pieceType].posits) do
  116.                 local pos = getThingPos(uid_piece)
  117.                 pos.x, pos.y = pos.x + mod_.x, pos.y + mod_.y
  118.                 if(self:hasTileSend(pos, pieceColor))then
  119.                     table.insert(positions, pos)
  120.                 end
  121.             end
  122.         elseif(pieceType == "pawn")then
  123.             local dir = (pieceColor == "black") and 0 or 2
  124.             local pos1 = getPositionByDirection(getThingPos(uid_piece), dir, 1)
  125.             if not(isCreature(getTopCreature(pos1).uid))then
  126.                 table.insert(positions, pos1)
  127.                 local pos2 = getPositionByDirection(getThingPos(uid_piece), dir, 2)
  128.                 if(self:isFirstPosPawn(getThingPos(uid_piece), pieceColor) and #positions > 0)then
  129.                     table.insert(positions, pos2)
  130.                 end
  131.             end
  132.         end
  133.         return positions
  134.     else
  135.         error("[Chess System] Invalid piece name")
  136.     end
  137. end
  138.  
  139. function Xadrez:isValidPos(uid, pos)
  140.     local listPos = self:getPathPos(uid)
  141.     for _, posi in pairs(listPos) do
  142.         if(posi.x == pos.x and posi.y == pos.y and posi.z == pos.z)then
  143.             return true
  144.         end
  145.     end
  146.     return false
  147. end
  148.  
  149. function Xadrez:sendEffects(uid)
  150.     local positions = self:getPathPos(uid)
  151.     doSendMagicEffect(getThingPos(uid), 55)
  152.     for _, pos in pairs(positions) do
  153.         doSendMagicEffect(pos, 56)
  154.     end
  155. end
  156. function Xadrez:getPiecesOnTab()
  157.     local black, white = {}, {}
  158.     for x = 0, 7 do
  159.         for y = 0, 7 do
  160.             pos.x, pos.y, pos.z = INITIAL_POS.x, INITIAL_POS.y, INITIAL_POS.z
  161.             pos.x, pos.y = pos.x + x, pos.y + y
  162.             local creature = getTopCreature(pos)
  163.             if(isCreature(creature.uid) and (not isPlayer(creature.uid)))then
  164.                 local name = getCreatureName(creature.uid)
  165.                 if(self:isPiece(name))then
  166.                     table.insert((name:find("black") and black or white), creature.uid)
  167.                 end
  168.             end
  169.         end
  170.     end
  171.     return black, white
  172. end
  173.  
  174. function Xadrez:isInCheque(uid) --- modificar quando chegar em casa, repensar como sera feito o cheque
  175.     local positions = {}
  176.     if(isCreature(uid) and self:isPiece(getCreatureName(uid)))then
  177.         if(getCreatureName(uid):find("king"))then
  178.             local white, black = self:getPiecesOnTab()
  179.             for _, piece in pairs(isInArray(white, uid) and white or black) do
  180.                 local posits = self:getPathPos(piece)
  181.                 for _, pos in pairs(posits) do
  182.                     if not(isInArray(positions, pos))then
  183.                         table.insert(positions, pos)
  184.                     end
  185.                 end
  186.             end
  187.         end
  188.     end
  189.     print("function isInCheque memory consume: " .. collectgarbage("count"))
  190.     return positions
  191. end
  192.  
  193. function Xadrez:createPieces()
  194.     local piecepos = {}
  195.     pieces = {"tower", "horse", "bishop", "queen", "king","bishop","horse","tower"}
  196.     for _, index in pairs({0,1,6,7}) do piecepos[index] = {} end
  197.     for index, piece in pairs(pieces) do
  198.         piecepos[0][index-1] = "white "..piece
  199.         piecepos[7][index-1] = "black "..piece
  200.     end
  201.     for i = 0, 7 do
  202.         piecepos[1][i] = "white pawn"
  203.         piecepos[6][i] = "black pawn"
  204.     end
  205.  
  206.     for x = 0, 7 do
  207.         for y = 0, 7 do
  208.             local pos = {x = INITIAL_POS.x, y = INITIAL_POS.y, z = INITIAL_POS.z}
  209.             pos.x, pos.y = (pos.x + x), (pos.y + y)
  210.             local creature = getTopCreature(pos)
  211.             if(isCreature(creature.uid) and not isPlayer(creature.uid))then
  212.                 doRemoveCreature(creature.uid)
  213.             end
  214.             if(piecepos[y] and piecepos[y][x]) then
  215.                 doCreateMonster(piecepos[y][x], pos)
  216.             end
  217.         end
  218.     end
  219. end
  220.  
  221. function onPush(cid, target, toTile)
  222.     local chess = Xadrez:new(cid)
  223.     local piecetarget = getTopCreature(getThingPos(toTile.uid)).uid
  224.     local namefind = getCreatureName(target):sub(1,5) == "black" and "white" or "black"
  225.     chess:isInCheque(target)
  226.     if(isCreature(piecetarget) and getCreatureName(piecetarget):find(namefind))then
  227.         doRemoveCreature(piecetarget)
  228.     end
  229.     if(chess:isValidPos(target,getThingPos(toTile.uid)))then
  230.         local dirs = chess:getDirsToPos(getThingPos(target), getThingPos(toTile.uid))
  231.         chess:moveDirs(target, dirs,1000)
  232.     else
  233.         doPlayerSendCancel(cid, "You can not move this piece for this position.")
  234.     end
  235.     return false
  236. end
  237.  
  238. function onLook(cid, thing, position, lookDistance)
  239.  
  240.     if(isPlayer(thing.uid) or (not isCreature(thing.uid)))then return true end
  241.     if not(exhaustion.check(cid, XADREZ_STORAGE))then
  242.         exhaustion.set(cid, XADREZ_STORAGE, 8)
  243.         local chess = Xadrez:new(cid)
  244.         chess:sendEffects(thing.uid)
  245.         return false
  246.     end
  247. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement