-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.lua
403 lines (328 loc) · 8.69 KB
/
ai.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
function AiLoad()
harbor = false
soldierCnt = 0
end
function ai(player, _base)
if player.currentEnergy ~= 0 then
print('-------------------- AI TURN --------------------')
if not _units then
_units = players:getUnits()
end
soldierCnt = 0
-- if units = 0 spawn a worker
if #_units == 0 then
-- spawn a worker
aiSpawnByID(player, _base, 1, true)
end
-- if not firstBoat then
_units = players:getUnits()
-- end
-- check if boat is full
for _,units in pairs(_units) do
if units.unit.type == 'boot'
and #units.unit.passengers == 3 then
_boat = true
end
end
if not coast_tile then
coast_tile = findCoast()
end
if not harbor then
for _,units in pairs(_units) do
if units.unit.type == 'worker' then
if aiWalkToCordinates(units, coast_tile.corX, coast_tile.corY) then
if buildHarbor(units, coast_tile) then
harbor = coast_tile
end
end
end
end
if #_units < 4 then
aiSpawnByID(player, _base, 2, true)
end
end
if harbor and not firstBoat then
aiSpawnByID(player, coast_tile, 1, false)
firstBoat = true
end
if firstBoat then
for _,units in pairs(_units) do
if units.unit.type == 'worker' then
mousepressTile(units)
getInBoat()
end
end
if aiWalkToCordinates(_units[1], coast_tile.corX, coast_tile.corY, soldier) then
mousepressTile(_units[1])
if getInBoat() then
-- aiSpawnByID(player, _base, 2, true)
end
end
end
-- if harbor then
-- aiSpawn(player, _base, 2)
-- end
-- if #units > 0 and not _boat then
-- -- aiWalkForwards(player, units[1])
-- if #units < 2 then
-- aiSpawn(player, _base, 2)
-- end
-- units = players:getUnits()
-- -- for _,i in pairs(units) do
-- -- if i.unit.type == 'boot' then
-- -- end
-- -- end
-- if units[1].unit.type == 'boot' then
-- aiWalkTo(player, units[1], units[2])
-- else
-- aiWalkTo(player, units[2], units[1])
-- end
-- -- aiWalkSidewards(player, units[1])
-- else
-- aiWalkToCordinates(units[1], -200, -200)
-- end
-- _walkable = findWalkables()
-- _unit = getRandomUnit()
-- if _walkable then
-- love.mousepressed(_walkable.x+40, _walkable.y + 40, 2)
-- elseif _unit then
-- love.mousepressed(_unit.x + 40, _unit.y + 40, 2)
-- _walkable = findWalkables()
-- if _walkable then
-- love.mousepressed(_walkable.x+40, _walkable.y + 40, 2)
-- end
-- end
-- delay_s(player, _base,1)
end
end
function aiSpawn(player, _base, id)
-- click on the base
love.mousepressed(_base.x + 40, _base.y + 40, 2)
-- loop through spawnable units
for i,unit in pairs(unitspawn.units) do
-- get "random unit"
if i == 2 then
-- click on unit you want to spawn
love.mousepressed(unit.x + 40, unit.y + 40, 2)
-- get all the walkables
_walkable = findWalkables()
if _walkable then
-- if there is a walkable click on it
love.mousepressed(_walkable.x+40, _walkable.y + 40, 2)
end
end
end
end
function aiWalkRight(unit)
mousepressTile(unit)
_newTileRight = board.getTile(unit.corX, unit.corY+1)
if not _newTileRight then
return false
end
if (_newTileRight and not _newTileRight.occupied and unit.unit.type ~= 'boot' and _newTileRight.type ~= 4) or (unit.unit.type == 'boot' and _newTileRight.type == 4) then
mousepressTile(_newTileRight)
return true
else
mousepressTile(unit)
return false
end
end
function aiWalkLeft(unit)
mousepressTile(unit)
_newTileLeft = board.getTile(unit.corX, unit.corY-1)
if ((_newTileLeft and not _newTileLeft.occupied and unit.unit.typet ~= 'boot' and _newTileLeft.type ~= 4) or (unit.unit.type == 'boot' and _newTileLeft.type == 4)) then
mousepressTile(_newTileLeft)
return _newTileLeft
else
mousepressTile(unit)
return false
end
end
function aiWalkUp(unit)
mousepressTile(unit)
_newTileUp = board.getTile(unit.corX-1, unit.corY)
if (_newTileUp and not _newTileUp.occupied and unit.unit.type ~= 'boot' and _newTileUp.type ~= 4) or (unit.unit.type == 'boot' and _newTileUp.type == 4) then
mousepressTile(_newTileUp)
return true
else
mousepressTile(unit)
return false
end
end
function aiWalkDown(unit)
mousepressTile(unit)
_newTileDown = board.getTile(unit.corX+1, unit.corY)
if (_newTileDown and not _newTileDown.occupied and unit.unit.type ~= 'boot' and _newTileDown.type ~= 4) or (_newTileDown and unit.unit.type == 'boot' and _newTileDown.type == 4) then
mousepressTile(_newTileDown)
return true
else
mousepressTile(unit)
return false
end
end
function aiWalkToCordinates(unit, corX, corY, soldier)
if (unit.corX-1 == corX
or unit.corX+1 == corX
or unit.corX == corX)
and (unit.corY-1 == corY
or unit.corY+1 == corY
or unit.corY == corY) then
return true
end
if unit.corY > corY and aiWalkLeft(unit) then
-- print('walkleft -> ', aiWalkLeft(unit))
print('walkleft -> ')
elseif unit.corY < corY and aiWalkRight(unit) then
-- print('walkright -> ', aiWalkRight(unit))
print('walkright -> ')
elseif unit.corX > corX and aiWalkUp(unit) then
-- print('walkUp -> ', aiWalkUp(unit))
print('walkUp -> ')
elseif unit.corX < corX and aiWalkDown(unit) then
print('walkDown -> ')
-- print('walkDown -> ', aiWalkDown(unit))
else
aiWalkDown(aiWalkLeft(unit))
end
end
function aiWalkTo(player, boot, unit)
hor = boot.corX - unit.corX
vert = boot.corY - unit.corY
-- go to vertical
if vert > 0 then
aiWalkRight(unit)
elseif vert < 0 then
aiWalkLeft(unit)
end
-- got to bottom
if hor > 0 then
aiWalkDown(unit)
elseif hor < 0 then
aiWalkUp(unit)
end
getInBoat()
end
function getInBoat()
for i,tile in pairs(board.tiles) do
if tile.loadable
and not tile.base then
mousepressTile(tile)
return true
end
end
end
function buildHarbor(_unit, _tile)
if not _tile.harbor then
mousepressTile(_unit)
mousepressTile(_tile)
else
return true
end
end
function mousepressTile(tile)
love.mousepressed(tile.x + 40, tile.y + 40, 2)
end
function aiSpawnByID(player, _base, id, move)
-- click on the base
love.mousepressed(_base.x + 40, _base.y + 40, 2)
-- loop through spawnable units
for i,unit in pairs(units) do
-- get "random unit"
if i == id then
-- click on unit you want to spawn
love.mousepressed(unit.x + 40, unit.y + 40, 2)
-- get all the walkables
_walkable = findWalkables()
-- if move is false dont spawn anywere
if _walkable
and move then
-- if there is a walkable click on it
love.mousepressed(_walkable.x+40, _walkable.y + 40, 2)
else
love.mousepressed(_base.x+40, _base.y + 40, 2)
end
end
end
end
function aiWalkForwards(player, unit)
love.mousepressed(unit.x+40, unit.y + 40, 2)
if unit.corX > 10 then
_tile = board.getTile(unit.corX-1, unit.corY)
if _tile.type ~= 4 then
love.mousepressed(_tile.x+40, _tile.y + 40, 2)
else
love.mousepressed(unit.x+40, unit.y + 40, 2)
return false
end
else
_tile = board.getTile(unit.corX+1, unit.corY)
if _tile.type ~= 4 then
love.mousepressed(_tile.x+40, _tile.y + 40, 2)
else
love.mousepressed(unit.x+40, unit.y + 40, 2)
return false
end
end
return aiWalkForwards(player, _tile)
end
function aiWalkSidewards(player, unit)
if unit.type ~= 4 then
love.mousepressed(unit.x+40, unit.y + 40, 2)
if unit.corY > 10 then
_tile = board.getTile(unit.corX, unit.corY-1)
love.mousepressed(_tile.x+40, _tile.y + 40, 2)
else
_tile = board.getTile(unit.corX, unit.corY+1)
love.mousepressed(_tile.x+40, _tile.y + 40, 2)
end
return aiWalkSidewards(player, _tile)
else
return false
end
end
function getRandomUnit()
_units = {}
for i,tile in pairs(board.tiles) do
if tile.occupied
and not tile.base
and tile.unit
and tile.owner == players:getActivePlayerId() then
table.insert(_units, tile)
end
end
if #_units == 0 then
return false
end
return _units[math.random(#_units)]
end
function findWalkables()
local _walkables = {}
for i,tile in pairs(board.tiles) do
if tile.walkable
and not tile.occupied
and not tile.base then
table.insert(_walkables, tile)
end
end
-- _walkables[math.random(#_walkables)]
if #_walkables == 0 then
return false
end
return _walkables[math.random(#_walkables)]
end
function findCoast()
local _walkables = {}
for i,tile in pairs(board.tiles) do
if tile.coast
and tile.originalOwner == players:getActivePlayerId()
and not tile.harbor
and not tile.occupied then
table.insert(_walkables, tile)
end
end
-- _walkables[math.random(#_walkables)]
if #_walkables == 0 then
return false
end
return _walkables[math.random(#_walkables)]
end