-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.py
1389 lines (1116 loc) · 38.8 KB
/
blocks.py
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
from __future__ import annotations
from abc import abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import Any, Type, TypeAlias, TypeVar, TYPE_CHECKING, cast
from .actions import Action, ActionType, all_directions, cowboy_directions, bullet_directions
from .exceptions import OutOfStepsException, ProgramParseException
# Brake circular dependency only used for type checking
if TYPE_CHECKING:
from .map import GameMap, Cowboy, Bullet
Position = tuple[int, int]
class Run:
max_steps: int
steps: int
variables: dict[str, int | bool | Position]
map: GameMap
context: Cowboy | Bullet
def __init__(self, max_steps: int, variables: dict[str, int | bool | Position],
map: GameMap, context: Cowboy | Bullet) -> None:
self.max_steps = max_steps
self.steps = 0
self.variables = variables
self.map = map
self.context = context
def add_steps(self, steps: int):
self.steps += steps
if self.steps > self.max_steps:
raise OutOfStepsException()
################################################################################
class Field:
is_variable: bool = False
name: str
@abstractmethod
def execute(self, run: Run) -> bool | int | str | Position:
pass
def check_type(self, wanted: type) -> None:
pass
class VariableField(Field):
is_variable = True
var_type: type | Any
def __init__(self, name: str) -> None:
self.name = name
self.var_type = Any
def __str__(self) -> str:
return f"variable {self.name}"
def check_type(self, wanted: type) -> None:
# Variable could be any type, will be checked after parsing the whole program
self.var_type = wanted
def execute(self, run: Run) -> bool | int | Position:
return run.variables[self.name]
class StaticField(Field):
# Cannot be Position
value: bool | int | str
returns: type
def __init__(self, name: str, raw_value: str) -> None:
self.name = name
if name == "BOOL":
self.value = raw_value.lower() == "true"
self.returns = bool
elif name == "NUM":
self.value = int(raw_value)
self.returns = int
else:
self.value = raw_value
self.returns = str
def __str__(self) -> str:
return f"field {self.name}"
def check_type(self, wanted: type) -> None:
if wanted != self.returns:
raise ProgramParseException(f"{self.name} is {self.returns} but {wanted} wanted")
def execute(self, run: Run) -> bool | int | str:
return self.value
################################################################################
type_to_js_type: dict[type | TypeAlias, str] = {
bool: "Boolean",
int: "Number",
str: "String",
Position: "Position",
}
class BlockInputKind(Enum):
FIELD = "field"
VALUE = "value"
STATEMENT = "statement"
def __str__(self) -> str:
return str(self.value)
@dataclass
class BlockInput:
kind: BlockInputKind
attr: str | None # name of the object attribute to save into
name: str
data_type: type | Any = Any
dropdown: list[tuple[str, str]] | None = None
variable: bool = False
arg_group: int = 0 # for grouping block inputs in graphical blocks
def json_definition(self) -> dict:
if self.kind == BlockInputKind.FIELD:
t = "field_input"
if self.dropdown is not None:
t = "field_dropdown"
elif self.data_type == int:
t = "field_number"
elif self.variable:
t = "field_variable"
elif self.kind == BlockInputKind.VALUE:
t = "input_value"
elif self.kind == BlockInputKind.STATEMENT:
t = "input_statement"
out: dict = {
"type": t,
"name": self.name,
}
if self.dropdown:
out["options"] = self.dropdown
elif self.data_type != Any:
out["check"] = type_to_js_type[self.data_type]
return out
################################################################################
class Block:
is_blockly_default: bool = False # do not generate JSON definition for this block
# For generating Blockly definition and for checks:
name: str = "???"
color: int | None = None
tooltip: str | None = None
inputs: list[BlockInput] = []
inputs_inline: bool = True
messages: list[str] = [] # texts displayed in graphical blocks
returns: None | type | TypeAlias = None # if itself returns something (bool / int)
has_prev: bool = False # could run after another block
has_next: bool = False # another block could run after this one
# Instance variables:
next: Block | None
def __str__(self) -> str:
return f"block '{self.name}'"
@classmethod
def json_definition(cls) -> dict:
out: dict = {
"type": cls.name,
}
if cls.has_prev:
out["previousStatement"] = None
if cls.has_next:
out["nextStatement"] = None
if cls.returns is not None:
out["output"] = type_to_js_type[cls.returns]
if cls.color is not None:
out["colour"] = cls.color
if cls.tooltip is not None:
out["tooltip"] = cls.tooltip
if cls.inputs_inline:
out["inputsInline"] = True
for i, message in enumerate(cls.messages):
out[f"message{i}"] = message
arg_groups: dict[int, list[BlockInput]] = {}
for input in cls.inputs:
if input.arg_group not in arg_groups:
arg_groups[input.arg_group] = []
arg_groups[input.arg_group].append(input)
for i, arg_group in arg_groups.items():
out[f"args{i}"] = [input.json_definition() for input in arg_group]
return out
def execute(self, run: Run) -> Action | Position | bool | int | None:
if self.next:
return self.next.execute(run)
return None
def _set_return_type(self, return_type: type):
raise ProgramParseException(f"cannot set return type for {self.name}")
U = TypeVar('U')
def _get_inputs(self, kind: BlockInputKind, given: dict[str, U]) -> list[tuple[BlockInput, U]]:
wanted = [x for x in self.inputs if x.kind == kind]
if len(wanted) == 0 and len(given) > 0:
raise ProgramParseException(f"no {kind} allowed ({given} given)")
wanted_keys = [x.name for x in wanted]
missing = set(wanted_keys).difference(given.keys())
extra = set(given.keys()).difference(wanted_keys)
if len(missing) > 0:
raise ProgramParseException(f"missing {kind}: {missing}")
if len(extra) > 0:
raise ProgramParseException(f"extra {kind}: {extra}")
return [(input, given[input.name]) for input in wanted]
def __init__(self, mutation: dict[str, str],
fields: dict[str, Field], values: dict[str, Block],
statements: dict[str, Block], next: Block | None) -> None:
wanted_fields = self._get_inputs(BlockInputKind.FIELD, fields)
for input, field in wanted_fields:
if input.attr is not None:
setattr(self, input.attr, field)
field.check_type(input.data_type)
wanted_values = self._get_inputs(BlockInputKind.VALUE, values)
for input, val in wanted_values:
if input.attr is not None:
setattr(self, input.attr, val)
if input.data_type == Any:
continue
if val.returns == Any:
val._set_return_type(input.data_type)
if val.returns != input.data_type:
raise ProgramParseException(f"value {input.name}: return {val.returns} but {input.data_type} expected")
wanted_statements = self._get_inputs(BlockInputKind.STATEMENT, statements)
for input, st in wanted_statements:
if input.attr is not None:
setattr(self, input.attr, st)
if st.returns is not None:
raise ProgramParseException(f"statement {input.name}: should not return value (returns {st.returns})")
if self.has_next:
self.next = next
elif next is not None:
raise ProgramParseException(f"Block {self.name}: next not supported")
class Nop(Block):
name = "nop"
messages = ["Stát"]
has_prev = True
color = 120
tooltip = "Nedělat nic"
def execute(self, run: Run) -> Action:
return Action(ActionType.NOP)
class InfoTeam(Block):
name = "info_team"
messages = ["Můj tým"]
returns = int
color = 340
tooltip = "Index mého týmu (int)"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.context.team
class InfoPoints(Block):
name = "info_points"
messages = ["Počet bodů"]
returns = int
color = 340
tooltip = "Počet bodů mého týmu (int)"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.map.my_points(run.context)
class InfoIndex(Block):
name = "info_index"
messages = ["Můj index"]
returns = int
color = 340
tooltip = "Moje pořadové číslo v týmu (stejné po celou hru)"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.context.index
class InfoID(Block):
name = "info_id"
messages = ["Moje ID"]
returns = int
color = 300
tooltip = "Moje ID v seznamu všech entit, mění se každé kolo"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.map.my_id(run.context)
class InfoMyDirection(Block):
name = "info_my_direction"
messages = ["Můj směr"]
returns = int
color = 300
tooltip = "Vrátí aktuální směr střely jako číslo (0 je ←, pořadí: ←,↖,↑,↗,→,↘,↓,↙)"
def execute(self, run: Run) -> int:
run.add_steps(1)
assert isinstance(run.context, Bullet)
return run.context.direction
class InfoMyRange(Block):
name = "info_my_range"
messages = ["Zbývá kroků"]
returns = int
color = 300
tooltip = "Vrátí kolik střele zbývá kroků"
def execute(self, run: Run) -> int:
run.add_steps(1)
assert isinstance(run.context, Bullet)
return run.map.BULLET_LIFETIME - run.context.turns_made
class InfoTurn(Block):
name = "info_turn"
messages = ["Číslo kola"]
returns = int
color = 300
tooltip = "Vrací číslo aktuálního kola"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.map.turn_idx
class InfoMyPosition(Block):
name = "info_position"
messages = ["Moje pozice"]
returns = Position
color = 300
tooltip = "Vrátí moji současnou pozici (X, Y)"
def execute(self, run: Run) -> Position:
run.add_steps(1)
return run.map.my_position(run.context)
# Generic query:
class InfoMapPosition(Block):
name = "info_map_position"
messages = ["Je na %1 %2?"]
inputs = [
BlockInput(BlockInputKind.VALUE, "position", "POSITION", Position),
BlockInput(BlockInputKind.FIELD, "entity", "ENTITY", str, dropdown=[
("Zeď", "WALL"), ("Zlato", "GOLD"),
("Kovboj", "COWBOY"), ("Střela", "BULLET"),
]),
]
returns = bool
color = 300
tooltip = "Ověří, jestli je na daných souřadnicích zeď, zlato, kovboj nebo střela."
position: Block
entity: Field
def execute(self, run: Run) -> int:
run.add_steps(1)
pos = cast(Position, self.position.execute(run))
(c, r) = pos
entity = self.entity.execute(run)
if entity == "WALL":
return run.map.wall_grid[r][c]
elif entity == "GOLD":
return run.map.gold_grid[r][c] is not None
elif entity == "COWBOY":
return run.map.cowboy_grid[r][c] is not None
elif entity == "BULLET":
return run.map.bullet_grid[r][c] is not None
return False # should not happen
# Golds:
class InfoGoldCount(Block):
name = "info_gold_count"
messages = ["# zlata"]
returns = int
color = 340
tooltip = "Vrátí počet zlata na hracím plánu"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.map.number_of_golds()
class InfoGoldPosition(Block):
name = "info_gold_position"
messages = ["Pozice zlata %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "gold_block", "GOLD", int),
]
returns = Position
color = 340
tooltip = "Vrátí souřadnice (X, Y) zlata s daným ID"
gold_block: Block
def execute(self, run: Run) -> Position:
run.add_steps(1)
gold = self.gold_block.execute(run)
assert isinstance(gold, int)
return run.map.gold_i_position(gold)
# Cowboys:
class InfoCowboyCount(Block):
name = "info_cowboy_count"
messages = ["# kovbojů"]
returns = int
color = 340
tooltip = "Vrátí počet kovbojů na hracím plánu"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.map.number_of_cowboys()
class InfoCowboyTeam(Block):
name = "info_cowboy_team"
messages = ["Tým kovboje %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "cowboy_block", "COWBOY", int),
]
returns = int
color = 340
tooltip = "Vrátí index týmu kovboje s daným ID"
cowboy_block: Block
def execute(self, run: Run) -> int:
run.add_steps(1)
cowboy = self.cowboy_block.execute(run)
assert isinstance(cowboy, int)
return run.map.cowboy_i_team(cowboy)
class InfoCowboyPosition(Block):
name = "info_cowboy_position"
messages = ["Pozice kovboje %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "cowboy_block", "COWBOY", int),
]
returns = Position
color = 340
tooltip = "Vrátí souřadnice (X, Y) kovboje s daným ID"
cowboy_block: Block
def execute(self, run: Run) -> Position:
run.add_steps(1)
cowboy = self.cowboy_block.execute(run)
assert isinstance(cowboy, int)
return run.map.cowboy_i_position(cowboy)
# Bullets:
class InfoBulletCount(Block):
name = "info_bullet_count"
messages = ["# střel"]
returns = int
color = 340
tooltip = "Vrátí počet střel na hracím plánu"
def execute(self, run: Run) -> int:
run.add_steps(1)
return run.map.number_of_bullets()
class InfoBulletTeam(Block):
name = "info_bullet_team"
messages = ["Tým střely %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "bullet_block", "BULLET", int),
]
returns = int
color = 340
tooltip = "Vrátí index týmu střely s daným ID"
bullet_block: Block
def execute(self, run: Run) -> int:
run.add_steps(1)
bullet = self.bullet_block.execute(run)
assert isinstance(bullet, int)
return run.map.bullet_i_team(bullet)
class InfoBulletPosition(Block):
name = "info_bullet_position"
messages = ["Pozice střely %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "bullet_block", "BULLET", int),
]
returns = Position
color = 340
tooltip = "Vrátí souřadnice (X, Y) střely s daným ID"
bullet_block: Block
def execute(self, run: Run) -> Position:
run.add_steps(1)
bullet = self.bullet_block.execute(run)
assert isinstance(bullet, int)
return run.map.bullet_i_position(bullet)
# Position transformations:
class ModifyPosition(Block):
name = "modify_position"
messages = ["posuň %1 o %2"]
inputs = [
BlockInput(BlockInputKind.VALUE, "position", "POSITION", Position),
BlockInput(BlockInputKind.VALUE, "direction", "DIRECTION", int),
]
returns = Position
color = 30
tooltip = "Posune souřadnice o jedno políčko daným směrem (směr bere jako číslo modulo 8, ← je 0)"
position: Block
direction: Block
def execute(self, run: Run) -> Position:
run.add_steps(1)
pos = cast(Position, self.position.execute(run))
dir = self.direction.execute(run)
assert type(dir) is int
(dx, dy) = all_directions[dir % 8].value
(x, y) = pos
x = (x + dx) % run.map.width
y = (y + dy) % run.map.height
return (x, y)
class TransformPositionX(Block):
name = "transform_position_x"
messages = ["%1→X"]
inputs = [
BlockInput(BlockInputKind.VALUE, "block_position", "POSITION", Position),
]
returns = int
color = 30
tooltip = "Vrátí X souřadnici z (X, Y)"
block_position: Block
def execute(self, run: Run) -> int:
run.add_steps(1)
pos = cast(Position, self.block_position.execute(run))
return pos[0]
class TransformPositionY(Block):
name = "transform_position_y"
messages = ["%1→Y"]
inputs = [
BlockInput(BlockInputKind.VALUE, "block_position", "POSITION", Position),
]
returns = int
color = 30
tooltip = "Vrátí Y souřadnici z (X, Y)"
block_position: Block
def execute(self, run: Run) -> int:
run.add_steps(1)
pos = cast(Position, self.block_position.execute(run))
return pos[1]
class TransformXYPosition(Block):
name = "transform_x_y_position"
messages = ["(%1:%2)"]
inputs = [
BlockInput(BlockInputKind.VALUE, "block_x", "X", int),
BlockInput(BlockInputKind.VALUE, "block_y", "Y", int),
]
returns = Position
color = 30
tooltip = "Vytvoří (X, Y) souřadnice z X a Y"
block_x: Block
block_y: Block
def execute(self, run: Run) -> Position:
run.add_steps(1)
x = self.block_x.execute(run)
y = self.block_y.execute(run)
assert isinstance(x, int) and isinstance(y, int)
return (x, y)
class CountDistance(Block):
name = "count_distance"
messages = ["Přímá vzdálenost k %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "block_position", "POSITION", Position),
]
returns = int
color = 30
tooltip = "Vrátí přímou vzdálenost k zadaným souřadnicím (při pohybu osmi směry)"
block_position: Block
def execute(self, run: Run) -> int:
run.add_steps(1)
pos = cast(Position, self.block_position.execute(run))
assert run.context.position is not None
return run.map.maximum_metric(run.context.position, pos)
class GetDirection(Block):
name = "compute_direction"
messages = ["Směr k %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "pos", "POSITION", Position),
]
returns = int
color = 30
tooltip = "Vrátí nejlepší směr (z množiny [←,↖,↑,↗,→,↘,↓,↙]) k zadaným souřadnicím. Vrací číslo, ← je 0. Výpočet je bez ohledu na zdi."
pos: Block
def execute(self, run: Run) -> int:
pos = cast(Position, self.pos.execute(run))
run.add_steps(1)
assert run.context.position is not None
x, y = run.context.position
tx, ty = pos
dx, dy = (tx - x, ty - y)
# Wrap over the edge of the map
if dx > run.map.width/2:
dx -= run.map.width
elif dx < -run.map.width/2:
dx += run.map.width
if dy > run.map.height/2:
dy -= run.map.height
elif dy < -run.map.height/2:
dy += run.map.height
out_x, out_y = 0, 0
# if difference in one axis is more than twice the difference in the
# other axis -> move only in one axis
if abs(dy) <= 2*abs(dx):
out_x = 1 if dx > 0 else -1
if abs(dx) <= 2*abs(dy):
out_y = 1 if dy > 0 else -1
direction = (out_x, out_y)
for i, d in enumerate(bullet_directions):
if d.value == direction:
return i
print(f"ERROR in ComputeDirection: {direction} not found")
return -1 # Should not happen
# Computations:
class ComputeDistance(Block):
name = "compute_distance"
messages = ["Počet kroků k %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "pos", "POSITION", Position),
]
returns = int
color = 300
tooltip = "Vrátí počet kroků kovboje od současné pozice k bodu X:Y (s uvažováním překážek ale bez kovbojů)"
pos: Block
def execute(self, run: Run) -> int:
pos = cast(Position, self.pos.execute(run))
run.add_steps(1)
assert isinstance(run.context, Cowboy)
return run.map.distance_from(run.context, pos)
class ComputeFirstStep(Block):
name = "compute_first_step"
messages = ["První krok k %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "pos", "POSITION", Position),
]
returns = int
color = 300
# FIXME: indexování kroků?
tooltip = ("Vrátí index (číslo 0, 2, 4 nebo 6) prvního kroku nejkratší cesty "
"pro kovboje od současné pozice k bodu X:Y (s uvažováním překážek ale bez kovbojů). ")
pos: Block
def execute(self, run: Run) -> int:
pos = cast(Position, self.pos.execute(run))
run.add_steps(1)
assert isinstance(run.context, Cowboy)
direction = run.map.which_way(run.context, pos)
for i, d in enumerate(all_directions):
if d.value == direction:
return i
return -1
# Variables:
class VariablesGet(Block):
name = "variables_get"
inputs = [
BlockInput(BlockInputKind.FIELD, "var", "VAR", variable=True),
]
returns = Any
is_blockly_default = True
var: VariableField
def _set_return_type(self, return_type: type):
self.var.check_type(return_type)
self.returns = return_type
def execute(self, run: Run) -> bool | int | Position:
run.add_steps(1)
return self.var.execute(run)
class VariablesSet(Block):
name = "variables_set"
inputs = [
BlockInput(BlockInputKind.FIELD, "var", "VAR", variable=True),
BlockInput(BlockInputKind.VALUE, "value", "VALUE"),
]
is_blockly_default = True
has_next = True
var: VariableField
value: Block
def __init__(self, mutation: dict[str, str],
fields: dict[str, Field], values: dict[str, Block],
statements: dict[str, Block], next: Block | None) -> None:
super().__init__(mutation, fields, values, statements, next)
if self.value.returns and self.value.returns != Any:
self.var.check_type(self.value.returns)
def execute(self, run: Run) -> Action | Position | int | None:
run.add_steps(1)
ret = self.value.execute(run)
assert ret is not None and not isinstance(ret, Action)
run.variables[self.var.name] = ret
return super().execute(run)
class MathChange(Block):
name = "math_change"
inputs = [
BlockInput(BlockInputKind.FIELD, "var", "VAR", int, variable=True),
BlockInput(BlockInputKind.VALUE, "delta", "DELTA", int),
]
is_blockly_default = True
has_next = True
var: VariableField
delta: Block
def __init__(self, mutation: dict[str, str],
fields: dict[str, Field], values: dict[str, Block],
statements: dict[str, Block], next: Block | None) -> None:
super().__init__(mutation, fields, values, statements, next)
if self.delta.returns and self.delta.returns != Any:
self.var.check_type(self.delta.returns)
def execute(self, run: Run) -> Action | Position | int | None:
run.add_steps(1)
delta = self.delta.execute(run)
assert isinstance(delta, int)
run.variables[self.var.name] += delta # type: ignore
return super().execute(run)
class LogicBoolean(Block):
name = "logic_boolean"
inputs = [
BlockInput(BlockInputKind.FIELD, "field", "BOOL", bool),
]
is_blockly_default = True
returns = bool
field: Field
def execute(self, run: Run) -> bool:
run.add_steps(1)
ret = self.field.execute(run)
assert isinstance(ret, bool)
return ret
class ConstantDirection(Block):
name = "constant_direction"
messages = ["%1"]
inputs = [
BlockInput(BlockInputKind.FIELD, "direction", "DIRECTION", str, dropdown=[
("←", "0"), ("↖", "1"), ("↑", "2"), ("↗", "3"),
("→", "4"), ("↘", "5"), ("↓", "6"), ("↙", "7"),
]),
]
returns = int
color = 220
tooltip = "Směr jako číslo (← je 0 a pak po směru hodinových ručiček)"
direction: Field
def execute(self, run: Run) -> int:
run.add_steps(1)
ret = self.direction.execute(run)
assert isinstance(ret, int)
return ret
class LogicCompare(Block):
name = "logic_compare"
inputs = [
BlockInput(BlockInputKind.FIELD, "op", "OP", str, dropdown=[]), # TODO: add dropdown
BlockInput(BlockInputKind.VALUE, "block_A", "A", Any),
BlockInput(BlockInputKind.VALUE, "block_B", "B", Any),
]
returns = bool
is_blockly_default = True
op: Field
block_A: Block
block_B: Block
def execute(self, run: Run) -> bool:
run.add_steps(1)
op = self.op.execute(run)
# A and B could be any comparable type:
A = self.block_A.execute(run)
B = self.block_B.execute(run)
if op == "EQ":
return A == B
elif op == "NEQ":
return A != B
elif op == "LT":
return A < B # type: ignore
elif op == "LTE":
return A <= B # type: ignore
elif op == "GT":
return A > B # type: ignore
elif op == "GTE":
return A >= B # type: ignore
return False # should not happen
class LogicOperation(Block):
name = "logic_operation"
inputs = [
BlockInput(BlockInputKind.FIELD, "op", "OP", str, dropdown=[]), # TODO: add dropdown
BlockInput(BlockInputKind.VALUE, "block_A", "A", bool),
BlockInput(BlockInputKind.VALUE, "block_B", "B", bool),
]
returns = bool
is_blockly_default = True
op: Field
block_A: Block
block_B: Block
def execute(self, run: Run) -> bool:
run.add_steps(1)
op = self.op.execute(run)
A = self.block_A.execute(run)
B = self.block_B.execute(run)
assert isinstance(A, bool) and isinstance(B, bool)
if op == "AND":
return A and B
elif op == "OR":
return A or B
return False # should not happen
class LogicNegate(Block):
name = "logic_negate"
inputs = [
BlockInput(BlockInputKind.VALUE, "bool_child", "BOOL", bool),
]
returns = bool
is_blockly_default = True
bool_child: Block
def execute(self, run: Run) -> bool:
run.add_steps(1)
return not self.bool_child.execute(run)
class MathNumber(Block):
name = "math_number"
inputs = [
BlockInput(BlockInputKind.FIELD, "field", "NUM", int),
]
returns = int
is_blockly_default = True
field: Field
def execute(self, run: Run) -> int:
run.add_steps(1)
ret = self.field.execute(run)
assert isinstance(ret, int)
return ret
class MathAbs(Block):
name = "math_abs"
messages = ["abs %1"]
inputs = [
BlockInput(BlockInputKind.VALUE, "number", "NUM", int),
]
returns = int
color = 220
tooltip = "Absolutní hodnota čísla"
number: Field
def execute(self, run: Run) -> int:
run.add_steps(1)
ret = self.number.execute(run)
assert isinstance(ret, int)
return abs(ret)
class MathArithmeticCustom(Block):
name = "math_arithmetic_custom"
messages = ["%2%1%3"]
inputs = [
BlockInput(BlockInputKind.FIELD, "op", "OP", str, dropdown=[
("+", "ADD"), ("-", "MINUS"), ("×", "MULTIPLY"), ("/", "DIVIDE"),
("^", "POWER"), ("%", "MODULO"),
]),
BlockInput(BlockInputKind.VALUE, "block_A", "A", int),
BlockInput(BlockInputKind.VALUE, "block_B", "B", int),
]
returns = int
color = 220
tooltip = ("Provede aritmetickou operaci (+ součet, - rozdíl, × násobení, "
"/ celočíselné dělení, ^ umocňování, % zbytek po dělení)")
# is_blockly_default = True
op: Field
block_A: Block
block_B: Block
def execute(self, run: Run) -> int:
run.add_steps(1)
op = self.op.execute(run)
A = self.block_A.execute(run)