-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_elements.lua
More file actions
611 lines (487 loc) · 13.3 KB
/
Copy pathgame_elements.lua
File metadata and controls
611 lines (487 loc) · 13.3 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
available_colors = {15, 3, 12, 6, 9, 18}
faction_color = {21,21,21,21}
colors = {15, 18, 3, 6, 12}
default_names = {
[15] = "Blueberry",
[18] = "Grape",
[3] = "Strawberry",
[6] = "Lemon",
[9] = "Coconut",
[12] = "Lime"
}
is_default_name = { ["Hello!"] = true }
for _,str in pairs(default_names) do
is_default_name[str] = true
end
function update_unit(s)
s.animt = s.animt + delta_time
if s.state == "idling" then
local a,b,c = anim_step("unit", "idling", s.animt)
if c > 0 then
s.state = "idle"
end
end
s.blinkt = s.blinkt - delta_time
if s.blinkt < 0 then
s.state = "idling"
s.animt = 0
if selected == s then
s.blinkt = 0.5
else
s.blinkt = 1+rnd(1)
end
end
if not server_only and s.hp < s.maxhp/2 and fx_t <= 0 and chance(10) then
local x,y = board_to_screen(s.x, s.y)
local c = faction_color[s.faction]
create_smoke(x, y, 0.25+rnd(0.25), 0.5+rnd(2), pick{c, c_drk[c], c_drk[x], 0, 0})
end
update_task(s)
board[s.y][s.x].unit = s
end
function get_path(ax, ay, bx, by, faction, s)
if ax==bx and ay==by then return {} end
-- check for impossible destination
local b_d = board[by][bx]
if b_d.wall or (b_d.unit and b_d.unit.faction == faction) then
return {}
end
local way = {}
local map={[ay*GRID_WN+ax]=0, [by*GRID_WN+bx]=128}
local doors_a={{x=ax,y=ay,v=0}}
local doors_b={{x=bx,y=by,v=128}}
local found = nil
local dirs={{x=-1,y=0},{x=1,y=0},{x=0,y=-1},{x=0,y=1}}
while not found do
local ndoors_a = {}
local ndoors_b = {}
for d in all(doors_a) do
for di in all(dirs) do
local x = d.x + di.x
local y = d.y + di.y
if x<0 or y<0 or x>=GRID_WN or y>=GRID_HN then
-- edge of the world
else
local m = map[y*GRID_WN+x]
if m then
if m > 63 then
local b
for bb in all(doors_b) do
if bb.x == x and bb.y == y then
b = bb
break
end
end
found = {x=x, y=y, a=d, b=b}
break
end
else
local b_d = board[y][x]
if not (b_d.wall or (b_d.unit and b_d.unit ~= s) or (b_d.building and b_d.building.faction ~= faction)) then
add(ndoors_a, {x=x, y=y, v=d.v+1, p=d})
map[y*GRID_WN+x]=d.v+1
end
end
end
if found then break end
end
if found then break end
end
doors_a = ndoors_a
if not found then
for d in all(doors_b) do
for di in all(dirs) do
local x = d.x + di.x
local y = d.y + di.y
if x<0 or y<0 or x>=GRID_WN or y>=GRID_HN then
-- edge of the world
else
local m = map[y*GRID_WN+x]
if m then
if m < 64 then
local a
for aa in all(doors_a) do
if aa.x == x and aa.y == y then
a = aa
break
end
end
found = {x=x, y=y, a=a, b=d, v=(a and a.v or "a?")}
break
end
else
local b_d = board[y][x]
if not (b_d.wall or (b_d.unit and b_d.unit ~= s) or (b_d.building and b_d.building.faction ~= faction)) then
add(ndoors_b, {x=x, y=y, v=d.v-1, p=d})
map[y*GRID_WN+x]=d.v-1
end
end
end
if found then break end
end
if found then break end
end
end
doors_b = ndoors_b
if not found and (#doors_a == 0 or #doors_b == 0) then
return {}
end
end
local a = found.a
local halfa = {}
while a.p do
add(halfa, a)
a = a.p
end
for i=#halfa,1,-1 do
local a=halfa[i]
a.p = nil
--a.v = nil
add(way, a)
end
-- add(way, {x=found.x, y=found.y, v=found.v})
local b = found.b
while b do
local p=b.p
b.p = nil
--b.v = nil
add(way, b)
b = p
end
return way
end
function do_damage(s, target)
target.hp = target.hp - (1+irnd(2))
sfx("hurtbot",nil,nil,nil,(target.faction == my_faction) and 1 or 0.25)
if target.hp <= 0 and server_only then
target:die()
local fac = target.faction
if server_only and group_size("unit"..fac) == 0 then
local pos
for _,p in pairs(spawn_points) do
if p.fac == fac then pos = p break end
end
if pos then
create_unit(pos.x, pos.y, fac)
end
end
--color_tile(target.x, target.y, s.faction)
else
if #target.task_queue == 0 and target.type == "unit" then
assign_task(target, new_task("hit", {target = s.id}))
end
local x, y = board_to_screen(target.x, target.y)
create_explosion(x, y, 6, faction_color[fac])
end
end
function draw_unit(s, x,y)
if not x then
x,y = board_to_screen(s.x, s.y)
end
-- circfill(x,y+1,3,23)
-- circfill(x,y,3,faction_color[s.faction])
-- circfill(x,y,1,23)
faction_pal(s.faction)
draw_anim(x, y-5, "unit", s.state, s.animt)
-- spr(80, x, y, 2, 1)
faction_pal()
end
function create_unit(tx,ty,faction,id)
if not id and not server_only then return end
while board[ty][tx].unit do
ty = ty + flr(rnd(3))-1
tx = tx + flr(rnd(3))-1
end
local s = {
animt = 0,
blinkt = 0,
type = "unit",
name = "unit",
state = "idle",
x = tx,
y = ty,
task = nil,
task_t = 0,
task_queue = {},
maxhp = 18,
faction = faction or 1,
update = update_unit,
draw = draw_unit,
die = destroy_unit,
regs = {"to_draw2", "to_update", "unit", "task_doer", "unit"..faction}
}
s.hp = s.maxhp
board[ty][tx].unit = s
if id then
entities[id], s.id = s, id
entity_id = max(entity_id, id+1)
else
entities[entity_id], s.id, entity_id = s, entity_id, entity_id + 1
end
if not server_only then
local c_id = client.share[14][faction]
s.player_name = client.share[7][c_id]
end
local x,y = board_to_screen(s.x, s.y)
local c = faction_color[s.faction]
for i=1,16 do
create_smoke(x, y, 1.5, 0.5+rnd(2), pick{23, 23, c, c, c_drk[c], c_lit[c]})
end
sfx("newunit",nil,nil,nil,(faction == my_faction) and 1 or 0.25)
register_object(s)
castle_print("Unit [id:"..s.id.." - faction:"..s.faction.."] was created.")
return s
end
function destroy_unit(s)
local x, y = board_to_screen(s.x, s.y)
create_explosion(x, y, 10, faction_color[s.faction])
add_shake(2)
sfx("deadbot")
scorch_ground(s.x, s.y, s.faction)
board[s.y][s.x].unit = nil
entities[s.id] = nil
s.dead = true
dead_ids[s.id] = true
deregister_object(s)
if selected == s then
selected = nil
refresh_control_ui()
end
castle_print("Unit [id: "..s.id.." - faction: "..s.faction.."] died.")
end
function draw_task_timer(x,y,task,t)
if not task then return end
local t_d = task_lib[task.type]
if not t_d.show_time then return end
local w = 10
local h = 4
y = y-h/2
x = x-w/2
rectfill(x-1,y-1,x+w+1,y+h+1,23)
local ww = (t/t_d.t)*(w-1)
rectfill(x+1,y+1,x+1+ww,y+h,21)
rect(x,y,x+w,y+h-1,20)
end
function draw_healthbar(s,x,y)
local w = 10
local h = 4
y = y-h/2
x = x-w/2
local c = faction_color[s.faction]
rectfill(x-1,y-1,x+w+1,y+h+1,23)
local ww = (s.hp/s.maxhp)*(w-1)
rectfill(x+1,y+1,x+1+ww,y+h-1,c)
rect(x,y,x+w,y+h-1,c_drk[c])
end
function draw_taskprevision(s)
if not s then return end
if s.faction ~= my_faction then return end
local steps = {}
local x,y = s.x, s.y
local do_step = function(task)
if not task then return end
local dx,dy
if task.type == "walk_left" then dx = -1
elseif task.type == "walk_right" then dx = 1
elseif task.type == "walk_up" then dy = -1
elseif task.type == "walk_down" then dy = 1
elseif task.type == "walk_to" then x,y = task.to.x,task.to.y
elseif task.type == "walk" then x,y = task.path[#task.path].x, task.path[#task.path].y
elseif task.type == "attack" then
local target = entities[task.target]
if target then
x,y = target.x, target.y
end
end
if dx or dy then
x = x + (dx or 0)
y = y + (dy or 0)
end
local stp = steps[y*GRID_WN+x] or 0
local c = faction_color[s.faction]
local sp = task_lib[task.type].sprite
local xx,yy = board_to_screen(x,y)
yy = yy - stp*3
pal(0,23)
spr(sp, xx, yy+2)
pal(0,c)
spr(sp, xx, yy+1)
pal(0,0)
spr(sp, xx, yy)
steps[y*GRID_WN+x] = stp+1
end
do_step(s.task)
for _,task in ipairs(s.task_queue) do
do_step(task)
end
end
function draw_selected()
if not selected or game_over then return end
local x,y = board_to_screen(selected.x, selected.y)
--circ(x, y, 5+1.5*cos(t), 21)
local d = round(6.5+1.5*cos(t*0.75))
spr(262, x-d, y-d)
spr(263, x+d+1, y-d)
spr(278, x-d, y+d+1)
spr(279, x+d+1, y+d+1)
--rect(x-d, y-d, x+d, y+d, 23) d = d+2
--rect(x-d, y-d, x+d, y+d, 23) d = d-1
--rect(x-d, y-d, x+d, y+d, 0)
-- selected:draw()
-- if mini_menu then
-- draw_minimenu(selected, x, y + 0.75*TILE_H)
-- end
end
function update_building(s)
s.animt = s.animt + delta_time
update_task(s)
end
function draw_building(s, x,y)
local on_board
if not x then
on_board = true
x,y = board_to_screen(s.x, s.y)
end
-- rectfill(x-4,y-3,x+4,y+5,23)
-- rectfill(x-4,y-4,x+4,y+4,faction_color[s.faction])
-- rectfill(x-2,y-2,x+2,y+2,23)
faction_pal(s.faction)
if s.produce then
-- production building
local s = ({resource = 96, unit = 128})[s.produce]
spr(s, x, y-4, 2, 2)
else
-- wall
if not on_board then
spr(65, x, y)
end
end
faction_pal()
end
function create_building(x, y, produce, faction, id)
if not id and not server_only then return end
local s = {
animt = 0,
type = "building",
produce = produce,
state = "idle",
x = x,
y = y,
task = nil,
task_t = 0,
task_queue = {},
maxhp = produce and 36 or 45,
faction = faction or 1,
update = update_building,
draw = draw_building,
die = destroy_building,
regs = {"to_update", "to_draw1", "building", "task_doer"}
}
s.hp = s.maxhp
if produce == "resource" then
assign_task(s, new_task("prod_res"))
add(s.regs, "res_building"..faction)
end
local b_d = board[y][x]
b_d.building = s
if produce then
for j = y-1, y+1 do
for i = x-1, x+1 do
color_tile(i, j, faction)
end
end
else
update_wallsurf(x, y, faction, true)
end
if id then
entities[id], s.id = s, id
entity_id = max(entity_id, id+1)
else
entities[entity_id], s.id, entity_id = s, entity_id, entity_id + 1
end
if not server_only then
local c_id = client.share[14][faction]
s.player_name = client.share[7][c_id]
end
sfx("newwall",nil,nil,nil,(faction == my_faction) and 1 or 0.25)
register_object(s)
castle_print("Building [id:"..s.id.." - faction:"..s.faction.."] was created.")
return s
end
function destroy_building(s)
board[s.y][s.x].building = nil
entities[s.id] = nil
s.dead = true
dead_ids[s.id] = true
add_shake(1)
sfx("deadwall")
if not s.produce then
update_wallsurf(s.x, s.y, s.faction, false)
end
deregister_object(s)
castle_print("Building [id:"..s.id.." - faction:"..s.faction.."] died.")
end
function update_resource(s)
s.prodt = s.prodt - delta_time
if s.prodt < 0 and s.hoard < 99 then
s.hoard = s.hoard + 1
s.prodt = s.prodt + s.prod
local x,y = board_to_screen(s.x, s.y)
for i=1,3 do
create_smoke(x, y, 1.5, 0.5+rnd(2), pick(colors),-0.15-rnd(0.2))
end
local u = board[s.y][s.x].unit
if u then
harvest_resource(s, u)
end
end
end
function draw_resource(s)
local x,y = board_to_screen(s.x, s.y)
-- spr(34+flr(s.prodt*8)%4, x, y)
draw_anim(x, y-4, "resource", nil, 1-s.prodt/s.prod)
-- font("small")
-- draw_text(""..s.hoard, x, y-9, 1, 0, 22, 23)
end
function harvest_resource(s, harvester)
if not server_only then return end
local fac = harvester.faction
faction_res[fac] = faction_res[fac] + s.hoard
s.hoard = 0
s.taker = fac
end
function create_resource(x, y, rate_per_sec, id)
local prod = 1/(rate_per_sec or 1)
local s = {
type = "res",
x = x,
y = y,
prod = prod,
prodt = prod,
rate = rate_per_sec,
hoard = 0,
update = update_resource,
draw = draw_resource,
die = destroy_resource,
regs = {"to_update", "to_draw2", "resource"}
}
local b_d = board[y][x]
b_d.resource = s
if id then
entities[id], s.id = s, id
entity_id = max(entity_id, id+1)
else
entities[entity_id], s.id, entity_id = s, entity_id, entity_id + 1
end
register_object(s)
castle_print("Resource point [id:"..s.id.."] was created.")
return s
end
function destroy_resource(s)
board[s.y][s.x].resource = nil
entities[s.id] = nil
s.dead = true
dead_ids[s.id] = true
deregister_object(s)
castle_print("Resource [id:"..s.id.."] was removed.")
end