-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_spawner.script
More file actions
558 lines (495 loc) · 17.7 KB
/
init_spawner.script
File metadata and controls
558 lines (495 loc) · 17.7 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
_G.window_width = tonumber(sys.get_config("display.width"))
_G.window_height = tonumber(sys.get_config("display.height"))
local selected_people = {} -- Table to store selected people
local global = require "main.global" -- Import the module
_G.persons = {}
_G.trees = {}
_G.stones = {}
_G.caves = {}
_G.waters = {}
_G.houses = {}
_G.farms = {}
_G.barracks = {}
_G.smiths = {}
_G.refinerys = {}
_G.turrets = {}
_G.enemys = {}
_G.lasers = {}
_G.rockets = {}
_G.bases = {}
_G.enemy_bases = {}
_G.base_pos = vmath.vector3()
_G.base_range = 500
_G.houses_to_build = {}
_G.farms_to_build = {}
_G.barracks_to_build = {}
_G.smiths_to_build = {}
_G.refinerys_to_build = {}
_G.turrets_to_build = {}
_G.level = 1
_G.animals = {}
_G.deers = {}
_G.boars = {}
_G.bears = {}
------------------------------------------------------------------------------------------------------------------------
--Create the objects
local function check_overlaps(pos)
local rect_arrays = {_G.trees, _G.stones, _G.caves, _G.waters, _G.persons, _G.bases, _G.enemy_bases} --, _G.houses, _G.farms, _G.refinerys, _G.turrets}
local col_size = 25
for _, array in ipairs(rect_arrays) do
if array == _G.bases then
col_size = 60
end
for _, rect in ipairs(array) do
local rect_pos = go.get_position(rect) -- Get the position of the tree
if global.rectangles_collide(pos.x, pos.y, col_size, col_size, rect_pos.x, rect_pos.y, col_size, col_size) then
return true
end
end
end
return false
end
local function search_enemy_range(pos, radius)
local target_arrays = {_G.persons, _G.houses, _G.farms, _G.barracks, _G.smiths, _G.refinerys, _G.turrets, _G.bases}
local targets = {}
for _, array in ipairs(target_arrays) do
for _, person in ipairs(array) do
if go.exists(person) then
local person_poss = go.get_position(person) -- Get the position of the tree
if global.distance_to(pos, person_poss) < radius then
table.insert(targets, person)
end
end
end
end
return targets
end
local function search_person_range(pos, radius)
local target_arrays = {_G.enemys, _G.enemy_bases}
for _, array in ipairs(target_arrays) do
for _, enemy in ipairs(array) do
local enemy_pos = go.get_position(enemy)
if global.distance_to(pos, enemy_pos) < radius then
return enemy
end
end
end
return nil
end
local function create_entity(entity_type, pos, base)
local random_pos = pos or global.random_position() -- Use given position or random
if entity_type == "person" then
random_pos = global.random_position_around_base(pos, _G.base_range/2)
elseif entity_type == "enemy" then
random_pos = global.random_position_around_base(pos, 150)
end
-- Generate the factory URL dynamically
local factory_url = "#" .. entity_type .. "_factory"
-- Find the correct global storage table dynamically
local storage_table = _G[entity_type .. "s"] -- Example: _G["trees"]
-- Ensure the table exists before inserting
if not storage_table then
print("Error: Storage table for " .. entity_type .. " not found!")
return
end
local entity
if not check_overlaps(random_pos) then
entity = factory.create(factory_url, random_pos)
table.insert(storage_table, entity)
else
create_entity(entity_type, pos, base) -- Retry if overlaps
end
if entity_type == "enemy" and entity then
msg.post(entity, "info on spawn", {base = base})
end
end
local function create_resources_on_spawn(mult)
local m = mult or 1
local types = {"tree", "stone", "cave", "water"}
local amounts = {1000, 300, 100, 125}
for i = 1, #types, 1 do
for j = 0, amounts[i]*m, 1 do
create_entity(types[i])
end
end
end
------------------------------------------------------------------------------------------------------------------------
function init(self)
msg.post(".", "acquire_input_focus")
self.created = false
math.randomseed(os.time())
self.size = 25
self.mouse_pos = vmath.vector3()
self.camera_position = vmath.vector3()
self.people_done = false
self.enemy_spawner = 10
self.refining = false
self.all_selected = false
self.mouse_down_s = vmath.vector3()
self.mouse_up_s = vmath.vector3()
self.mouse_down_w = vmath.vector3()
self.mouse_up_w = vmath.vector3()
create_entity("base", vmath.vector3(300, 300, 0) )
--for i = 0, 3, 1 do
--create_entity("enemy_base", global.random_position_for_enemy_base())
--end
create_resources_on_spawn(2)
self.respawn_resources_time = 10
self.enemy_charge_time = 20
self.person_info = nil
end
function update(self, dt)
if #_G.trees < 200 then
create_entity("tree")
end
if #_G.stones < 100 then
create_entity("stone")
end
if #_G.caves < 50 then
create_entity("cave")
end
--if #_G.animals < 200 then
--create_entity("animal")
--end
self.enemy_charge_time = self.enemy_charge_time - dt
if self.enemy_charge_time < 0 and #_G.enemys > 3 then
for _, enemy in ipairs(_G.enemys) do
msg.post(enemy, "charge")
end
self.enemy_charge_time = 1 --math.random(500,2000)
end
if self.person_info then
msg.post(self.person_info, "show_info")
else
msg.post("/gui#stats", "hide_info")
end
end
local function something_clicked(self, post)
local arrays = {_G.persons, _G.trees, _G.stones, _G.caves, _G.waters, _G.houses, _G.farms, _G.barracks, _G.smiths, _G.refinerys, _G.turrets, _G.bases, _G.animals}
local array_names = {"person", "tree", "stone", "cave", "water", "house", "farm", "barrack", "smith", "refinery", "turret", "base", "animal"} -- Corresponding names for the arrays
for index, i in ipairs(arrays) do -- Change pairs to ipairs to match the index with the array names
for _, j in ipairs(i) do
local pos = go.get_position(j) -- Get the position of the resource
if global.is_mouse_inside_collision(self.mouse_pos, pos, self.size, self.size) then
if post then
msg.post(j, "click") -- Send a message to the clicked resource
end
return j, array_names[index] -- Return the resource and its array name
end
end
end
return nil, nil -- Return nil if no resource is clicked
end
local function get_selected(self)
selected_people = {}
for _, person in ipairs(_G.persons) do
msg.post(person, "get_state")
end
end
local function move_selected_people(self)
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = self.mouse_pos, target = nil, todo = "Idle"})
end
end
local function assign_to_resource(self, resource, resource_type)
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = self.mouse_pos, target = resource, todo = "Harvesting", target_type = resource_type})
end
end
local function assign_to_fight(self, target, enemy_type)
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = self.mouse_pos, target = target, todo = "Fighting", target_type = enemy_type})
end
end
local function assign_to_build(self, build, build_type)
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = self.mouse_pos, target = build, todo = "Building", target_type = build_type})
end
end
local function assign_to_train(self, build, barracks)
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = self.mouse_pos, target = build, todo = "Training", target_type = barracks})
end
end
local function assign_to_smith(self, build, smith)
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = self.mouse_pos, target = build, todo = "Smithing", target_type = smith})
end
end
local function get_points_inside_drag_area(points, pos1, pos2)
local rect_pos, rect_size = global.get_rect_from_drag(pos1, pos2)
local inside_points = {}
for _, point in ipairs(points) do
if global.is_point_inside_rect(go.get_position(point), rect_pos, rect_size) then
table.insert(inside_points, point)
end
end
return inside_points
end
function on_input(self, action_id, action)
-- Check for mouse click (left mouse button)
if action_id == hash("click") and action.pressed then
self.mouse_pos = global.convert_screen_pos(vmath.vector3(action.x, action.y, 0), self.camera_position) -- Get the mouse position from action
local clicked, type = something_clicked(self, true)
local for_person
if type == "animal" then
if global.contains(_G.deers, clicked) then
type = "deer"
elseif global.contains(_G.boars, clicked) then
type = "boar"
else
type = "bear"
end
print(type)
end
if type then
for_person = global.capitalize(type)
end
if type == "person" then
get_selected(self)
end
if #selected_people > 0 then
if type == nil then
move_selected_people(self)
selected_people = {}
elseif type == "enemy" or type == "deer" or type == "boar" or type == "bear" then
assign_to_fight(self, clicked, for_person)
elseif type == "tree" or type == "stone" or type == "cave" or type == "water" then
assign_to_resource(self, clicked, for_person)
else
if type == "barrack" and not global.contains(_G.barracks_to_build, clicked) then
assign_to_train(self, clicked, for_person)
elseif type == "smith" and not global.contains(_G.smiths_to_build, clicked) then
assign_to_smith(self, clicked, for_person)
else
assign_to_build(self, clicked, for_person)
end
end
end
if type == "smith" and #selected_people == 0 then
msg.post("/gui#manufacturing", "toggle", {smith = clicked})
end
if type ~= nil and type ~= "person" then
selected_people = {}
end
self.mouse_pos = vmath.vector3()
end
if #selected_people == 0 and (action_id == hash("touch") or action_id == hash("click")) and not _G.build_in_hand then
-- Get the world position from the screen position
local current_mouse_pos = (vmath.vector3(action.x, action.y, 0))
if action.pressed then
-- Store the start position of the selection box when clicking down
self.mouse_down_s = current_mouse_pos
msg.post("/gui#ui", "select_box", { start_pos = self.mouse_down_s, end_pos = self.mouse_down_s }) -- Start drawing
elseif action.released then
-- Store the end position when releasing the click
self.mouse_up_s = current_mouse_pos
msg.post("/gui#ui", "select_box", { start_pos = self.mouse_down_s, end_pos = self.mouse_up_s }) -- Final update
msg.post("/gui#ui", "hide_select_box") -- Hide the box after selection
--print("Start Pos (Screen):", self.mouse_down_s)
--print("End Pos (Screen):", self.mouse_up_s)
self.mouse_down_w = global.convert_screen_pos(self.mouse_down_s, self.camera_position)
self.mouse_up_w = global.convert_screen_pos(self.mouse_up_s, self.camera_position)
--print("Start Pos (World):", self.mouse_down_w)
--print("End Pos (World):", self.mouse_up_w)
--print("Camera offset", self.camera_position - vmath.vector3(750, 400, 0))
local selected = get_points_inside_drag_area(_G.persons, self.mouse_down_w, self.mouse_up_w)
for _, person in ipairs(selected) do
msg.post(person, "all", {de = false, idle = false})
end
else
-- While dragging, continuously update the selection box size
msg.post("/gui#ui", "select_box", { start_pos = self.mouse_down_s or current_mouse_pos, end_pos = current_mouse_pos })
end
end
if action_id == hash("toggle_auto") and action.pressed then
for _, person in ipairs(selected_people) do
msg.post(person, "toggle-auto")
end
end
if action_id == hash("toggle_attack_type") and action.pressed then
for _, person in ipairs(selected_people) do
msg.post(person, "toggle_attack_type")
end
end
if action_id == hash("toggle_ref") and action.pressed then
self.refining = not self.refining
for _, ref in ipairs(_G.refinerys) do
msg.post(ref, "toggle", {r = self.refining})
end
end
if action_id == hash("spread") and action.pressed then
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = self.mouse_pos, target = nil, todo = "Idle", tags = "spread"})
end
end
if action_id == hash("select_all") and action.pressed then
if self.all_selected == false then
selected_people = {}
for _, person in ipairs(_G.persons) do
msg.post(person, "all", {idle = false, de = false})
end
self.all_selected = true
else
for _, person in ipairs(_G.persons) do
msg.post(person, "all", {idle = false, de = true})
end
self.all_selected = false
selected_people = {}
end
end
if action_id == hash("select_all_idle") and action.pressed then
if self.all_selected == false then
selected_people = {}
for _, person in ipairs(_G.persons) do
msg.post(person, "all", {idle = true, de = false})
end
self.all_selected = true
else
for _, person in ipairs(selected_people) do
msg.post(person, "all", {idle = true, de = true})
end
selected_people = {}
self.all_selected = false
end
end
if action_id == hash("get_info") and action.pressed then
self.mouse_pos = global.convert_screen_pos(vmath.vector3(action.x, action.y, 0), self.camera_position) -- Get the mouse position from action
local clicked, type = something_clicked(self, false)
if type == "person" then
self.person_info = clicked
else
self.person_info = nil
end
end
if action_id == hash("unload") and action.pressed then
for _, person in ipairs(selected_people) do
msg.post(person, "move", {pos = _G.base_pos, target = _G.bases[1], todo = "Unloading"})
end
end
end
local function find_nearest_resource(self, person_pos, type)
local distance = 20000
local nearest
local action
local target_table = _G.trees
if type == "Tree" or type == "Stone" or type == "Cave" or type == "Water" then
target_table = _G[string.lower(type) .. "s"] -- Example: _G["trees"]
action = "Harvesting"
elseif type == "Deer" or type == "Boar" or type == "Bear" then
target_table = _G[string.lower(type) .. "s"] -- Example: _G["trees"]
action = "Fighting"
else
target_table = _G[string.lower(type) .. "s_to_build"] -- Example: _G["trees"]
action = "Building"
end
for _, i in ipairs(target_table) do
local rpos = go.get_position(i)
local temp_dist = math.abs(vmath.length(rpos - person_pos))
if temp_dist < distance then
distance = temp_dist
nearest = i
end
end
return nearest, action
end
function on_message(self, message_id, message, sender)
if message_id == hash("state") then
if message.state == "Selected" then
table.insert(selected_people, sender)
elseif message.state == "Idle" and global.contains(selected_people, sender) then
table.remove(selected_people, global.remove_by_value(selected_people, sender))
end
end
if message_id == hash("gone") then
local storage_table = _G[message.type .. "s"]
if global.contains(storage_table, message.r_id) then
table.remove(storage_table, global.remove_by_value(storage_table, message.r_id))
if storage_table == "trees" or storage_table == "stones" or storage_table == "caves" then
for _, person in ipairs(_G.persons) do
msg.post(person, "check_target_is_there", {r_id = message.r_id})
end
end
end
if message.type == "enemy" then
msg.post("/gui#ui", "progress", {amount = 3})
elseif message.type == "tree" or message.type == "stone" or message.type == "cave" or message.type == "water" then
msg.post("/gui#ui", "progress", {amount = 1})
end
end
if message_id == hash("get_nearest") then
local nearest, action = find_nearest_resource(self, message.pos, message.type)
print(nearest, action)
if nearest ~= nil then
msg.post(sender, "move", {pos = go.get_position(nearest), target = nearest, todo = action, target_type = message.type})
end
end
if message_id == hash("offset") then
self.camera_position = message.pos
local objects_to_offset = {_G.persons, _G.enemys, _G.farms, _G.barracks, _G.smiths, _G.refinerys, _G.turrets, _G.houses, _G.bases}
for _, type in ipairs(objects_to_offset) do
for _, object in ipairs(type) do
msg.post(object, "offset", {pos = message.pos})
end
end
msg.post("/gui#ui", "offset_ui", {pos = message.pos})
end
if message_id == hash("build_button") then
create_entity(message.type)
msg.post("/gui#ui", "progress", {amount = 300})
print(message.type)
_G.build_in_hand = true
self.mouse_pos = vmath.vector3()
end
if message_id == hash("try_place") then
msg.post(sender, "try_place", {bool = not check_overlaps(message.pos) and global.distance_to(message.pos, _G.base_pos) < _G.base_range/2})
end
if message_id == hash("scan_enemy_radius") then
local targets_to_return = search_enemy_range(message.enemy_pos, 300)
if targets_to_return then
msg.post(sender, "targets_for_enemy", {targets = targets_to_return})
end
end
if message_id == hash("scan_person_radius") then
local enemy = search_person_range(message.person_pos, 300)
if enemy then
msg.post(sender, "move", {pos = go.get_position(enemy), target = enemy, todo = "Fighting", target_type = "Enemy"})
end
end
if message_id == hash("scan_turret_radius") then
local enemy = search_person_range(message.person_pos, 300)
if enemy then
msg.post(sender, "enemy", {pos = go.get_position(enemy), target = enemy})
end
end
if message_id == hash("all") then
if not message.de then
table.insert(selected_people, sender)
end
end
if message_id == hash("animal_spawned") then
table.insert(_G[message.type .. "s"], message.r_id)
end
if message_id == hash("create_enemy") then
create_entity("enemy", message.pos, message.base)
end
if message_id == hash("create_person") then
create_entity("person", message.pos)
end
if message_id == hash("create_fire") then
local new_scale
local offset
if message.big then
new_scale = global.random_float(0.5, 2)
offset = vmath.vector3(math.random(-40, 40), math.random(-40, 40), 0)
else
new_scale = global.random_float(0.3, 0.5)
offset = vmath.vector3(math.random(-8, 8), math.random(-8, 8), 0)
end
local fire = factory.create("#fire_factory", message.pos + offset)
local smoke = factory.create("#smoke_factory", message.pos + offset)
msg.post(sender, "created_fire", {fire = fire, smoke = smoke})
msg.post(fire, "info", {scale = new_scale})
msg.post(smoke, "info", {scale = new_scale})
end
end