-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
243 lines (207 loc) · 7.34 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
235
236
237
238
239
240
241
242
243
-----------------------------------------------------------------------------------------
-- main.lua
-----------------------------------------------------------------------------------------
--this function sets up a few global variables and baseline config.
--remember, lua requires code to be in order to reference (cant call a function that's lower in the file than the current one)
--Remember: in LUA, if you use strings to index a table, you can't use #table to get a count accurately, but the string reference will work.
system.setIdleTimer(false) --disables screen auto-off.
require("helpers")
require("gameLogic")
require("database")
require("plusCodes")
local lfs = require( "lfs" )
local composer = require("composer")
netCallCount = 0
forceRedraw = false --used to tell the screen to redraw even if we havent moved.
debug = false --set false for release builds. Set true for lots of console info being dumped. Must be global to apply to all files.
composer.isDebug = debug
debugGPS = false --display data for the GPS event and timer loop and auto-move
debugDB = false
debugLocal = false
debugNetwork = false
--uncomment when testing to clear local data.
--ResetDatabase()
startDatabase()
serverURL = ""
currentPlusCode = "" -- where the user is sitting now
lastPlusCode = "" --the previously received value for the location event, may be the same as currentPlusCode
previousPlusCode = "" --the previous DIFFERENT pluscode value we visited.
currentHeading = 0
lastScoreLog = ""
lastLocationEvent = ""
tappedAreaName = ""
tappedAreaScore = 0
tappedAreaMapDataId = ""
tappedCell = " "
redrawOverlay = false
factionID = 0 --composer.getVariable(factionID) is used in some spots
requestedCells = ""
--store server bounds in memory on startup.
serverBounds = {}
serverBounds["south"] = -90
serverBounds["west"] = -180
serverBounds["north"] = 90
serverBounds["east"] = 180
playerInBounds = true
function InBounds(lat, lon)
if (lat >= serverBounds["south"] and lat <= serverBounds["north"]) then
if (lon >= serverBounds["west"] and lon <= serverBounds["east"]) then
return true
end
end
return false
end
--Game specific common data for Area Tag.
factions = {}
factions[1] = {}
factions[1].id = 1
factions[1].name = "Red Team"
factions[2] = {}
factions[2].id = 2
factions[2].name = "Green Team"
factions[3] = {}
factions[3].id = 3
factions[3].name = "Blue Team"
--making the network indicator persist through all scenes
networkDown = display.newImageRect("themables/networkDown.png", 25, 25)
networkDown.x = 0
networkDown.y = 0
networkDown.anchorX = 0
networkDown.anchorY = 0
networkUp = display.newImageRect("themables/networkUp.png", 25, 25)
networkUp.x = 0
networkUp.y = 0
networkUp.anchorX = 0
networkUp.anchorY = 0
networkUp.isVisible = false
networkTx = display.newImageRect("themables/networkTransfer.png", 25, 25)
networkTx.x = 0
networkTx.y = 0
networkTx.anchorX = 0
networkTx.anchorY = 0
networkTx.isVisible = false
tapData = display.newText("Cell Tapped:", 20, 1250, native.systemFont, 20)
tapData.anchorX = 0
--OSM License Compliance. Do not remove this line.
--It might be moved, but it must be visible when maptiles are.
--TODO: link to OSM license info when tapped?
local osmLicenseText = display.newText("Map Data © OpenStreetMap contributors", 530, 1250, native.systemFont, 20)
print("shifting to loading scene")
composer.gotoScene("loadingScene")
function gpsListener(event)
if (debugGPS) then
print("main gps fired")
if (event.errorCode ~= nil) then
print("GPS Error " .. event.errorCode)
return
end
print("Coords " .. event.latitude .. " " ..event.longitude)
end
if not InBounds(event.latitude, event.longitude) then
--skip the rest of the processing.
playerInBounds = false
composer.showOverlay("oobOverlay")
return
end
if (not playerInBounds) then
composer.hideOverlay()
end
playerInBounds = true
if (event.direction ~= 0) then
currentHeading = event.direction
end
local pluscode = encodeLatLon(event.latitude, event.longitude, 10); --only goes to 10 right now.
if (debugGPS) then print ("Plus Code: " .. pluscode) end
currentPlusCode = pluscode
local plusCode8 = currentPlusCode:sub(0,8)
if (lastPlusCode ~= currentPlusCode) then
--update score stuff, we moved a cell.
if(debugGPS) then print("calculating score") end
lastScoreLog = "Earned " .. grantPoints(currentPlusCode) .. " points from cell " .. currentPlusCode
lastPlusCode = currentPlusCode
end
if(debugGPS) then print("Finished location event") end
lastLocationEvent = event
end
function backListener(event)
if (debug) then print("key listener got") end
if (event.keyName == "back" and event.phase == "up") then
local currentScene = composer.getSceneName("current")
--TOOD: this detection does not work
if currentScene == "leaveHintOverlay" or currentScene == "creatureOverlay" or currentScene == "creatureList"
or currentScene == "getSecretOverlay" or currentScene == "leaveSecretOverlay" or currentScene == "overlayMPAreaClaim" then
composer.hideOverlay()
elseif (currentScene == "SceneSelect") then
return false
end
if (debug) then print("back to scene select") end
local options = {effect = "flip", time = 125}
composer.gotoScene("SceneSelect", options)
return true
end
if (debug) then print("didn't handle this one") end
end
--function clearMACcache()
--print("clearing tile cache")
--requestedMPMapTileCells = {}
--local temp_path = system.pathForFile( "", system.TemporaryDirectory )
--for file in lfs.dir( temp_path ) do
--os.remove(system.pathForFile( file, system.TemporaryDirectory ))
--end
--end
--timer.performWithDelay(20000, clearMACcache, -1)
Runtime:addEventListener("location", gpsListener)
Runtime:addEventListener("key", backListener)
function netUp()
netCallCount = netCallCount - 1
--print("Active net calls: " .. netCallCount)
--if netCallCount > 0 then
if #networkQueue > 0 then
return
end
networkUp.isVisible = true
networkDown.isVisible = false
networkTx.isVisible = false
end
function netDown(event)
netCallCount = netCallCount - 1
--print("Active net calls: " .. netCallCount)
--if netCallCount > 0 then
if #networkQueue > 0 then
return
end
networkDown.isVisible = true
networkUp.isVisible = false
networkTx.isVisible = false
if (event ~= nil and debug) then
native.showAlert("net error", event.status .. " | " .. string.gsub(event.url, serverURL, "") .. " |" .. event.response)
end
end
function netTransfer()
netCallCount = netCallCount + 1
--print("Active net calls: " .. netCallCount)
networkDown.isVisible = false
networkUp.isVisible = false
networkTx.isVisible = true
end
function DefaultNetCallHandler(event)
if (event.status ~= 200) then
netDown(event)
else
netUp()
end
end
function DefaultQueuedNetCallHandler(event)
if (event.status ~= 200) then
netDown(event)
else
netUp()
end
networkQueueBusy = false
end
function netQueueCheck()
if #networkQueue> 0 and networkQueueBusy == false then
nextNetworkQueue()
end
end
timer.performWithDelay(5, netQueueCheck, -1)