Advertisement
dalvorsn

Untitled

Apr 26th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.82 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.  
  10. ]]
  11. -- const
  12. INITIAL_POS = {x=90, y = 139, z = 7}
  13. XADREZ_STORAGE = 759593
  14. tile_white, tile_black = 9146, 407
  15.  
  16. Chess = {}
  17.  
  18. function Chess:new(cid)
  19.     local attr = {cid = cid}
  20.     return setmetatable(attr, {__index = self})
  21. end
  22.  
  23. function Chess:hasTileSend(pos, namefind)
  24.     if((getTileInfo(pos).itemid == tile_black or getTileInfo(pos).itemid == tile_white) and not (isCreature(getTopCreature(pos).uid) and getCreatureName(getTopCreature(pos).uid):find(namefind)))then
  25.         return true
  26.     end
  27.     return false
  28. end
  29.  
  30. function Chess:getDirsToPos(fromPos, toPos)
  31.     local dirs, fakeFromPos, fakeToPos = {}, {}, {}
  32.     fakeFromPos, fakeToPos = {x = fromPos.x, y=fromPos.y, z=fromPos.z}, {x = toPos.x, y = toPos.y, z= toPos.z}
  33.     while (not self:tableCompare(fakeFromPos, fakeToPos)) do
  34.         local dir = getDirectionTo(fakeFromPos, fakeToPos)
  35.         table.insert(dirs, dir)
  36.         local newPos = getPositionByDirection(fakeFromPos, dir, 1)
  37.         fakeFromPos.x, fakeFromPos.y, fakeFromPos.z = newPos.x, newPos.y, newPos.z
  38.     end
  39.     return dirs
  40. end
  41.  
  42. function Chess:moveDirs(uid, listDir, delayStep)
  43.     for i, dir in pairs(listDir) do
  44.         addEvent(function()
  45.                 if(isCreature(uid))then
  46.                     doMoveCreature(uid, dir)
  47.                 end
  48.             end, i*delayStep)
  49.     end
  50. end
  51.  
  52. function Chess:tableCompare(tab, tab2)
  53.     bool = true
  54.     for index,value in pairs(tab) do
  55.         if not(value == tab2[index])then
  56.             bool = false
  57.         end
  58.     end
  59.     return bool
  60. end
  61.  
  62. function Chess:isFirstPosPawn(pos, color)
  63.     local fakePos = {}
  64.     fakePos.x, fakePos.y, fakePos.z = pos.x,pos.y, pos.z
  65.     fakePos.y = (color == "black") and (fakePos.y + 2) or (fakePos.y - 2)
  66.     return ((getTileInfo(fakePos).itemid == tile_black or getTileInfo(fakePos).itemid == tile_white) == false)
  67. end
  68.  
  69. function Chess:isPiece(name)
  70.     local pieceType = name:sub(7)
  71.     if(isPlayer(getCreatureByName(name)))then return false end
  72.     return isInArray({"king", "pawn", "horse", "tower", "queen", "bishop"}, pieceType)
  73. end
  74. function Chess:getPathPos(uid_piece)
  75.     local piece_moves =
  76.     {
  77.         ["king"] = {distMove=1, dirs = {0,1,2,3,4,5,6,7}},
  78.         ["queen"] = {distMove=8, dirs = {0,1,2,3,4,5,6,7}},
  79.         ["bishop"] = {distMove=8, dirs = {4,5,6,7}},
  80.         ["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}}},
  81.         ["tower"] = {distMove=8, dirs = {0,1,2,3}},
  82.         ["pawn"] = 0 -- ignore this
  83.     }
  84.     if not(isCreature(uid_piece))then return error("[Chess System] Creature Not Found.") end
  85.     local positions, piecename = {}, getCreatureName(uid_piece)
  86.     local pieceType, pieceColor = piecename:sub(7):lower(), piecename:sub(1,5):lower()
  87.  
  88.     if(piece_moves[pieceType])then
  89.         if(isInArray({"king", "queen", "tower", "bishop"}, pieceType))then
  90.             for _, dir in pairs(piece_moves[pieceType].dirs)do
  91.                 for dist = 1, piece_moves[pieceType].distMove do
  92.                     local pos = getPositionByDirection(getThingPos(uid_piece), dir, dist)
  93.                     if(self:hasTileSend(pos, pieceColor))then
  94.                         table.insert(positions, pos)
  95.                     end
  96.                     local uid_pos = getTopCreature(pos).uid
  97.                     if(isCreature(uid_pos) or (not self:hasTileSend(pos, pieceColor)))then break end
  98.                 end
  99.             end
  100.         elseif(pieceType == "horse")then
  101.             for _, mod_ in pairs(piece_moves[pieceType].posits) do
  102.                 local pos = getThingPos(uid_piece)
  103.                 pos.x, pos.y = pos.x + mod_.x, pos.y + mod_.y
  104.                 if(self:hasTileSend(pos, pieceColor))then
  105.                     table.insert(positions, pos)
  106.                 end
  107.             end
  108.         elseif(pieceType == "pawn")then
  109.             local dir = (pieceColor == "black") and 0 or 2
  110.             local pos1 = getPositionByDirection(getThingPos(uid_piece), dir, 1)
  111.             if not(isCreature(getTopCreature(pos1).uid))then
  112.                 table.insert(positions, pos1)
  113.                 local pos2 = getPositionByDirection(getThingPos(uid_piece), dir, 2)
  114.                 if(self:isFirstPosPawn(getThingPos(uid_piece), pieceColor) and #positions > 0)then
  115.                     table.insert(positions, pos2)
  116.                 end
  117.             end
  118.         end
  119.         return positions
  120.     else
  121.         error("[Chess System] Invalid piece name")
  122.     end
  123. end
  124.  
  125. function Chess:isValidPos(uid, pos)
  126.     local listPos = self:getPathPos(uid)
  127.     for _, posi in pairs(listPos) do
  128.         if(posi.x == pos.x and posi.y == pos.y and posi.z == pos.z)then
  129.             return true
  130.         end
  131.     end
  132.     return false
  133. end
  134.  
  135. function Chess:verifyXeque(uid, toPos)
  136.     -- Chess:verifyXeque(uid[, toPos])
  137.     --[[
  138.     * listar peรงas adversarias
  139.     * iterar sobre elas gerando o pathpos de cada uma e verificando se coincide com a pos do rei
  140.     * caso uma seja verdadeiro retornar true finalizando o loop
  141.     * caso nenhum seja verdadeira retornar false
  142.     ]]
  143.     if(isCreature(uid))then
  144.         local piecename = getCreatureName(uid)
  145.         local pieceType, pieceColor = piecename:sub(7):lower(), piecename:sub(1,5):lower()
  146.         if(self:isPiece(piecename) and pieceType == "king")then
  147.             local black, white = self:getPiecesOnTab()
  148.             for _, piece in pairs(pieceColor == "black" and white or black) do
  149.                 if(self:isValidPos(piece, type(toPos) == "table" and toPos or getThingPos(uid)))then
  150.                     return true
  151.                 end
  152.             end
  153.         end
  154.     end
  155.     return false
  156. end
  157.  
  158. function Chess:sendEffects(uid)
  159.     local positions = self:getPathPos(uid)
  160.     doSendMagicEffect(getThingPos(uid), 55)
  161.     for _, pos in pairs(positions) do
  162.         doSendMagicEffect(pos, 56)
  163.     end
  164. end
  165.  
  166. function Chess:getPiecesOnTab()
  167.     local black, white = {}, {}
  168.     for x = 0, 7 do
  169.         for y = 0, 7 do
  170.             local pos = {x = INITIAL_POS.x, y = INITIAL_POS.y, z = INITIAL_POS.z}
  171.             pos.x, pos.y = pos.x + x, pos.y + y
  172.             local creature = getTopCreature(pos)
  173.             if(isCreature(creature.uid) and (not isPlayer(creature.uid)))then
  174.                 local name = getCreatureName(creature.uid)
  175.                 if(self:isPiece(name))then
  176.                     table.insert(name:find("black") and black or white, creature.uid)
  177.                 end
  178.             end
  179.         end
  180.     end
  181.     return black, white
  182. end
  183.  
  184. function Chess:createPieces()
  185.     local piecepos = {}
  186.     pieces = {"tower", "horse", "bishop", "queen", "king","bishop","horse","tower"}
  187.     for _, index in pairs({0,1,6,7}) do piecepos[index] = {} end
  188.     for index, piece in pairs(pieces) do
  189.         piecepos[0][index-1] = "white "..piece
  190.         piecepos[7][index-1] = "black "..piece
  191.     end
  192.     for i = 0, 7 do
  193.         piecepos[1][i] = "white pawn"
  194.         piecepos[6][i] = "black pawn"
  195.     end
  196.  
  197.     for x = 0, 7 do
  198.         for y = 0, 7 do
  199.             local pos = {x = INITIAL_POS.x, y = INITIAL_POS.y, z = INITIAL_POS.z}
  200.             pos.x, pos.y = (pos.x + x), (pos.y + y)
  201.             local creature = getTopCreature(pos)
  202.             if(isCreature(creature.uid) and not isPlayer(creature.uid))then
  203.                 doRemoveCreature(creature.uid)
  204.             end
  205.             if(piecepos[y] and piecepos[y][x]) then
  206.                 doCreateMonster(piecepos[y][x], pos)
  207.             end
  208.         end
  209.     end
  210. end
  211.  
  212. function onPush(cid, target, toTile)
  213.     local chess = Chess:new(cid)
  214.     local piecetarget = getTopCreature(getThingPos(toTile.uid)).uid
  215.     local namefind = getCreatureName(target):sub(1,5) == "black" and "white" or "black"
  216.  
  217.     if(chess:isValidPos(target,getThingPos(toTile.uid)))then
  218.         if(chess:verifyXeque(target, getThingPos(toTile.uid)))then
  219.             doPlayerSendCancel(cid, "You can not leave his king in check.")
  220.             return false
  221.         end
  222.  
  223.         local dirs = chess:getDirsToPos(getThingPos(target), getThingPos(toTile.uid))
  224.         chess:moveDirs(target, dirs,1000)
  225.  
  226.         if(isCreature(piecetarget) and getCreatureName(piecetarget):find(namefind))then
  227.             doRemoveCreature(piecetarget)
  228.         end
  229.     else
  230.         doPlayerSendCancel(cid, "You can not move this piece for this position.")
  231.     end
  232.     return false
  233. end
  234.  
  235. function onLook(cid, thing, position, lookDistance)
  236.  
  237.     if(isPlayer(thing.uid) or (not isCreature(thing.uid)))then return true end
  238.     if not(exhaustion.check(cid, XADREZ_STORAGE))then
  239.         exhaustion.set(cid, XADREZ_STORAGE, 8)
  240.         local chess = Chess:new(cid)
  241.         chess:sendEffects(thing.uid)
  242.         return false
  243.     end
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement