-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinecraft Redstone Simulator 1.1c.cpp.c
More file actions
1655 lines (1623 loc) · 52.1 KB
/
Minecraft Redstone Simulator 1.1c.cpp.c
File metadata and controls
1655 lines (1623 loc) · 52.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
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
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>//链接参数-mwindows
#define RedstoneTick 100
enum BlockID
{
Air = 0,//空气
Redstone_Wire = 1,//红石线
Redstone_Torch = 2,//红石火把
Redstone_Block = 3,//红石块
Redstone_Lamp = 4,//红石灯
Redstone_Repeater = 5,//红石中继器
Redstone_Comparator = 6,//红石比较器
Lever = 7,//拉杆
Button = 8,//按钮
};
int numberOfBlockType = 9;
typedef struct _World//方块属性
{
BlockID id;
int direction;//方向
int onBlock;//空气、红石线、红石火把、拉杆、按钮是否位于方块上
int delay;//红石中继器的延时红石刻(1-4)
char mode;//红石比较器的模式(c比较m减法)
}World;
typedef struct _State//红石状态
{
int state;//状态
int redstoneSignal;//红石信号强度(0-15)
int redstoneTick;//元件改变状态的红石刻(0.1s)
int isLocked;//红石中继器是否被锁定
int mainInput;//红石比较器后面输入的红石信号强度
int sideInput;//红石比较器侧面输入的红石信号强度
int energyLevel;//方块充能等级(1弱充能2强充能3强弱混合充能)
int weakChargingSignal;//方块被弱充能的红石信号强度
int strongChargingSignal;//方块被强充能的红石信号强度
}State;
enum ColorElem
{
Color_BackGround = 0,//背景
Color_Border = 1,//边界线
Color_Inventory_BK = 2,//物品栏背景
Color_Inventory_MainHandBorder = 3,//物品栏主手边框
Color_InventoryCell_BK = 4,//物品栏单元格背景
Color_InventoryNumber = 5,//物品栏单元格序号
Color_InventoryNumber_Current = 6,//主手序号
Color_MouseHighlight = 7,//鼠标悬浮高亮
Color_RedstoneSignal_0 = 8,//红石信号强度0
Color_RedstoneSignal_1 = 9,//红石信号强度1
Color_RedstoneSignal_2 = 10,//红石信号强度2
Color_RedstoneSignal_3 = 11,//红石信号强度3
Color_RedstoneSignal_4 = 12,//红石信号强度4
Color_RedstoneSignal_5 = 13,//红石信号强度5
Color_RedstoneSignal_6 = 14,//红石信号强度6
Color_RedstoneSignal_7 = 15,//红石信号强度7
Color_RedstoneSignal_8 = 16,//红石信号强度8
Color_RedstoneSignal_9 = 17,//红石信号强度9
Color_RedstoneSignal_10 = 18,//红石信号强度10
Color_RedstoneSignal_11 = 19,//红石信号强度11
Color_RedstoneSignal_12 = 20,//红石信号强度12
Color_RedstoneSignal_13 = 21,//红石信号强度13
Color_RedstoneSignal_14 = 22,//红石信号强度14
Color_RedstoneSignal_15 = 23,//红石信号强度15
Color_Wood = 24,//木质材料
Color_Block = 25,//方块
Color_Bedrock = 26//基岩,红石中继器锁
};
color_t Color[] = {
EGERGB(15, 15, 15),//背景
EGERGB(80, 80, 80),//边界线
EGERGB(24, 24, 24),//物品栏背景
RED,//物品栏主手边框
EGERGB(15, 15, 15),//物品栏单元格背景
WHITE,//物品栏单元格序号
RED,//主手序号
EGEARGB(30, 255, 0, 0),//鼠标悬浮高亮
EGERGB(0x87, 0, 0),//红石信号强度0
EGERGB(0x8f, 0, 0),//红石信号强度1
EGERGB(0x97, 0, 0),//红石信号强度2
EGERGB(0x9f, 0, 0),//红石信号强度3
EGERGB(0xa7, 0, 0),//红石信号强度4
EGERGB(0xaf, 0, 0),//红石信号强度5
EGERGB(0xb7, 0, 0),//红石信号强度6
EGERGB(0xbf, 0, 0),//红石信号强度7
EGERGB(0xc7, 0, 0),//红石信号强度8
EGERGB(0xcf, 0, 0),//红石信号强度9
EGERGB(0xd7, 0, 0),//红石信号强度10
EGERGB(0xdf, 0, 0),//红石信号强度11
EGERGB(0xe7, 0, 0),//红石信号强度12
EGERGB(0xef, 0, 0),//红石信号强度13
EGERGB(0xf7, 0, 0),//红石信号强度14
EGERGB(0xff, 0, 0),//红石信号强度15
PERU,//木质材料
EGERGB(47, 178, 221),//方块
EGERGB(51, 51, 51)//基岩,红石中继器锁
};
World** world;
State** state;
State** newState;
World* inventoryWorld;
State* defaultState;
int height = 0;
int width = 0;
int blockSideLength = 0;
int heightOfInventory = 0;
int currentTick = 0;
//int cameraR = 0;
//int cameraC = 0;
int showInformation = 0;
//int showStep = 0;
void InitInventory();
void RotateInventory(int id);
void ResizeWorld(int h, int w);
int ReadWorld(const char* fileName);
void WriteWorld();
void ResizeWindow(int length);
void DrawTorch(int x0, int y0, int state);
void DrawBlockID(int x, int y, int id, State s);
void DrawBlock(int r, int c);
void DrawWorld(int mainhand);
void PutBlock(BlockID id, int r, int c);
void BreakBlock(int r, int c);
int IsConnected(int rt, int ct, int rs, int cs);
void SpreadSignalOnWire(int r, int c);
void SpreadSignalToComponent(int rt, int ct, int rs, int cs);
void SpreadEnergyToComponent(int rt, int ct, int rs, int cs);
void SpreadEnergyToBlock(int r, int c, int energyLevel, int redstoneSignal);
void UpdateWire();
void UpdateWorld();
int main(int argc, char* argv[])
{
int r, c;
int mainhand = 0;
mouse_msg mouseMsg;
key_msg keyMsg;
InitInventory();
if(argc == 2) ReadWorld(argv[1]);
else if(ReadWorld("mcrs-world.txt") == 0) ResizeWorld(18, 32);
UpdateWire();
ResizeWindow(32);
setbkcolor(Color[Color_BackGround]);
while(is_run())
{
//更新世界
UpdateWorld();
//绘制世界
DrawWorld(mainhand);
//检测输入
while(mousemsg())
{
mouseMsg = getmouse();
if(mouseMsg.is_wheel())
{
if(keystate(key_control))
{
if(mouseMsg.wheel > 0) ResizeWindow(blockSideLength+4);
else if(blockSideLength > 4) ResizeWindow(blockSideLength-4);
}
else//滚轮切物品栏
{
if(mouseMsg.wheel < 0) mainhand = (mainhand+1)%numberOfBlockType;
else
{
mainhand--;
if(mainhand < 0) mainhand = numberOfBlockType-1;
}
}
}
if(mouseMsg.is_up())
{
r = mouseMsg.y/blockSideLength;
c = mouseMsg.x/blockSideLength;
if(r >= 0 && r < height && c >= 0 && c < width)
{
if(mouseMsg.is_right())
{
if(world[r][c].id == Air)
{
PutBlock((BlockID)mainhand, r, c);
UpdateWire();
}
else if(world[r][c].id == Redstone_Repeater)
{
world[r][c].delay++;
if(world[r][c].delay > 4) world[r][c].delay = 1;
}
else if(world[r][c].id == Redstone_Comparator)
{
if(world[r][c].mode == 'c') world[r][c].mode = 'm';
else world[r][c].mode = 'c';
}
else if(world[r][c].id == Lever)
{
state[r][c].state = !(state[r][c].state);
if(state[r][c].state == 0) state[r][c].redstoneSignal = 0;
else state[r][c].redstoneSignal = 15;
}
else if(world[r][c].id == Button)
{
state[r][c].state = 1;
if(state[r][c].state == 0) state[r][c].redstoneSignal = 0;
else state[r][c].redstoneSignal = 15;
state[r][c].redstoneTick = currentTick+15;//延时
}
}
else if(mouseMsg.is_left())
{
BreakBlock(r, c);
UpdateWire();
}
}
else
{
if(mouseMsg.is_right())
{
RotateInventory(mainhand);
}
//点击切物品栏(不做)
}
}
}
while(kbmsg())
{
keyMsg = getkey();
if(keyMsg.msg == key_msg_char)
{
if(keyMsg.key >= '0' && keyMsg.key <= '9')
{
mainhand = keyMsg.key-'0'+0;
}
else if(numberOfBlockType > 10 && keyMsg.key >= 'A' && keyMsg.key <= 'Z')
{
mainhand = keyMsg.key-'A'+10;
}
if(mainhand >= numberOfBlockType || mainhand < 0) mainhand = 0;
}
if(keystate(key_f3))
{
showInformation = !showInformation;
}
/*if(keystate(key_f5))
{
showStep = !showStep;
}*/
}
delay_ms(RedstoneTick);
currentTick++;
//if(showStep == 1) getch();
}
WriteWorld();
closegraph();
return 0;
}
void InitInventory()//设置所有元件默认状态
{
int i;
inventoryWorld =(World*) calloc(numberOfBlockType, sizeof(World));
defaultState =(State*) calloc(numberOfBlockType, sizeof(State));
for(i=0; i<numberOfBlockType; i++)
{
inventoryWorld[i].id =(BlockID) i;
inventoryWorld[i].direction = 0;
inventoryWorld[i].onBlock = 0;
inventoryWorld[i].delay = 1;
inventoryWorld[i].mode = 0;
defaultState[i].state = 0;
defaultState[i].redstoneSignal = 0;
defaultState[i].redstoneTick = -1;
defaultState[i].isLocked = 0;
defaultState[i].mainInput = 0;
defaultState[i].sideInput = 0;
defaultState[i].energyLevel = 0;
defaultState[i].weakChargingSignal = 0;
defaultState[i].strongChargingSignal = 0;
if(i == Air)
{
inventoryWorld[i].onBlock = 1;//物品栏首格为方块
}
else if(i == Redstone_Wire)
{
inventoryWorld[i].delay = 0;//认为信号源延时,不认为红石线延时
}
else if(i == Redstone_Torch)
{
defaultState[i].redstoneSignal = 15;
}
else if(i == Redstone_Block)
{
defaultState[i].redstoneSignal = 15;
defaultState[i].energyLevel = 2;
defaultState[i].strongChargingSignal = 15;
}
else if(i == Redstone_Lamp);
else if(i == Redstone_Repeater);
else if(i == Redstone_Comparator)
{
inventoryWorld[i].mode = 'c';
}
else if(i == Lever);
else if(i == Button);//按钮按下时延迟1刻打开
}
}
void RotateInventory(int id)//旋转物品栏物品
{
if(id == Air);
else if(id == Redstone_Wire);
else if(id == Redstone_Torch)
{
inventoryWorld[id].direction = (inventoryWorld[id].direction+1)%5;
}
else if(id == Redstone_Block);
else if(id == Redstone_Lamp);
else if(id == Redstone_Repeater)
{
inventoryWorld[id].direction = (inventoryWorld[id].direction+1)%4;
}
else if(id == Redstone_Comparator)
{
inventoryWorld[id].direction = (inventoryWorld[id].direction+1)%4;
}
else if(id == Lever);
}
void ResizeWorld(int h, int w)//初始化或重置世界大小
{
int r, c;
if(height != 0 && width != 0)
{
for(r=0; r<height; r++)
{
free(world[r]);
free(state[r]);
}
free(world);
free(state);
}
if(h != 0 && w != 0)
{
world =(World**) calloc(h, sizeof(World*));
state =(State**) calloc(h, sizeof(State*));
for(r=0; r<h; r++)
{
world[r] =(World*) calloc(w, sizeof(World));
state[r] =(State*) calloc(w, sizeof(State));
}
}
height = h;
width = w;
}
int ReadWorld(const char* fileName)
{
FILE* file;
int r, c;
World worldTemp;
//State stateTemp;
if((file = fopen(fileName, "r")))
{
fscanf(file, "size:%d*%d\n", &r, &c);
ResizeWorld(r, c);
while(fscanf(file, "\n%d %d %d %d %d", &r, &c, &(worldTemp.id), &(worldTemp.direction), &(worldTemp.onBlock)) != EOF)
{
world[r][c].id = worldTemp.id;
world[r][c].direction = worldTemp.direction;
world[r][c].onBlock = worldTemp.onBlock;
if(worldTemp.id == Redstone_Repeater)
{
fscanf(file, " %d", &(worldTemp.delay));
world[r][c].delay = worldTemp.delay;
}
else if(worldTemp.id == Redstone_Comparator)
{
fscanf(file, " %d", &(worldTemp.delay));
if(worldTemp.delay) world[r][c].mode = 'm';
else world[r][c].mode = 'c';
}
state[r][c].redstoneTick = -1;//防止红石中继器亮起
}
fclose(file);
return 1;
}
return 0;
}
void WriteWorld()
{
FILE* file;
int r, c;
file = fopen("mcrs-world.txt", "w");
fprintf(file, "size:%d*%d\n", height, width);
for(r=0; r<height; r++)
{
for(c=0; c<width; c++)
{
if(world[r][c].id == Air && world[r][c].onBlock == 0);//跳过空气
else
{
fprintf(file, "\n%d %d %d %d %d", r, c, world[r][c].id, world[r][c].direction, world[r][c].onBlock);
if(world[r][c].id == Redstone_Repeater)
{
fprintf(file, " %d", world[r][c].delay);
}
else if(world[r][c].id == Redstone_Comparator)
{
fprintf(file, " %d", (world[r][c].mode == 'm'));
}
}
}
}
fclose(file);
}
void ResizeWindow(int length)//初始化或改变窗口大小
{
blockSideLength = length;
heightOfInventory = 2*length;
setcaption("Minecraft Redstone Simulator");
SetProcessDPIAware();//避免Windows缩放造成模糊
initgraph(width*blockSideLength, height*blockSideLength+heightOfInventory, INIT_RENDERMANUAL | INIT_NOFORCEEXIT);
setfont(blockSideLength/2, 0, "Consolas");
setbkmode(TRANSPARENT);//默认设置为无背景字体
ege_enable_aa(true);
}
void DrawTorch(int x0, int y0, int state)//绘制红石火把
{
int x = x0-blockSideLength/2;
int y = y0-blockSideLength/2;
if(state == 0)//亮起
{
setfillcolor(Color[Color_RedstoneSignal_15]);
ege_fillellipse(x+blockSideLength*3/8, y+blockSideLength*3/8, blockSideLength/4, blockSideLength/4);
setfillcolor(YELLOW);
ege_fillellipse(x+blockSideLength*7/16, y+blockSideLength*7/16, blockSideLength/8, blockSideLength/8);
}
else//熄灭
{
setfillcolor(Color[Color_RedstoneSignal_0]);
ege_fillellipse(x+blockSideLength*3/8, y+blockSideLength*3/8, blockSideLength/4, blockSideLength/4);
setfillcolor(EGERGB(49, 26, 17));
ege_fillellipse(x+blockSideLength*7/16, y+blockSideLength*7/16, blockSideLength/8, blockSideLength/8);
}
}
void DrawBlockID(int x, int y, World w, State s)//绘制元件
{
if(w.onBlock)
{
setfillcolor(Color[Color_Block]);
ege_fillrect(x, y, blockSideLength, blockSideLength);
}
//标准方块边长16
if(w.id == Air);
else if(w.id == Redstone_Wire)
{
//中点画圆
setfillcolor(Color[Color_RedstoneSignal_0+s.redstoneSignal]);
//ege_fillcircle(x+blockSideLength/2, y+blockSideLength/2, blockSideLength/4);
ege_fillellipse(x+blockSideLength*3/8, y+blockSideLength*3/8, blockSideLength/4, blockSideLength/4);
//方向,4二进制位从高到低表示上,右,下,左是否连接
if(w.direction & 8)
{
ege_fillrect(x+blockSideLength*7/16, y, blockSideLength*2/16, blockSideLength/2);
}
if(w.direction & 4)
{
ege_fillrect(x+blockSideLength/2, y+blockSideLength*7/16, blockSideLength/2, blockSideLength*2/16);
}
if(w.direction & 2)
{
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength/2, blockSideLength*2/16, blockSideLength/2);
}
if(w.direction & 1)
{
ege_fillrect(x, y+blockSideLength*7/16, blockSideLength/2, blockSideLength*2/16);
}
//能量数字
if(showInformation)
{
setcolor(Color[Color_RedstoneSignal_0+s.redstoneSignal]);
xyprintf(x+blockSideLength*8/16, y, "%2d", s.redstoneSignal);
}
}
else if(w.id == Redstone_Torch)
{
//方向,0底,1234上右下左是否连接方块侧壁
setfillcolor(Color[Color_Wood]);
if(w.direction == 1)
{
ege_fillrect(x+blockSideLength*7/16, y, blockSideLength*2/16, blockSideLength/2);
}
else if(w.direction == 2)
{
ege_fillrect(x+blockSideLength/2, y+blockSideLength*7/16, blockSideLength/2, blockSideLength*2/16);
}
else if(w.direction == 3)
{
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength/2, blockSideLength*2/16, blockSideLength/2);
}
else if(w.direction == 4)
{
ege_fillrect(x, y+blockSideLength*7/16, blockSideLength/2, blockSideLength*2/16);
}
DrawTorch(x+blockSideLength/2, y+blockSideLength/2, s.state);
}
else if(w.id == Redstone_Block)
{
setfillcolor(EGERGB(230, 32, 8));
ege_fillrect(x, y, blockSideLength, blockSideLength);
setfillcolor(EGERGB(115, 12, 0));
ege_fillrect(x+blockSideLength/16, y+blockSideLength/16, blockSideLength*14/16, blockSideLength*14/16);
}
else if(w.id == Redstone_Lamp)
{
setfillcolor(EGERGB(49, 26, 17));
ege_fillrect(x, y, blockSideLength, blockSideLength);
if(s.state == 0) setfillcolor(EGERGB(134, 78, 41));//off
else setfillcolor(EGERGB(243, 192, 121));//on
ege_fillrect(x+blockSideLength/16, y+blockSideLength/16, blockSideLength*14/16, blockSideLength*14/16);
}
else if(w.id == Redstone_Repeater)
{
setfillcolor(EGERGB(140, 140, 140));
ege_fillrect(x, y, blockSideLength, blockSideLength);
setfillcolor(EGERGB(200, 200, 200));
ege_fillrect(x+blockSideLength/16, y+blockSideLength/16, blockSideLength*14/16, blockSideLength*14/16);
//方向,0123朝向上右下左
if(s.state == 0) setfillcolor(Color[Color_RedstoneSignal_0]);
else setfillcolor(Color[Color_RedstoneSignal_15]);
if(w.direction == 0)
{
//上2*2,间隔2,下8*2
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*2/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*6/16, blockSideLength*2/16, blockSideLength*8/16);
DrawTorch(x+blockSideLength*8/16, y+blockSideLength*3/16, !(s.state));
DrawTorch(x+blockSideLength*8/16, y+blockSideLength*(5+w.delay*2)/16, !(s.state));
if(s.isLocked)
{
setfillcolor(Color[Color_Bedrock]);
ege_fillrect(x+blockSideLength*2/16, y+blockSideLength*(4+w.delay*2)/16, blockSideLength*12/16, blockSideLength*2/16);
}
}
else if(w.direction == 1)
{
ege_fillrect(x+blockSideLength*12/16, y+blockSideLength*7/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*2/16, y+blockSideLength*7/16, blockSideLength*8/16, blockSideLength*2/16);
DrawTorch(x+blockSideLength*13/16, y+blockSideLength*8/16, !(s.state));
DrawTorch(x+blockSideLength*(11-w.delay*2)/16, y+blockSideLength*8/16, !(s.state));
if(s.isLocked)
{
setfillcolor(Color[Color_Bedrock]);
ege_fillrect(x+blockSideLength*(10-w.delay*2)/16, y+blockSideLength*2/16, blockSideLength*2/16, blockSideLength*12/16);
}
}
else if(w.direction == 2)
{
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*12/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*2/16, blockSideLength*2/16, blockSideLength*8/16);
DrawTorch(x+blockSideLength*8/16, y+blockSideLength*13/16, !(s.state));
DrawTorch(x+blockSideLength*8/16, y+blockSideLength*(11-w.delay*2)/16, !(s.state));
if(s.isLocked)
{
setfillcolor(Color[Color_Bedrock]);
ege_fillrect(x+blockSideLength*2/16, y+blockSideLength*(10-w.delay*2)/16, blockSideLength*12/16, blockSideLength*2/16);
}
}
else if(w.direction == 3)
{
ege_fillrect(x+blockSideLength*2/16, y+blockSideLength*7/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*6/16, y+blockSideLength*7/16, blockSideLength*8/16, blockSideLength*2/16);
DrawTorch(x+blockSideLength*3/16, y+blockSideLength*8/16, !(s.state));
DrawTorch(x+blockSideLength*(5+w.delay*2)/16, y+blockSideLength*8/16, !(s.state));
if(s.isLocked)
{
setfillcolor(Color[Color_Bedrock]);
ege_fillrect(x+blockSideLength*(4+w.delay*2)/16, y+blockSideLength*2/16, blockSideLength*2/16, blockSideLength*12/16);
}
}
//延时数字
if(showInformation)
{
if(s.state == 0) setcolor(Color[Color_RedstoneSignal_0]);
else setcolor(Color[Color_RedstoneSignal_15]);
xyprintf(x+blockSideLength*11/16, y, "%d", w.delay);
}
}
else if(w.id == Redstone_Comparator)
{
setfillcolor(EGERGB(140, 140, 140));
ege_fillrect(x, y, blockSideLength, blockSideLength);
setfillcolor(EGERGB(200, 200, 200));
ege_fillrect(x+blockSideLength/16, y+blockSideLength/16, blockSideLength*14/16, blockSideLength*14/16);
//方向,0123朝向上右下左
if(s.state == 0) setfillcolor(Color[Color_RedstoneSignal_0]);
else setfillcolor(Color[Color_RedstoneSignal_15]);
if(w.direction == 0)
{
//上2*2,间隔7,下2*8
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*2/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*4/16, y+blockSideLength*11/16, blockSideLength*8/16, blockSideLength*2/16);
DrawTorch(x+blockSideLength*8/16, y+blockSideLength*3/16, !(w.mode == 'm'));
DrawTorch(x+blockSideLength*5/16, y+blockSideLength*12/16, !(s.state));
DrawTorch(x+blockSideLength*11/16, y+blockSideLength*12/16, !(s.state));
}
else if(w.direction == 1)
{
ege_fillrect(x+blockSideLength*12/16, y+blockSideLength*7/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*3/16, y+blockSideLength*4/16, blockSideLength*2/16, blockSideLength*8/16);
DrawTorch(x+blockSideLength*13/16, y+blockSideLength*8/16, !(w.mode == 'm'));
DrawTorch(x+blockSideLength*4/16, y+blockSideLength*5/16, !(s.state));
DrawTorch(x+blockSideLength*4/16, y+blockSideLength*11/16, !(s.state));
}
else if(w.direction == 2)
{
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*12/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*4/16, y+blockSideLength*3/16, blockSideLength*8/16, blockSideLength*2/16);
DrawTorch(x+blockSideLength*8/16, y+blockSideLength*13/16, !(w.mode == 'm'));
DrawTorch(x+blockSideLength*5/16, y+blockSideLength*4/16, !(s.state));
DrawTorch(x+blockSideLength*11/16, y+blockSideLength*4/16, !(s.state));
}
else if(w.direction == 3)
{
ege_fillrect(x+blockSideLength*2/16, y+blockSideLength*7/16, blockSideLength*2/16, blockSideLength*2/16);
ege_fillrect(x+blockSideLength*11/16, y+blockSideLength*4/16, blockSideLength*2/16, blockSideLength*8/16);
DrawTorch(x+blockSideLength*3/16, y+blockSideLength*8/16, !(w.mode == 'm'));
DrawTorch(x+blockSideLength*12/16, y+blockSideLength*5/16, !(s.state));
DrawTorch(x+blockSideLength*12/16, y+blockSideLength*11/16, !(s.state));
}
//模式字符
if(showInformation)
{
if(s.state == 0) setcolor(Color[Color_RedstoneSignal_0]);
else setcolor(Color[Color_RedstoneSignal_15]);
xyprintf(x+blockSideLength*6/16, y+blockSideLength*3/16, "%c", w.mode);
}
}
else if(w.id == Lever)
{
//8*6
setfillcolor(GRAY);
ege_fillrect(x+blockSideLength*5/16, y+blockSideLength*4/16, blockSideLength*6/16, blockSideLength*8/16);
setfillcolor(Color[Color_Wood]);
if(s.state == 0)
{
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*2/16, blockSideLength*2/16, blockSideLength*6/16);
}
else
{
ege_fillrect(x+blockSideLength*7/16, y+blockSideLength*8/16, blockSideLength*2/16, blockSideLength*6/16);
}
//激活状态
if(showInformation)
{
if(s.state == 0) setcolor(Color[Color_RedstoneSignal_0]);
else setcolor(Color[Color_RedstoneSignal_15]);
xyprintf(x+blockSideLength*12/16, y, "%d", s.state);
}
}
else if(w.id == Button)
{
//4*6
setfillcolor(Color[Color_Wood]);
if(s.state == 0) ege_fillrect(x+blockSideLength*5/16, y+blockSideLength*6/16, blockSideLength*6/16, blockSideLength*4/16);
else ege_fillrect(x+blockSideLength*6/16, y+blockSideLength*7/16, blockSideLength*6/16, blockSideLength*4/16);
//激活状态
if(showInformation)
{
if(s.state == 0) setcolor(Color[Color_RedstoneSignal_0]);
else setcolor(Color[Color_RedstoneSignal_15]);
xyprintf(x+blockSideLength*12/16, y, "%d", s.state);
}
}
//setcolor(Color[Color_Border]);
//xyprintf(x, y, "%d", w.id);
//xyprintf(x, y, "%d", w.onBlock);
//xyprintf(x, y, "%d", s.energyLevel);
//xyprintf(x, y, "%d", s.redstoneTick);
}
void DrawBlock(int r, int c)//绘制方块
{
setfillcolor(Color[Color_Border]);
ege_fillrect(c*blockSideLength, r*blockSideLength, blockSideLength, blockSideLength);
setfillcolor(Color[Color_BackGround]);
ege_fillrect(c*blockSideLength+blockSideLength*1/32, r*blockSideLength+blockSideLength*1/32, blockSideLength*31/32, blockSideLength*31/32);
DrawBlockID(c*blockSideLength, r*blockSideLength, world[r][c], state[r][c]);
}
void DrawWorld(int mainhand)//绘制世界
{
int r, c, i;
//清除旧世界,减少锯齿感
cleardevice();
//显示所有方块
for(r=0; r<height; r++)
{
for(c=0; c<width; c++)
{
DrawBlock(r, c);
}
}
//显示物品栏
setfillcolor(Color[Color_Inventory_BK]);
ege_fillrect(0, height*blockSideLength, width*blockSideLength, heightOfInventory);
int x = (width*blockSideLength-(numberOfBlockType*blockSideLength*3/2+(numberOfBlockType-1)*blockSideLength/8))/2;
int y = height*blockSideLength+(heightOfInventory-blockSideLength*3/2)/2;
for(i=0; i<numberOfBlockType; i++)
{
//主手边框
if(i == mainhand)
{
setfillcolor(Color[Color_Inventory_MainHandBorder]);
ege_fillrect(x-blockSideLength/16+i*(blockSideLength*3/2+blockSideLength/8), y-blockSideLength/16, blockSideLength*13/8, blockSideLength*13/8);
}
//单元格内容
//边长sl*3/2
//a = n*sl*3/2+(n-1)*sl/8
//b = (w*sl-a)/2
//c = b+i*sl/8
setfillcolor(Color[Color_InventoryCell_BK]);
ege_fillrect(x+i*(blockSideLength*3/2+blockSideLength/8), y, blockSideLength*3/2, blockSideLength*3/2);
DrawBlockID(x+blockSideLength/4+i*(blockSideLength*3/2+blockSideLength/8), y+blockSideLength/4, inventoryWorld[i], defaultState[i]);
//单元格序号
if(i == mainhand)
{
setcolor(Color[Color_InventoryNumber_Current]);
xyprintf(x+i*(blockSideLength*3/2+blockSideLength/8), y, "%d", i);
}
else
{
setcolor(Color[Color_InventoryNumber]);
xyprintf(x+i*(blockSideLength*3/2+blockSideLength/8), y, "%d", i);
}
}
//鼠标悬浮高亮
mousepos(&c, &r);
r /= blockSideLength;
c /= blockSideLength;
if(r >= 0 && r < height && c >= 0 && c < width)//不在物品栏
{
setfillcolor(Color[Color_MouseHighlight]);
ege_fillrect(c*blockSideLength, r*blockSideLength, blockSideLength, blockSideLength);
DrawBlockID(c*blockSideLength, r*blockSideLength, world[r][c], state[r][c]);//重绘该方块
}
if(showInformation == 1)
{
setcolor(Color[Color_InventoryNumber]);
xyprintf(0, height*blockSideLength, "RedstoneTick:%d", currentTick);
xyprintf(0, height*blockSideLength+blockSideLength*3/8, "Position:%d,%d", r, c);
//if(state[r][c].onBlock) xyprintf(0, height*blockSideLength+blockSideLength*6/8, "EnergyLevel:%d", state[r][c].energyLevel);
/*if(world[r][c] == Redstone_Comparator)
{
xyprintf(0, height*blockSideLength+blockSideLength*6/8, "ABC:%d(%d)->%d",
state[r][c].mainInput, state[r][c].sideInput, state[r][c].redstoneSignal);
}*/
}
}
void PutBlock(BlockID id, int r, int c)//在单元格放置物品栏物品
{
if(world[r][c].onBlock)//在方块上
{
if(id == Redstone_Wire || id == Redstone_Torch || id == Lever || id == Button)
{
world[r][c].id = id;
world[r][c] = inventoryWorld[id];
world[r][c].onBlock = 1;
if(id == Redstone_Torch && inventoryWorld[Redstone_Torch].direction != 0)
{
world[r][c].direction = 0;//方块上不能放置连接方块侧壁的红石火把
}
}
}
else
{
world[r][c].id = id;
world[r][c] = inventoryWorld[id];
if(id == Redstone_Torch && inventoryWorld[Redstone_Torch].direction != 0)
{
if(inventoryWorld[Redstone_Torch].direction == 1)
{
if(r > 0 && world[r-1][c].onBlock);
else world[r][c].direction = 0;//连接方块侧壁的红石火把放置时必须存在对应方块
}
else if(inventoryWorld[Redstone_Torch].direction == 2)
{
if(c+1 < width && world[r][c+1].onBlock);
else world[r][c].direction = 0;
}
else if(inventoryWorld[Redstone_Torch].direction == 3)
{
if(r+1 < height && world[r+1][c].onBlock);
else world[r][c].direction = 0;
}
else if(inventoryWorld[Redstone_Torch].direction == 4)
{
if(c > 0 && world[r][c-1].onBlock);
else world[r][c].direction = 0;
}
}
}
}
void BreakBlock(int r, int c)
{
if(world[r][c].onBlock)//在方块上
{
if(world[r][c].id == Air)
{
world[r][c].onBlock = 0;
}
else
{
world[r][c].id = Air;//破坏方块上物品
world[r][c] = inventoryWorld[Air];//维持方块存在,其他状态清空
}
}
else//无方块
{
world[r][c].id = Air;//清空物品
world[r][c] = inventoryWorld[Air];
world[r][c].onBlock = 0;
}
}
int IsConnected(int rt, int ct, int rs, int cs)//s为红石线,t是否连接
{
if(world[rt][ct].id == Air) return 0;
else if(world[rt][ct].id == Redstone_Wire) return 1;
else if(world[rt][ct].id == Redstone_Torch)
{
if(world[rt][ct].onBlock == world[rs][cs].onBlock) return 1;//处于同层时连接
}
else if(world[rt][ct].id == Redstone_Block)
{
if(world[rt][ct].onBlock == world[rs][cs].onBlock) return 1;//处于同层时连接
}
else if(world[rt][ct].id == Redstone_Lamp)
{
if(world[rt][ct].onBlock == world[rs][cs].onBlock) return 1;//处于同层时连接
}
else if(world[rt][ct].id == Redstone_Repeater)
{
if(world[rt][ct].onBlock == world[rs][cs].onBlock)//处于同层时连接
{
//处于输入输出方向时连接
if(world[rt][ct].direction == 0 || world[rt][ct].direction == 2)
{
if((rt == rs-1 || rt == rs+1) && ct == cs) return 1;
}
else if(world[rt][ct].direction == 1 || world[rt][ct].direction == 3)
{
if(rt == rs && (ct == cs-1 || ct == cs+1)) return 1;
}
}
}
else if(world[rt][ct].id == Redstone_Comparator)
{
if(world[rt][ct].onBlock == world[rs][cs].onBlock) return 1;//处于同层时连接
}
else if(world[rt][ct].id == Lever)
{
if(world[rt][ct].onBlock == world[rs][cs].onBlock) return 1;//处于同层时连接
}
else if(world[rt][ct].id == Button)
{
if(world[rt][ct].onBlock == world[rs][cs].onBlock) return 1;//处于同层时连接
}
return 0;
}
void SpreadSignalOnWire(int r, int c)//递归在红石线上传播信号
{
int signal = newState[r][c].redstoneSignal-1;
if(r > 0 && world[r-1][c].id == Redstone_Wire && newState[r-1][c].redstoneSignal < signal)//默认为0,不会向负数传播
{
newState[r-1][c].state = 1;
newState[r-1][c].redstoneSignal = signal;
SpreadSignalOnWire(r-1, c);
}
if(c+1 < width && world[r][c+1].id == Redstone_Wire && newState[r][c+1].redstoneSignal < signal)
{
newState[r][c+1].state = 1;
newState[r][c+1].redstoneSignal = signal;
SpreadSignalOnWire(r, c+1);
}
if(r+1 < height && world[r+1][c].id == Redstone_Wire && newState[r+1][c].redstoneSignal < signal)
{
newState[r+1][c].state = 1;
newState[r+1][c].redstoneSignal = signal;
SpreadSignalOnWire(r+1, c);
}
if(c > 0 && world[r][c-1].id == Redstone_Wire && newState[r][c-1].redstoneSignal < signal)
{
newState[r][c-1].state = 1;
newState[r][c-1].redstoneSignal = signal;
SpreadSignalOnWire(r, c-1);
}
}
void SpreadSignalToComponent(int rt, int ct, int rs, int cs)
{
if(world[rt][ct].id != Air && world[rt][ct].id != Redstone_Wire && world[rs][cs].onBlock == world[rt][ct].onBlock)
{
if(world[rt][ct].id == Redstone_Lamp)
{
newState[rt][ct].state = 1;
}
else if(world[rt][ct].id == Redstone_Repeater)
{
if((rt == rs-1 && world[rt][ct].direction == 0)
|| (ct == cs+1 && world[rt][ct].direction == 1)
|| (rt == rs+1 && world[rt][ct].direction == 2)
|| (ct == cs-1 && world[rt][ct].direction == 3))
{
newState[rt][ct].state = 1;
newState[rt][ct].redstoneSignal = 15;
}
if(world[rs][cs].id == Redstone_Repeater || world[rs][cs].id == Redstone_Comparator)
{
if(world[rt][ct].direction == 0 || world[rt][ct].direction == 2)
{
if(rt == rs && (ct == cs-1 || ct == cs+1))
{
if((world[rs][cs].direction == 1 && ct == cs+1)
|| (world[rs][cs].direction == 3 && ct == cs-1))
{
newState[rt][ct].isLocked = 1;
}
}
}
else if(world[rt][ct].direction == 1 || world[rt][ct].direction == 3)
{
if((rt == rs-1 || rt == rs+1) && ct == cs)
{
if((world[rs][cs].direction == 0 && rt == rs-1)
|| (world[rs][cs].direction == 2 && rt == rs+1))
{
newState[rt][ct].isLocked = 1;
}
}
}
}
}
else if(world[rt][ct].id == Redstone_Comparator)
{
if((rt == rs-1 && world[rt][ct].direction == 0)
|| (ct == cs+1 && world[rt][ct].direction == 1)
|| (rt == rs+1 && world[rt][ct].direction == 2)
|| (ct == cs-1 && world[rt][ct].direction == 3))
{
newState[rt][ct].mainInput = state[rs][cs].redstoneSignal;
}
//只有红石线,红石中继器,红石比较器会产生红石比较器的侧边输入
if(state[rs][cs].redstoneSignal > newState[rt][ct].sideInput)
{
if(world[rs][cs].id == Redstone_Wire || world[rs][cs].id == Redstone_Repeater || world[rs][cs].id == Redstone_Comparator)
{
if(world[rt][ct].direction == 0 || world[rt][ct].direction == 2)
{
if(rt == rs && (ct == cs-1 || ct == cs+1))
{
newState[rt][ct].sideInput = state[rs][cs].redstoneSignal;
}
}
else if(world[rt][ct].direction == 1 || world[rt][ct].direction == 3)
{
if((rt == rs-1 || rt == rs+1) && ct == cs)
{
newState[rt][ct].sideInput = state[rs][cs].redstoneSignal;
}
}
}
}
}
}
}
void SpreadEnergyToComponent(int rt, int ct, int rs, int cs)
{
if(world[rt][ct].id != Air && world[rt][ct].id != Redstone_Wire && world[rt][ct].onBlock == 0)
{
if(world[rt][ct].id == Redstone_Torch)
{
if((rs == rt-1 && world[rt][ct].direction == 1)
|| (cs == ct+1 && world[rt][ct].direction == 2)