-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
1264 lines (1027 loc) · 41.8 KB
/
__init__.py
File metadata and controls
1264 lines (1027 loc) · 41.8 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
import copy
import random
import time
import math
from collections import deque
from dataclasses import dataclass
from typing import Optional, TypeVar
import arcade
import ipdb
import logging
import pyglet.math
import pyperclip
from hack.path_finding import navigate, get_player_coord_from_state
import hack.constants as vk
import hack.hack_util as hack_util
import constants
constants.FONT_NAME = "Arial"
Properties = tuple[tuple[str, any]]
T = TypeVar("T")
BackupOrNone = Optional[tuple[T, any]]
import engine.hitbox
def smart_dup(value) -> any:
if isinstance(value, (
list,
set,
deque,
)):
return value.__class__(smart_dup(v) for v in value)
if isinstance(value, dict):
return {k: smart_dup(v) for k, v in value.items()}
if isinstance(value, (
engine.hitbox.Point,
)):
return copy.deepcopy(value)
return value
def generic_backup(obj: object, ignore_attrs=()) -> Properties:
return Properties(
(key, smart_dup(value))
for key, value in obj.__dict__.items()
if key not in ignore_attrs
)
def generic_restore(obj: T, state: Properties):
for key, value in state:
obj.__dict__[key] = value
return obj
def backup_or_none(obj: T) -> BackupOrNone:
if obj is None:
return None
return obj, obj.backup()
def restore_or_none(backup: BackupOrNone) -> T:
if backup is None:
return None
obj, state = backup
obj.restore(state)
return obj
def inject_class(cls, hacked_cls):
for subclass in cls.__subclasses__():
if subclass is not hacked_cls:
subclass.__bases__ = (hacked_cls,)
import hashlib
def string_to_color(s: str) -> tuple:
hash_object = hashlib.md5(s.encode())
hashed = hash_object.hexdigest()
r = int(hashed[:2], 16)
g = int(hashed[2:4], 16)
b = int(hashed[4:6], 16)
blend_factor = 0.4
r = int(r + (255 - r) * blend_factor)
g = int(g + (255 - g) * blend_factor)
b = int(b + (255 - b) * blend_factor)
return (r, g, b)
# engine/rng.py
import engine.rng
@dataclass(frozen=True, kw_only=True)
class RngSystemBackupState:
random_state: tuple
frng_state: tuple
prng_state: tuple
class HackedRngSystem(engine.rng.RngSystem):
def backup(self) -> RngSystemBackupState:
return RngSystemBackupState(
random_state=random.getstate(),
frng_state=self.frng.getstate(),
prng_state=self.prng.getstate(),
)
def restore(self, state: RngSystemBackupState):
random.setstate(state.random_state)
self.frng.setstate(state.frng_state)
self.prng.setstate(state.prng_state)
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.rng.RngSystem, HackedRngSystem)
engine.rng.RngSystem = HackedRngSystem
# engine/walk_data.py
import engine.walk_data
class HackedWalkData(engine.walk_data.WalkData):
def backup(self) -> Properties:
return generic_backup(self, ('obj',))
def restore(self, state: Properties):
generic_restore(self, state)
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.walk_data.WalkData, HackedWalkData)
engine.walk_data.WalkData = HackedWalkData
# engine/generics.py
import engine.generics
@dataclass(frozen=True, kw_only=True)
class GenericObjectBackupState:
properties: Properties
walk_data: BackupOrNone
hashable_outline: tuple
class FakeHashableOutline(set):
string = None
def __str__(self):
assert self.string is not None
return self.string
class HackedGenericObject(engine.generics.GenericObject):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.game = None
self.tracer_label_outline = None
self.tracer_label = None
self.title_label = None
def backup(self) -> GenericObjectBackupState:
return GenericObjectBackupState(
properties=generic_backup(self, ignore_attrs=('game', 'hashable_outline')),
walk_data=backup_or_none(self.walk_data),
hashable_outline=copy.deepcopy(self.hashable_outline),
)
def restore(self, state: GenericObjectBackupState):
generic_restore(self, state.properties)
self.walk_data = restore_or_none(state.walk_data)
self.hashable_outline = state.hashable_outline
def _has_health(self):
# NOTE: only Enemys, Players, and destroyable weapons can take damage
# from projectiles. (This is because when CombatSystem is initialized it
# is passed with only the targets including the set of Enemy objects
match self.nametype:
case 'Enemy':
has_health = True
case 'Player':
has_health = True
case 'Weapon':
has_health = self.destroyable
case _:
has_health = False
return has_health
def draw(self):
super().draw()
self._draw()
has_drawn_title = False
# Draw a line and label to each object
draw_label_blocklist = ["Player", "Spike"]
if self.game is not None and self.game.item_tracer and not self.nametype in draw_label_blocklist:
dx = self.x - self.game.player.x
dy = self.y - self.game.player.y
distance_to_object = (dx**2 + dy**2)**0.5
adjusted_dx = dx / distance_to_object if distance_to_object else 0
adjusted_dy = dy / distance_to_object if distance_to_object else 0
label_distance = 400
if distance_to_object < label_distance:
label_distance = distance_to_object // 2
label_x = self.game.player.x + adjusted_dx * label_distance
label_y = self.game.player.y + adjusted_dy * label_distance
text_color = string_to_color(self.nametype)
arcade.draw_line(self.game.player.x, self.game.player.y, self.x, self.y, text_color)
if (abs(dx / self.game.gui.camera.scale) >= self.game.gui.camera.viewport_width // 2) or \
(abs(dy / self.game.gui.camera.scale) >= self.game.gui.camera.viewport_height // 2):
if self.tracer_label_outline is None:
self.tracer_label_outline = arcade.Text(self.name or self.nametype, 0, 0, arcade.color.BLACK, 11)
self.tracer_label = arcade.Text(self.name or self.nametype, 0, 0, text_color, 11)
self.tracer_label.x = label_x
self.tracer_label.y = label_y
self.tracer_label_outline.x = label_x + 1
self.tracer_label_outline.y = label_y - 1
self.tracer_label_outline.draw()
self.tracer_label.draw()
r = self.get_rect()
if self.title_label is None:
self.title_label = arcade.Text(self.name or self.nametype, 0, 0, text_color, 11, anchor_x='center', anchor_y='baseline',)
self.title_label.x = (r.x1() + r.x2()) // 2
self.title_label.y = r.y2() + 5
self.title_label.draw()
has_drawn_title = True
# Draw soul grenade trajectory
if self.game is not None and self.game.soul_tracer and self.game.current_mode.value == "platformer":
rad = abs((self.game.tics % constants.SWING_TICKS) / constants.SWING_TICKS * 2 - 1) * 0.5 * math.pi
cosrad = math.cos(rad)
sinrad = math.sin(rad)
x_speed, y_speed = cosrad * constants.SOUL_SPEED, sinrad * constants.SOUL_SPEED
player_w = self.game.player.get_width()
player_h = self.game.player.get_height()
offset = (player_w ** 2 + player_h ** 2) ** 0.5 // 2 + 20
offset_x, offset_y = cosrad * offset, sinrad * offset
if self.game.player.face_towards == "W":
offset_x = -offset_x
x_speed = -x_speed
offset_x += self.game.player.x + 10
offset_y += self.game.player.y + 10
arcade.draw_line(self.game.player.x, self.game.player.y, offset_x, offset_y, arcade.color.YELLOW)
# Draw line of sight for enemies
if self.nametype == 'Enemy':
if self.one_sided:
if self.sprite.flipped:
start_angle = 90.0
end_angle = 270.0
else:
start_angle = -90.0
end_angle = 90.0
else:
start_angle = 0.0
end_angle = 360.0
arcade.draw_arc_outline(self.x, self.y, self.sight * 2, self.sight * 2,
arcade.color.GREEN, start_angle, end_angle)
arcade.draw_arc_filled(self.x, self.y, self.sight * 2, self.sight * 2,
(0, 255, 0, 20), start_angle, end_angle)
# Draw healthbar
if self._has_health():
bar_width = self.max_health
bar_height = 10
bar_padding = 20 if has_drawn_title else 5
arcade.draw_xywh_rectangle_outline(self.get_leftmost_point() - bar_width // 2 + self.get_width() // 2,
self.get_highest_point() + bar_padding, bar_width, bar_height, arcade.color.GREEN)
arcade.draw_xywh_rectangle_filled(self.get_leftmost_point() - bar_width // 2 + self.get_width() // 2,
self.get_highest_point() + bar_padding, self.health, bar_height, arcade.color.GREEN)
@property
def hashable_outline(self):
return self.__dict__['hashable_outline']
@hashable_outline.setter
def hashable_outline(self, value):
if value is None:
self.__dict__['hashable_outline'] = None
return
fake = FakeHashableOutline(value)
fake.string = str(value)
self.__dict__['hashable_outline'] = fake
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.generics.GenericObject, HackedGenericObject)
engine.generics.GenericObject = HackedGenericObject
# components/enemy/enemy.py
import components.enemy.enemy
components.enemy.enemy.Enemy.sight = 400
components.enemy.enemy.Enemy.one_sided = True
components.enemy.enemy.StaticJellyfish.sight = 70 # This is the stinging radius
components.enemy.enemy.StaticJellyfish.one_sided = False
# components/player.py
import components.player
components.player.Player.max_health = 100
# components/weapon_systems/base.py
import components.weapon_systems.base
import components.logic
class HackedWeapon(components.weapon_systems.base.Weapon):
max_health = 100
# allow copy/deepcopy for Weapon
# __copy__ = None
__deepcopy__ = None
inject_class(components.weapon_systems.base.Weapon, HackedWeapon)
components.weapon_systems.base.Weapon = HackedWeapon
# components/danmaku.py
import components.danmaku
class FakeBulletIterator:
def __init__(self, bullet_obj, updater):
self.bullet_obj = bullet_obj
self.updater = updater
self.state = None
self.next = None
def __next__(self):
if self.next is not None:
if self.next.state is None:
self.bullet_obj.kill()
else:
generic_restore(self.bullet_obj, self.next.state)
return
next(self.updater)
if self.bullet_obj.fake_iterator is self: # the updater might change the iterator as well...
self.bullet_obj.fake_iterator = FakeBulletIterator(self.bullet_obj, self.updater)
self.next = self.bullet_obj.fake_iterator
self.updater = None
class HackedBullet(components.danmaku.Bullet):
fake_iterator = None
@property
def updater(self):
return self.fake_iterator
@updater.setter
def updater(self, value):
self.fake_iterator = FakeBulletIterator(self, value)
def backup(self):
state = generic_backup(self)
self.fake_iterator.state = state
return state
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(components.danmaku.Bullet, HackedBullet)
components.danmaku.Bullet = HackedBullet
# map_loading/tilemap.py
import map_loading.tilemap
@dataclass(frozen=True, kw_only=True)
class BasicTileMapBackupState:
properties: Properties
moving_platforms: tuple
class HackedBasicTileMap(map_loading.tilemap.BasicTileMap):
def backup(self) -> BasicTileMapBackupState:
return BasicTileMapBackupState(
properties=generic_backup(self, ('moving_platforms','texts','layers','static_objs','parsed_map', 'map_size')),
moving_platforms=tuple(
(platform, platform.backup())
for platform in self.moving_platforms
)
)
def restore(self, state: BasicTileMapBackupState):
generic_restore(self, state.properties)
self.moving_platforms.clear()
for platform, platform_state in state.moving_platforms:
platform.restore(platform_state)
self.moving_platforms.append(platform)
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(map_loading.tilemap.BasicTileMap, HackedBasicTileMap)
map_loading.tilemap.BasicTileMap = HackedBasicTileMap
# engine/logic.py
import engine.logic
@dataclass(frozen=True, kw_only=True)
class LogicEngineBackupState:
logic_map: tuple
logic_countdown: int
class HackedLogicEngine(engine.logic.LogicEngine):
def backup(self) -> LogicEngineBackupState:
return LogicEngineBackupState(
logic_map=tuple(
(key, value.backup())
for key, value in self.logic_map.items()
),
logic_countdown=self.logic_countdown,
)
def restore(self, state: LogicEngineBackupState):
self.logic_countdown = state.logic_countdown
for key, state in state.logic_map:
self.logic_map[key].restore(state)
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.logic.LogicEngine, HackedLogicEngine)
engine.logic.LogicEngine = HackedLogicEngine
# engine/physics.py
import engine.physics
class HackedPhysicsEngine(engine.physics.PhysicsEngine):
def backup(self) -> Properties:
return generic_backup(self, ('player',))
def restore(self, state: Properties):
generic_restore(self, state)
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.physics.PhysicsEngine, HackedPhysicsEngine)
engine.physics.PhysicsEngine = HackedPhysicsEngine
# engine/danmaku.py
import engine.danmaku
@dataclass(frozen=True, kw_only=True)
class DanmakuSystemBackupState:
properties: Properties
gui: tuple
bullets: tuple
player_bullets: tuple
class HackedDanmakuSystem(engine.danmaku.DanmakuSystem):
def backup(self) -> DanmakuSystemBackupState:
return DanmakuSystemBackupState(
properties=generic_backup(self, ignore_attrs=('gui', 'player', 'boss')),
gui=smart_dup(self.gui),
bullets=tuple((bullet, bullet.backup()) for bullet in self.bullets),
player_bullets=tuple((bullet, bullet.backup()) for bullet in self.player_bullets),
)
def restore(self, state: DanmakuSystemBackupState):
self.bullets.clear(deep=False)
self.player_bullets.clear(deep=False)
generic_restore(self, state.properties)
self.gui = dict(state.gui)
self.bullets.extend(generic_restore(bullet, state) for bullet, state in state.bullets)
self.player_bullets.extend(generic_restore(bullet, state) for bullet, state in state.player_bullets)
def draw(self):
super().draw()
self._draw()
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.danmaku.DanmakuSystem, HackedDanmakuSystem)
engine.danmaku.DanmakuSystem = HackedDanmakuSystem
# components/portal.py
import components.portal
OriginalPortal = components.portal.Portal
class HackedPortal(components.portal.Portal):
def draw(self):
super().draw()
# The Portal object is missing the super call so we manually patch it here
super(OriginalPortal, self).draw()
inject_class(components.portal.Portal, HackedPortal)
components.portal.Portal = HackedPortal
# engine/grenade.py
import engine.grenade
@dataclass(frozen=True, kw_only=True)
class GrenadeSystemBackupState:
properties: Properties
grenades: tuple
class HackedGrenadeSystem(engine.grenade.GrenadeSystem):
def backup(self) -> GrenadeSystemBackupState:
return GrenadeSystemBackupState(
properties=generic_backup(self, ('game',)),
grenades=tuple(grenade.backup() for grenade in self.grenades),
)
def restore(self, state: GrenadeSystemBackupState):
generic_restore(self, state.properties)
for grenade, grenade_state in zip(self.grenades, state.grenades):
grenade.restore(grenade_state)
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.grenade.GrenadeSystem, HackedGrenadeSystem)
engine.grenade.GrenadeSystem = HackedGrenadeSystem
# engine/map_switcher.py
import engine.map_switcher
# engine/combat.py
import engine.combat
@dataclass(frozen=True, kw_only=True)
class CombatSystemBackupState:
properties: Properties
active_projectiles: tuple
class HackedCombatSystem(engine.combat.CombatSystem):
def backup(self) -> CombatSystemBackupState:
return CombatSystemBackupState(
properties=generic_backup(self, ('game', 'active_projectiles')),
active_projectiles=tuple((projectile, projectile.backup()) for projectile in self.active_projectiles),
)
def restore(self, state: CombatSystemBackupState):
generic_restore(self, state.properties)
self.active_projectiles.clear()
for projectile, projectile_state in state.active_projectiles:
projectile.restore(projectile_state)
self.active_projectiles.append(projectile)
# disable copy/deepcopy
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
inject_class(engine.combat.CombatSystem, HackedCombatSystem)
engine.combat.CombatSystem = HackedCombatSystem
# components/textbox.py
import components.textbox
# ludicer.py
import ludicer
@dataclass(frozen=True, kw_only=True)
class LudicerBackupState:
properties: Properties
map_switch: engine.map_switcher.MapSwitch
objects: tuple[GenericObjectBackupState]
items: tuple
textbox: components.textbox.Textbox
rng_system: RngSystemBackupState
player: BackupOrNone
tiled_map: BackupOrNone
combat_system: BackupOrNone
physics_engine: BackupOrNone
logic_engine: BackupOrNone
danmaku_system: BackupOrNone
grenade_system: BackupOrNone
boss: any
sent_game_info: dict
class FakeNet:
def __init__(self):
self.msg = None
def send_one(self, msg):
self.msg = msg
class HackedLudicer(ludicer.Ludicer):
objects: tuple[HackedGenericObject]
__last_sent = None
inverted_controls = False
real_time = True
simulating = False
item_tracer = False
soul_tracer = False
def backup(self) -> LudicerBackupState:
return LudicerBackupState(
properties=tuple(
(key, getattr(self, key))
for key in (
'tics',
'player_last_base_position',
'current_map',
'scene',
'prerender',
'current_mode',
'state_hash',
'cheating_detected',
'won',
'prev_display_inventory',
'display_inventory',
'save_cooldown',
'save_cooldown_timer',
'rand_seed',
)
) + tuple(
(key, copy.copy(getattr(self, key)))
for key in (
'unlocked_doors',
'newly_pressed_keys',
'prev_pressed_keys',
'pressed_keys',
'objects',
'static_objs',
)
),
map_switch=copy.copy(self.map_switch),
objects=tuple(o.backup() for o in self.objects),
items=tuple(self.items),
tiled_map=backup_or_none(self.tiled_map),
textbox=copy.copy(self.textbox),
rng_system=self.rng_system.backup(),
player=backup_or_none(self.player),
combat_system=backup_or_none(self.combat_system),
physics_engine=backup_or_none(self.physics_engine),
logic_engine=backup_or_none(self.logic_engine),
danmaku_system=backup_or_none(self.danmaku_system),
grenade_system=backup_or_none(self.grenade_system),
boss=self.boss,
sent_game_info=self.__last_sent,
)
def dump_items(self, *args, **kwargs):
_G_WINDOW.console_add_msg('save_state written')
super().dump_items(*args, **kwargs)
def restore(self, state: LudicerBackupState):
generic_restore(self, state.properties)
for o, s in zip(self.objects, state.objects):
o.restore(s)
self.boss = state.boss
self.items = list(state.items)
self.rng_system.restore(state.rng_system)
self.map_switch = state.map_switch
self.textbox = state.textbox
self.tiled_map = restore_or_none(state.tiled_map)
self.player = restore_or_none(state.player)
self.combat_system = restore_or_none(state.combat_system)
self.physics_engine = restore_or_none(state.physics_engine)
self.logic_engine = restore_or_none(state.logic_engine)
self.danmaku_system = restore_or_none(state.danmaku_system)
self.grenade_system = restore_or_none(state.grenade_system)
self.__last_sent = None
def setup(self):
super().setup()
# For some reason the game is not set for the weapons,
# unlike other objects so we need to manually set them here so
# we can draw the traces from the player
for o in list(self.combat_system.weapons):
o.game = self
# The `generic_platform` are walls, which are too many.
# The `zone`s are not interesting so skipping it too.
for o in list(self.static_objs):
if o.name != "generic_platform" and not "zone" in o.name:
o.game = self
def send_game_info(self):
if self.real_time and not self.simulating:
super().send_game_info()
else:
net = self.net
self.net = FakeNet()
super().send_game_info()
self.__last_sent = self.net.msg
self.net = net
@property
def raw_pressed_keys(self):
raw_pressed_keys = self.__dict__['raw_pressed_keys']
if not self.real_time and not self.simulating and self.inverted_controls:
raw_pressed_keys_copy = raw_pressed_keys.copy()
inverted_sets = [
(vk.VK_MOVE_UP[1], vk.VK_MOVE_DOWN[1]),
(vk.VK_MOVE_LEFT[1], vk.VK_MOVE_RIGHT[1]),
]
dir_pressed = {}
for (k1, k2) in inverted_sets:
if k1 in raw_pressed_keys:
if k2 not in raw_pressed_keys:
raw_pressed_keys.remove(k1)
raw_pressed_keys.add(k2)
elif k2 in raw_pressed_keys:
raw_pressed_keys.remove(k2)
raw_pressed_keys.add(k1)
return frozenset(raw_pressed_keys)
@raw_pressed_keys.setter
def raw_pressed_keys(self, value):
self.__dict__['raw_pressed_keys'] = value
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, _):
raise NotImplementedError
ludicer.Ludicer = HackedLudicer
# ludicer_gui.py
import ludicer_gui
ludicer_gui.SCREEN_TITLE += " [Blue Water 💦]"
REFRESH_RATE_DELTA = (120 - 60) / 10
SPEED_DIAL = [.1, .2, .5, 1.0, 1.5, 2.0, 4.0, 10.0]
def get_update_rate(ind):
ind = max(0, min(len(SPEED_DIAL) - 1, ind))
speed = SPEED_DIAL[ind]
return 1 / (60 * speed)
global _G_WINDOW
class HackedHackceler8(ludicer_gui.Hackceler8):
game: HackedLudicer
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__history = []
self.__history_index = -1 # location of the current state in history
self.__speed_dial = 4
self.__key_pressed = set()
self.__mouse = (0, 0)
self.__free_camera = False
self.__last_path_find = []
# self.on_click_start(None)
self.__console = False
self.__console_cmd_buf = ''
self.__console_msgs = []
self.__console_commands = {
'help': self.cmd_help,
'dumplogic': self.cmd_logic,
}
# silly :-)
global _G_WINDOW
_G_WINDOW = self
def start_game(self):
super().start_game()
self.game.gui = self
def append_history(self, state):
self.__history_index += 1
self.__history = self.__history[:self.__history_index]
self.__history.append(state)
def restore_history(self, forward):
if forward:
if self.__history_index + 1 >= len(self.__history):
return
self.__history_index += 1
else:
if self.__history_index <= 0:
return
self.__history_index -= 1
if self.__history[self.__history_index] is None:
# seek
self.__history_index += 1 if forward else -1
while 0 <= self.__history_index < len(self.__history):
state = self.__history[self.__history_index]
self.__history_index += 1 if forward else -1
if state is None:
break
else:
raise RuntimeError('seek failed')
if 0 <= self.__history_index < len(self.__history):
self.game.restore(self.__history[self.__history_index])
def sim_should_run(self):
return self.game is not None and len(self.game.raw_pressed_keys) > 0
def window_to_game_coord(self, x, y):
half_width = self.camera.viewport_width / 2
half_height = self.camera.viewport_height / 2
actual_x = (x - half_width) * self.camera.scale + half_width + self.camera.goal_position.x
actual_y = (y - half_height) * self.camera.scale + half_height + self.camera.goal_position.y
return actual_x, actual_y
def extra_draw(self):
if self.game.real_time:
text = 'REALTIME'
else:
text = f'SIM ({self.__history_index + 1}/{len(self.__history)}) '
if vk.VK_UNDO_FRAME[1] in self.__key_pressed and self.__history_index > 0:
text += 'UNDOING'
elif vk.VK_REDO_FRAME[1] in self.__key_pressed and self.__history_index < len(self.__history) - 1:
text += 'REDOING'
elif self.sim_should_run():
text += 'RUNNING'
else:
text += 'PAUSED'
arcade.draw_text(
text + ' (%.1fx)' % (SPEED_DIAL[self.__speed_dial]),
self.camera.viewport_width,
0,
arcade.csscolor.WHITE,
18,
anchor_x='right',
anchor_y='bottom',
)
if self.game and self.game.player and self.game.player.get_height() // 2 == 15:
arcade.draw_text(
"sticky triggered",
self.camera.viewport_width / 2,
self.camera.viewport_height,
arcade.csscolor.BLUE,
16,
anchor_x='center',
anchor_y='top',
)
x, y = self.window_to_game_coord(*self.__mouse)
arcade.draw_text(
f'({x:.1f}, {y:.1f})',
self.__mouse[0],
self.__mouse[1],
arcade.csscolor.WHITE,
12,
)
console_fontsize = 14
lines = [(l,arcade.csscolor.WHITE) for l in self.__console_msgs[-5:]]
if self.__console:
lines.append(('>' + self.__console_cmd_buf, arcade.csscolor.WHITE))
for cmdname in self.__console_commands:
if cmdname.startswith(self.__console_cmd_buf):
lines.append(('>' + cmdname, arcade.csscolor.DIM_GRAY))
for i,(line,color) in enumerate(lines):
arcade.draw_text(
line,
1,
self.camera.viewport_height-i*(console_fontsize*1.15)-1,
arcade.csscolor.BLACK,
console_fontsize,
anchor_x='left',
anchor_y='top',
)
arcade.draw_text(
line,
0,
self.camera.viewport_height-i*(console_fontsize*1.15),
color,
console_fontsize,
anchor_x='left',
anchor_y='top',
)
self.camera.use()
for x, y in self.__last_path_find:
arcade.draw_circle_filled(x, y, 1, arcade.csscolor.BLUE)
if 'visited' in self.game.__dict__:
for x,y,vx,vy in self.game.visited:
arcade.draw_circle_filled(x, y, 1, arcade.csscolor.GREEN)
self.gui_camera.use()
def change_refresh_rate(self, delta):
self.__speed_dial = max(0, min(len(SPEED_DIAL) - 1, self.__speed_dial + delta))
self.set_update_rate(get_update_rate(self.__speed_dial))
def actual_tick(self):
if self.game.player is None:
return self.game.tick()
if 0 <= self.__history_index < len(self.__history):
state = self.__history[self.__history_index]
else:
state = self.game.backup()
self.game.inverted_controls = self.game.player.inverted_controls
self.game.tick()
if self.game.inverted_controls != self.game.player.inverted_controls:
# inverted controls changed, rerun the tick
self.game.inverted_controls = self.game.player.inverted_controls
self.game.restore(state)
self.game.tick()
def on_update(self, _delta_time: float):
if self.game is None:
return
if self.game.real_time:
return super().on_update(_delta_time)
if vk.VK_UNDO_FRAME[1] in self.__key_pressed:
self.restore_history(forward=False)
if vk.VK_REDO_FRAME[1] in self.__key_pressed:
self.restore_history(forward=True)