-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.lua
More file actions
303 lines (217 loc) · 8.39 KB
/
engine.lua
File metadata and controls
303 lines (217 loc) · 8.39 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Entry = {
title = "Cellular Automaton Engine", version = "1.0",
description = [[Customizable cellular automaton engine]],
author = "Cole Dieckhaus",
url = "https://github.com/colesteroni"
}
Engine = {}
--
-- Defaults
local import_filename = "game_template" -- File to import Engine data from
-- Pulled from conf.lua
Engine.update_delay = update_delay
Engine.save_directory = save_directory
-- Pulled from game file(import_filename)
Engine.cell_size = 25
Engine.states = {}
function Engine:mousepressed(normalized_x, normalized_y, button) end
function Engine:mousereleased(normalized_x, normalized_y, button) end
function Engine:keypressed(key) end
function Engine:keyreleased(key) end
--
-- Import engine data from file
local status, lfs = pcall(require, import_filename)
if not status then
print("No game loaded!")
love.window.setTitle(love.window.getTitle() .. " (No game loaded!)")
end
--
-- Engine functionality
function Engine:create_cells()
--------------------------------------------------
-- Create cell matrix
-- @params {int} grid_length Number of cells per row in matrix
-- @params {int} grid_width Number of cells per column in matrix
--------------------------------------------------
grid_height = math.ceil(love.graphics.getHeight() / self.cell_size)
grid_width = math.ceil(love.graphics.getWidth() / self.cell_size)
--------------------------------------------------
-- cells = 2D matrix of cell states
--------------------------------------------------
self.cells = {}
for x=1, grid_width do
self.cells[x] = {}
for y=1, grid_height do
self.cells[x][y] = 1
end
end
Engine.generation = 0
end
function Engine:update_states()
--------------------------------------------------
-- Update states of all cells
--------------------------------------------------
local function deepcopy(orig)
--------------------------------------------------
-- Creates a deep copy of a nested table structure
-- credit: https://stackoverflow.com/questions/27434142/how-do-i-append-to-a-table-in-lua
-- @param {table} tbl Table to be copied
-- @returns {table} Deep copy of tbl
--------------------------------------------------
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
Engine.generation = Engine.generation + 1
local temp = deepcopy(self.cells)
for x, column in ipairs(temp) do
for y, cell in ipairs(column) do
local curr_state = cell
-- if func
if self.states[curr_state] and self.states[curr_state]['func'] then
local updated_state = self.states[curr_state]['func'](x, y, temp)
self.cells[x][y] = updated_state
end
end
end
end
function Engine:get_color(state)
--------------------------------------------------
-- Generates color based on state
-- @param {number} state Index of state to get color for
-- @returns {int, int, int} Color value
--------------------------------------------------
local output_color, input_color = {}, self.states[state] and self.states[state]['color'] and self.states[state]['color'] or {255, 255, 255}
for i, c in ipairs(input_color) do
if c > 0 then
output_color[i] = c / 255 -- Standard rgb -> love's 0-1 range
else
output_color[i] = c
end
end
return output_color
end
function Engine:draw_cells()
--------------------------------------------------
-- Shows all cells on screen w/ colors based on state
--------------------------------------------------
for x, column in ipairs(self.cells) do
for y, cell in ipairs(column) do
love.graphics.setColor(self:get_color(cell))
love.graphics.rectangle("fill", (x - 1) * self.cell_size, (y - 1) * self.cell_size, self.cell_size, self.cell_size)
end
end
end
function Engine:show_pause()
--------------------------------------------------
-- Shows pause button on screen
--------------------------------------------------
love.graphics.setColor(.6, .6, .6, .8)
local center_w = love.graphics.getWidth() / 2
local center_h = love.graphics.getHeight() / 2
love.graphics.rectangle("fill", center_w - 5 - 10, center_h - 40, 10, 40)
love.graphics.rectangle("fill", center_w - 5 + 10, center_h - 40, 10, 40)
end
function Engine:show_data()
--------------------------------------------------
-- Show data of cell hovered over
--------------------------------------------------
local x, y = love.mouse.getX(), love.mouse.getY()
local grid_x, grid_y = math.floor(x / self.cell_size) + 1, math.floor(y / self.cell_size) + 1 -- Normalized x & y from mouse pos
local data = self.cells[grid_x][grid_y] -- Get cell data
-- Show data on screen
love.graphics.setColor(0, 0, 0)
love.graphics.print('Gen = ' .. Engine.generation .. '; Cell[' .. grid_x .. '][' .. grid_y .. '] = '.. data, 10, 10)
end
function Engine:show_prompt()
--------------------------------------------------
-- Text input & background(for readability) to serve as save filename prompt
--------------------------------------------------
-- Show small white background
local center_w = love.graphics.getWidth() / 2
local center_h = love.graphics.getHeight() / 2
love.graphics.setColor(.8, .8, .8, .8)
love.graphics.rectangle("fill", 5, center_h - 42, love.graphics.getWidth() - 10, 20)
-- Show current text
love.graphics.setColor(0, 0, 0)
love.graphics.print(get_input_buffer(), 10, center_h - 40)
end
function Engine:normalize_coords(x, y)
--------------------------------------------------
-- Returns the cell location that x and y coords sit on
--------------------------------------------------
return {math.floor(x / self.cell_size) + 1, math.floor(y / self.cell_size) + 1}
end
function Engine:load_file(filename)
--------------------------------------------------
-- Delete current matrix content and then load new from file
-- @param {string} filename Name of file to load
--------------------------------------------------
print("Loading '" .. filename .. "'")
Engine.cells = {} -- Empty
-- First load in data
local file = io.open(filename, "rb")
if not file then return nil end
local raw_data = file:read "*a"
file:close()
-- Parse
local parsed_data, letter = {{""}}, ""
for x = 1, #raw_data do
letter = raw_data:sub(x,x)
if letter == '\n' then
parsed_data[#parsed_data + 1] = {""}
elseif letter == ',' then
parsed_data[#parsed_data][#parsed_data[#parsed_data]+1] = "0"
else
parsed_data[#parsed_data][#parsed_data[#parsed_data]] = parsed_data[#parsed_data][#parsed_data[#parsed_data]] .. letter
end
end
-- Generate new list based on data dimensions
-- If bigger than map will be truncated
Engine:create_cells()
-- Insert as int
for x, column in ipairs(parsed_data) do
for y, cell in ipairs(column) do
if x <= #Engine.cells and y <= #Engine.cells[x] then
Engine.cells[x][y] = tonumber(cell) <= #Engine.states and tonumber(cell) or 1
end
end
end
end
function Engine:save_file(filename)
--------------------------------------------------
-- Save cell matrix to file
-- @param {string} filename Name of file to be written
--------------------------------------------------
print("Saving state to '" .. filename .. "'")
local file = io.open(filename, 'w')
if not file then return nil end
for x, column in ipairs(Engine.cells) do
for y, cell in ipairs(column) do
file:write(cell .. ",")
end
file:write("\n")
end
file:close()
end
function Engine:start_save_prompt()
--------------------------------------------------
-- Start prompt to save file
-- See engine.lua:Engine:show_prompt(),
-- main.lua:love.textinput(t)
-- main.lua:love.keypressed(key)
--------------------------------------------------
paused = true
clear_input_buffer()
get_text_intput = true
save_prompt = true
end