forked from The-Powder-Toy/The-Powder-Toy
/
Simulation.cpp
2866 lines (2696 loc) · 73.1 KB
/
Simulation.cpp
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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cmath>
//Simulation stuff
#include "Simulation.h"
#include "CoordStack.h"
#include "gravity.h"
#include "interface.h" //for framenum, try and remove this later
#include "luaconsole.h" //for lua_el_mode
#include "misc.h"
#include "powder.h"
#include "Element.h"
#include "ElementDataContainer.h"
#include "Tool.h"
#include "common/tpt-math.h"
#include "common/tpt-minmax.h"
#include "game/Brush.h"
#include "game/Sign.h"
#include "simulation/elements/MOVS.h"
#include "simulation/elements/FIGH.h"
#include "simulation/elements/STKM.h"
// Declare the element initialisation functions
#define ElementNumbers_Include_Decl
#define DEFINE_ELEMENT(name, id) void name ## _init_element(ELEMENT_INIT_FUNC_ARGS);
#include "ElementNumbers.h"
#include "WallNumbers.h"
#include "ToolNumbers.h"
Simulation *globalSim = NULL; // TODO: remove this global variable
Simulation::Simulation():
currentTick(0),
pfree(-1),
parts_lastActiveIndex(NPART-1),
debug_currentParticle(0),
forceStackingCheck(false),
edgeMode(0),
saveEdgeMode(0),
msRotation(true),
maxFrames(25),
#ifdef NOMOD
instantActivation(false),
#else
instantActivation(true),
#endif
lightningRecreate(0)
{
std::fill(&elementData[0], &elementData[PT_NUM], static_cast<ElementDataContainer*>(NULL));
air = new Air();
Clear();
InitElements();
InitCanMove();
}
Simulation::~Simulation()
{
for (int t = 0; t < PT_NUM; t++)
{
if (elementData[t])
{
delete elementData[t];
elementData[t] = NULL;
}
}
delete air;
}
void Simulation::InitElements()
{
#define DEFINE_ELEMENT(name, id) if (id>=0 && id<PT_NUM) { name ## _init_element(this, &elements[id], id); };
#define ElementNumbers_Include_Call
#include "simulation/ElementNumbers.h"
Simulation_Compat_CopyData(this);
}
void Simulation::Clear()
{
air->Clear();
for (int t = 0; t < PT_NUM; t++)
{
if (elementData[t])
{
elementData[t]->Simulation_Cleared(this);
}
}
std::fill(&elementCount[0], &elementCount[PT_NUM], 0);
pfree = 0;
parts_lastActiveIndex = NPART-1;
#ifdef NOMOD
instantActivation = false;
#else
instantActivation = true;
#endif
saveEdgeMode = -1;
if (edgeMode == 1)
draw_bframe();
}
void Simulation::RecountElements()
{
std::fill(&elementCount[0], &elementCount[PT_NUM], 0);
for (int i = 0; i < NPART; i++)
if (parts[i].type)
elementCount[parts[i].type]++;
}
// the function for creating a particle
// p=-1 for normal creation (checks whether the particle is allowed to be in that location first)
// p=-3 to create without checking whether the particle is allowed to be in that location
// or p = a particle number, to replace a particle
int Simulation::part_create(int p, int x, int y, int t, int v)
{
// This function is only for actually creating particles.
// Not for tools, or changing things into spark, or special brush things like setting clone ctype.
int i, oldType = PT_NONE;
if (x<0 || y<0 || x>=XRES || y>=YRES || t<=0 || t>=PT_NUM || !elements[t].Enabled)
{
return -1;
}
// Spark Checks here
#ifndef NOMOD
if (p == -2 && (pmap[y][x]&0xFF) == PT_BUTN && parts[pmap[y][x]>>8].life == 10)
{
spark_conductive(pmap[y][x]>>8, x, y);
return pmap[y][x]>>8;
}
#endif
if (t==PT_SPRK)
{
int type = pmap[y][x]&0xFF;
int index = pmap[y][x]>>8;
if(type == PT_WIRE)
{
parts[index].ctype = PT_DUST;
return index;
}
if (p==-2 && ((elements[type].Properties & PROP_DRAWONCTYPE) || type==PT_CRAY))
{
parts[index].ctype = PT_SPRK;
return index;
}
if (!(type == PT_INST || (elements[type].Properties&PROP_CONDUCTS)))
return -1;
if (parts[index].life!=0)
return -1;
if (type == PT_INST)
{
if (p == -2)
INST_flood_spark(this, x, y);
else
spark_conductive(index, x, y);
return index;
}
spark_conductive_attempt(index, x, y);
return index;
}
// End Spark checks
//Brush Creation
if (p == -2)
{
if (pmap[y][x])
{
int drawOn = pmap[y][x]&0xFF;
if (drawOn == t)
return -1;
//If an element has the PROP_DRAWONCTYPE property, and the element being drawn to it does not have PROP_NOCTYPEDRAW (Also some special cases), set the element's ctype
if (((elements[drawOn].Properties & PROP_DRAWONCTYPE) ||
(drawOn == PT_STOR && !(elements[t].Properties & TYPE_SOLID)) ||
(drawOn == PT_PCLN && t != PT_PSCN && t != PT_NSCN) ||
(drawOn == PT_PBCN && t != PT_PSCN && t != PT_NSCN))
&& (!(elements[t].Properties & PROP_NOCTYPEDRAW)))
{
parts[pmap[y][x]>>8].ctype = t;
if (t == PT_LIFE && v >= 0 && v < NGOL)
{
if (drawOn == PT_CONV)
parts[pmap[y][x]>>8].ctype |= v<<8;
else if (drawOn != PT_STOR)
parts[pmap[y][x]>>8].tmp = v;
}
}
else if (drawOn == PT_DTEC || (drawOn == PT_PSTN && t != PT_FRME) || drawOn == PT_DRAY)
{
parts[pmap[y][x]>>8].ctype = t;
if (t == PT_LIFE && v >= 0 && v < NGOL)
{
if (drawOn == PT_DTEC)
parts[pmap[y][x]>>8].tmp = v;
else if (drawOn == PT_DRAY)
parts[pmap[y][x]>>8].ctype |= v<<8;
}
}
else if (drawOn == PT_CRAY)
{
parts[pmap[y][x]>>8].ctype = t;
if (t == PT_LIFE && v >= 0 && v < NGOL)
parts[pmap[y][x]>>8].ctype |= v<<8;
else if (t == PT_LIGH)
parts[pmap[y][x]>>8].ctype |= 30<<8;
parts[pmap[y][x]>>8].temp = elements[t].DefaultProperties.temp;
}
return -1;
}
else if (IsWallBlocking(x, y, t))
return -1;
if (photons[y][x] && (elements[t].Properties & TYPE_ENERGY))
return -1;
}
// End Brush Creation
if (elements[t].Func_Create_Allowed)
{
if (!(*(elements[t].Func_Create_Allowed))(this, p, x, y, t))
return -1;
}
if (p == -1)
{
// Check whether the particle can be created here
// If there is a particle, only allow creation if the new particle can occupy the same space as the existing particle
// If there isn't a particle but there is a wall, check whether the new particle is allowed to be in it
// (not "!=2" for wall check because eval_move returns 1 for moving into empty space)
// If there's no particle and no wall, assume creation is allowed
if (pmap[y][x] ? (EvalMove(t, x, y)!=2) : (bmap[y/CELL][x/CELL] && EvalMove(t, x, y)==0))
{
return -1;
}
i = part_alloc();
}
else if (p == -3) // skip pmap checks, e.g. for sing explosion
{
i = part_alloc();
}
else if (p >= 0) // Replace existing particle
{
int oldX = (int)(parts[p].x+0.5f);
int oldY = (int)(parts[p].y+0.5f);
oldType = parts[p].type;
if (elements[oldType].Func_ChangeType)
{
(*(elements[oldType].Func_ChangeType))(this, p, oldX, oldY, oldType, t);
}
if (oldType) elementCount[oldType]--;
pmap_remove(p, oldX, oldY);
i = p;
}
else // Dunno, act like it was p=-3
{
i = part_alloc();
}
// Check whether a particle was successfully allocated
if (i<0)
return -1;
// Set some properties
parts[i] = elements[t].DefaultProperties;
parts[i].type = t;
parts[i].x = (float)x;
parts[i].y = (float)y;
// Fancy dust effects for powder types
if ((elements[t].Properties & TYPE_PART) && pretty_powder)
{
int sandcolor = (int)(20.0f*sin((float)(currentTick%360)*(M_PI/180.0f)));
int colr = (int)(COLR(elements[t].Colour)+sandcolor*1.3f+(rand()%40)-20+(rand()%30)-15);
int colg = (int)(COLG(elements[t].Colour)+sandcolor*1.3f+(rand()%40)-20+(rand()%30)-15);
int colb = (int)(COLB(elements[t].Colour)+sandcolor*1.3f+(rand()%40)-20+(rand()%30)-15);
colr = std::max(0, std::min(255, colr));
colg = std::max(0, std::min(255, colg));
colb = std::max(0, std::min(255, colb));
parts[i].dcolour = COLARGB(rand()%150, colr, colg, colb);
}
// Set non-static properties (such as randomly generated ones)
if (elements[t].Func_Create)
{
(*(elements[t].Func_Create))(this, i, x, y, t, v);
}
pmap_add(i, x, y, t);
if (elements[t].Func_ChangeType)
{
(*(elements[t].Func_ChangeType))(this, i, x, y, oldType, t);
}
elementCount[t]++;
return i;
}
// changes the type of particle number i, to t. This also changes pmap at the same time.
bool Simulation::part_change_type(int i, int x, int y, int t)
{
if (x<0 || y<0 || x>=XRES || y>=YRES || i>=NPART || t<0 || t>=PT_NUM)
return false;
if (t == parts[i].type)
return true;
if (!elements[t].Enabled)
{
part_kill(i);
return true;
}
if (elements[parts[i].type].Properties&PROP_INDESTRUCTIBLE)
return false;
if (elements[t].Func_Create_Allowed)
{
if (!(*(elements[t].Func_Create_Allowed))(this, i, x, y, t))
return false;
}
int oldType = parts[i].type;
if (oldType)
elementCount[oldType]--;
parts[i].type = t;
pmap_remove(i, x, y);
if (t)
{
pmap_add(i, x, y, t);
elementCount[t]++;
}
if (elements[oldType].Func_ChangeType)
{
(*(elements[oldType].Func_ChangeType))(this, i, x, y, oldType, t);
}
if (elements[t].Func_ChangeType)
{
(*(elements[t].Func_ChangeType))(this, i, x, y, oldType, t);
}
return true;
}
// used by lua to change type while deleting any particle specific info, and also keep pmap / elementCount up to date
void Simulation::part_change_type_force(int i, int t)
{
int x = (int)(parts[i].x), y = (int)(parts[i].y);
if (t<0 || t>=PT_NUM)
return;
int oldType = parts[i].type;
if (oldType)
elementCount[oldType]--;
parts[i].type = t;
pmap_remove(i, x, y);
if (t)
{
pmap_add(i, x, y, t);
elementCount[t]++;
}
if (elements[oldType].Func_ChangeType)
{
(*(elements[oldType].Func_ChangeType))(this, i, x, y, oldType, t);
}
if (elements[t].Func_ChangeType)
{
(*(elements[t].Func_ChangeType))(this, i, x, y, oldType, t);
}
}
// kills particle ID #i
void Simulation::part_kill(int i)
{
int x = (int)(parts[i].x+0.5f);
int y = (int)(parts[i].y+0.5f);
int t = parts[i].type;
if (t && elements[t].Func_ChangeType)
{
(*(elements[t].Func_ChangeType))(this, i, x, y, t, PT_NONE);
}
if (x>=0 && y>=0 && x<XRES && y<YRES)
pmap_remove(i, x, y);
if (t == PT_NONE) // TODO: remove this? (//This shouldn't happen anymore, but it's here just in case)
return;
elementCount[t]--;
part_free(i);
}
// calls kill_part with the particle located at (x,y)
void Simulation::part_delete(int x, int y)
{
if (x<0 || y<0 || x>=XRES || y>=YRES)
return;
if (photons[y][x])
part_kill(photons[y][x]>>8);
else if (pmap[y][x])
part_kill(pmap[y][x]>>8);
}
/* Recalculates the pfree/parts[].life linked list for particles with ID <= parts_lastActiveIndex.
* This ensures that future particle allocations are done near the start of the parts array, to keep parts_lastActiveIndex low.
* parts_lastActiveIndex is also decreased if appropriate.
* Does not modify or even read any particles beyond parts_lastActiveIndex */
void Simulation::RecalcFreeParticles()
{
int x, y, t;
int lastPartUsed = 0;
int lastPartUnused = -1;
std::fill_n(&pmap[0][0], XRES*YRES, 0);
std::fill_n(&pmap_count[0][0], XRES*YRES, 0);
std::fill_n(&photons[0][0], XRES*YRES, 0);
NUM_PARTS = 0;
//the particle loop that resets the pmap/photon maps every frame, to update them.
for (int i = 0; i <= parts_lastActiveIndex; i++)
{
if (parts[i].type)
{
t = parts[i].type;
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
if (parts[i].flags&FLAG_SKIPMOVE)
parts[i].flags &= ~FLAG_SKIPMOVE;
if (x>=0 && y>=0 && x<XRES && y<YRES)
{
#ifndef NOMOD
if (t == PT_PINV && (parts[i].tmp2>>8) >= i)
parts[i].tmp2 = 0;
#endif
if (elements[t].Properties & TYPE_ENERGY)
photons[y][x] = t|(i<<8);
else
{
#ifdef NOMOD
if (!pmap[y][x] || (t!=PT_INVIS && t!= PT_FILT))
pmap[y][x] = t|(i<<8);
if (t!=PT_THDR && t!=PT_EMBR && t!=PT_FIGH && t!=PT_PLSM)
pmap_count[y][x]++;
#else
// Particles are sometimes allowed to go inside INVS and FILT
// To make particles collide correctly when inside these elements, these elements must not overwrite an existing pmap entry from particles inside them
if (!pmap[y][x] || (t!=PT_INVIS && t!=PT_FILT && (t!=PT_MOVS || (pmap[y][x]&0xFF)==PT_MOVS) && (pmap[y][x]&0xFF)!=PT_PINV))
pmap[y][x] = t|(i<<8);
else if ((pmap[y][x]&0xFF) == PT_PINV)
parts[pmap[y][x]>>8].tmp2 = t|(i<<8);
// Count number of particles at each location, for excess stacking check
// (does not include energy particles or THDR - currently no limit on stacking those)
if (t!=PT_THDR && t!=PT_EMBR && t!=PT_FIGH && t!=PT_PLSM && t!=PT_MOVS)
pmap_count[y][x]++;
#endif
}
}
lastPartUsed = i;
NUM_PARTS++;
//decrease the life of certain elements by 1 every frame
if (!sys_pause || framerender)
decrease_life(i);
}
else
{
if (lastPartUnused < 0)
pfree = i;
else
parts[lastPartUnused].life = i;
lastPartUnused = i;
}
}
if (lastPartUnused == -1)
{
if (parts_lastActiveIndex >= NPART-1)
pfree = -1;
else
pfree = parts_lastActiveIndex+1;
}
else
{
if (parts_lastActiveIndex >= NPART-1)
parts[lastPartUnused].life = -1;
else
parts[lastPartUnused].life = parts_lastActiveIndex+1;
}
parts_lastActiveIndex = lastPartUsed;
}
void Simulation::UpdateBefore()
{
//update wallmaps
for (int y = 0; y < YRES/CELL; y++)
{
for (int x = 0; x < XRES/CELL; x++)
{
if (emap[y][x])
emap[y][x]--;
air->bmap_blockair[y][x] = (bmap[y][x]==WL_WALL || bmap[y][x]==WL_WALLELEC || bmap[y][x]==WL_BLOCKAIR || (bmap[y][x]==WL_EWALL && !emap[y][x]));
air->bmap_blockairh[y][x] = (bmap[y][x]==WL_WALL || bmap[y][x]==WL_WALLELEC || bmap[y][x]==WL_BLOCKAIR || bmap[y][x]==WL_GRAV || (bmap[y][x]==WL_EWALL && !emap[y][x])) ? 0x8:0;
}
}
//check for excessive stacked particles, create BHOL if found
if (forceStackingCheck || !(rand()%10))
{
bool excessiveStackingFound = false;
forceStackingCheck = 0;
for (int y = 0; y < YRES; y++)
{
for (int x = 0; x < XRES; x++)
{
//Use a threshold, since some particle stacking can be normal (e.g. BIZR + FILT)
//Setting pmap_count[y][x] > NPART means BHOL will form in that spot
if (pmap_count[y][x] > 5)
{
if (bmap[y/CELL][x/CELL] == WL_EHOLE)
{
//Allow more stacking in E-hole
if (pmap_count[y][x] > 1500)
{
pmap_count[y][x] = pmap_count[y][x] + NPART;
excessiveStackingFound = true;
}
}
//Random chance to turn into BHOL that increases with the amount of stacking, up to a threshold where it is certain to turn into BHOL
else if (pmap_count[y][x] > 1500 || (rand()%1600) <= pmap_count[y][x]+100)
{
pmap_count[y][x] = pmap_count[y][x] + NPART;
excessiveStackingFound = true;
}
}
}
}
if (excessiveStackingFound)
{
for (int i = 0; i <= parts_lastActiveIndex; i++)
{
if (parts[i].type)
{
int t = parts[i].type;
int x = (int)(parts[i].x+0.5f);
int y = (int)(parts[i].y+0.5f);
if (x >= 0 && y >= 0 && x < XRES && y < YRES && !(elements[t].Properties&TYPE_ENERGY))
{
if (pmap_count[y][x] >= NPART)
{
if (pmap_count[y][x] > NPART)
{
part_create(i, x, y, PT_NBHL);
parts[i].temp = MAX_TEMP;
parts[i].tmp = pmap_count[y][x] - NPART;//strength of grav field
if (parts[i].tmp > 51200)
parts[i].tmp = 51200;
pmap_count[y][x] = NPART;
}
else
{
part_kill(i);
}
}
}
}
}
}
}
// For elements with extra data, run special update functions
// This does things like LIFE recalculation and LOLZ patterns
for (int t = 1; t < PT_NUM; t++)
{
if (elementData[t])
{
elementData[t]->Simulation_BeforeUpdate(this);
}
}
// lightning recreation time (TODO: move to elementData)
if (lightningRecreate)
lightningRecreate--;
}
void Simulation::UpdateParticles(int start, int end)
{
// The main particle loop function, goes over all particles.
for (int i = start; i <= end && i <= parts_lastActiveIndex; i++)
if (parts[i].type)
{
UpdateParticle(i);
}
}
void Simulation::UpdateAfter()
{
// For elements with extra data, run special update functions
// Used only for moving solids
for (int t = 1; t < PT_NUM; t++)
{
if (elementData[t])
{
elementData[t]->Simulation_AfterUpdate(this);
}
}
}
void Simulation::Tick()
{
RecalcFreeParticles();
if (!sys_pause || framerender)
{
UpdateBefore();
UpdateParticles(0, NPART);
UpdateAfter();
currentTick++;
}
// In automatic heat mode, calculate highest and lowest temperature points (maybe could be moved)
if (heatmode == 1)
{
highesttemp = MIN_TEMP;
lowesttemp = MAX_TEMP;
for (int i = 0; i <= parts_lastActiveIndex; i++)
{
if (parts[i].type)
{
if (parts[i].temp > highesttemp)
highesttemp = (int)parts[i].temp;
if (parts[i].temp < lowesttemp)
lowesttemp = (int)parts[i].temp;
}
}
}
}
int PCLN_update(UPDATE_FUNC_ARGS);
int CLNE_update(UPDATE_FUNC_ARGS);
int PBCN_update(UPDATE_FUNC_ARGS);
int BCLN_update(UPDATE_FUNC_ARGS);
int MOVS_update(UPDATE_FUNC_ARGS);
bool Simulation::UpdateParticle(int i)
{
unsigned int t = (unsigned int)parts[i].type;
int x = (int)(parts[i].x+0.5f);
int y = (int)(parts[i].y+0.5f);
float pGravX, pGravY, pGravD;
bool transitionOccurred = false;
//this kills any particle out of the screen, or in a wall where it isn't supposed to go
if (OutOfBounds(x, y) ||
( bmap[y/CELL][x/CELL] &&
( bmap[y/CELL][x/CELL] == WL_WALL ||
(bmap[y/CELL][x/CELL] == WL_WALLELEC) ||
(bmap[y/CELL][x/CELL] == WL_ALLOWAIR) ||
(bmap[y/CELL][x/CELL] == WL_DESTROYALL) ||
(bmap[y/CELL][x/CELL] == WL_ALLOWLIQUID && !(elements[t].Properties&TYPE_LIQUID)) ||
(bmap[y/CELL][x/CELL] == WL_ALLOWPOWDER && !(elements[t].Properties&TYPE_PART)) ||
(bmap[y/CELL][x/CELL] == WL_ALLOWGAS && !(elements[t].Properties&TYPE_GAS)) || //&& elements[t].Falldown!=0 && t!=PT_FIRE && t!=PT_SMKE && t!=PT_HFLM) ||
(bmap[y/CELL][x/CELL] == WL_ALLOWENERGY && !(elements[t].Properties&TYPE_ENERGY)) ||
(bmap[y/CELL][x/CELL] == WL_DETECT && (t==PT_METL || t==PT_SPRK)) ||
(bmap[y/CELL][x/CELL] == WL_EWALL && !emap[y/CELL][x/CELL])
#ifdef NOMOD
) && t!=PT_STKM && t!=PT_STKM2 && t!=PT_FIGH))
#else
) && t!=PT_STKM && t!=PT_STKM2 && t!=PT_FIGH && t != PT_MOVS))
#endif
{
part_kill(i);
return true;
}
if (bmap[y/CELL][x/CELL]==WL_DETECT && emap[y/CELL][x/CELL]<8)
set_emap(x/CELL, y/CELL);
if (parts[i].flags&FLAG_SKIPMOVE)
return false;
//adding to velocity from the particle's velocity
air->vx[y/CELL][x/CELL] = air->vx[y/CELL][x/CELL]*elements[t].AirLoss + elements[t].AirDrag*parts[i].vx;
air->vy[y/CELL][x/CELL] = air->vy[y/CELL][x/CELL]*elements[t].AirLoss + elements[t].AirDrag*parts[i].vy;
if (elements[t].HotAir)
{
if (t == PT_GAS || t == PT_NBLE)
{
if (air->pv[y/CELL][x/CELL] < 3.5f)
air->pv[y/CELL][x/CELL] += elements[t].HotAir*(3.5f - air->pv[y/CELL][x/CELL]);
if (y+CELL < YRES && air->pv[y/CELL+1][x/CELL] < 3.5f)
air->pv[y/CELL+1][x/CELL] += elements[t].HotAir*(3.5f - air->pv[y/CELL+1][x/CELL]);
if (x+CELL < XRES)
{
if (air->pv[y/CELL][x/CELL+1] < 3.5f)
air->pv[y/CELL][x/CELL+1] += elements[t].HotAir*(3.5f - air->pv[y/CELL][x/CELL+1]);
if (y+CELL<YRES && air->pv[y/CELL+1][x/CELL+1] < 3.5f)
air->pv[y/CELL+1][x/CELL+1] += elements[t].HotAir*(3.5f - air->pv[y/CELL+1][x/CELL+1]);
}
}
//add the hotair variable to the pressure map, like black hole, or white hole.
else
{
float value = elements[t].HotAir;
value = restrict_flt(value, -256.0f, 256.0f);
air->pv[y/CELL][x/CELL] += value;
if (y+CELL < YRES)
air->pv[y/CELL+1][x/CELL] += value;
if (x+CELL < XRES)
{
air->pv[y/CELL][x/CELL+1] += value;
if (y+CELL < YRES)
air->pv[y/CELL+1][x/CELL+1] += value;
}
}
}
if (elements[t].Gravity || !(elements[t].Properties & TYPE_SOLID))
{
//Gravity mode by Moach
switch (gravityMode)
{
default:
case 0:
pGravX = 0.0f;
pGravY = elements[t].Gravity;
break;
case 1:
pGravX = pGravY = 0.0f;
break;
case 2:
pGravD = 0.01f - hypotf(((float)x - XCNTR), ((float)y - YCNTR));
pGravX = elements[t].Gravity * ((float)(x - XCNTR) / pGravD);
pGravY = elements[t].Gravity * ((float)(y - YCNTR) / pGravD);
}
//Get some gravity from the gravity map
if (t == PT_ANAR)
{
pGravX -= gravx[(y/CELL)*(XRES/CELL)+(x/CELL)];
pGravY -= gravy[(y/CELL)*(XRES/CELL)+(x/CELL)];
}
else if (t != PT_STKM && t != PT_STKM2 && t != PT_FIGH && !(elements[t].Properties & TYPE_SOLID))
{
pGravX += gravx[(y/CELL)*(XRES/CELL)+(x/CELL)];
pGravY += gravy[(y/CELL)*(XRES/CELL)+(x/CELL)];
}
}
else
pGravX = pGravY = 0;
//velocity updates for the particle
parts[i].vx *= elements[t].Loss;
parts[i].vy *= elements[t].Loss;
//particle gets velocity from the vx and vy maps
parts[i].vx += elements[t].Advection*air->vx[y/CELL][x/CELL] + pGravX;
parts[i].vy += elements[t].Advection*air->vy[y/CELL][x/CELL] + pGravY;
if (elements[t].Diffusion)//the random diffusion that gases have
{
if (realistic)
{
//The magic number controls diffusion speed
parts[i].vx += 0.05f*sqrtf(parts[i].temp)*elements[t].Diffusion*(rand()/(0.5f*RAND_MAX)-1.0f);
parts[i].vy += 0.05f*sqrtf(parts[i].temp)*elements[t].Diffusion*(rand()/(0.5f*RAND_MAX)-1.0f);
}
else
{
parts[i].vx += elements[t].Diffusion*(rand()/(0.5f*RAND_MAX)-1.0f);
parts[i].vy += elements[t].Diffusion*(rand()/(0.5f*RAND_MAX)-1.0f);
}
}
//surround_space stores the number of empty spaces around a particle, nt stores the number of empty spaces + the number of particles of a different type
int surround_space = 0, surround_particle = 0, nt = 0;
//surround stores the 8 particle types around the current one, used in heat transfer
int surround[8];
for (int nx = -1; nx <= 1; nx++)
for (int ny = -1; ny <= 1; ny++)
{
if (nx || ny)
{
int r;
if (!OutOfBounds(x+nx, y+ny))
{
surround[surround_particle] = r = pmap[y+ny][x+nx];
surround_particle++;
//there is empty space
if (!(r&0xFF))
surround_space++;
//there is nothing or a different particle
if ((r&0xFF) != t)
nt++;
}
else
{
surround[surround_particle] = 0;
surround_particle++;
surround_space++;
nt++;
}
}
}
if (!legacy_enable)
{
if (TransferHeat(i, t, surround))
{
transitionOccurred = true;
t = parts[i].type;
}
if (!t)
return true;
}
//spark updates from walls
if ((elements[t].Properties&PROP_CONDUCTS) || t == PT_SPRK)
{
int nx = x%CELL;
if (nx == 0)
nx = x/CELL - 1;
else if (nx == CELL-1)
nx = x/CELL + 1;
else
nx = x/CELL;
int ny = y%CELL;
if (ny == 0)
ny = y/CELL - 1;
else if (ny == CELL-1)
ny = y/CELL + 1;
else
ny = y/CELL;
if (nx >= 0 && ny >= 0 && nx < XRES/CELL && ny < YRES/CELL)
{
if (t != PT_SPRK)
{
if (emap[ny][nx] == 12 && !parts[i].life)
{
spark_conductive(i, x, y);
parts[i].life = 4;
t = PT_SPRK;
}
}
else if (bmap[ny][nx] == WL_DETECT || bmap[ny][nx] == WL_EWALL || bmap[ny][nx] == WL_ALLOWLIQUID || bmap[ny][nx] == WL_WALLELEC || bmap[ny][nx] == WL_ALLOWALLELEC || bmap[ny][nx] == WL_EHOLE)
set_emap(nx, ny);
}
}
//the basic explosion, from the .explosive variable
if (!(elements[t].Properties&PROP_INDESTRUCTIBLE) && (elements[t].Explosive&2) && air->pv[y/CELL][x/CELL] > 2.5f)
{
parts[i].life = rand()%80 + 180;
parts[i].temp = restrict_flt(elements[PT_FIRE].DefaultProperties.temp + (elements[t].Flammable/2), MIN_TEMP, MAX_TEMP);
t = PT_FIRE;
part_change_type(i, x, y, t);
air->pv[y/CELL][x/CELL] += 0.25f * CFDS;
}
if (!(elements[t].Properties&PROP_INDESTRUCTIBLE) && (elements[t].HighPressureTransitionThreshold != -1 || elements[t].HighPressureTransitionElement != -1))
{
if (CheckPressureTransitions(i, t))
{
transitionOccurred = true;
t = parts[i].type;
}
if (!t)
return true;
}
//call the particle update function, if there is one
#ifdef LUACONSOLE
if (lua_el_mode[parts[i].type] == 3)
{
if (luacon_part_update(t, i, x, y, surround_space, nt) || t != (unsigned int)parts[i].type)
return true;
// Need to update variables, in case they've been changed by Lua
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
if (lua_el_mode[t] != 2)
{
#endif
if (elements[t].Properties&PROP_POWERED)
{
if (update_POWERED(this, i, x, y, surround_space, nt))
return true;
}
if (elements[t].Properties&PROP_CLONE)
{
if (elements[t].Properties&PROP_POWERED)
PCLN_update(this, i, x, y, surround_space, nt);
else
CLNE_update(this, i, x, y, surround_space, nt);
}
else if (elements[t].Properties&PROP_BREAKABLECLONE)
{
if (elements[t].Properties&PROP_POWERED)
{
if (PBCN_update(this, i, x, y, surround_space, nt))
return true;
}
else
{
if (BCLN_update(this, i, x, y, surround_space, nt))
return true;
}
}
if (elements[t].Update)
{
if ((*(elements[t].Update))(this, i, x, y, surround_space, nt))
return true;
else if (t == PT_WARP)
{
// Warp does some movement in its update func, update variables to avoid incorrect data in pmap
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
}
#ifdef LUACONSOLE
}
if (lua_el_mode[parts[i].type] && lua_el_mode[parts[i].type] != 3)
{
if (luacon_part_update(t, i, x, y, surround_space, nt) || t != (unsigned int)parts[i].type)
return true;
// Need to update variables, in case they've been changed by Lua
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
#endif
if (legacy_enable)//if heat sim is off
update_legacy_all(this, i, x, y,surround_space, nt);
//if its dead, skip to next particle
if (parts[i].type == PT_NONE)
return true;
if (parts[i].flags&FLAG_EXPLODE)
{
if (!(rand()%10))
{
parts[i].flags &= ~FLAG_EXPLODE;
air->pv[y/CELL][x/CELL] += 5.0f;
if(!(rand()%3))
{
if(!(rand()%2))
{
part_create(i, x, y, PT_BOMB);
parts[i].temp = MAX_TEMP;
}
else
{
part_create(i, x, y, PT_PLSM);
parts[i].temp = MAX_TEMP;
}
}
else
{
part_create(i, x, y, PT_EMBR);
parts[i].temp = MAX_TEMP;
parts[i].vx = rand()%20-10.0f;
parts[i].vy = rand()%20-10.0f;
}
return true;
}
}
if (transitionOccurred)
return false;
if (!parts[i].vx && !parts[i].vy)//if its not moving, skip to next particle, movement code is next
return false;
float mv = std::max(fabsf(parts[i].vx), fabsf(parts[i].vy));
int fin_x, fin_y, clear_x, clear_y;
float fin_xf, fin_yf, clear_xf, clear_yf;
if (mv < ISTP)
{
clear_x = x;