-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogicgates.c
4193 lines (4136 loc) · 195 KB
/
logicgates.c
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
/* select OS */
// #define OS_WINDOWS
// #define OS_LINUX
#include "include/ribbon.h"
#include "include/popup.h"
#ifdef OS_WINDOWS
#include "include/win32tools.h"
#endif
#ifdef OS_LINUX
#include "include/zenityFileDialog.h"
#endif
#include <time.h>
#define STB_IMAGE_IMPLEMENTATION
#include "include/stb_image.h" // THANK YOU https://github.com/nothings/stb
GLFWwindow* window; // global window
enum texture {
TEXTURE_POWER = 1,
TEXTURE_NOT = 2,
TEXTURE_AND = 3,
TEXTURE_OR = 4,
TEXTURE_XOR = 5,
TEXTURE_NOR = 6,
TEXTURE_NAND = 7,
TEXTURE_BUFFER = 8
};
typedef struct { // all logicgates variables (shared state) are defined here
double globalsize; // size multiplier - bigger means zoomed in
double themeColors[55]; // rgb colour array, there are 9 colours, accross 2 themes each with 3 components for rgb
char theme; // this is the index to use in themeColors when determining what colour to render an object in
char sidebar; // whether sidebar is shown, 0 - hidden, 1 - shown
char selecting; // i dont know
char indicators; // idk
char mouseType; // mouseType?
char wireMode; // there are three wireModes, 0 is classic, 1 is angular (new style), and 2 is no wire render
char debugMode; // debug mode is 1 when the editor is in debug step mode. In this mode, CTRL + Space steps a single tick and CTRL + Scroll will also step (allowing for many steps to be done quickly)
char debugTick;
char flashTicks; // when turning on debug mode, there's a white flash over the screen
char showComponentIDOnHover; // if this is 1, hovering over a component shows you it's ID
char gridMode; // if this is 1, components snap to grid when placing them
char textureMode; // if this is 1, textures are used for component rendering
double snapRad; // how many pixels to align grid to
double scrollSpeed; // how fast the scroll zooms in, I think it's a 1.15x
double arrowScrollSpeed; // how fast the arrow keys zoom, 1.001x
double rotateSpeed; // how fast the arrow keys rotate components
int rotateCooldown; // what?
double mx; // mouseX bounded by window
double my; // mouseY bounded by window
double mw; // mouseWheel
double boundXmin; // bound for mx
double boundYmin; // bound for my
double boundXmax; // bound for mx
double boundYmax; // bound for my
double scaling; // idk
double bezierPrez; /* how many segments are drawn in the bezier curves used to construct gates with curves,
this should scale with the zoom level but actually i don't think it does that. I should add that */
char *holding; // oh god
double holdingAng; // idk
double wxOffE; // some offset var idk
double wyOffE; // offset for y
double screenX; // no idea what the screen variables do
double screenY;
double sxmax;
double sxmin;
double symax;
double symin;
int hlgcomp; // bad design decisions
int hglmove; // bad naming scheme
double tempX; // wow tempX that's just great
double tempY; // these were made at a time when this project was small
double offX; // another offset, but older
double offY;
double focalX; // these have to do with dragging the screen around
double focalY; // they keep track of the mouse position
double focalCSX;
double focalCSY;
double selectX; // i believe these are the bounds of the selection box
double selectX2; // these are likely absolute coordinates, but i dont know
double selectY;
double selectY2;
list_t *compSlots; // a lookup table for which components have two inputs vs one
char movingItems; // to keep track of movement of components so it can be detected and undone
char wireHold; // whether you are toggled for wiring (hold space or click the wire symbol)
char gotoMainLoop; // exiting out of popup prompt
char saved; // is the current circuit saved
int wiringStart; // the component ID of the start of a (under construction) wire
int wiringEnd; // the component ID of the end of a wire at the moment it is constructed
/* these 6 lists make up the data of the circuit */
list_t *components; // list of components (1 item for each component, a string with "POWER", "AND", etc), a component's "ID" or "Handle" is that component's index in this list
list_t *groups; // list of group data (1 element per component, integer represents group ID, -1 is no group. IDs start at 1. 0 is not used)
list_t *positions; // list of component positions (3 items for each component, doubles specifying x, y, and angle)
list_t *io; // list of binary inputs and outputs of a component (3 items for each component, 2 inputs followed by the output of the component (either a 0 or 1))
list_t *inpComp; // list of component ID inputs, 3 items per component, format: number of possible inputs (either 1 or 2), input 1 (ID, 0 or less if none), input 2 (ID, 0 or less if none)
list_t *wiring; // list of component connections (3 items per connection, it goes sender (ID), reciever (ID), powered (0 or 1))
char keys[32];
list_t *groupSelect; // list of component IDs in the hovered/selected group
list_t *deleteQueue; // when many components are deleted, they are queued here
list_t *selected; // list of selected component IDs
list_t *selectOb; // list of selected component IDs (but transferred to selected a tick later?)
list_t *copyBuffer; // for ctrl+c and ctrl+v
list_t *undoBuffer; // list of lists containing the state of the program after every undoable action
list_t *debugUndoBuffer; // like the undoBuffer but specifically updated for every tick advanced in debug mode so you can roll back time
int undoIndex; // what position in the undoBuffer are we at
int debugUndoIndex;
double sinRot;
double cosRot;
char defaultShape; // having to do with the penshape
double defaultPrez; // having to do with the circle triangle precision
double specialPrez; // having to do with special cases where more circle precision is necessary
} logicgates;
void init(logicgates *selfp) { // initialises the logicgates variabes (shared state)
logicgates self = *selfp;
self.showComponentIDOnHover = 0; // unused (for now)
self.gridMode = 0; // unfinished, should not be difficult to do just time consuming
self.snapRad = 8;
self.debugMode = 0;
self.globalsize = 1.5;
double themes[55] = {0,
/* light theme */
0, 0, 0, // component color
195, 195, 195, // selection box color
255, 0, 0, // powered color
255, 146, 146, // powered selected
230, 230, 230, // sidebar color
95, 95, 95, // selected component (sidebar) color
255, 234, 0, // receiving power color (POWER component)
255, 248, 181, // receive power color selected
255, 255, 255, // background color
/* dark theme */
0, 0, 0, // component color
40, 40, 40, // selection box color
200, 200, 200, // powered color
190, 190, 190, // powered selected
50, 50, 50, // sidebar color
40, 40, 40, // selected component (sidebar) color
19, 236, 48, // receiving power color (POWER component)
116, 255, 133, // receive power color selected
60, 60, 60 // background color
};
memcpy(self.themeColors, themes, sizeof(self.themeColors));
self.theme = 27;
if (self.theme == 27) // for testing dark mode default
ribbonDarkTheme();
popupDarkTheme();
turtleBgColor(self.themeColors[25 + self.theme], self.themeColors[26 + self.theme], self.themeColors[27 + self.theme]);
self.flashTicks = 0;
self.debugTick = 0;
self.scrollSpeed = 1.15;
self.arrowScrollSpeed = 1.001;
self.rotateSpeed = 1;
self.rotateCooldown = 1;
self.mx = 0;
self.my = 0;
self.scaling = 2;
self.sidebar = 1;
self.bezierPrez = 12;
self.holding = "a"; // in hindsight this should have been an int
self.holdingAng = 90;
self.indicators = 1;
self.mouseType = 0;
self.wxOffE = 0;
self.wyOffE = 0;
self.wireMode = 1;
self.textureMode = 0;
self.screenX = 0;
self.screenY = 0;
self.sxmax = 0;
self.sxmin = 0;
self.symax = 0;
self.symin = 0;
self.selecting = 0;
self.hlgcomp = 0;
self.hglmove = 0;
self.tempX = 0;
self.tempY = 0;
self.offX = 0;
self.offY = 0;
self.focalX = 0;
self.focalY = 0;
self.focalCSX = 0;
self.focalCSY = 0;
self.selectX = 0;
self.selectX2 = 0;
self.selectY = 0;
self.selectY2 = 0;
self.movingItems = 0;
self.wireHold = 0;
self.wiringStart = 0;
self.wiringEnd = 0;
self.gotoMainLoop = 0;
self.saved = 1;
self.compSlots = list_init();
list_append(self.compSlots, (unitype) "null", 's');
list_append(self.compSlots, (unitype) "POWER", 's');
list_append(self.compSlots, (unitype) 1, 'i');
list_append(self.compSlots, (unitype) "NOT", 's');
list_append(self.compSlots, (unitype) 1, 'i');
list_append(self.compSlots, (unitype) "AND", 's');
list_append(self.compSlots, (unitype) 2, 'i');
list_append(self.compSlots, (unitype) "OR", 's');
list_append(self.compSlots, (unitype) 2, 'i');
list_append(self.compSlots, (unitype) "XOR", 's');
list_append(self.compSlots, (unitype) 2, 'i');
list_append(self.compSlots, (unitype) "NOR", 's');
list_append(self.compSlots, (unitype) 2, 'i');
list_append(self.compSlots, (unitype) "NAND", 's');
list_append(self.compSlots, (unitype) 2, 'i');
list_append(self.compSlots, (unitype) "BUFFER", 's');
list_append(self.compSlots, (unitype) 1, 'i');
self.components = list_init();
list_append(self.components, (unitype) "null", 's'); // they start with an 'n' char or "null" string so they are 1-indexed not 0-indexed because I'm a bad coder
self.groups = list_init();
list_append(self.groups, (unitype) 'n', 'c');
self.positions = list_init();
list_append(self.positions, (unitype) 'n', 'c');
self.io = list_init();
list_append(self.io, (unitype) 'n', 'c');
self.inpComp = list_init();
list_append(self.inpComp, (unitype) 'n', 'c');
self.wiring = list_init();
list_append(self.wiring, (unitype) 'n', 'c');
for (int i = 0; i < 32; i++) {
self.keys[i] = 0;
}
self.groupSelect = list_init();
list_append(self.groupSelect, (unitype) 'n', 'c');
self.deleteQueue = list_init();
list_append(self.deleteQueue, (unitype) 'n', 'c');
self.selected = list_init();
list_append(self.selected, (unitype) "null", 's');
self.selectOb = list_init();
list_append(self.selectOb, (unitype) "null", 's');
self.copyBuffer = list_init();
list_append(self.copyBuffer, (unitype) 'n', 'c');
for (int i = 1; i < 7; i++) {
list_append(self.copyBuffer, (unitype) list_init(), 'r');
list_append(self.copyBuffer -> data[i].r, (unitype) 'n', 'c');
}
self.undoBuffer = list_init();
list_append(self.undoBuffer, (unitype) 'n', 'c');
self.debugUndoBuffer = list_init();
list_append(self.debugUndoBuffer, (unitype) 'n', 'c');
self.undoIndex = 0;
self.debugUndoIndex = 0;
self.sinRot = 0;
self.cosRot = 0;
self.defaultShape = 0; // 0 for circle (pretty), 3 for none (fastest), basically 0 is prettiest 3 is fastest, everything between is a spectrum
self.defaultPrez = 5; // normal use doesn't need super precise circles
self.specialPrez = 9; // in special cases such as the power block and ends of NOT blocks require more precise circles
*selfp = self;
}
// initialise associated textures
void textureInit(const char *filepath) {
/*
Notes:
https://stackoverflow.com/questions/75976623/how-to-use-gl-texture-2d-array-for-binding-multiple-textures-as-array
https://stackoverflow.com/questions/72648980/opengl-sampler2d-array
*/
int pathLen = strlen(filepath) + 32;
char filename[pathLen];
/* setup texture parameters */
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned int texturePower[8];
glGenTextures(8, texturePower);
for (int i = 0; i < 8; i++) {
glBindTexture(GL_TEXTURE_2D, texturePower[i]);
}
/* each of our images are 512 by 512 */
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 512, 512, 9, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
int width;
int height;
int nbChannels;
unsigned char *imgData;
/* load POWER texture */
strcpy(filename, filepath);
strcat(filename, "POWERi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
stbi_image_free(imgData);
/* load NOT texture */
strcpy(filename, filepath);
strcat(filename, "NOTi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 2, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
/* load AND texture */
strcpy(filename, filepath);
strcat(filename, "ANDi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 3, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
/* load OR texture */
strcpy(filename, filepath);
strcat(filename, "ORi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 4, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
/* load XOR texture */
strcpy(filename, filepath);
strcat(filename, "XORi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 5, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
/* load NOR texture */
strcpy(filename, filepath);
strcat(filename, "NORi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 6, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
/* load NAND texture */
strcpy(filename, filepath);
strcat(filename, "NANDi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 7, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
stbi_image_free(imgData);
/* load BUFFER texture */
strcpy(filename, filepath);
strcat(filename, "BUFFERi.png");
imgData = stbi_load(filename, &width, &height, &nbChannels, 0);
if (imgData != NULL) {
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 8, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
} else {
printf("Could not load texture: %s\n", filename);
}
stbi_image_free(imgData);
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
}
// clears the canvas
void clearAll(logicgates *selfp) {
logicgates self = *selfp;
list_clear(self.components);
list_append(self.components, (unitype) "null", 's');
list_clear(self.groups);
list_append(self.groups, (unitype) 'n', 'c');
list_clear(self.positions);
list_append(self.positions, (unitype) 'n', 'c');
list_clear(self.io);
list_append(self.io, (unitype) 'n', 'c');
list_clear(self.inpComp);
list_append(self.inpComp, (unitype) 'n', 'c');
list_clear(self.wiring);
list_append(self.wiring, (unitype) 'n', 'c');
self.saved = 1;
*selfp = self;
}
// imports a file
void import(logicgates *selfp, const char *filename) {
logicgates self = *selfp;
printf("Attempting to load %s\n", filename);
FILE *file = fopen(filename, "r");
char str1[20] = "null";
double doub1;
int int1;
int end = 0;
int num = 0;
int oldCompLen = self.components -> length - 1;
while (end != EOF) {
end = fscanf(file, "%s", str1);
if (str1[0] == '-') {break;}
if (str1[0] == '0') {break;}
if (str1[0] == '1') {break;}
if (str1[0] == '2') {break;}
if (str1[0] == '3') {break;}
if (str1[0] == '4') {break;}
if (str1[0] == '5') {break;}
if (str1[0] == '6') {break;}
if (str1[0] == '7') {break;}
if (str1[0] == '8') {break;}
if (str1[0] == '9') {break;}
num += 1;
}
if (end == EOF) {
printf("%s Bad Read\n", filename);
fclose(file);
} else {
rewind(file);
for (int i = 0; i < num; i++) {
end = fscanf(file, "%s", str1);
if (end == EOF) {
printf("file corrupted at word %d\n", i + 1);
return;
}
// parse string (component from group)
int j = 0;
char str2[12] = "null";
while (str1[j] != '\0') {
if (str1[j] > 47 && str1[j] < 58) {
strcpy(str2, &str1[j]);
str1[j] = '\0';
break;
}
j++;
}
list_append(self.components, (unitype) str1, 's');
if (strcmp(str2, "null") == 0) {
list_append(self.groups, (unitype) -1, 'i');
} else {
list_append(self.groups, (unitype) atoi(str2), 'i');
}
}
for (int i = 0; i < num * 3; i++) {
end = fscanf(file, "%lf", &doub1);
if (end == EOF) {
printf("file corrupted at word %d\n", i + num + 1);
return;
}
list_append(self.positions, (unitype) doub1, 'd');
}
for (int i = 0; i < num * 3; i++) {
end = fscanf(file, "%d", &int1);
if (end == EOF) {
printf("file corrupted at word %d\n", i + num * 4 + 1);
return;
}
list_append(self.io, (unitype) int1, 'i');
}
for (int i = 0; i < num * 3; i += 3) {
end = fscanf(file, "%d", &int1);
if (end == EOF) {
printf("file corrupted at word %d\n", i + num * 7 + 1);
return;
}
list_append(self.inpComp, (unitype) int1, 'i');
for (int j = 0; j < 2; j++) {
end = fscanf(file, "%d", &int1);
if (end == EOF) {
printf("file corrupted at word %d\n", i + j + num * 10 + 1);
return;
}
if (int1 == 0) {
// special case: 0 means no component attached
list_append(self.inpComp, (unitype) 0, 'i');
} else {
list_append(self.inpComp, (unitype) (int1 + oldCompLen), 'i');
}
}
}
int i = 0;
while (end != EOF) {
end = fscanf(file, "%d", &int1);
if (end == EOF) {
continue;
return;
}
list_append(self.wiring, (unitype) (int1 + oldCompLen), 'i');
end = fscanf(file, "%d", &int1);
if (end == EOF) {
printf("file corruptedd at word %d\n", i + 1 + num * 13 + 1);
return;
}
list_append(self.wiring, (unitype) (int1 + oldCompLen), 'i');
end = fscanf(file, "%d", &int1);
if (end == EOF) {
printf("file corrupteddd at word %d\n", i + 2 + num * 13 + 1);
return;
}
list_append(self.wiring, (unitype) int1, 'i');
i += 3;
}
self.screenX = -self.positions -> data[1].d;
self.screenY = -self.positions -> data[2].d;
printf("%s loaded successfully\n", filename);
fclose(file);
}
*selfp = self;
}
// exports working data a file
void export(logicgates *selfp, const char *filename) {
logicgates self = *selfp;
FILE *file = fopen(filename, "w+");
for (int i = 1; i < self.components -> length; i++) {
fprintf(file, "%s", self.components -> data[i].s);
if (self.groups -> data[i].i == -1) {
fprintf(file, " ");
} else {
fprintf(file, "%d ", self.groups -> data[i].i);
}
}
for (int i = 1; i < self.positions -> length; i++) {
fprintf(file, "%.0lf ", self.positions -> data[i].d);
}
for (int i = 1; i < self.io -> length; i++) {
fprintf(file, "%d ", self.io -> data[i].i);
}
for (int i = 1; i < self.inpComp -> length; i++) {
fprintf(file, "%d ", self.inpComp -> data[i].i);
}
for (int i = 1; i < self.wiring -> length; i++) {
fprintf(file, "%d ", self.wiring -> data[i].i);
}
self.saved = 1;
printf("Successfully saved to %s\n", filename);
fclose(file);
*selfp = self;
}
// draws a POWER component
void POWER(logicgates *selfp, double x, double y, double size, double rot, char state, char select) {
logicgates self = *selfp;
if (self.textureMode == 0) {
// rot /= 57.2958; // convert to radians
turtleGoto(x, y);
turtlePenShape("circle");
turtlePenPrez(self.specialPrez);
turtlePenSize(size * 12.5 * self.scaling);
turtlePenDown();
turtlePenUp();
if (state == 2) {
turtlePenSize(size * 10 * self.scaling);
if (select == 1) {
turtlePenColor(self.themeColors[22 + self.theme], self.themeColors[23 + self.theme], self.themeColors[24 + self.theme]);
} else {
turtlePenColor(self.themeColors[19 + self.theme], self.themeColors[20 + self.theme], self.themeColors[21 + self.theme]);
}
turtlePenDown();
turtlePenUp();
turtlePenColor(self.themeColors[1 + self.theme], self.themeColors[2 + self.theme], self.themeColors[3 + self.theme]);
}
if (state == 1) {
turtlePenSize(size * 10 * self.scaling);
if (select == 1) {
turtlePenColor(self.themeColors[10 + self.theme], self.themeColors[11 + self.theme], self.themeColors[12 + self.theme]);
} else {
turtlePenColor(self.themeColors[7 + self.theme], self.themeColors[8 + self.theme], self.themeColors[9 + self.theme]);
}
turtlePenDown();
turtlePenUp();
turtlePenColor(self.themeColors[1 + self.theme], self.themeColors[2 + self.theme], self.themeColors[3 + self.theme]);
}
turtle.penshape = self.defaultShape;
turtlePenPrez(self.defaultPrez);
} else {
const double innerRadius = 5.9;
const double outerRadius = 7.2;
turtleTexture(TEXTURE_POWER, x - size * outerRadius * self.scaling, y - size * outerRadius * self.scaling, x + size * outerRadius * self.scaling, y + size * outerRadius * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
if (state == 2) {
if (select == 1) {
turtleTexture(TEXTURE_POWER, x - size * innerRadius * self.scaling, y - size * innerRadius * self.scaling, x + size * innerRadius * self.scaling, y + size * innerRadius * self.scaling, rot, self.themeColors[22 + self.theme], self.themeColors[23 + self.theme], self.themeColors[24 + self.theme]);
} else {
turtleTexture(TEXTURE_POWER, x - size * innerRadius * self.scaling, y - size * innerRadius * self.scaling, x + size * innerRadius * self.scaling, y + size * innerRadius * self.scaling, rot, self.themeColors[19 + self.theme], 236.0, self.themeColors[21 + self.theme]);
}
}
if (state == 1) {
if (select == 1) {
turtleTexture(TEXTURE_POWER, x - size * innerRadius * self.scaling, y - size * innerRadius * self.scaling, x + size * innerRadius * self.scaling, y + size * innerRadius * self.scaling, rot, self.themeColors[10 + self.theme], self.themeColors[11 + self.theme], self.themeColors[12 + self.theme]);
} else {
turtleTexture(TEXTURE_POWER, x - size * innerRadius * self.scaling, y - size * innerRadius * self.scaling, x + size * innerRadius * self.scaling, y + size * innerRadius * self.scaling, rot, self.themeColors[7 + self.theme], self.themeColors[8 + self.theme], self.themeColors[9 + self.theme]);
}
}
}
}
// draws a NOT component
void NOT(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
if (self.textureMode == 0) {
rot /= 57.2958; // convert to radians
double sinRot = sin(rot);
double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
turtleGoto(x + (-11 * size * sinRot) - (11 * size * cosRot), y + (-11 * size * cosRot) + (11 * size * sinRot));
turtlePenDown();
turtleGoto(x + (7 * size * sinRot), y + (7 * size * cosRot));
turtleGoto(x + (-11 * size * sinRot) - (-11 * size * cosRot), y + (-11 * size * cosRot) + (-11 * size * sinRot));
turtleGoto(x + (-11 * size * sinRot) - (11 * size * cosRot), y + (-11 * size * cosRot) + (11 * size * sinRot));
turtlePenUp();
turtleGoto(x + (10 * size * sinRot), y + (10 * size * cosRot));
turtlePenShape("circle");
turtlePenPrez(self.specialPrez);
turtlePenSize(size * 3.5 * self.scaling);
turtlePenDown();
turtlePenUp();
turtlePenSize(size * 1.5 * self.scaling);
turtlePenColor(self.themeColors[25 + self.theme], self.themeColors[26 + self.theme], self.themeColors[27 + self.theme]);
turtlePenDown();
turtlePenUp();
turtlePenColor(self.themeColors[1 + self.theme], self.themeColors[2 + self.theme], self.themeColors[3 + self.theme]);
turtle.penshape = self.defaultShape;
turtlePenPrez(self.defaultPrez);
} else {
const double textureScale = 7.1;
turtleTexture(TEXTURE_NOT, x - size * textureScale * self.scaling, y - size * textureScale * self.scaling, x + size * textureScale * self.scaling, y + size * textureScale * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
}
}
// draws an AND component
void AND(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
if (self.textureMode == 0) {
rot /= 57.2958; // convert to radians
double sinRot = sin(rot);
double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
turtleGoto(x + (-12 * size * sinRot) - (-9 * size * cosRot), y + (-12 * size * cosRot) + (-9 * size * sinRot));
turtlePenDown();
turtleGoto(x + (4 * size * sinRot) - (-9 * size * cosRot), y + (4 * size * cosRot) + (-9 * size * sinRot));
double i = 180;
for (int j = 0; j < self.bezierPrez + 1; j++) {
double k = i / 57.2958;
turtleGoto(x + ((4 * size + sin(k) * 8 * size) * sinRot) - (cos(k) * 9 * size * cosRot), y + ((4 * size + sin(k) * 8 * size) * cosRot) + (cos(k) * 9 * size * sinRot));
i -= (180 / self.bezierPrez);
}
turtleGoto(x + (-12 * size * sinRot) - (9 * size * cosRot), y + (-12 * size * cosRot) + (9 * size * sinRot));
turtleGoto(x + (-12 * size * sinRot) - (-9 * size * cosRot), y + (-12 * size * cosRot) + (-9 * size * sinRot));
turtlePenUp();
} else {
const double textureScale = 7.5;
turtleTexture(TEXTURE_AND, x - size * textureScale * self.scaling, y - size * textureScale * self.scaling, x + size * textureScale * self.scaling, y + size * textureScale * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
}
}
// draws an OR component
void OR(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
if (self.textureMode == 0) {
rot /= 57.2958; // convert to radians
double sinRot = sin(rot);
double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
turtleGoto(x + (-11 * size * sinRot) - (9 * size * cosRot), y + (-11 * size * cosRot) + (9 * size * sinRot));
turtlePenDown();
double k;
double i = 180;
for (int j = 0; j < self.bezierPrez + 1; j++) {
k = i / 57.2958;
double tempX = x + ((-11 * size + sin(k) * 5 * size) * sinRot) - (cos(k) * -9 * size * cosRot);
double tempY = y + ((-11 * size + sin(k) * 5 * size) * cosRot) + (cos(k) * -9 * size * sinRot);
turtleGoto(tempX, tempY);
i -= (180 / self.bezierPrez);
}
i += (180 / self.bezierPrez);
for (int j = 0; j < (self.bezierPrez + 1) / 1.5; j++) {
k = i / 57.2958;
turtleGoto(x + ((-11 * size + sin(k) * 25 * size) * sinRot) - ((9 * size - cos(k) * 18 * size) * cosRot), y + ((-11 * size + sin(k) * 25 * size) * cosRot) + ((9 * size - cos(k) * 18 * size) * sinRot));
i += (90 / self.bezierPrez);
}
turtleGoto(x + (10.3 * size * sinRot), y + (10.3 * size * cosRot));
turtlePenUp();
turtleGoto(x + (-11 * size * sinRot) - (9 * size * cosRot), y + (-11 * size * cosRot) + (9 * size * sinRot));
turtlePenDown();
i = 0;
for (int j = 0; j < (self.bezierPrez + 1) / 1.5; j++) {
k = i / 57.2958;
turtleGoto(x + ((-11 * size + sin(k) * 25 * size) * sinRot) - ((-9 * size + cos(k) * 18 * size) * cosRot), y + ((-11 * size + sin(k) * 25 * size) * cosRot) + ((-9 * size + cos(k) * 18 * size) * sinRot));
i += (90 / self.bezierPrez);
}
turtleGoto(x + (10.3 * size * sinRot), y + (10.3 * size * cosRot));
turtlePenUp();
} else {
const double textureScale = 6.9;
turtleTexture(TEXTURE_OR, x - size * textureScale * self.scaling, y - size * textureScale * self.scaling, x + size * textureScale * self.scaling, y + size * textureScale * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
}
}
// draws an XOR component
void XOR(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
if (self.textureMode == 0) {
rot /= 57.2958; // convert to radians
double sinRot = sin(rot);
double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
double k;
double i = 180;
i -= 180 / self.bezierPrez;
k = i / 57.2958;
turtleGoto(x + ((-15 * size + sin(k) * 5 * size) * sinRot) - (cos(k) * -9 * size * cosRot), y + ((-15 * size + sin(k) * 5 * size) * cosRot) + (cos(k) * -9 * size * sinRot));
turtlePenDown();
for (int j = 0; j < self.bezierPrez - 1; j++) {
k = i / 57.2958;
turtleGoto(x + ((-15 * size + sin(k) * 5 * size) * sinRot) - (cos(k) * -9 * size * cosRot), y + ((-15 * size + sin(k) * 5 * size) * cosRot) + (cos(k) * -9 * size * sinRot));
i -= 180 / self.bezierPrez;
}
turtlePenUp();
i = 180;
i -= 180 / self.bezierPrez;
k = i / 57.2958;
turtleGoto(x + ((-11 * size + sin(k) * 5 * size) * sinRot) - (cos(k) * -9 * size * cosRot), y + ((-11 * size + sin(k) * 5 * size) * cosRot) + (cos(k) * -9 * size * sinRot));
turtlePenDown();
for (int j = 0; j < self.bezierPrez - 1; j++) {
k = i / 57.2958;
turtleGoto(x + ((-11 * size + sin(k) * 5 * size) * sinRot) - (cos(k) * -9 * size * cosRot), y + ((-11 * size + sin(k) * 5 * size) * cosRot) + (cos(k) * -9 * size * sinRot));
i -= (180 / self.bezierPrez);
}
i += (180 / self.bezierPrez);
for (int j = 0; j < (self.bezierPrez - 2) / 1.5; j++) {
k = i / 57.2958;
turtleGoto(x + ((-11 * size + sin(k) * 25 * size) * sinRot) - ((9 * size - cos(k) * 18 * size) * cosRot), y + ((-11 * size + sin(k) * 25 * size) * cosRot) + ((9 * size - cos(k) * 18 * size) * sinRot));
i += (90 / self.bezierPrez);
}
turtleGoto(x + (10.3 * size * sinRot), y + (10.3 * size * cosRot));
turtlePenUp();
i = 180;
i -= 180 / self.bezierPrez;
k = i / 57.2958;
turtleGoto(x + ((-11 * size + sin(k) * 5 * size) * sinRot) - (cos(k) * -9 * size * cosRot), y + ((-11 * size + sin(k) * 5 * size) * cosRot) + (cos(k) * -9 * size * sinRot));
turtlePenDown();
i = 0;
i += 180 / self.bezierPrez;
for (int j = 0; j < (self.bezierPrez - 2) / 1.5; j++) {
k = i / 57.2958;
turtleGoto(x + ((-11 * size + sin(k) * 25 * size) * sinRot) - ((-9 * size + cos(k) * 18 * size) * cosRot), y + ((-11 * size + sin(k) * 25 * size) * cosRot) + ((-9 * size + cos(k) * 18 * size) * sinRot));
i += (90 / self.bezierPrez);
}
turtleGoto(x + (10.3 * size * sinRot), y + (10.3 * size * cosRot));
turtlePenUp();
} else {
const double textureScale = 7.5;
turtleTexture(TEXTURE_XOR, x - size * textureScale * self.scaling, y - size * textureScale * self.scaling, x + size * textureScale * self.scaling, y + size * textureScale * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
}
}
// draws a NOR component
void NOR(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
if (self.textureMode == 0) {
rot /= 57.2958; // convert to radians
double sinRot = sin(rot);
double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
turtleGoto(x + (-13 * size * sinRot) - (9 * size * cosRot), y + (-13 * size * cosRot) + (9 * size * sinRot));
turtlePenDown();
double k;
double i = 180;
for (int j = 0; j < self.bezierPrez + 1; j++) {
k = i / 57.2958;
turtleGoto(x + ((-13 * size + sin(k) * 5 * size) * sinRot) - (cos(k) * -9 * size * cosRot), y + ((-13 * size + sin(k) * 5 * size) * cosRot) + (cos(k) * -9 * size * sinRot));
i -= (180 / self.bezierPrez);
}
i += (180 / self.bezierPrez);
for (int j = 0; j < (self.bezierPrez + 1) / 1.5; j++) {
k = i / 57.2958;
turtleGoto(x + ((-13 * size + sin(k) * 25 * size) * sinRot) - ((9 * size - cos(k) * 18 * size) * cosRot), y + ((-13 * size + sin(k) * 25 * size) * cosRot) + ((9 * size - cos(k) * 18 * size) * sinRot));
i += (90 / self.bezierPrez);
}
turtleGoto(x + (8.3 * size * sinRot), y + (8.3 * size * cosRot));
turtlePenUp();
turtleGoto(x + (-13 * size * sinRot) - (9 * size * cosRot), y + (-13 * size * cosRot) + (9 * size * sinRot));
turtlePenDown();
i = 0;
for (int j = 0; j < (self.bezierPrez + 1) / 1.5; j++) {
k = i / 57.2958;
turtleGoto(x + ((-13 * size + sin(k) * 25 * size) * sinRot) - ((-9 * size + cos(k) * 18 * size) * cosRot), y + ((-13 * size + sin(k) * 25 * size) * cosRot) + ((-9 * size + cos(k) * 18 * size) * sinRot));
i += (90 / self.bezierPrez);
}
turtleGoto(x + (8.3 * size * sinRot), y + (8.3 * size * cosRot));
turtlePenUp();
turtleGoto(x + (11.5 * size * sinRot), y + (11.5 * size * cosRot));
turtlePenShape("circle");
turtlePenPrez(self.specialPrez);
turtlePenSize(size * 3.5 * self.scaling);
turtlePenDown();
turtlePenUp();
turtlePenSize(size * 1.5 * self.scaling);
turtlePenColor(self.themeColors[25 + self.theme], self.themeColors[26 + self.theme], self.themeColors[27 + self.theme]);
turtlePenDown();
turtlePenUp();
turtlePenColor(self.themeColors[1 + self.theme], self.themeColors[2 + self.theme], self.themeColors[3 + self.theme]);
turtle.penshape = self.defaultShape;
turtlePenPrez(self.defaultPrez);
} else {
const double textureScale = 8;
turtleTexture(TEXTURE_NOR, x - size * textureScale * self.scaling, y - size * textureScale * self.scaling, x + size * textureScale * self.scaling, y + size * textureScale * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
}
}
// draws a NAND component
void NAND(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
if (self.textureMode == 0) {
rot /= 57.2958; // convert to radians
double sinRot = sin(rot);
double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
turtleGoto(x + (-12 * size * sinRot) - (-9 * size * cosRot), y + (-12 * size * cosRot) + (-9 * size * sinRot));
turtlePenDown();
turtleGoto(x + (4 * size * sinRot) - (-9 * size * cosRot), y + (4 * size * cosRot) + (-9 * size * sinRot));
double k;
double i = 180;
for (int j = 0; j < self.bezierPrez + 1; j++) {
k = i / 57.2958;
turtleGoto(x + ((4 * size + sin(k) * 8 * size) * sinRot) - (cos(k) * 9 * size * cosRot), y + ((4 * size + sin(k) * 8 * size) * cosRot) + (cos(k) * 9 * size * sinRot));
i -= (180 / self.bezierPrez);
}
turtleGoto(x + (-12 * size * sinRot) - (9 * size * cosRot), y + (-12 * size * cosRot) + (9 * size * sinRot));
turtleGoto(x + (-12 * size * sinRot) - (-9 * size * cosRot), y + (-12 * size * cosRot) + (-9 * size * sinRot));
turtlePenUp();
turtleGoto(x + (15 * size * sinRot), y + (15 * size * cosRot));
turtlePenShape("circle");
turtlePenPrez(self.specialPrez);
turtlePenSize(size * 3.5 * self.scaling);
turtlePenDown();
turtlePenUp();
turtlePenSize(size * 1.5 * self.scaling);
turtlePenColor(self.themeColors[25 + self.theme], self.themeColors[26 + self.theme], self.themeColors[27 + self.theme]);
turtlePenDown();
turtlePenUp();
turtlePenColor(self.themeColors[1 + self.theme], self.themeColors[2 + self.theme], self.themeColors[3 + self.theme]);
turtle.penshape = self.defaultShape;
turtlePenPrez(self.defaultPrez);
} else {
const double textureScale = 8.2;
turtleTexture(TEXTURE_NAND, x - size * textureScale * self.scaling, y - size * textureScale * self.scaling, x + size * textureScale * self.scaling, y + size * textureScale * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
}
}
// draws a BUFFER component
void BUFFER(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
if (self.textureMode == 0) {
rot /= 57.2958; // convert to radians
double sinRot = sin(rot);
double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
turtleGoto(x + (-8 * size * sinRot) - (11 * size * cosRot), y + (-8 * size * cosRot) + (11 * size * sinRot));
turtlePenDown();
turtleGoto(x + (10 * size * sinRot), y + (10 * size * cosRot));
turtleGoto(x + (-8 * size * sinRot) - (-11 * size * cosRot), y + (-8 * size * cosRot) + (-11 * size * sinRot));
turtleGoto(x + (-8 * size * sinRot) - (11 * size * cosRot), y + (-8 * size * cosRot) + (11 * size * sinRot));
turtlePenUp();
} else {
const double textureScale = 6.3;
turtleTexture(TEXTURE_BUFFER, x - size * textureScale * self.scaling, y - size * textureScale * self.scaling, x + size * textureScale * self.scaling, y + size * textureScale * self.scaling, rot, turtle.penr * 255, turtle.peng * 255, turtle.penb * 255);
}
}
// draws the wireSymbol on the sidebar
void wireSymbol(logicgates *selfp, double x, double y, double size, double rot) {
logicgates self = *selfp;
rot /= 57.2958; // convert to radians
// double sinRot = sin(rot);
// double cosRot = cos(rot);
turtlePenSize(size * self.scaling);
turtleGoto(x + -12 * size, y + -9 * size);
turtlePenDown();
turtleGoto(x + -6 * size, y + -9 * size);
turtleGoto(x + 6 * size, y + 9 * size);
turtleGoto(x + 12 * size, y + 9 * size);
turtlePenUp();
}
void deleteComp(logicgates *selfp, int index, int replace) { // deletes a component
/* bad foresight compelled me to identify a component's ID
based on the index of it's position in the self.components array
This means that when a component is deleted all components with an ID greater than that one has
themselves reassigned a new ID. This means that every reference to that component (in the other data lists)
must be updated to the new ID.
This allows fast access to a component's data by just given its ID, because it's an array lookup
but this is hell for deleting, and if there are any references to a component via its ID anywhere in
the program it must be updated.
yknow having these "components" with associated "data" is something that perhaps, just maybe,
could've had its own type.
We unfortunately live with our decisions, somehow this seemed like the best way to do this at the time,
and to be fair it was written in scratch
The replace feature:
This is delete and replace. This only works with POWER, NOT, and BUFFER
It takes the input (if any) of the gate and connects it to all outputs while deleting the gate
*/
logicgates self = *selfp;
// identify input to deleted component
// list_print(self.inpComp);
int inputCon = -1;
if (replace && self.inpComp -> data[index * 3].i < 1) { // any component with one input
// identify input (if any)
if (self.inpComp -> data[index * 3 - 1].i > 0) {
inputCon = self.inpComp -> data[index * 3 - 1].i;
if (inputCon > index) {
inputCon--;
}
}
// for (int j = 1; j < self.wiring -> length; j += 3) {
// if (self.wiring -> data[j + 1].i == index) {
// inputCon = self.wiring -> data[j].i;
// if (inputCon > index) {
// inputCon--;
// }
// break;
// }
// }
}
// gather numInputs and numInputsHolding
char doDelete = 1;
/* set numInputs to how many actual wire inputs are connected to this component */
int numInputs = 2;
if (numInputs == 2 && self.inpComp -> data[index * 3].i < 1) {
// if it can support two but only has one
numInputs = 1;
}
if (self.inpComp -> data[index * 3 - 1].i < 1) {
// this is equivalent to 0 inputs
numInputs = 0;
}
int numInputsHolding = 0;
if (strcmp(self.holding, "a") != 0 && strcmp(self.holding, "b") != 0) {
numInputsHolding = self.compSlots -> data[list_find(self.compSlots, (unitype) self.holding, 's') + 1].i; // this is how many inputs can be taken by self.holding
if (replace && (numInputsHolding >= numInputs || numInputs == 0)) {
doDelete = 0;
inputCon = 0; // in the case where you replace a 2 input with another 2 input
// printf("replaced %s with %s\n", self.components -> data[index].s, self.holding);
// replace component with self.holding
free(self.components -> data[index].s);
self.components -> data[index].s = strdup(self.holding);
// group does not change
// position does not change
// io does not change
self.inpComp -> data[index * 3 - 2].i = numInputsHolding;
// other inpComp does not change
// wiring does not change
*selfp = self;
return;
}
}
// reform selected components
if (inputCon == -1 || numInputsHolding < numInputs) {
int len = self.selected -> length;
for (int i = 1; i < len; i++) {
if (self.selected -> data[i].i > index) {
self.selected -> data[i] = (unitype) (self.selected -> data[i].i - 1);
}
}
}
// printf("inputCon: %d\n", inputCon);
// printf("%s %d %d %d\n", self.holding, numInputs, numInputsHolding, inputCon);
int i = 1;
int k = (int) round((self.wiring -> length - 1) / 3);
for (int j = 0; j < k; j++) {
if (self.wiring -> data[i].i == index || self.wiring -> data[i + 1].i == index) {
if (inputCon == -1) {
list_delete(self.wiring, i);
list_delete(self.wiring, i);
list_delete(self.wiring, i);
} else {
if (numInputsHolding >= numInputs) {
// case: replace
// no change in wiring
i += 3;
} else {
if (self.wiring -> data[i + 1].i == index) {
// case: normal delete wire
list_delete(self.wiring, i);
list_delete(self.wiring, i);
list_delete(self.wiring, i);
} else if (self.wiring -> data[i].i == index) {
// case: replace wire
self.wiring -> data[i].i = inputCon;
if (self.wiring -> data[i + 1].i > index) {
self.wiring -> data[i + 1].i--;
}
i += 3;
}
}
}
} else {
if (inputCon == -1 || numInputsHolding < numInputs) {
// assume a component is getting deleted
if (self.wiring -> data[i].i > index) {
self.wiring -> data[i].i--;
}
if (self.wiring -> data[i + 1].i > index) {
self.wiring -> data[i + 1].i--;
}
}
i += 3;
}
}
i = 2;