-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperson.script
More file actions
636 lines (589 loc) · 20.2 KB
/
person.script
File metadata and controls
636 lines (589 loc) · 20.2 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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
local bounce_height = 5 -- Height of the bounce (how high the sprite will go)
local bounce_speed = 5 -- Speed at which the sprite will bounce up and down
local function get_offset(self)
return vmath.vector3(math.random(-150,150), math.random(-150,150), 0)
end
local global = require "main.global" -- Import the module
function init(self)
self.state = "Idle"
self.start_pos = go.get_position()
self.offset = 100
self.new_pos = vmath.vector3()
self.speed = 50
self.todo = "Idle"
self.harvest_time = 0.5 + math.random()
self.auto = true
self.harvest_amount = 1
self.eat_time = math.random(15,50)
self.drink_time = math.random(5,30)
self.health = 100
self.rockets = {}
self.attack_time = 3
self.scan_time = 1
self.damage = 0
self.target = nil
self.stopping_distance = 25
self.heal_time = 10
self.attack_offset = get_offset(self)
self.bounce_offset = 0
self.accuracy = 15
self.food = 500
self.build_time = 1
self.build_amount = 10
self.train_time = 1
self.arrow_type = "stone"
self.smith_time = 1
self.running_away = false
self.attack_type = "Meele"
self.ranged_weapon = "slingshot"
self.meele_weapon = "club"
self.ranged_durability = 100
self.meele_durability = 100
self.max_ranged_durability = self.ranged_durability
self.max_meele_durability = self.meele_durability
self.ranged_damage = 1
self.meele_damage = 1
self.max_resources = 50
self.unload_time = 0.1
self.inventory = {}
msg.post("#weapon", "disable")
end
local function get_nearest(self, type)
msg.post("/initfactory#init_spawner", "get_nearest", {pos = go.get_position(), type = global.capitalize(self.target_type)})
end
local function update_alpha(self)
-- Set sprite color to red
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 0, 0, 1))
-- Reset color after 1 second
timer.delay(0.5, false, function()
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 1, 1, 1)) -- Back to normal
end)
end
local function scan(self, dt)
self.scan_time = self.scan_time - dt
if self.scan_time < 0.5 then
msg.post("/initfactory#init_spawner", "scan_person_radius", {person_pos = go.get_position()})
self.scan_time = 1
end
end
local function bounce(self, dt)
self.bounce_offset = self.bounce_offset + bounce_speed * dt
local new_y = self.start_pos.y + math.sin(self.bounce_offset) * bounce_height
local new_pos = vmath.vector3(self.start_pos.x, new_y, self.start_pos.z)
go.set_position(new_pos)
end
local function move(self, dt)
local current_pos = go.get_position()
if self.todo == "Fighting" and self.attack_type == "Ranged" then
self.new_pos = go.get_position(self.target) + self.attack_offset
end
msg.post("#personUI", "line", {r_id = go.get_id(), start_pos = go.get_position(), end_pos = self.new_pos, todo = self.todo})
self.dir = vmath.normalize(self.new_pos - current_pos) -- Get direction to target
local distance = vmath.length(self.new_pos - current_pos) -- Distance to target
if distance >= self.stopping_distance then -- Stop moving if close enough
local step = self.speed * dt -- Calculate step size based on speed
local new_position = current_pos + self.dir * math.min(step, distance) -- Move towards target
go.set_position(new_position)
self.start_pos = go.get_position()
if self.todo == "Fighting" and self.attack_type == "Meele" then
self.stopping_distance = 5
elseif self.stopping_distance == 5 then
self.stopping_distance = 25
end
else
msg.post("#personUI", "done", {r_id = go.get_id(), pos = go.get_position()})
self.state = self.todo -- Stop moving once close enough
self.todo = "Idle"
self.start_pos = go.get_position()
self.running_away = false
end
if self.todo == "Fighting" and distance < 175 and self.attack_type == "Ranged" then
msg.post("#personUI", "done", {r_id = go.get_id(), pos = go.get_position()})
self.state = self.todo -- Stop moving once close enough
self.todo = "Idle"
self.start_pos = go.get_position()
self.attack_offset = get_offset(self)
self.running_away = false
end
end
local function harvest(self, dt)
self.harvest_time = self.harvest_time - dt
if self.harvest_time > 0 then return end -- Early exit if not enough time passed
local type = string.lower(self.target_type)
local collection = _G[type .. "s"]
if type == "tree" then
type = "wood"
end
if type and global.contains(collection, self.target) then
--msg.post("/gui#ui", "resource_update", {type = type, amount = self.harvest_amount})
if #self.inventory + self.harvest_amount <= self.max_resources then
for i = 0, self.harvest_amount, 1 do
table.insert(self.inventory, type)
end
msg.post(self.target, "harvested", {
person_id = go.get_id(),
amount = self.harvest_amount
})
local picker = math.random(1, 10000)
if picker < 5 then
self.harvest_amount = self.harvest_amount + 1
end
self.harvest_time = 0.5 + math.random() -- Reset timer
else
self.stopping_distance = 50
msg.post(go.get_id(), "move", {pos = _G.base_pos, target = _G.bases[1], todo = "Unloading", target_type = self.target_type})
end
end
end
local function unload(self, dt)
self.unload_time = self.unload_time - dt
if self.unload_time < 0 then
msg.post("/gui#ui", "resource_update", {type = self.inventory[1], amount = 1})
table.remove(self.inventory, 1)
if #self.inventory == 0 then
self.stopping_distance = 25
get_nearest(self, self.target_type)
end
self.unload_time = 0.1
end
end
local function animate_weapon(self)
msg.post("#weapon", "enable")
local anim = nil
if not self.ranged_weapon and self.meele_weapon then
self.attack_type = "Meele"
elseif not self.meele_weapon and self.ranged_weapon then
self.attack_type = "Ranged"
end
if self.attack_type == "Ranged" then
if self.ranged_weapon == "slingshot" then
anim = hash("slingshot")
elseif self.ranged_weapon == "bow" then
anim = hash("bow")
end
elseif self.attack_type == "Meele" then
if self.meele_weapon == "club" then
anim = hash("club")
elseif self.meele_weapon == "sword" then
anim = hash("sword")
end
end
if anim then
msg.post("#weapon", "play_animation", {id = anim})
end
end
local function animate(self)
local anim = hash("person-idle")
if self.meele_weapon or self.ranged_weapon and (self.state == "Fighting" or self.state == "Training") then
animate_weapon(self)
else
msg.post("#weapon", "disable")
end
if self.state == "Moving" then
anim = hash("person-moving")
end
if self.state == "Harvesting" then
if self.auto then
anim = hash("person-auto-harvesting")
else
anim = hash("person-harvesting")
end
end
if self.state == "Selected" then
anim = hash("person-selected")
end
if self.state == "Fighting" then
anim = hash("person-fighting")
end
if self.state == "Building" then
anim = hash("person-building")
end
if self.state == "Training" then
anim = hash("person-training")
end
if self.state == "Smithing" then
anim = hash("person-smithing")
end
if anim ~= self.current_anim then
msg.post("#sprite", "play_animation", { id = anim })
self.current_anim = anim
end
end
local function eat(self, dt)
self.eat_time = self.eat_time - dt
if self.eat_time < 0 and _G.food > 0 then
msg.post("/gui#ui", "resource_update", {type = "food", amount = -1})
if self.state == "Idle" or self.state == "Selected" then
self.eat_time = math.random(50,150)
elseif self.state == "Fighting" or self.state == "Training" then
self.eat_time = math.random(5,25)
elseif self.state == "Moving" or self.state == "Harvesting" or self.state == "Building" or self.state == "Smithing" or self.state == "Unloading" then
self.eat_time = math.random(5,75)
end
end
end
local function drink(self, dt)
self.drink_time = self.drink_time - dt
if self.drink_time < 0 and _G.water > 0 then
msg.post("/gui#ui", "resource_update", {type = "water", amount = -1})
if self.state == "Idle" or self.state == "Selected" then
self.drink_time = math.random(25,75)
elseif self.state == "Fighting" or self.state == "Training" then
self.drink_time = math.random(2,13)
elseif self.state == "Moving" or self.state == "Harvesting" or self.state == "Building" or self.state == "Smithing" or self.state == "Unloading" then
self.drink_time = math.random(2,37)
end
end
end
local function shoot(self)
if self.arrow_type == "stone" and _G.stone <= 0 then
return nil
elseif self.arrow_type == "arrows" and _G.arrows <= 0 then
return nil
elseif self.arrow_type == "rckts" and _G.rckts <= 0 then
return nil
end
msg.post("/gui#ui", "resource_update", {type = self.arrow_type, amount = -1})
local direction = vmath.normalize(go.get_position(self.target) - go.get_position())
local angle = math.atan2(direction.y, direction.x)
local angle2 = math.rad(math.random(-self.accuracy,self.accuracy))
local new_rotation = vmath.quat_rotation_z(angle) * vmath.quat_rotation_z(angle2)
local rocket = factory.create("#rocket_factory", go.get_position(), new_rotation)
msg.post(rocket, "info", {damage = self.damage, life = 3, design = self.arrow_type})
end
local function hit(self)
msg.post(self.target, "hit", {damage = self.damage})
print("hit")
end
local function weapon_durability(self, dt)
if self.attack_type == "Ranged" and self.ranged_weapon then
self.ranged_durability = self.ranged_durability - 1
if self.ranged_durability <= 0 then
self.ranged_weapon = nil
scan(self, dt)
end
elseif self.attack_type == "Meele" and self.meele_weapon then
self.meele_durability = self.meele_durability - 1
if self.meele_durability <= 0 then
self.meele_weapon = nil
scan(self, dt)
end
end
end
local function fight(self, dt)
self.attack_time = self.attack_time - dt
if self.attack_time < 0 and (self.meele_weapon or self.ranged_weapon) then
if self.attack_type == "Ranged" and self.ranged_weapon then
self.damage = self.ranged_damage
shoot(self)
elseif self.attack_type == "Meele" and self.meele_weapon then
self.damage = self.meele_damage
print(global.distance_to(go.get_position(self.target), go.get_position()))
if global.distance_to(go.get_position(self.target), go.get_position()) <= 10 then
hit(self)
end
end
self.attack_time = 2
local durabilty = math.random(1,10)
if durabilty < 5 then
weapon_durability(self, dt)
end
end
if global.distance_to(go.get_position(self.target), go.get_position()) >= 5 and self.attack_type == "Meele" then
msg.post(go.get_id(), "move", {pos = go.get_position(self.target), target = self.target, todo = "Fighting", target_type = self.target_type})
end
end
local function run(self, dt)
local current_pos = go.get_position()
self.new_pos = go.get_position(self.target)
self.dir = vmath.normalize(self.new_pos - current_pos) -- Get direction to target
local distance = vmath.length(self.new_pos - current_pos) -- Distance to target
local step = self.speed * dt -- Calculate step size based on speed
self.start_pos = go.get_position()
if (distance < 200 and self.health < 20) or not (self.ranged_weapon or self.meele_weapon) then
local new_position = current_pos - (self.dir * math.min(step, distance)) -- Move towards target
go.set_position(new_position)
if not (self.ranged_weapon or self.meele_weapon) and distance > 350 then
self.state = "Idle"
self.todo = "Idle"
self.target = nil
end
elseif (distance > 150 and self.health > 20) or distance > 210 then
local new_position = current_pos + (self.dir * math.min(step, distance)) -- Move towards target
go.set_position(new_position)
end
end
local function heal(self, dt)
self.heal_time = self.heal_time - dt
if self.heal_time < 0 and _G.food > 0 then
if self.health < 100 then
self.health = self.health + 1
end
self.heal_time = 1
end
end
local function build(self, dt)
self.build_time = self.build_time - dt
if self.build_time < 0 then
msg.post(self.target, "build", {amount = self.build_amount})
self.build_time = 1
end
end
local function try_new_weapon(self)
if self.ranged_weapon == nil then
if _G.bows > 0 then
msg.post("/gui#ui", "resource_update", {type = "bows", amount = -1})
self.ranged_weapon = "bow"
self.arrow_type = "arrows"
self.ranged_durability = 1000
self.ranged_damage = 7
self.accuracy = 10
elseif _G.slingshots > 0 then
msg.post("/gui#ui", "resource_update", {type = "slingshots", amount = -1})
self.ranged_weapon = "slingshot"
self.arrow_type = "stone"
self.ranged_durability = 150
self.ranged_damage = 1
self.accuracy = 20
end
elseif self.ranged_weapon == "slingshot" and _G.bows > 0 then
msg.post("/gui#ui", "resource_update", {type = "bows", amount = -1})
self.ranged_weapon = "bow"
self.arrow_type = "arrows"
self.ranged_durability = 1000
self.ranged_damage = 7
self.accuracy = 10
end
if self.meele_weapon == nil then
if _G.swords > 0 then
msg.post("/gui#ui", "resource_update", {type = "swords", amount = -1})
self.meele_weapon = "sword"
self.meele_durability = 500
self.meele_damage = 5
elseif _G.clubs > 0 then
msg.post("/gui#ui", "resource_update", {type = "clubs", amount = -1})
self.meele_weapon = "club"
self.meele_durability = 100
self.meele_damage = 1
end
elseif self.meele_weapon == "club" and _G.swords > 0 then
msg.post("/gui#ui", "resource_update", {type = "swords", amount = -1})
self.meele_weapon = "sword"
self.meele_durability = 500
self.meele_damage = 5
end
end
local function train(self, dt)
self.train_time = self.train_time - dt
if self.train_time < 0 then
try_new_weapon(self)
self.max_ranged_durability = self.ranged_durability
self.max_meele_durability = self.meele_durability
local picker = math.random(1,5000)
self.train_time = 1
end
end
local function smith(self, dt)
self.smith_time = self.smith_time - dt
if self.smith_time < 0 then
msg.post(self.target, "smithed", {r_id = go.get_id()})
self.smith_time = 0.1
end
end
local function update_display(self, dt)
msg.post("#personUI", "update_health", {
health = self.health,
position = go.get_position(),
owner = go.get_id()
})
if #self.inventory ~= 0 then
msg.post("#personUI", "update_inventory", {position = go.get_position(),
owner = go.get_id(), space = #self.inventory})
end
end
function update(self, dt)
if not global.contains(_G.enemys, self.target) and not global.contains(_G.enemy_bases, self.target) and not self.running_away and self.state ~= "Building" then
scan(self, dt)
end
if self.state == "Selected" then
bounce(self, dt)
elseif self.state == "Moving" then
move(self, dt)
elseif self.state == "Harvesting" then
harvest(self, dt)
scan(self, dt)
elseif self.state == "Fighting" then
run(self, dt)
fight(self, dt)
elseif self.state == "Building" then
build(self, dt)
elseif self.state == "Training" then
train(self, dt)
elseif self.state == "Smithing" then
smith(self, dt)
elseif self.state == "Unloading" then
unload(self, dt)
end
animate(self)
eat(self, dt)
drink(self, dt)
update_display(self, dt)
if self.state ~= "Fighting" then
heal(self, dt)
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("click") then
if self.state == "Idle" or self.state == "Training" or self.state == "Smithing" or self.state == "Building" then
self.state = "Selected"
self.target = nil
elseif self.state == "Selected"then
self.state = "Idle"
elseif self.state == "Fighting" then
self.state = "Selected"
self.running_away = true
end
if self.state == "Harvesting" then
self.state = "Selected"
self.todo = "Idle"
msg.post(self.target, "leaving", {person_id = go.get_id()})
self.target = nil
end
elseif message_id == hash("get_state") then
msg.post(sender, "state", { state = self.state })
elseif message_id == hash("build_done") and message.r_id == self.target then --Change to work for auto
self.target = nil
if self.state == "Moving" then
self.todo = "Idle"
elseif self.auto and self.state == "Building" then
self.state = "Idle"
get_nearest(self, message.type)
end
elseif message_id == hash("task_done") and message.r_id == self.target then
self.target = nil
self.state = "Idle"
self.todo = "Idle"
elseif message_id == hash("all") then
if not message.de then
if self.state == "Idle" or self.state == "Training" or self.state == "Smithing" then
msg.post(sender, "all", {de = false})
self.state = "Selected"
self.todo = "Idle"
print("Selected")
elseif self.state == "Harvesting" and message.idle == false then
msg.post(sender, "all", {de = false})
self.state = "Selected"
self.todo = "Idle"
if self.target then
msg.post(self.target, "leaving", {person_id = go.get_id()})
end
self.target = nil
elseif self.state == "Moving" and message.idle == false then
self.start_pos = go.get_position()
msg.post(sender, "all", {de = false})
self.state = "Selected"
self.todo = "Idle"
self.target = nil
msg.post("#personUI", "done", {r_id = go.get_id(), pos = go.get_position()})
elseif self.state == "Fighting" and message.idle == false then
self.start_pos = go.get_position()
msg.post(sender, "all", {de = false})
self.state = "Selected"
self.todo = "Idle"
self.target = nil
self.running_away = true
end
else
msg.post(sender, "all", {de = true})
self.state = "Idle"
self.todo = "Idle"
end
elseif message_id == hash("move") then
--print("Currently ", self.state, " Now ", message.todo)
self.state = "Moving"
self.new_pos = message.pos
self.todo = message.todo
self.target = message.target
self.target_type = message.target_type or self.target_type
if message.target ~= nil then
if not go.exists(self.target) then
print("Target does not exist")
self.state = "Idle"
self.todo = "Idle"
self.target_type = nil
self.target = nil
end
end
if self.todo == "Fighting" and self.target_type == "Animal" and self.attack_type == "Ranged" then
self.attack_offset = vmath.vector3(0,0,0)
end
if message.tags == "spread" then
self.new_pos = go.get_position() + self.attack_offset
self.todo = "Idle"
end
elseif message_id == hash("resource_gone") and self.target == message.r_id then
--print("Target", self.target, "Type", message.type)
self.target = nil
if self.state == "Moving" then
self.todo = "Idle"
elseif self.auto and self.state == "Harvesting" then
self.state = "Idle"
get_nearest(self, message.type)
end
elseif message_id == hash("check_target_is_there") and self.state == "Moving" then
if message.r_id == self.target then
self.state = "Idle"
self.todo = "Idle"
if self.auto then
get_nearest(self, message.type)
end
end
elseif message_id == hash("toggle-auto") then
self.auto = not self.auto
elseif message_id == hash("toggle_attack_type") then
if self.attack_type == "Ranged" then
self.attack_type = "Meele"
elseif self.attack_type == "Meele" then
self.attack_type = "Ranged"
end
end
if message_id == hash("hit") then
self.health = self.health - message.damage
update_alpha(self)
if self.health < 0 then
print("Deleted person")
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="person"})
go.delete()
end
end
if message_id == hash("enemy_destroyed") and self.target == message.enemy_id then
self.target = nil -- Reset target only if it was the one being destroyed
self.state = "Idle"
self.todo = "Idle"
self.start_pos = go.get_position()
if message.rewards then
print("Rewardss", #self.inventory)
for i = 0, message.rewards[2], 1 do
table.insert(self.inventory, message.rewards[1])
end
if #self.inventory >= self.max_resources then
msg.post(go.get_id(), "move", {pos = _G.base_pos, target = _G.bases[1], todo = "Unloading", target_type = self.target_type})
elseif self.auto then
get_nearest(self, self.target_type)
end
end
end
if message_id == hash("show_info") then
msg.post("/gui#stats", "person_info", {info = {state = self.state, todo = self.todo,
target_type = self.target_type, health = self.health, meele_weapon = self.meele_weapon,
ranged_weapon = self.ranged_weapon, damage = self.damage, harvest = self.harvest_amount, build = self.build_amount,
accuracy = self.accuracy, inventory = #self.inventory, meele_weapon_durability = self.meele_durability,
ranged_weapon_durability = self.ranged_durability, max_meele = self.max_meele_durability, max_ranged = self.max_ranged_durability
}})
end
if message_id == hash("offset_ui") then
msg.post("#personUI", "offset", {
pos = message.pos
})
end
end