summaryrefslogtreecommitdiff
path: root/game/polyomino.lua
blob: ba6da92bb4ed563055d45b10338072968e51cf8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local M = {}
M.__index = M

function M.def(name, shape, kick_table)
	local new = setmetatable({name = name}, M)
	new.kick_table = kick_table or {{{0, 0}}, {{0, 0}}, {{0, 0}}, {{0, 0}}}
	new.cells = {}
	for l in shape:gmatch "[^%s]+" do
		local line = {}
		new.size = #l
		for i = 1, #l do
			local c = l:sub(i, i)
			if c ~= "." then
				if c == "#" then
					line[i] = new.name
				else
					line[i] = new.name .. "." .. c
				end
			end
		end
		table.insert(new.cells, 1, line)
	end
	new.bottom = new.size
	new.top = 1
	new.left_side = new.size
	new.right_side = 1
	for line = 1, new.size do
		for column = 1, new.size do
			if new.cells[line][column] then
				if new.bottom > line then new.bottom = line end
				if new.right_side < column then new.right_side = column end
				if new.top < line then new.top = line end
				if new.left_side > column then new.left_side = column end
			end
		end
	end
	new.height = new.top - new.bottom + 1 
	new.width = new.right_side - new.left_side + 1
	return new
end

local rotations = {
	{1, 0, 0, 1},
	{0, 1, -1, 0},
	{-1, 0, 0, -1},
	{0, -1, 1, 0},
}

local function rotate(line, column, rotation, size)
	local center = size / 2 - 0.5
	line = line - center
	column = column - center
	local rotation = rotations[rotation or rotation]
	local l = line * rotation[1] + column * rotation[2]
	local c = line * rotation[3] + column * rotation[4]
	line = l + center
	column = c + center
	return line, column
end

function M:get_cell(line, column, rotation)
	line, column = rotate(line, column, rotation or 1, self.size)
	return self.cells[line + 1][column + 1]
end

local piece = {}
piece.__index = piece

function M:drop(field)
	local new = setmetatable({poly = self}, piece)
	new.field = field
	new.line = field.lines - (self.bottom - 1)
	new.column = math.floor(field.columns / 2 - self.size / 2 + 1)
	new.rotation = 1
	if not new:can_occupy() then
		return
	end
	return new
end

function piece:get_cell(line, column, rotation)
	return self.poly:get_cell(line, column, rotation or self.rotation)
end

function piece:can_occupy(line, column, rotation)
	line = line or self.line
	column = column or self.column
	for l = 0, self.poly.size - 1 do
		for c = 0, self.poly.size - 1 do
			if self:get_cell(l, c, rotation)
					and self.field:cell_full(line + l, column + c) then
				return false
			end
		end
	end
	return true
end

function piece:can_move(lines, columns, rotation)
	local rotation = self.rotation + (rotation or 0)
	if rotation > 4 then
		rotation = 1
	elseif rotation < 1 then
		rotation = 4
	end
	local line, column = self.line + lines or 0, self.column + columns or 0
	return self:can_occupy(line, column, rotation)
end

function piece:place()
	if self.placed then
		return
	end
	for line = 0, self.poly.size - 1 do
		for column = 0, self.poly.size - 1 do
			local cell = self:get_cell(line, column)
			if cell then
				self.field.cells[self.line + line][self.column + column] = cell
			end
		end
	end
	self.placed = true
end

function piece:rotate(ccw)
	local rotation = self.rotation + (ccw and -1 or 1)
	if rotation > 4 then
		rotation = 1
	elseif rotation < 1 then
		rotation = 4
	end
	if not self:can_occupy(nil, nil, rotation) then
		return false
	end
	self.rotation = rotation
	return true
end

function piece:move(lines, columns)
	if self:can_move(lines, columns) then
		self.line = self.line + lines or 0
		self.column = self.column + columns or 0
		return true
	end
	return false
end

return M