2012-08-20 14:30:40 -07:00
|
|
|
-- CHESS MOD
|
|
|
|
-- ======================================
|
|
|
|
-- chess/init.lua
|
|
|
|
-- ======================================
|
|
|
|
-- Registers the basic chess stuff
|
|
|
|
--
|
|
|
|
-- Contents:
|
|
|
|
--
|
|
|
|
-- [regis] Spawn Block
|
|
|
|
-- [craft] Spawn Block
|
|
|
|
-- [regis] board_black
|
|
|
|
-- [regis] board_white
|
|
|
|
-- ======================================
|
|
|
|
|
2012-08-20 16:36:37 -07:00
|
|
|
dofile(minetest.get_modpath("chess").."/pieces.lua")
|
|
|
|
dofile(minetest.get_modpath("chess").."/rules.lua")
|
|
|
|
|
2012-08-20 14:30:40 -07:00
|
|
|
-- Register the spawn block
|
2012-08-23 14:34:50 -07:00
|
|
|
minetest.register_node("chess:spawn",{
|
2012-08-20 14:30:40 -07:00
|
|
|
description = "Chess Board",
|
|
|
|
tile_images = {"chess_spawn.png"},
|
|
|
|
inventory_image = "chess_spawn.png",
|
|
|
|
groups = {tree=1,snappy=1,choppy=2,oddly_breakable_by_hand=1,flammable=2},
|
2012-08-20 20:48:57 -07:00
|
|
|
after_dig_node = function(pos, node, digger)
|
2012-08-23 14:29:47 -07:00
|
|
|
|
2012-08-20 20:48:57 -07:00
|
|
|
local size = 10
|
|
|
|
|
2012-08-23 14:29:47 -07:00
|
|
|
local p = {x=pos.x+1, y=pos.y, z=pos.z+1}
|
|
|
|
local n = minetest.env:get_node(p)
|
|
|
|
|
|
|
|
if n.name == "chess:board_black" then
|
|
|
|
for i = size, 1, -1 do
|
|
|
|
for ii = size, 1, -1 do
|
2012-08-27 12:22:52 -07:00
|
|
|
|
|
|
|
--remove board
|
2012-08-23 14:29:47 -07:00
|
|
|
local p = {x=pos.x+i, y=pos.y, z=pos.z+ii}
|
|
|
|
minetest.env:remove_node(p)
|
2012-08-27 12:22:52 -07:00
|
|
|
|
|
|
|
--remove pieces ontop
|
|
|
|
local p = {x=pos.x+i, y=pos.y+1, z=pos.z+ii}
|
|
|
|
minetest.env:remove_node(p)
|
|
|
|
|
2012-08-23 14:29:47 -07:00
|
|
|
end
|
2012-08-20 20:48:57 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_punch = function(pos)
|
|
|
|
|
|
|
|
--reset the pieces
|
|
|
|
|
|
|
|
end,
|
2012-08-27 12:22:52 -07:00
|
|
|
after_place_node = function(pos, placer)
|
|
|
|
|
|
|
|
--spawn box ownership
|
|
|
|
|
|
|
|
local player = placer:get_player_name()
|
|
|
|
print(player)
|
|
|
|
local placed = minetest.env:get_node(pos)
|
|
|
|
|
|
|
|
local placed_meta = minetest.env:get_meta(placed)
|
|
|
|
placed_meta:set_string("owner", player)
|
2012-08-20 14:30:40 -07:00
|
|
|
|
2012-08-27 12:22:52 -07:00
|
|
|
--chessboard placement that is coded really ugly. I hate it!!! and i will fix it!!! I MUST!
|
2012-08-20 20:48:57 -07:00
|
|
|
local size = 10
|
2012-08-23 14:29:47 -07:00
|
|
|
local isFree = true
|
2012-08-27 12:22:52 -07:00
|
|
|
|
|
|
|
for i = size, 1, -1 do --check if there is room for a chessboard with pieces ontop
|
2012-08-20 17:24:46 -07:00
|
|
|
for ii = size, 1, -1 do
|
2012-08-20 14:30:40 -07:00
|
|
|
|
2012-08-20 20:48:57 -07:00
|
|
|
local p = {x=pos.x+i, y=pos.y, z=pos.z+ii}
|
2012-08-23 14:29:47 -07:00
|
|
|
local n = minetest.env:get_node(p)
|
|
|
|
|
|
|
|
local p_top = {x=pos.x+i, y=pos.y+1, z=pos.z+ii}
|
|
|
|
local n_top = minetest.env:get_node(p_top)
|
2012-08-20 20:48:57 -07:00
|
|
|
|
2012-08-23 14:29:47 -07:00
|
|
|
if n.name ~= "air" and n_top.name ~= "air" then
|
|
|
|
isFree = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local alternate = true
|
|
|
|
|
2012-08-27 12:22:52 -07:00
|
|
|
if isFree then -- if there is room for a chessboard and pieces, make the chessboard with pieces
|
2012-08-23 14:29:47 -07:00
|
|
|
minetest.chat_send_all("Chess board has been placed, let the match begin!")
|
|
|
|
for i = size, 1, -1 do
|
|
|
|
for ii = size, 1, -1 do
|
2012-08-27 12:22:52 -07:00
|
|
|
--place pieces
|
2012-08-23 14:29:47 -07:00
|
|
|
local p = {x=pos.x+i, y=pos.y, z=pos.z+ii}
|
2012-08-27 12:22:52 -07:00
|
|
|
local p_top = {x=pos.x+i, y=pos.y+1, z=pos.z+ii}
|
|
|
|
|
|
|
|
if (i > 1 and i < size) then
|
|
|
|
--black pieces
|
|
|
|
if (ii == 3) then --pawns
|
|
|
|
minetest.env:add_node(p_top, {name="chess:pawn_black"})
|
|
|
|
end
|
|
|
|
|
|
|
|
if (ii == 2) then --behind pawns
|
|
|
|
if (i == 2 or i == 9) then minetest.env:add_node(p_top, {name="chess:rook_black"}) end
|
|
|
|
if (i == 3 or i == 8) then minetest.env:add_node(p_top, {name="chess:knight_black"}) end
|
|
|
|
if (i == 4 or i == 7) then minetest.env:add_node(p_top, {name="chess:bishop_black"}) end
|
|
|
|
if (i == 5) then minetest.env:add_node(p_top, {name="chess:queen_black"}) end
|
|
|
|
if (i == 6) then minetest.env:add_node(p_top, {name="chess:king_black"}) end
|
|
|
|
end
|
|
|
|
|
|
|
|
--white pieces
|
|
|
|
if (ii == 8) then --pawns
|
|
|
|
minetest.env:add_node(p_top, {name="chess:pawn_white"})
|
|
|
|
end
|
|
|
|
|
|
|
|
if (ii == 9) then --behind pawns
|
|
|
|
if (i == 2 or i == 9) then minetest.env:add_node(p_top, {name="chess:rook_white"}) end
|
|
|
|
if (i == 3 or i == 8) then minetest.env:add_node(p_top, {name="chess:knight_white"}) end
|
|
|
|
if (i == 4 or i == 7) then minetest.env:add_node(p_top, {name="chess:bishop_white"}) end
|
|
|
|
if (i == 5) then minetest.env:add_node(p_top, {name="chess:queen_white"}) end
|
|
|
|
if (i == 6) then minetest.env:add_node(p_top, {name="chess:king_white"}) end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-08-23 14:29:47 -07:00
|
|
|
if (ii == 1) or (ii == size) or (i ==1) or (i == size) then--create border
|
2012-08-20 20:48:57 -07:00
|
|
|
minetest.env:add_node(p, {name="chess:board_black"})
|
2012-08-23 14:29:47 -07:00
|
|
|
else
|
|
|
|
if alternate then
|
|
|
|
minetest.env:add_node(p, {name="chess:board_black"})
|
|
|
|
alternate = false
|
|
|
|
else
|
|
|
|
minetest.env:add_node(p, {name="chess:board_white"})
|
|
|
|
alternate = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if (math.floor(size/2) == size/2) then
|
|
|
|
if alternate then
|
2012-08-20 20:48:57 -07:00
|
|
|
alternate = false
|
|
|
|
else
|
|
|
|
alternate = true
|
|
|
|
end
|
2012-08-20 14:30:40 -07:00
|
|
|
end
|
|
|
|
end
|
2012-08-23 14:29:47 -07:00
|
|
|
else
|
|
|
|
minetest.chat_send_all("Chess board does not fit")
|
2012-08-20 14:30:40 -07:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
-- Add crafting for the spawn block
|
|
|
|
minetest.register_craft({
|
|
|
|
output="chess:spawn",
|
|
|
|
recipe = {
|
|
|
|
{'default:mese','default:mese','default:mese'},
|
|
|
|
{'default:mese','default:mese','default:mese'},
|
|
|
|
{'default:mese','default:mese','default:mese'},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
--Register the Board Blocks: white
|
2012-08-23 14:34:50 -07:00
|
|
|
minetest.register_node("chess:board_white",{
|
2012-08-20 14:30:40 -07:00
|
|
|
description = "White Chess Board Piece",
|
2012-08-20 17:24:46 -07:00
|
|
|
tile_images = {"chess_board_white.png"},
|
2012-08-20 14:30:40 -07:00
|
|
|
inventory_image = "chess_white.png",
|
2012-08-25 10:50:33 -07:00
|
|
|
groups = {indestructable},
|
2012-08-20 14:30:40 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
--Register the Board Blocks: black
|
2012-08-23 14:34:50 -07:00
|
|
|
minetest.register_node("chess:board_black",{
|
2012-08-20 14:30:40 -07:00
|
|
|
description = "Black Chess Board Piece",
|
2012-08-20 17:24:46 -07:00
|
|
|
tile_images = {"chess_board_black.png"},
|
2012-08-20 14:30:40 -07:00
|
|
|
inventory_image = "chess_black.png",
|
2012-08-25 10:50:33 -07:00
|
|
|
groups = {indestructable},
|
2012-08-20 14:30:40 -07:00
|
|
|
})
|