Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ###################################
- ## Chess System ##
- ## Developed by Dalvo ##
- ## Date: 22/04/2012 ##
- ## Tested on TFS 0.4_rev3884 ##
- ## Obs.: Need modifiqued sources ##
- ###################################
- ]]
- -- const
- INITIAL_POS = {x=90, y = 139, z = 7}
- XADREZ_STORAGE = 759593
- tile_white, tile_black = 9146, 407
- Chess = {}
- function Chess:new(cid)
- local attr = {cid = cid}
- return setmetatable(attr, {__index = self})
- end
- function Chess:hasTileSend(pos, namefind)
- 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
- return true
- end
- return false
- end
- function Chess:getDirsToPos(fromPos, toPos)
- local dirs, fakeFromPos, fakeToPos = {}, {}, {}
- fakeFromPos, fakeToPos = {x = fromPos.x, y=fromPos.y, z=fromPos.z}, {x = toPos.x, y = toPos.y, z= toPos.z}
- while (not self:tableCompare(fakeFromPos, fakeToPos)) do
- local dir = getDirectionTo(fakeFromPos, fakeToPos)
- table.insert(dirs, dir)
- local newPos = getPositionByDirection(fakeFromPos, dir, 1)
- fakeFromPos.x, fakeFromPos.y, fakeFromPos.z = newPos.x, newPos.y, newPos.z
- end
- return dirs
- end
- function Chess:moveDirs(uid, listDir, delayStep)
- for i, dir in pairs(listDir) do
- addEvent(function()
- if(isCreature(uid))then
- doMoveCreature(uid, dir)
- end
- end, i*delayStep)
- end
- end
- function Chess:tableCompare(tab, tab2)
- bool = true
- for index,value in pairs(tab) do
- if not(value == tab2[index])then
- bool = false
- end
- end
- return bool
- end
- function Chess:isFirstPosPawn(pos, color)
- local fakePos = {}
- fakePos.x, fakePos.y, fakePos.z = pos.x,pos.y, pos.z
- fakePos.y = (color == "black") and (fakePos.y + 2) or (fakePos.y - 2)
- return ((getTileInfo(fakePos).itemid == tile_black or getTileInfo(fakePos).itemid == tile_white) == false)
- end
- function Chess:isPiece(name)
- local pieceType = name:sub(7)
- if(isPlayer(getCreatureByName(name)))then return false end
- return isInArray({"king", "pawn", "horse", "tower", "queen", "bishop"}, pieceType)
- end
- function Chess:getPathPos(uid_piece)
- local piece_moves =
- {
- ["king"] = {distMove=1, dirs = {0,1,2,3,4,5,6,7}},
- ["queen"] = {distMove=8, dirs = {0,1,2,3,4,5,6,7}},
- ["bishop"] = {distMove=8, dirs = {4,5,6,7}},
- ["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}}},
- ["tower"] = {distMove=8, dirs = {0,1,2,3}},
- ["pawn"] = 0 -- ignore this
- }
- if not(isCreature(uid_piece))then return error("[Chess System] Creature Not Found.") end
- local positions, piecename = {}, getCreatureName(uid_piece)
- local pieceType, pieceColor = piecename:sub(7):lower(), piecename:sub(1,5):lower()
- if(piece_moves[pieceType])then
- if(isInArray({"king", "queen", "tower", "bishop"}, pieceType))then
- for _, dir in pairs(piece_moves[pieceType].dirs)do
- for dist = 1, piece_moves[pieceType].distMove do
- local pos = getPositionByDirection(getThingPos(uid_piece), dir, dist)
- if(self:hasTileSend(pos, pieceColor))then
- table.insert(positions, pos)
- end
- local uid_pos = getTopCreature(pos).uid
- if(isCreature(uid_pos) or (not self:hasTileSend(pos, pieceColor)))then break end
- end
- end
- elseif(pieceType == "horse")then
- for _, mod_ in pairs(piece_moves[pieceType].posits) do
- local pos = getThingPos(uid_piece)
- pos.x, pos.y = pos.x + mod_.x, pos.y + mod_.y
- if(self:hasTileSend(pos, pieceColor))then
- table.insert(positions, pos)
- end
- end
- elseif(pieceType == "pawn")then
- local dir = (pieceColor == "black") and 0 or 2
- local pos1 = getPositionByDirection(getThingPos(uid_piece), dir, 1)
- if not(isCreature(getTopCreature(pos1).uid))then
- table.insert(positions, pos1)
- local pos2 = getPositionByDirection(getThingPos(uid_piece), dir, 2)
- if(self:isFirstPosPawn(getThingPos(uid_piece), pieceColor) and #positions > 0)then
- table.insert(positions, pos2)
- end
- end
- end
- return positions
- else
- error("[Chess System] Invalid piece name")
- end
- end
- function Chess:isValidPos(uid, pos)
- local listPos = self:getPathPos(uid)
- for _, posi in pairs(listPos) do
- if(posi.x == pos.x and posi.y == pos.y and posi.z == pos.z)then
- return true
- end
- end
- return false
- end
- function Chess:verifyXeque(uid, toPos)
- -- Chess:verifyXeque(uid[, toPos])
- --[[
- * listar peรงas adversarias
- * iterar sobre elas gerando o pathpos de cada uma e verificando se coincide com a pos do rei
- * caso uma seja verdadeiro retornar true finalizando o loop
- * caso nenhum seja verdadeira retornar false
- ]]
- if(isCreature(uid))then
- local piecename = getCreatureName(uid)
- local pieceType, pieceColor = piecename:sub(7):lower(), piecename:sub(1,5):lower()
- if(self:isPiece(piecename) and pieceType == "king")then
- local black, white = self:getPiecesOnTab()
- for _, piece in pairs(pieceColor == "black" and white or black) do
- if(self:isValidPos(piece, type(toPos) == "table" and toPos or getThingPos(uid)))then
- return true
- end
- end
- end
- end
- return false
- end
- function Chess:sendEffects(uid)
- local positions = self:getPathPos(uid)
- doSendMagicEffect(getThingPos(uid), 55)
- for _, pos in pairs(positions) do
- doSendMagicEffect(pos, 56)
- end
- end
- function Chess:getPiecesOnTab()
- local black, white = {}, {}
- for x = 0, 7 do
- for y = 0, 7 do
- local pos = {x = INITIAL_POS.x, y = INITIAL_POS.y, z = INITIAL_POS.z}
- pos.x, pos.y = pos.x + x, pos.y + y
- local creature = getTopCreature(pos)
- if(isCreature(creature.uid) and (not isPlayer(creature.uid)))then
- local name = getCreatureName(creature.uid)
- if(self:isPiece(name))then
- table.insert(name:find("black") and black or white, creature.uid)
- end
- end
- end
- end
- return black, white
- end
- function Chess:createPieces()
- local piecepos = {}
- pieces = {"tower", "horse", "bishop", "queen", "king","bishop","horse","tower"}
- for _, index in pairs({0,1,6,7}) do piecepos[index] = {} end
- for index, piece in pairs(pieces) do
- piecepos[0][index-1] = "white "..piece
- piecepos[7][index-1] = "black "..piece
- end
- for i = 0, 7 do
- piecepos[1][i] = "white pawn"
- piecepos[6][i] = "black pawn"
- end
- for x = 0, 7 do
- for y = 0, 7 do
- local pos = {x = INITIAL_POS.x, y = INITIAL_POS.y, z = INITIAL_POS.z}
- pos.x, pos.y = (pos.x + x), (pos.y + y)
- local creature = getTopCreature(pos)
- if(isCreature(creature.uid) and not isPlayer(creature.uid))then
- doRemoveCreature(creature.uid)
- end
- if(piecepos[y] and piecepos[y][x]) then
- doCreateMonster(piecepos[y][x], pos)
- end
- end
- end
- end
- function onPush(cid, target, toTile)
- local chess = Chess:new(cid)
- local piecetarget = getTopCreature(getThingPos(toTile.uid)).uid
- local namefind = getCreatureName(target):sub(1,5) == "black" and "white" or "black"
- if(chess:isValidPos(target,getThingPos(toTile.uid)))then
- if(chess:verifyXeque(target, getThingPos(toTile.uid)))then
- doPlayerSendCancel(cid, "You can not leave his king in check.")
- return false
- end
- local dirs = chess:getDirsToPos(getThingPos(target), getThingPos(toTile.uid))
- chess:moveDirs(target, dirs,1000)
- if(isCreature(piecetarget) and getCreatureName(piecetarget):find(namefind))then
- doRemoveCreature(piecetarget)
- end
- else
- doPlayerSendCancel(cid, "You can not move this piece for this position.")
- end
- return false
- end
- function onLook(cid, thing, position, lookDistance)
- if(isPlayer(thing.uid) or (not isCreature(thing.uid)))then return true end
- if not(exhaustion.check(cid, XADREZ_STORAGE))then
- exhaustion.set(cid, XADREZ_STORAGE, 8)
- local chess = Chess:new(cid)
- chess:sendEffects(thing.uid)
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement