forked from Nightwolf-47/KleleAtoms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
234 lines (214 loc) · 8.33 KB
/
main.lua
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
utf8 = require("utf8")
--Extension to utf8 table, string.sub for utf-8 strings
function utf8.sub(s,i,j)
j = j or #s
if j == -1 then j = #s end
return string.sub(s,(utf8.offset(s,i) or #s+1),(utf8.offset(s,j+1) or #s+1)-1)
end
_CAState = require("state")
local settsvals = { --A table to convert settings2.txt line number to a setting name
"_CAGridW",
"_CAGridH",
"_CAAILevel",
"_CAPlayer1",
"_CAPlayer2",
"_CAPlayer3",
"_CAPlayer4"
}
local nasettsvals = { --A table to convert natoms.txt line number to a setting name
"_NAServerIP",
"_NAPort",
"_NAPlayerNick"
}
local function checkValidPlayers() --Makes sure that at least 2 players are present
local pc = 0
for i = 4,7 do
if _G[settsvals[i]] > 0 then
pc = pc + 1
end
end
if pc < 2 then
local pos = 4
while pc < 2 do
if _G[settsvals[pos]] == 0 then
pc = pc + 1
_G[settsvals[pos]] = 1
end
pos = pos + 1
end
end
end
local function checkValidNetOptions() --Checks if net (NAtoms) settings are valid
if _NAHostIP then
_NAHostIP = _NAHostIP:gsub("%c+",""):sub(1,24)
end
if _NAServerIP then
_NAServerIP = _NAServerIP:gsub("%c+",""):sub(1,24)
end
local tnum = math.floor(tonumber(_NAPort) or 5047)
tnum = math.min(math.max(tnum, 1025), 49150)
_NAPort = tostring(tnum)
_NAPlayerNick = utf8.sub(_NAPlayerNick:gsub("%c+",""):gsub("%s+",""),1,16)
end
local function checkSetValues() --Checks if values are in a valid range
_CAGridW = math.max(math.min(_CAGridW, 30), 7)
_CAGridH = math.max(math.min(_CAGridH, 20), 4)
_CAAILevel = math.max(math.min(_CAAILevel, 3), 1)
_CAPlayer1 = math.max(math.min(_CAPlayer1, 4), 0)
_CAPlayer2 = math.max(math.min(_CAPlayer2, 4), 0)
_CAPlayer3 = math.max(math.min(_CAPlayer3, 4), 0)
_CAPlayer4 = math.max(math.min(_CAPlayer4, 4), 0)
checkValidPlayers()
checkValidNetOptions()
end
local function readArgs(args)
local valmode = 0 --0 - nothing, 1 - grid width, 2 - grid height, 3 - AI difficulty, 4-7 - Player types, 96 - Port, 97 - Host IP, 98 - Connect IP, 99 - Player Nick, 100 - OS type
for k,v in ipairs(args) do
if valmode == 0 then
if v == "-gridwidth" or v == "-gw" then
valmode = 1
elseif v == "-gridheight" or v == "-gh" then
valmode = 2
elseif (string.sub(v,1,-2) == "-player" and v ~= "-players") or string.sub(v,1,-2) == "-p" then
local pnum = tonumber(string.sub(v,-1,-1))
if pnum and pnum >= 1 and pnum <= 4 then
valmode = 3 + pnum
end
elseif v == "-mobilemode" or v == "-mobile" then
_CAIsMobile = true
elseif v == "-port" then
valmode = 96
elseif v == "-host" then
valmode = 97
elseif v == "-connect" then
valmode = 98
elseif v == "-nick" then
valmode = 99
elseif v == "-forceos" or v == "-os" then
valmode = 100
elseif v == "-kbmode" then --Keyboard mode (adds virtual mouse controlled by keyboard)
_CAKBMode = true
end
elseif valmode > 0 and valmode <= 7 then
local index = settsvals[valmode]
_G[index] = tonumber(v) or _G[index]
valmode = 0
elseif valmode == 96 then -- -port <port>
_NAPort = v
valmode = 0
elseif valmode == 97 then -- -host <ip>
_NAArgsUsed = true
_NAHostIP = v
valmode = 0
elseif valmode == 98 then -- -connect <ip>
_NAArgsUsed = true
_NAServerIP = v
valmode = 0
elseif valmode == 99 then -- nick <name>
_NAPlayerNick = v
valmode = 0
elseif valmode == 100 then --Force OS type
_CAOSType = v
_CAIsMobile = _CAIsMobile or (_CAOSType == "Android" or _CAOSType == "iOS" or _CAOSType == "Web")
end
end
end
local function loadSettings()
local inum = 1
if love.filesystem.getInfo("settings2.txt") then --Load a settings file (supports both 1.2 and 1.3 files)
for line in love.filesystem.lines("settings2.txt") do
if inum <= #settsvals then
local index = settsvals[inum]
_G[index] = tonumber(line) or _G[index]
inum = inum + 1
end
end
elseif love.filesystem.getInfo("settings.txt") then --Convert settings format from 1.1.2 and older versions to 1.2 format (with 1.3 changes)
local pcount = nil
for line in love.filesystem.lines("settings.txt") do
if inum <= 2 then
local index = settsvals[inum]
_G[index] = tonumber(line) or _G[index]
elseif inum == 3 then --Old index 3 and 4 are replaced by separate player type settings
pcount = tonumber(line)
_CAPlayer1,_CAPlayer2,_CAPlayer3,_CAPlayer4 = 0,0,0,0
if pcount then
pcount = math.min(pcount,4)
for i = 1,pcount do
local index = settsvals[3+i]
_G[index] = 1
end
end
elseif inum == 4 then
local aicount = tonumber(line)
if aicount and pcount then
aicount = math.min(aicount,4)
for i = pcount-aicount+1,pcount do
local index = settsvals[3+i]
if _G[index] == 0 then break end
_G[index] = 3
end
end
elseif inum == 5 then
local index = settsvals[3]
_G[index] = tonumber(line) or _G[index]
end
inum = inum + 1
end
_CAState.printmsg("Converted old settings to 1.2+ format!",4)
end
if love.filesystem.getInfo("natoms.txt") then
local lnum = 1
for line in love.filesystem.lines("natoms.txt") do
if nasettsvals[lnum] then
_G[nasettsvals[lnum]] = line
end
lnum = lnum + 1
end
end
end
function love.load(args)
love.graphics.setDefaultFilter("linear","linear",0)
_NATextBox = require("textbox")
_CAFont16 = love.graphics.newFont(16) --Default font, size 16
_CAFont24 = love.graphics.newFont(24) --Default font, size 24
_CAFont32 = love.graphics.newFont(32) --Default font, size 32
_CAGridW = 10 --Grid width
_CAGridH = 6 --Grid Height
_CAPlayer1 = 1 --Player 1 type (0 - not present, 1 - player, 2 - AI easy, 3 - AI medium, 4 - AI hard, 9 - dummy/scripted)
_CAPlayer2 = 3 --Player 2 type
_CAPlayer3 = 0 --Player 3 type
_CAPlayer4 = 0 --Player 4 type
_CAAILevel = 2 --Only used for settings/savefile compatibility
_CAOSType = love.system.getOS()
_CAIsMobile = (_CAOSType == "Android" or _CAOSType == "iOS" or _CAOSType == "Web") --If true, mobile mode will be enabled
_CAKBMode = false --Keyboard mode
_NAOnline = false --If game is in online mode
_NAArgsUsed = false --Were NAtoms autojoin/autohost arguments used?
_NAHostIP = nil --IP address and port of host
_NAServerIP = nil --IP address and port of server
_NAPort = "5047" --Port number
_NAPlayerNick = "Player" --Player nickname
loadSettings()
readArgs(args) --Read commandline parameters
checkSetValues() --Make sure the settings are within the acceptable range
net = require("network.networkhandler")
_CAState.list["game"] = require("states.game.gamestate")
_CAState.list["menu"] = require("states.menu.menustate")
_CAState.list["lobby"] = require("states.menu.lobbystate")
if _NAArgsUsed and (_NAHostIP ~= nil or _NAServerIP ~= nil) then --If host mode or client mode is enabled
_CAState.change("lobby")
else
_CAState.change("menu")
end
end
love.update = _CAState.update
love.draw = _CAState.draw
love.keypressed = _CAState.keypressed
love.keyreleased = _CAState.keyreleased
love.mousepressed = _CAState.mousepressed
love.mousereleased = _CAState.mousereleased
love.textinput = _CAState.textinput
love.focus = _CAState.focus
love.filedropped = _CAState.filedropped
love.quit = _CAState.quit