-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.lua
More file actions
469 lines (379 loc) · 10.1 KB
/
Copy pathtask.lua
File metadata and controls
469 lines (379 loc) · 10.1 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
task_lib = {
["walk"] = {
t = 0.5,
show_time = true,
sprite = 164,
task_done = function(s, task)
local pos = task.path[1]
board[s.y][s.x].unit = nil
s.x, s.y = pos.x, pos.y
--color_tile(s.x, s.y, s.faction)
end,
start_task = function(s, task)
local npos = task.path[1]
if not npos then return false end
local b_d = board[npos.y][npos.x]
if b_d.unit or (b_d.building and b_d.building.faction ~= s.faction) then
task.type = "walk_to"
task.to = task.path[#task.path]
task.path = nil
do_remove_task = false
return true
end
local dx = npos.x - s.x
local dy = npos.y - s.y
local dirs = {["-10"] = "walk_left", ["10"] = "walk_right", ["0-1"] = "walk_up", ["01"] = "walk_down"}
local ntask = dirs[""..dx..dy]
s.task = new_task(ntask)
-- s.task = new_task("walk", {path = {npos}})
table.remove(task.path, 1)
if #task.path > 0 then
do_remove_task = false
end
--board[npos.y][npos.x].unit = s
return true
end
},
["walk_to"] = {
t = 0.0,
show_time = false,
sprite = 164,
start_task = function(s, task)
local way = get_path(s.x, s.y, task.to.x, task.to.y, s.faction, s)
if #way == 0 then
return false
end
do_remove_task = false
if server_only then
task.type = "walk"
task.to = nil
task.path = way
else
task.path = way
task_lib.walk.start_task(s, task)
-- local dx = way[1].x - s.x
-- local dy = way[1].y - s.y
-- local dirs = {["-10"] = "walk_left", ["10"] = "walk_right", ["0-1"] = "walk_up", ["01"] = "walk_down"}
-- local ntask = dirs[""..dx..dy]
-- s.task = new_task(ntask)
return true
end
return false
end
},
["walk_left"] = {
t = 0.5,
show_time = true,
sprite = 160,
condition = function(s, task)
return condition_walk_dir(s,task,-1,0)
end,
task_done = function(s, task)
done_walk_dir(s,task,-1,0)
end,
},
["walk_right"] = {
t = 0.5,
show_time = true,
sprite = 161,
condition = function(s, task)
return condition_walk_dir(s,task,1,0)
end,
task_done = function(s, task)
done_walk_dir(s,task,1,0)
end,
},
["walk_up"] = {
t = 0.5,
show_time = true,
sprite = 162,
condition = function(s, task)
return condition_walk_dir(s,task,0,-1)
end,
task_done = function(s, task)
done_walk_dir(s,task,0,-1)
end,
},
["walk_down"] = {
t = 0.5,
show_time = true,
sprite = 163,
condition = function(s, task)
return condition_walk_dir(s,task,0,1)
end,
task_done = function(s, task)
done_walk_dir(s,task,0,1)
end,
},
["juice"] = {
t = 0.5,
show_time = true,
cost = 1,
sprite = 169,
condition = function(s, task)
local b_d = board[s.y][s.x]
return (b_d.faction ~= s.faction)
end,
task_done = function(s, task)
color_tile(s.x, s.y, s.faction)
end
},
["duplicate"] = {
t = 5,
show_time = true,
cost = 30,
sprite = 170,
--condition = function(s, task)
-- local b_d = board[s.y][s.x]
-- return (b_d.faction ~= s.faction)
--end,
task_done = function(s, task)
--color_tile(s.x, s.y, s.faction)
create_unit(s.x, s.y, s.faction)
end
},
["attack"] = {
t = 0,
show_time = false,
sprite = 165,
condition = function(s, task)
return not dead_ids[task.target]
end,
start_task = function(s, task)
local target = entities[task.target]
if not target then return false end
if abs(target.x-s.x)+abs(target.y-s.y)>1 then
local way = get_path(s.x, s.y, target.x, target.y, s.faction, s)
if #way == 0 then
return false
end
local npos = way[1]
local b_d = board[npos.y][npos.x]
if b_d.unit or (b_d.building and b_d.building.faction ~= s.faction) then
s.task = nil
do_remove_task = false
return true
end
--board[npos.y][npos.x].unit = s
local dx = npos.x - s.x
local dy = npos.y - s.y
local dirs = {["-10"] = "walk_left", ["10"] = "walk_right", ["0-1"] = "walk_up", ["01"] = "walk_down"}
local ntask = dirs[""..dx..dy]
s.task = new_task(ntask)
--s.task = new_task("walk", { path = {npos}})
else
s.task = new_task("hit", { target = task.target })
end
do_remove_task = false
return true
end
},
["hit"] = {
t = 0.4,
shot_time = true,
sprite = 165,
condition = function(s, task)
local target = entities[task.target]
return target and abs(target.x-s.x)+abs(target.y-s.y)<=1
end,
task_done = function(s, task)
local target = entities[task.target]
if (not target) or abs(target.x-s.x)+abs(target.y-s.y)>1 then
return
else
do_damage(s, target)
end
end
},
["build_wall"] = {
t = 1.0,
show_time = true,
cost = 10,
sprite = 166,
condition = function(s, task)
local b_d = board[s.y][s.x]
return not (b_d.building or b_d.wall or b_d.resource)
end,
task_done = function(s, task)
color_tile(s.x, s.y, s.faction)
create_building(s.x, s.y, nil, s.faction)
add_shake(1)
end
},
["build_prod"] = {
t = 10,
show_time = true,
cost = 50,
sprite = 166,
condition = function(s, task)
local b_d = board[s.y][s.x]
return not (b_d.building or b_d.wall or b_d.resource)
end,
task_done = function(s, task)
color_tile(s.x, s.y, s.faction)
create_building(s.x, s.y, task.produce, s.faction)
add_shake(3)
end
},
["prod_res"] = {
t = 0.5,
show_time = true,
cost = 20,
sprite = 168,
task_done = function(s, task)
faction_res[s.faction] = faction_res[s.faction] + 1
assign_task(s, task, true)
end
},
["prod_unit"] = {
t = 5,
show_time = true,
sprite = 167,
task_done = function(s, task)
create_unit(s.x, s.y, s.faction)
castle_print("Created unit for faction "..s.faction.."at x"..s.x.." y"..s.y)
end
},
["ressource_tile"] = {
t = 2,
show_time = true,
sprite = 175
}
}
function update_task(s)
if s.task then
-- task progress
local info = task_lib[s.task.type]
s.task_t = s.task_t + delta_time
if s.task_t >= info.t then
info.task_done(s, s.task)
s.task = nil
s.task_t = s.task_t - info.t
end
if s.task and s.task.type == "hit" and not info.condition(s, s.task) then
s.task = nil
end
end
if not s.task then
::process_next_task::
local ntask = s.task_queue[1]
if ntask then
local info = task_lib[ntask.type]
if (not info.condition) or info.condition(s, ntask) then
do_remove_task = true
if info.start_task then
if not info.start_task(s, ntask) then
if do_remove_task then
table.remove(s.task_queue, 1)
end
goto process_next_task
end
else
s.task = ntask
end
if do_remove_task then
table.remove(s.task_queue, 1)
end
else
table.remove(s.task_queue, 1)
goto process_next_task
end
else
s.task_t = 0
end
if s.task and s == selected and s.faction == my_faction then
sfx("newtask")
end
end
end
function assign_task(s, task, in_queue)
if not task then return false end
local info = task_lib[task.type]
if info.cost then
if faction_res[s.faction] < info.cost then
return false
end
faction_res[s.faction] = faction_res[s.faction] - info.cost
end
if in_queue then
add(s.task_queue, task)
else
s.task_queue = {task}
end
end
function new_task(type, info)
local task = { type = type }
if info then
for n,v in pairs(info) do
task[n] = v
end
end
return task
end
function copy_task(task)
if not task then return nil end
local new_task = {}
for n,v in pairs(task) do
if n == "path" then
new_task.path = copy_table(v)
else
new_task[n] = v
end
end
return new_task
end
function clear_task_queue(s)
while cancel_last_task(s) do end
end
function cancel_last_task(s)
local n = #s.task_queue
if n > 0 then
local info = task_lib[s.task_queue[n].type]
if info.cost then -- refund
faction_res[s.faction] = faction_res[s.faction] + info.cost
end
table.remove(s.task_queue, n)
return true
end
return false
end
function is_task_possible(s, task)
local info = task_lib[task.type]
return ((not info.condition) or info.condition(s, task))
end
function same_task(ta, tb)
if not ta or not tb then
return (ta == tb)
end
if ta.type ~= tb.type then
return false
end
if ta.type == "walk" then
if not tb.path then error("on tb path") end
if not ta.path then error("on ta path") end
if not tb.path[1] then error("on tb path 1 : "..#tb.path) end
if not ta.path[1] then error("on ta path 1 : "..#ta.path) end
return (ta.path[1].x == tb.path[1].x and ta.path[1].y == tb.path[1].y)
elseif ta.type == "walk_to" then
return (ta.to.x == tb.to.x and ta.to.y == tb.to.y)
elseif ta.type == "attack" or ta.type == "hit" then
return (ta.target == tb.target)
elseif ta.type == "build_prod" then
return (ta.produce == tb.produce)
end
end
function condition_walk_dir(s,task,dx,dy)
if s.x+dx < 0 or s.x+dx >= GRID_WN or s.y+dy < 0 or s.y+dy >= GRID_HN then return false end
local b_d = board[s.y+dy][s.x+dx]
return not (b_d.wall or b_d.unit or (b_d.building and b_d.building.faction ~= s.faction))
end
function done_walk_dir(s,task,dx,dy)
board[s.y][s.x].unit = nil
s.x = s.x+dx
s.y = s.y+dy
local b_d = board[s.y][s.x]
b_d.unit = s
if b_d.resource then
harvest_resource(b_d.resource, s)
end
end