-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathliquid_groups.dm
1043 lines (866 loc) · 36.3 KB
/
liquid_groups.dm
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************/
/********************PROPER GROUPING**************/
//Whenever you add a liquid cell add its contents to the group, have the group hold the reference to total reagents for processing sake
//Have the liquid turfs point to a partial liquids reference in the group for any interactions
//Have the liquid group handle the total reagents datum, and reactions too (apply fraction?)
GLOBAL_VAR_INIT(liquid_debug_colors, FALSE)
/datum/liquid_group
///the generated color given to the group on creation for debugging
var/color
///list of all current members of the group saved in true/false format
var/list/members = list()
///list of all current burning members of our group
var/list/burning_members = list()
///our reagent holder, where the entire liquid groups reagents are stored
var/datum/reagents/reagents
///our reagent holder for a single turf
var/datum/reagents/turf_reagents
///the expected height of all the collective turfs
var/expected_turf_height = 1
///A saved variable of the total reagent volumes to avoid calling reagents.total_volume constantly
var/total_reagent_volume = 0
///a cached value of our reagents per turf, used to determine liquid height and state
var/reagents_per_turf = 0
///the icon state our group currently uses
var/group_overlay_state = LIQUID_STATE_PUDDLE
///the calculated alpha cache for our group
var/group_alpha = 0
///the calculated temperature cache for our group
var/group_temperature = 300
///the generated color used to apply coloring to all the members
var/group_color
///have we failed a process? if so we are added to a death check so it will gracefully die on its own
var/failed_death_check = FALSE
///the burn power of our group, used to determine how strong we burn each process_fire()
var/group_burn_power = 0
///the icon state of our fire
var/group_fire_state = LIQUID_FIRE_STATE_NONE
///the amount of reagents we attempt to burn each process_fire()
var/group_burn_rate = 0
///the viscosity of our group, determines how much we can spread with our total reagent pool, higher means less turfs per reagent
var/group_viscosity = 1
///are we currently attempting a merge? if so don't process groups
var/merging = FALSE
///list of cached edge turfs with a sublist of directions stored
var/list/cached_edge_turfs = list()
///list of cached spreadable turfs for each burning member
var/list/cached_fire_spreads = list()
///list of old reagents
var/list/cached_reagent_list = list()
///cached temperature between turfs recalculated on group_process
var/cached_temperature_shift = 0
///does temperature need action
var/temperature_shift_needs_action = FALSE
///this groups list of currently running turfs, we iterate over this to stop redundancy
var/list/current_temperature_queue = list()
///do we evaporate
var/evaporates = TRUE
/// Liquids in this group will always evaporate regardless of height
var/always_evaporates = FALSE
/// The multiplier added to the evaporation rate of all reagents in this group.
var/evaporation_multiplier = 1
///can we merge?
var/can_merge = TRUE
///number in decimal value that acts as a multiplier to the amount of liquids lost in applications
var/loss_precent = 1
///do we have any containing expose turf chemicals with volume to look for?
var/exposure = FALSE
///array generated by bulk splitting
var/list/splitting_array = list()
///are we slippery
var/slippery = TRUE
///NEW/DESTROY
/datum/liquid_group/New(height, obj/effect/abstract/liquid_turf/created_liquid)
color = "#[random_short_color()]"
expected_turf_height = height
reagents = new(100000) // this is a random number used on creation it expands based on the turfs in the group
if(!QDELETED(created_liquid))
add_to_group(created_liquid.my_turf)
cached_edge_turfs[created_liquid.my_turf] = list(NORTH, SOUTH, EAST, WEST)
SSliquids.active_groups |= src
/datum/liquid_group/Destroy()
SSliquids.active_groups -= src
if(src in SSliquids.arrayed_groups)
SSliquids.arrayed_groups -= src /// Someone made a massive fucky wucky if this is happening
for(var/turf/member_turf as anything in members)
member_turf?.liquids?.liquid_group = null
members = list()
burning_members = null
return ..()
/datum/liquid_group/proc/copy_properties(datum/liquid_group/from)
if(isnull(from))
return
always_evaporates = from.always_evaporates
evaporation_multiplier = from.evaporation_multiplier
///GROUP CONTROLLING
/datum/liquid_group/proc/add_to_group(turf/T)
if(QDELETED(T))
return
if(QDELETED(T.liquids))
T.liquids = new(T, src)
cached_edge_turfs[T] = list(NORTH, SOUTH, EAST, WEST)
if(!members)
QDEL_NULL(T.liquids)
return
members[T] = TRUE
T.liquids.liquid_group = src
reagents.maximum_volume += 1000 /// each turf will hold 1000 units plus the base amount spread across the group
if(group_color)
T.liquids.color = group_color
process_group()
/datum/liquid_group/proc/remove_from_group(turf/T, should_reprocess = TRUE)
if(T in burning_members)
burning_members -= T
if(T in SSliquids.burning_turfs)
SSliquids.burning_turfs -= T
members -= T
T.liquids?.liquid_group = null
if(!length(members))
qdel(src)
return
if(should_reprocess)
process_group()
/datum/liquid_group/proc/remove_all()
for(var/turf/member in members)
QDEL_NULL(member.liquids)
/datum/liquid_group/proc/merge_group(datum/liquid_group/otherg)
if(otherg == src)
return
if(!length(members) || !total_reagent_volume)
return
otherg.merging = TRUE
var/list/created_reagent_list = list()
for(var/datum/reagent/reagent in otherg.reagents.reagent_list)
created_reagent_list |= reagent.type
created_reagent_list[reagent.type] = reagent.volume
add_reagents(reagent_list = created_reagent_list, chem_temp = otherg.group_temperature)
cached_edge_turfs |= otherg.cached_edge_turfs
for(var/turf/liquid_turf as anything in otherg.members)
otherg.remove_from_group(liquid_turf)
add_to_group(liquid_turf)
total_reagent_volume = reagents.total_volume
reagents_per_turf = total_reagent_volume / length(members)
always_evaporates ||= otherg.always_evaporates
evaporation_multiplier = (evaporation_multiplier + otherg.evaporation_multiplier) * 0.5 // average the evaporation multipliers
qdel(otherg)
process_group()
/datum/liquid_group/proc/break_group()
qdel(src)
/datum/liquid_group/proc/check_dead()
if(!members && !total_reagent_volume)
if(failed_death_check)
qdel(src)
return
failed_death_check = TRUE
///PROCESSING
/datum/liquid_group/proc/process_group(from_SS = FALSE)
if(merging)
return
if(!members || !length(members)) // this ideally shouldn't exist, ideally groups would die before they got to this point but alas here we are
check_dead()
return
if(group_temperature != reagents.chem_temp)
reagents.chem_temp = group_temperature
handle_visual_changes()
reagents.my_atom = pick(members) /// change the location of explosions and sounds every group process
for(var/turf/turf as anything in members)
if(isopenturf(turf))
continue
remove_from_group(turf)
var/turf/open/open_turf = pick(members)
var/datum/gas_mixture/math_cache = open_turf.air
if(math_cache && total_reagent_volume)
if(!(group_temperature <= math_cache.return_temperature() + 5 && math_cache.return_temperature() - 5 <= group_temperature) && !temperature_shift_needs_action)
cached_temperature_shift =((math_cache.return_temperature() * max(1, math_cache.total_moles())) + ((group_temperature * max(1, (total_reagent_volume * 0.025))))) / (max(1, (total_reagent_volume * 0.025)) + max(1, math_cache.total_moles()))
temperature_shift_needs_action = TRUE
if(from_SS)
total_reagent_volume = reagents.total_volume
reagents.handle_reactions()
if(!total_reagent_volume || !members)
return
reagents_per_turf = total_reagent_volume / length(members)
expected_turf_height = CEILING(reagents_per_turf, 1) / LIQUID_HEIGHT_DIVISOR
var/old_overlay = group_overlay_state
switch(expected_turf_height)
if(0 to LIQUID_ANKLES_LEVEL_HEIGHT-1)
group_overlay_state = LIQUID_STATE_PUDDLE
if(LIQUID_ANKLES_LEVEL_HEIGHT to LIQUID_WAIST_LEVEL_HEIGHT-1)
group_overlay_state = LIQUID_STATE_ANKLES
if(LIQUID_WAIST_LEVEL_HEIGHT to LIQUID_SHOULDERS_LEVEL_HEIGHT-1)
group_overlay_state = LIQUID_STATE_WAIST
if(LIQUID_SHOULDERS_LEVEL_HEIGHT to LIQUID_FULLTILE_LEVEL_HEIGHT-1)
group_overlay_state = LIQUID_STATE_SHOULDERS
if(LIQUID_FULLTILE_LEVEL_HEIGHT to INFINITY)
group_overlay_state = LIQUID_STATE_FULLTILE
if(old_overlay != group_overlay_state)
for(var/turf/member in members)
if(QDELETED(member.liquids))
remove_from_group(member)
continue
member.liquids.set_new_liquid_state(group_overlay_state)
member.liquid_height = expected_turf_height + member.turf_height
/datum/liquid_group/proc/cleanse_members()
for(var/turf/listed_turf as anything in members)
if(isclosedturf(listed_turf))
remove_from_group(listed_turf)
qdel(listed_turf.liquids)
/datum/liquid_group/proc/process_member(turf/member)
if(isspaceturf(member))
remove_any(member.liquids, reagents_per_turf)
if(!(member in members))
return
if(QDELETED(turf_reagents))
return
turf_reagents.reaction(member, TOUCH)
/datum/liquid_group/proc/build_turf_reagent()
if(!length(members))
return
if(!turf_reagents)
turf_reagents = new(100000)
exposure = FALSE
slippery = FALSE
for(var/reagent_type in reagents.reagent_list)
var/datum/reagent/pulled_reagent = reagent_type
var/amount = pulled_reagent.volume / length(members)
if(!amount)
continue
turf_reagents.add_reagent(pulled_reagent.type, amount)
if(pulled_reagent.turf_exposure && amount > 10)
exposure = TRUE
if(pulled_reagent.slippery)
slippery = TRUE
/datum/liquid_group/proc/process_turf_disperse()
if(!total_reagent_volume)
for(var/turf/member in members)
remove_from_group(member)
QDEL_NULL(member.liquids)
return
var/list/removed_turf = list()
if(reagents_per_turf < 5)
var/turfs_to_remove = round(length(members) - (total_reagent_volume / 6))
if(turfs_to_remove <= 0)
return
while(turfs_to_remove > 0)
turfs_to_remove--
if(members && length(members))
var/turf/picked_turf = pick(members)
if(!QDELETED(picked_turf.liquids))
remove_from_group(picked_turf)
QDEL_NULL(picked_turf.liquids)
removed_turf |= picked_turf
if(!total_reagent_volume)
reagents_per_turf = 0
else
reagents_per_turf = total_reagent_volume / length(members)
else
members -= picked_turf
if(!length(removed_turf))
return
try_bulk_split(removed_turf)
///REAGENT ADD/REMOVAL HANDLING
/datum/liquid_group/proc/check_liquid_removal(obj/effect/abstract/liquid_turf/remover, amount)
if(amount >= reagents_per_turf)
remove_from_group(remover.my_turf)
var/turf/remover_turf = remover.my_turf
qdel(remover)
try_split(remover_turf)
for(var/dir in GLOB.cardinals)
var/turf/open/open_turf = get_step(remover_turf, dir)
if(!isopenturf(open_turf) || QDELETED(open_turf.liquids))
continue
check_edges(open_turf)
process_group()
/datum/liquid_group/proc/remove_edge_liquids()
var/list/plucked_turfs = list()
var/list/edge_queue = list()
total_reagent_volume = reagents.total_volume
var/minimum_turfs = total_reagent_volume * 0.2
var/turfs_till_fine = length(members) - minimum_turfs
while(turfs_till_fine > 0)
turfs_till_fine--
if(!length(edge_queue))
edge_queue |= cached_edge_turfs
var/turf/plucked = pick_n_take(edge_queue)
remove_from_group(plucked, FALSE)
if(QDELETED(src))
return
qdel(plucked.liquids)
plucked_turfs |= plucked
if(!length(members))
qdel(src)
return
for(var/dir in GLOB.cardinals)
var/turf/open/open_turf = get_step(plucked, dir)
if(!isopenturf(open_turf) || QDELETED(open_turf.liquids))
continue
check_edges(open_turf)
if(!length(plucked_turfs))
return
reagents_per_turf = total_reagent_volume / length(members)
try_bulk_split(plucked_turfs)
process_group()
/datum/liquid_group/proc/remove_any(obj/effect/abstract/liquid_turf/remover, amount)
reagents.remove_any(amount, TRUE)
if(!QDELETED(remover))
check_liquid_removal(remover, amount)
total_reagent_volume = reagents.total_volume
if(length(members))
reagents_per_turf = total_reagent_volume / length(members)
else
reagents_per_turf = 1
expected_turf_height = CEILING(reagents_per_turf, 1) / LIQUID_HEIGHT_DIVISOR
if(!total_reagent_volume && !reagents.total_volume)
remove_all()
qdel(src)
/datum/liquid_group/proc/remove_specific(obj/effect/abstract/liquid_turf/remover, amount, datum/reagent/reagent_type)
reagents.remove_reagent(reagent_type.type, amount)
if(!QDELETED(remover))
check_liquid_removal(remover, amount)
total_reagent_volume = reagents.total_volume
/datum/liquid_group/proc/transfer_to_atom(obj/effect/abstract/liquid_turf/remover, amount, atom/transfer_target, transfer_method = INGEST)
reagents.trans_to(transfer_target, amount)
if(!QDELETED(remover))
check_liquid_removal(remover, amount)
total_reagent_volume = reagents.total_volume
/datum/liquid_group/proc/move_liquid_group(obj/effect/abstract/liquid_turf/member)
remove_from_group(member.my_turf)
member.liquid_group = new(1, member)
member.liquid_group.copy_properties(src)
var/remove_amount = reagents_per_turf / length(reagents.reagent_list)
for(var/datum/reagent/reagent_type in reagents.reagent_list)
member.liquid_group.reagents.add_reagent(reagent_type, remove_amount, no_react = TRUE)
remove_specific(amount = remove_amount, reagent_type = reagent_type)
/datum/liquid_group/proc/add_reagents(obj/effect/abstract/liquid_turf/member, reagent_list, chem_temp)
reagents.add_reagent_list(reagent_list, _no_react = TRUE)
var/amount = 0
for(var/list_item in reagent_list)
amount += reagent_list[list_item]
handle_temperature(amount, chem_temp)
handle_visual_changes()
process_group()
/datum/liquid_group/proc/add_reagent(obj/effect/abstract/liquid_turf/member, datum/reagent/reagent, amount, temperature)
reagents.add_reagent(reagent, amount, temperature, no_react = TRUE)
handle_temperature(amount, temperature)
handle_visual_changes()
process_group()
/datum/liquid_group/proc/transfer_reagents_to_secondary_group(obj/effect/abstract/liquid_turf/member, obj/effect/abstract/liquid_turf/transfer)
if(!total_reagent_volume && !reagents.total_volume)
return
else if(!total_reagent_volume)
total_reagent_volume = reagents.total_volume
var/total_removed = length(members) + 1 / total_reagent_volume
var/remove_amount = total_removed / length(reagents.reagent_list)
if(QDELETED(transfer))
transfer = new()
if(QDELETED(transfer.liquid_group))
transfer.liquid_group = new(1, transfer)
for(var/datum/reagent/reagent_type in reagents.reagent_list)
transfer.liquid_group.reagents.add_reagent(reagent_type.type, remove_amount, no_react = TRUE)
remove_specific(amount = remove_amount, reagent_type = reagent_type)
total_removed += remove_amount
check_liquid_removal(member, total_removed)
handle_visual_changes()
process_group()
/datum/liquid_group/proc/trans_to_seperate_group(datum/reagents/secondary_reagent, amount, obj/effect/abstract/liquid_turf/remover, merge = FALSE)
reagents.trans_to(secondary_reagent, amount)
if(remover)
check_liquid_removal(remover, amount)
else if(!merge)
process_removal(amount)
handle_visual_changes()
/datum/liquid_group/proc/transfer_specific_reagents(datum/reagents/secondary_reagent, amount, list/reagents_to_check, obj/effect/abstract/liquid_turf/remover, merge = FALSE)
if(!length(reagents_to_check))
return
var/total_hits = 0
var/total_volume = 0
for(var/datum/reagent/reagent_type in reagents.reagent_list)
if(!(reagent_type.type in reagents_to_check))
continue
total_hits++
total_volume += reagent_type.volume
if(!total_hits)
return
var/precent = (amount / total_volume)
for(var/datum/reagent/reagent_type in reagents.reagent_list)
if(!(reagent_type.type in reagents_to_check))
continue
secondary_reagent.add_reagent(reagent_type.type, reagent_type.volume * precent, no_react = TRUE)
if(remover)
remove_specific(remover, amount = reagent_type.volume * precent, reagent_type = reagent_type.type)
else
remove_specific(amount = reagent_type.volume * precent, reagent_type = reagent_type.type)
process_removal()
handle_visual_changes()
/datum/liquid_group/proc/process_removal(amount)
total_reagent_volume = reagents.total_volume
if(total_reagent_volume && length(members)) //Otherwise we are probably just sending the last of things
reagents_per_turf = total_reagent_volume / length(members)
else
reagents_per_turf = 0
process_turf_disperse()
process_group()
/datum/liquid_group/proc/handle_temperature(previous_reagents, temp)
var/baseline_temperature = ((total_reagent_volume * group_temperature) + (previous_reagents * temp)) / (total_reagent_volume + previous_reagents)
group_temperature = baseline_temperature
reagents.chem_temp = group_temperature
/datum/liquid_group/proc/handle_visual_changes()
var/new_color
var/old_color = group_color
if(GLOB.liquid_debug_colors)
new_color = color
else if(length(cached_reagent_list) != length(reagents.reagent_list))
new_color = mix_color_from_reagents(reagents.reagent_list)
cached_reagent_list = list()
cached_reagent_list |= reagents.reagent_list
var/alpha_setting = 1
var/alpha_divisor = 1
for(var/r in reagents.reagent_list)
var/datum/reagent/R = r
alpha_setting += max((R.opacity * R.volume), 1)
alpha_divisor += max((1 * R.volume), 1)
var/old_alpha = group_alpha
if(new_color == old_color && group_alpha == old_alpha || !new_color)
return
group_alpha = clamp(round(alpha_setting / alpha_divisor, 1), 120, 255)
group_color = new_color
for(var/turf/member in members)
if(QDELETED(member.liquids))
continue
member.liquids.alpha = group_alpha
member.liquids.color = group_color
///Fire Related Procs / Handling
/datum/liquid_group/proc/get_group_burn()
var/total_burn_power = 0
var/total_burn_rate = 0
for(var/datum/reagent/reagent_type in reagents.reagent_list)
var/burn_power = initial(reagent_type.accelerant_quality)
if(burn_power)
total_burn_power += burn_power * reagent_type.volume
total_burn_rate += burn_power
group_burn_rate = total_burn_rate * 0.5 //half power because reasons
if(!total_burn_power)
if(length(burning_members))
extinguish_all()
group_burn_power = 0
return
total_burn_power /= reagents.total_volume //We get burn power per unit.
if(total_burn_power <= REQUIRED_FIRE_POWER_PER_UNIT)
return FALSE
//Finally, we burn
var/old_burn = group_burn_power
group_burn_power = total_burn_power
if(old_burn == group_burn_power)
return
switch(group_burn_power)
if(0 to 7)
group_fire_state = LIQUID_FIRE_STATE_SMALL
if(7 to 8)
group_fire_state = LIQUID_FIRE_STATE_MILD
if(8 to 9)
group_fire_state = LIQUID_FIRE_STATE_MEDIUM
if(9 to 10)
group_fire_state = LIQUID_FIRE_STATE_HUGE
if(10 to INFINITY)
group_fire_state = LIQUID_FIRE_STATE_INFERNO
/datum/liquid_group/proc/process_fire()
get_group_burn()
var/reagents_to_remove = group_burn_rate * (length(burning_members))
if(!group_burn_power)
extinguish_all()
return
remove_any(amount = reagents_to_remove)
if(!reagents_per_turf)
return
if(group_burn_rate >= reagents_per_turf)
var/list/removed_turf = list()
var/number = round(group_burn_rate / reagents_per_turf)
for(var/num in 1 to number)
if(!length(burning_members))
break
var/turf/picked_turf = burning_members[1]
extinguish(picked_turf)
remove_from_group(picked_turf)
QDEL_NULL(picked_turf.liquids)
removed_turf |= picked_turf
for(var/turf/remover in removed_turf)
for(var/dir in GLOB.cardinals)
var/turf/open/open_turf = get_step(remover, dir)
if(!isopenturf(open_turf) || QDELETED(open_turf.liquids))
continue
check_edges(open_turf)
while(length(removed_turf))
var/turf/picked_turf = pick(removed_turf)
var/list/output = try_split(picked_turf, TRUE)
removed_turf -= picked_turf
for(var/turf/outputted_turf in output)
if(outputted_turf in removed_turf)
removed_turf -= outputted_turf
/datum/liquid_group/proc/ignite_turf(turf/member)
get_group_burn()
if(!group_burn_power)
return
member.liquids.fire_state = group_fire_state
member.liquids.set_fire_effect()
burning_members |= member
SSliquids.burning_turfs |= member
/datum/liquid_group/proc/build_fire_cache(turf/burning_member)
cached_fire_spreads |= burning_member
var/list/directions = list(NORTH, SOUTH, EAST, WEST)
var/list/spreading_turfs = list()
for(var/dir in directions)
var/turf/open/open_adjacent = get_step(burning_member, dir)
if(QDELETED(open_adjacent) || QDELETED(open_adjacent.liquids))
continue
spreading_turfs |= open_adjacent
cached_fire_spreads[burning_member] = spreading_turfs
/datum/liquid_group/proc/process_spread(turf/member)
if(member.liquids.fire_state <= LIQUID_FIRE_STATE_MEDIUM) // fires to small to worth spreading
return
if(!cached_fire_spreads[member])
build_fire_cache(member)
for(var/turf/open/adjacent_turf in cached_fire_spreads[member])
if(!QDELETED(adjacent_turf.liquids) && adjacent_turf.liquids.liquid_group == src && adjacent_turf.liquids.fire_state < member.liquids.fire_state)
adjacent_turf.liquids.fire_state = group_fire_state
member.liquids.set_fire_effect()
burning_members |= adjacent_turf
SSliquids.burning_turfs |= adjacent_turf
for(var/atom/movable/movable in adjacent_turf)
movable.fire_act((T20C+50) + (50*adjacent_turf.liquids.fire_state), 125)
/datum/liquid_group/proc/extinguish_all()
group_burn_power = 0
group_fire_state = LIQUID_FIRE_STATE_NONE
for(var/turf/member in burning_members)
member.liquids.fire_state = LIQUID_FIRE_STATE_NONE
member.liquids.set_fire_effect()
if(burning_members[member])
burning_members -= member
if(SSliquids.burning_turfs[member])
SSliquids.burning_turfs -= member
/datum/liquid_group/proc/extinguish(turf/member)
if(SSliquids.burning_turfs[member])
SSliquids.burning_turfs -= member
burning_members -= member
if(QDELETED(member.liquids))
return
member.liquids.fire_state = LIQUID_FIRE_STATE_NONE
member.liquids.set_fire_effect()
///EDGE COLLECTION AND PROCESSING
/datum/liquid_group/proc/check_adjacency(turf/member)
var/adjacent_liquid = 0
for(var/tur in member.get_atmos_adjacent_turfs())
var/turf/adjacent_turf = tur
if(!QDELETED(adjacent_turf.liquids))
if(adjacent_turf.liquids.liquid_group == member.liquids.liquid_group)
adjacent_liquid++
if(adjacent_liquid < 2)
return FALSE
return TRUE
/datum/liquid_group/proc/process_cached_edges()
for(var/turf/cached_turf in cached_edge_turfs)
for(var/direction in cached_edge_turfs[cached_turf])
var/turf/directional_turf = get_step(cached_turf, direction)
if(isclosedturf(directional_turf))
continue
if(!(directional_turf in cached_turf.atmos_adjacent_turfs)) //i hate that this is needed
continue
if(!cached_turf.atmos_adjacent_turfs[directional_turf])
continue
if(spread_liquid(directional_turf, cached_turf))
cached_edge_turfs[cached_turf] -= direction
if(!length(cached_edge_turfs[cached_turf]))
cached_edge_turfs -= cached_turf
/datum/liquid_group/proc/check_edges(turf/checker)
var/list/passed_directions = list()
for(var/direction in GLOB.cardinals)
var/turf/directional_turf = get_step(checker, direction)
if(!QDELETED(directional_turf.liquids))
continue
passed_directions.Add(direction)
if(length(passed_directions))
cached_edge_turfs |= checker
cached_edge_turfs[checker] = passed_directions
///SPLITING PROCS AND RETURNING CONNECTED PROCS
/*okay for large groups we need some way to iterate over it without grinding the server to a halt to split them
* A breadth-first search or depth first search, are the most efficent but still cause issues with larger groups
* the easist way around this would be using an index of visted turfs and comparing it for changes to save cycles
* this has the draw back of being multiple times slower on small groups, but massively faster on large groups
* For a unique key the easist way to do so would be either to retrive its member number, or better its position
* key as that will be totally unique. this can be used for things aside from splitting by sucking up large groups
*/
/datum/liquid_group/proc/return_connected_liquids(obj/effect/abstract/liquid_turf/source, adjacent_checks = 0)
var/temporary_split_key = source.temporary_split_key
var/turf/first_turf = source.my_turf
var/list/connected_liquids = list()
///the current queue
var/list/queued_liquids = list(source)
///the turfs that we have previously visited with unique ids
var/list/previously_visited = list()
///the turf object the liquid resides on
var/turf/queued_turf
var/obj/effect/abstract/liquid_turf/current_head
///compares after each iteration to see if we even need to continue
var/visited_length = 0
while(length(queued_liquids))
current_head = queued_liquids[1]
queued_turf = current_head.my_turf
queued_liquids -= current_head
for(var/turf/adjacent_turf in get_adjacent_open_turfs(queued_turf))
if(QDELETED(adjacent_turf.liquids) || !members[adjacent_turf])
continue
if(!(adjacent_turf in queued_turf.atmos_adjacent_turfs)) //i hate that this is needed
continue
visited_length = length(previously_visited)
previously_visited["[adjacent_turf.liquids.x]_[adjacent_turf.liquids.y]"] = adjacent_turf.liquids
if(length(previously_visited) != visited_length)
queued_liquids |= adjacent_turf.liquids
connected_liquids |= adjacent_turf
if(adjacent_checks)
if(temporary_split_key == adjacent_turf.liquids.temporary_split_key && adjacent_turf != first_turf)
adjacent_checks--
if(adjacent_checks <= 0)
return FALSE
return connected_liquids
/datum/liquid_group/proc/return_connected_liquids_in_range(obj/effect/abstract/liquid_turf/source, total_turfs = 0)
var/list/connected_liquids = list()
///the current queue
var/list/queued_liquids = list(source)
///the turfs that we have previously visited with unique ids
var/list/previously_visited = list()
///the turf object the liquid resides on
var/turf/queued_turf
var/obj/effect/abstract/liquid_turf/current_head
///compares after each iteration to see if we even need to continue
var/visited_length = 0
while(length(queued_liquids))
current_head = queued_liquids[1]
queued_turf = current_head.my_turf
queued_liquids -= current_head
for(var/turf/adjacent_turf in get_adjacent_open_turfs(queued_turf))
if(QDELETED(adjacent_turf.liquids) || !members[adjacent_turf])
continue
visited_length = length(previously_visited)
previously_visited["[adjacent_turf.liquids.x]_[adjacent_turf.liquids.y]"] = adjacent_turf.liquids
if(length(previously_visited) != visited_length)
queued_liquids |= adjacent_turf.liquids
connected_liquids |= adjacent_turf
if(total_turfs > 0 && length(connected_liquids) >= total_turfs)
return connected_liquids
/datum/liquid_group/proc/try_split(turf/source, return_list = FALSE)
if(!length(members))
return
var/list/connected_liquids = list()
var/turf/head_turf = source
var/obj/effect/abstract/liquid_turf/current_head
var/generated_key = "[world.time]_activemembers[length(members)]"
var/adjacent_liquid_count = -1
for(var/turf/adjacent_turf in get_adjacent_open_turfs(head_turf))
if(QDELETED(adjacent_turf.liquids) || !members[adjacent_turf]) //empty turf or not our group just skip this
continue
///the section is a little funky, as if say a cross shaped liquid removal occurs this will leave 3 of them in the same group, not a big deal as this only affects turfs that are like 5 tiles total
current_head = adjacent_turf.liquids
current_head.temporary_split_key = generated_key
adjacent_liquid_count++
if(adjacent_liquid_count > 0) ///if there is only 1 adjacent liquid it physically can't split
return FALSE
if(current_head)
connected_liquids = return_connected_liquids(current_head, adjacent_liquid_count)
if(!length(connected_liquids) || length(connected_liquids) == length(members)) //yes yes i know if two groups are identical in size this will break but fixing this would add to much processing
if(return_list)
return connected_liquids
return FALSE
var/amount_to_transfer = length(connected_liquids) * reagents_per_turf
var/datum/liquid_group/new_group = new(1)
for(var/turf/connected_liquid in connected_liquids)
new_group.check_edges(connected_liquid)
if(connected_liquid in burning_members)
new_group.burning_members |= connected_liquid
remove_from_group(connected_liquid, TRUE)
new_group.add_to_group(connected_liquid)
trans_to_seperate_group(new_group.reagents, amount_to_transfer)
new_group.total_reagent_volume = new_group.reagents.total_volume
new_group.reagents_per_turf = new_group.total_reagent_volume / length(new_group.members)
///asses the group to see if it should exist
var/new_group_length = length(new_group.members)
if(new_group.total_reagent_volume == 0 || new_group.reagents_per_turf == 0 || !new_group_length)
qdel(new_group)
return FALSE
for(var/turf/new_turf in new_group.members)
if(new_turf in members)
new_group.members -= new_turf
if(!length(new_group.members))
qdel(new_group)
return FALSE
if(return_list)
return connected_liquids
return TRUE
/datum/liquid_group/proc/try_bulk_split(list/input_turfs)
var/list/connected_array = list()
for(var/turf/listed_input in input_turfs)
for(var/turf/cardinal in listed_input.get_atmos_adjacent_turfs())
var/exists_already = FALSE
for(var/list/arrayed_item in connected_array)
if(cardinal in arrayed_item)
exists_already = TRUE
break
if(!exists_already)
if(!QDELETED(cardinal.liquids))
var/list/temp = return_connected_liquids(cardinal.liquids)
if(isnull(temp) || !length(temp) || (temp in connected_array))
continue
connected_array |= list(temp)
if(!length(connected_array))
return
splitting_array = connected_array
SSliquids.arrayed_groups += src
///we try and work on the split queue from this point on
/datum/liquid_group/proc/work_on_split_queue()
if(!length(splitting_array))
SSliquids.arrayed_groups -= src
return
var/list/plucked_array = list()
var/pick_count = 3
while(pick_count > 0 && length(splitting_array))
var/list/temp = pick(splitting_array)
plucked_array += list(temp)
splitting_array -= list(temp)
if(!length(splitting_array))
SSliquids.arrayed_groups -= src
for(var/list/connected_liquids in plucked_array)
if(isnull(connected_liquids) || !length(connected_liquids))
continue
var/amount_to_transfer = length(connected_liquids) * reagents_per_turf
var/datum/liquid_group/new_group = new(1)
if(!members)
members = list()
trans_to_seperate_group(new_group.reagents, amount_to_transfer)
for(var/turf/connected_liquid in connected_liquids)
new_group.check_edges(connected_liquid)
if(connected_liquid in burning_members)
new_group.burning_members |= connected_liquid
remove_from_group(connected_liquid, TRUE)
new_group.add_to_group(connected_liquid)
new_group.total_reagent_volume = new_group.reagents.total_volume
new_group.reagents_per_turf = new_group.total_reagent_volume / length(new_group.members)
///asses the group to see if it should exist
var/new_group_length = length(new_group.members)
if(new_group.total_reagent_volume == 0 || new_group.reagents_per_turf == 0 || !new_group_length)
qdel(new_group)
return
for(var/turf/new_turf in new_group.members)
if(new_turf in members)
new_group.members -= new_turf
if(!length(new_group.members))
qdel(new_group)
return
///EXPOSURE AND SPREADING
/datum/liquid_group/proc/expose_members_turf(obj/effect/abstract/liquid_turf/member)
if(!turf_reagents)
return
var/turf/members_turf = member.my_turf
for(var/atom/movable/target_atom in members_turf)
turf_reagents.reaction(target_atom, TOUCH)
/datum/liquid_group/proc/expose_atom(atom/target, modifier = 0, method)
if(!turf_reagents)
return
if(HAS_TRAIT(target, LIQUID_PROTECTION))
return
turf_reagents.reaction(target, method)
/datum/liquid_group/proc/spread_liquid(turf/new_turf, turf/source_turf)
if(isclosedturf(new_turf) || !source_turf.atmos_adjacent_turfs)
return
if(!(new_turf in source_turf.atmos_adjacent_turfs)) //i hate that this is needed
return
if(!source_turf.atmos_adjacent_turfs[new_turf])
return
if(isopenspaceturf(new_turf))
var/turf/Z_turf_below = GET_TURF_BELOW(new_turf)
if(!Z_turf_below)
return
if(isspaceturf(Z_turf_below))
return FALSE
if(QDELETED(Z_turf_below.liquids))
Z_turf_below.liquids = new(Z_turf_below)
if(QDELETED(source_turf.liquids))
remove_from_group(source_turf)
if(source_turf in cached_edge_turfs)
cached_edge_turfs -= source_turf
return FALSE
source_turf.liquids.liquid_group.transfer_reagents_to_secondary_group(source_turf.liquids, Z_turf_below.liquids)
var/obj/splashy = new /obj/effect/temp_visual/liquid_splash(Z_turf_below)
if(!QDELETED(Z_turf_below.liquids?.liquid_group))
splashy.color = Z_turf_below.liquids.liquid_group.group_color
return FALSE
if(QDELETED(new_turf.liquids) && !istype(new_turf, /turf/open/openspace) && !isspaceturf(new_turf) && !istype(new_turf, /turf/open/floor/plating/ocean) && source_turf.turf_height == new_turf.turf_height) // no space turfs, or oceans turfs, also don't attempt to spread onto a turf that already has liquids wastes processing time
if(reagents_per_turf < LIQUID_HEIGHT_DIVISOR)
return FALSE
if(!length(members))
return FALSE
reagents_per_turf = total_reagent_volume / length(members)
expected_turf_height = CEILING(reagents_per_turf, 1) / LIQUID_HEIGHT_DIVISOR
new_turf.liquids = new(new_turf, src)
new_turf.liquids.alpha = group_alpha
check_edges(new_turf)
if(expected_turf_height >= LIQUID_ANKLES_LEVEL_HEIGHT) //liquid is deep enough, do a splash
var/obj/splashy = new /obj/effect/temp_visual/liquid_splash(new_turf)
if(!QDELETED(new_turf.liquids?.liquid_group))
splashy.color = new_turf.liquids.liquid_group.group_color
water_rush(new_turf, source_turf)
else if(!QDELETED(source_turf?.liquids?.liquid_group) && !QDELETED(new_turf?.liquids?.liquid_group) && new_turf.liquids.liquid_group != source_turf.liquids.liquid_group && source_turf.turf_height == new_turf.turf_height && new_turf.liquids.liquid_group.can_merge)
merge_group(new_turf.liquids.liquid_group)
return FALSE
else if(source_turf.turf_height != new_turf.turf_height)
if(new_turf.turf_height < source_turf.turf_height) // your going down
if(QDELETED(new_turf.liquids))
new_turf.liquids = new(new_turf)
if(new_turf.turf_height + new_turf.liquids.liquid_group.expected_turf_height < source_turf.turf_height)
var/obj/effect/abstract/liquid_turf/turf_liquids = new_turf.liquids
trans_to_seperate_group(turf_liquids.liquid_group.reagents, reagents_per_turf, source_turf)
turf_liquids.liquid_group.process_group()
else if(source_turf.turf_height < new_turf.turf_height)
if(source_turf.turf_height + expected_turf_height < new_turf.turf_height)
return
if(QDELETED(new_turf.liquids))
new_turf.liquids = new(new_turf)
var/obj/effect/abstract/liquid_turf/turf_liquids = new_turf.liquids
trans_to_seperate_group(turf_liquids.liquid_group.reagents, 10, source_turf) //overflows out
turf_liquids.liquid_group.process_group()
return TRUE
/datum/liquid_group/proc/water_rush(turf/new_turf, turf/source_turf)
var/direction = get_dir(source_turf, new_turf)
for(var/atom/movable/target_atom in new_turf)
reagents.reaction(target_atom, TOUCH, (reagents_per_turf * 0.5))
if(target_atom.anchored || target_atom.pulledby || (target_atom.move_resist >= INFINITY)) //thing is too heavy or secured to move