freenukum 0.3.5

A clone of the 1991 DOS game Duke Nukem 1
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
mod accesscard_door;
mod accesscard_slot;
mod acme;
mod balloon;
mod bomb;
mod camera;
mod conveyor;
mod door;
mod electric_arc;
mod elevator;
mod exitdoor;
mod expandingfloor;
mod fan;
mod fire;
mod firewheelbot;
mod glove_slot;
mod hostileshot;
mod item;
mod key;
mod keyhole;
mod mill;
mod notebook;
mod particle;
mod placeholder;
mod redball_jumping;
mod redball_lying;
mod robot;
mod rocket;
mod score;
mod shootable_wall;
mod simpleanimation;
mod singleanimation;
mod soda_flying;
mod spikes;
mod surveillancescreen;
mod tankbot;
mod teleporter;
mod unstablefloor;
mod wallcrawler;

use crate::{
    geometry::RectExt,
    hero::HeroData,
    infobox::InfoMessageQueue,
    level::{solids::LevelSolids, tiles::LevelTiles, PlayState},
    rendering::Renderer,
    Result,
};
use sdl2::rect::{Point, Rect};

#[derive(Debug)]
pub struct ActorsList {
    actors: Vec<Actor>,
    interaction_target: Option<usize>,
}

impl Default for ActorsList {
    fn default() -> Self {
        Self::new()
    }
}

impl ActorsList {
    pub fn new() -> Self {
        ActorsList {
            actors: Vec::new(),
            interaction_target: None,
        }
    }

    pub fn count(&self) -> usize {
        self.actors.len()
    }

    pub fn get_mut(&mut self, index: usize) -> Option<&mut Actor> {
        self.actors.get_mut(index)
    }

    pub fn remove_dead(&mut self) {
        let mut i = 0;
        while i < self.actors.len() {
            match self.actors.get(i) {
                Some(a) if !a.general.is_alive => {
                    self.actors.remove(i);
                    self.interaction_target = match self.interaction_target
                    {
                        Some(index) if index == i => None,
                        Some(index) if index > i => Some(index - 1),
                        Some(index) => Some(index),
                        None => None,
                    };
                }
                _ => {
                    i += 1;
                }
            }
        }
    }

    pub fn send_message(
        &mut self,
        receivers: ActorType,
        message: ActorMessageType,
        hero_data: &mut HeroData,
        solids: &mut LevelSolids,
    ) {
        for actor in self
            .actors
            .iter_mut()
            .filter(|a| a.general.actor_type == receivers)
        {
            let p = ReceiveMessageParameters {
                general: &mut actor.general,
                message,
                hero_data,
                solids,
            };
            actor.specific.receive_message(p);
        }
    }

    pub fn process_shot(
        &mut self,
        shot_position: Rect,
        solids: &mut LevelSolids,
        tiles: &mut LevelTiles,
        actor_adder: &mut dyn ActorAdder,
        hero_data: &mut HeroData,
        actor_message_queue: &mut ActorMessageQueue,
    ) -> bool {
        for actor in self.actors.iter_mut() {
            if actor.can_get_shot()
                && shot_position.touches(actor.position())
                && actor.shot(
                    solids,
                    tiles,
                    actor_adder,
                    hero_data,
                    actor_message_queue,
                ) == ShotProcessing::Absorb
            {
                return true;
            }
        }
        false
    }

    pub fn start_interaction(
        &mut self,
        play_state: &mut PlayState,
        hero_data: &mut HeroData,
        info_message_queue: &mut InfoMessageQueue,
        actor_message_queue: &mut ActorMessageQueue,
    ) {
        let new_interactor = self.actors.iter().position(|actor| {
            actor.hero_can_interact(hero_data)
                && hero_data
                    .position
                    .geometry
                    .touches(actor.general.position)
        });

        if let Some(i) = new_interactor {
            self.end_interaction(play_state, hero_data);

            let actor = self.actors.get_mut(i).unwrap();
            let p = HeroInteractStartParameters {
                general: &mut actor.general,
                play_state,
                hero_data,
                info_message_queue,
                actor_message_queue,
            };
            self.interaction_target = Some(i);
            actor.specific.hero_interact_start(p);
        }
    }

    pub fn end_interaction(
        &mut self,
        play_state: &mut PlayState,
        hero_data: &mut HeroData,
    ) {
        if let Some(i) = self.interaction_target.take() {
            if let Some(actor) = self.actors.get_mut(i) {
                let p = HeroInteractEndParameters {
                    general: &mut actor.general,
                    play_state,
                    hero_data,
                };
                actor.specific.hero_interact_end(p);
            }
        }
    }

    pub fn act(
        &mut self,
        solids: &mut LevelSolids,
        tiles: &mut LevelTiles,
        hero_data: &mut HeroData,
        actor_queue: &mut ActorQueue,
        play_state: &mut PlayState,
    ) {
        let mut actors_hurting_hero = 0usize;
        for actor in self.actors.iter_mut() {
            if actor.general.acts_while_invisible
                || actor.general.is_visible
            {
                actor.act(
                    solids,
                    tiles,
                    hero_data,
                    actor_queue,
                    play_state,
                );
                if actor.general.is_alive && actor.general.hurts_hero {
                    actors_hurting_hero += 1;
                }
            }
        }

        let mut adder = LevelActorAdder {
            solids,
            tiles,
            actors: self,
        };

        actor_queue.process(&mut adder);
        self.remove_dead();
        hero_data.gets_hurt = actors_hurting_hero > 0;
    }

    pub fn update_visibility(&mut self, visible_rect: Rect) {
        self.actors.iter_mut().for_each(|actor| {
            actor.general.is_visible =
                actor.general.position.has_intersection(visible_rect);
        });
    }

    pub fn render_background_actors(
        &mut self,
        renderer: &mut dyn Renderer,
        draw_collision_bounds: bool,
    ) -> Result<()> {
        Ok(self
            .actors
            .iter_mut()
            .filter(|actor| {
                actor.general.is_visible && !actor.general.is_in_foreground
            })
            .map(|actor| actor.render(renderer, draw_collision_bounds))
            .collect::<Result<_>>()?)
    }

    pub fn render_foreground_actors(
        &mut self,
        renderer: &mut dyn Renderer,
        draw_collision_bounds: bool,
    ) -> Result<()> {
        Ok(self
            .actors
            .iter_mut()
            .filter(|actor| {
                actor.general.is_visible && actor.general.is_in_foreground
            })
            .map(|actor| actor.render(renderer, draw_collision_bounds))
            .collect::<Result<_>>()?)
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ShotProcessing {
    Absorb,
    Ignore,
}

#[derive(Debug)]
pub struct Actor {
    pub(crate) general: ActorData,
    pub(crate) specific: Box<dyn ActorInterface>,
}

impl Actor {
    fn act(
        &mut self,
        solids: &mut LevelSolids,
        tiles: &mut LevelTiles,
        hero_data: &mut HeroData,
        actor_adder: &mut dyn ActorAdder,
        play_state: &mut PlayState,
    ) -> bool {
        self.check_hero_touch(hero_data, actor_adder);

        let p = ActParameters {
            general: &mut self.general,
            solids,
            tiles,
            hero_data,
            actor_adder,
            play_state,
        };
        self.specific.act(p);
        self.general.is_alive
    }

    fn check_hero_touch(
        &mut self,
        hero_data: &mut HeroData,
        actor_adder: &mut dyn ActorAdder,
    ) {
        let touching_hero = self
            .general
            .position
            .has_intersection(hero_data.position.geometry);

        if touching_hero {
            if !self.general.touches_hero {
                self.general.touches_hero = true;

                let p = HeroTouchStartParameters {
                    general: &mut self.general,
                    hero_data,
                    actor_adder,
                };

                self.specific.hero_touch_start(p);
            }
        } else if self.general.touches_hero {
            self.general.touches_hero = false;

            let p = HeroTouchEndParameters {
                general: &mut self.general,
                hero_data,
            };
            self.specific.hero_touch_end(p);
        }
    }

    fn hero_can_interact(&self, hero_data: &HeroData) -> bool {
        if self.general.actor_type == ActorType::Lift {
            /* This check needs to be done for elevator only because
             * if there are two elevators next to each other, the mostleft
             * elevator would be chosen for interaction instead of the one on
             * which the hero stands.
             */
            // TODO: this should be moved into ActorInterface::hero_can_interact
            self.general.position.x == hero_data.position.geometry.x
        } else {
            self.specific.hero_can_interact()
        }
    }

    pub fn can_get_shot(&self) -> bool {
        self.specific.can_get_shot(&self.general)
    }

    pub fn shot(
        &mut self,
        solids: &mut LevelSolids,
        tiles: &mut LevelTiles,
        actor_adder: &mut dyn ActorAdder,
        hero_data: &mut HeroData,
        actor_message_queue: &mut ActorMessageQueue,
    ) -> ShotProcessing {
        let p = ShotParameters {
            general: &mut self.general,
            solids,
            tiles,
            actor_adder,
            hero_data,
            actor_message_queue,
        };
        self.specific.shot(p)
    }

    pub fn is_alive(&self) -> bool {
        self.general.is_alive
    }

    pub fn position(&self) -> Rect {
        self.general.position
    }

    pub fn render(
        &mut self,
        renderer: &mut dyn Renderer,
        draw_collision_bounds: bool,
    ) -> Result<()> {
        let p = RenderParameters {
            general: &mut self.general,
            renderer,
        };
        self.specific.render(p)?;

        if draw_collision_bounds {
            let color = crate::collision_bounds_color();
            renderer.draw_rect(self.general.position, color)?;
        }
        Ok(())
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ActorType {
    FireWheelBot,
    FlameGnomeBot,
    FlyingBot,
    FootBot,
    HelicopterBot,
    RabbitoidBot,
    RedBallJumping,
    RedBallLying,
    Robot,
    RobotDisappearing,
    SnakeBot,
    TankBot,
    WallCrawlerBotLeft,
    WallCrawlerBotRight,
    DrProton,
    Camera,
    Explosion,
    DustCloud,
    Steam,
    ParticlePink,
    ParticleBlue,
    ParticleWhite,
    ParticleGreen,
    Rocket,
    Bomb,
    BombFire,
    Water,
    ExitDoor,
    Notebook,
    SurveillanceScreen,
    HostileShotLeft,
    HostileShotRight,
    Soda,
    SodaFlying,
    UnstableFloor,
    ExpandingFloor,
    ConveyorLeftMovingRightEnd,
    ConveyorRightMovingRightEnd,
    FanLeft,
    FanRight,
    BrokenWallBackground,
    StoneBackground,
    Teleporter1,
    Teleporter2,
    FenceBackground,
    StoneWindowBackground,
    WindowLeftBackground,
    WindowRightBackground,
    Screen,
    BoxGreyEmpty,
    BoxGreyBoots,
    Boots,
    BoxGreyClamps,
    Clamps,
    BoxGreyGun,
    Gun,
    BoxGreyBomb,
    BoxRedSoda,
    BoxRedChicken,
    ChickenSingle,
    ChickenDouble,
    BoxBlueFootball,
    Football,
    Flag,
    BoxBlueJoystick,
    Joystick,
    BoxBlueDisk,
    Disk,
    BoxBlueBalloon,
    Balloon,
    BoxGreyGlove,
    Glove,
    BoxGreyFullLife,
    FullLife,
    BoxBlueFlag,
    BlueFlag,
    BoxBlueRadio,
    Radio,
    BoxGreyAccessCard,
    AccessCard,
    BoxGreyLetterD,
    LetterD,
    BoxGreyLetterU,
    LetterU,
    BoxGreyLetterK,
    LetterK,
    BoxGreyLetterE,
    LetterE,
    AccessCardSlot,
    GloveSlot,
    KeyRed,
    KeyholeRed,
    DoorRed,
    KeyBlue,
    KeyholeBlue,
    DoorBlue,
    KeyPink,
    KeyholePink,
    DoorPink,
    KeyGreen,
    KeyholeGreen,
    DoorGreen,
    ShootableWall,
    Lift,
    Acme,
    FireRight,
    FireLeft,
    Mill,
    ElectricArc,
    AccessCardDoor,
    SpikesUp,
    SpikesDown,
    Spike,
    Score100,
    Score200,
    Score500,
    Score1000,
    Score2000,
    Score5000,
    Score10000,
    ScoreBonus1Left,
    ScoreBonus1Right,
    ScoreBonus2Left,
    ScoreBonus2Right,
    ScoreBonus3Left,
    ScoreBonus3Right,
    ScoreBonus4Left,
    ScoreBonus4Right,
    ScoreBonus5Left,
    ScoreBonus5Right,
    ScoreBonus6Left,
    ScoreBonus6Right,
    ScoreBonus7Left,
    ScoreBonus7Right,
    BlueLightBackground1,
    BlueLightBackground2,
    BlueLightBackground3,
    BlueLightBackground4,
    TextOnScreenBackground,
    HighVoltageFlashBackground,
    RedFlashlightBackground,
    BlueFlashlightBackground,
    KeypanelBackground,
    RedRotationLightBackground,
    UpArrowBackground,
    GreenPoisonBackground,
    LavaBackground,
}

impl ActorType {
    pub(crate) fn create_actor_interface(
        &self,
        g: &mut ActorData,
        s: &mut LevelSolids,
        t: &mut LevelTiles,
    ) -> Box<dyn ActorInterface> {
        match self {
            ActorType::FireWheelBot => {
                firewheelbot::Specific::create_boxed(g, s, t)
            }
            ActorType::FlameGnomeBot => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::FlyingBot => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::FootBot => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::HelicopterBot => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::RabbitoidBot => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::RedBallJumping => {
                redball_jumping::Specific::create_boxed(g, s, t)
            }
            ActorType::RedBallLying => {
                redball_lying::Specific::create_boxed(g, s, t)
            }
            ActorType::Robot => robot::Specific::create_boxed(g, s, t),
            ActorType::RobotDisappearing => {
                singleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::SnakeBot => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::TankBot => tankbot::Specific::create_boxed(g, s, t),
            ActorType::WallCrawlerBotLeft => {
                wallcrawler::Specific::create_boxed(g, s, t)
            }
            ActorType::WallCrawlerBotRight => {
                wallcrawler::Specific::create_boxed(g, s, t)
            }
            ActorType::DrProton => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::Camera => camera::Specific::create_boxed(g, s, t),
            ActorType::Explosion => {
                singleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::DustCloud => {
                singleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::Steam => {
                singleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::ParticlePink => {
                particle::Specific::create_boxed(g, s, t)
            }
            ActorType::ParticleBlue => {
                particle::Specific::create_boxed(g, s, t)
            }
            ActorType::ParticleWhite => {
                particle::Specific::create_boxed(g, s, t)
            }
            ActorType::ParticleGreen => {
                particle::Specific::create_boxed(g, s, t)
            }
            ActorType::Rocket => rocket::Specific::create_boxed(g, s, t),
            ActorType::Bomb => bomb::Specific::create_boxed(g, s, t),
            ActorType::BombFire => {
                singleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::Water => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::ExitDoor => {
                exitdoor::Specific::create_boxed(g, s, t)
            }
            ActorType::Notebook => {
                notebook::Specific::create_boxed(g, s, t)
            }
            ActorType::SurveillanceScreen => {
                surveillancescreen::Specific::create_boxed(g, s, t)
            }
            ActorType::HostileShotLeft => {
                hostileshot::Specific::create_boxed(g, s, t)
            }
            ActorType::HostileShotRight => {
                hostileshot::Specific::create_boxed(g, s, t)
            }
            ActorType::Soda => item::Specific::create_boxed(g, s, t),
            ActorType::SodaFlying => {
                soda_flying::Specific::create_boxed(g, s, t)
            }
            ActorType::UnstableFloor => {
                unstablefloor::Specific::create_boxed(g, s, t)
            }
            ActorType::ExpandingFloor => {
                expandingfloor::Specific::create_boxed(g, s, t)
            }
            ActorType::ConveyorLeftMovingRightEnd => {
                conveyor::Specific::create_boxed(g, s, t)
            }
            ActorType::ConveyorRightMovingRightEnd => {
                conveyor::Specific::create_boxed(g, s, t)
            }
            ActorType::FanLeft => fan::Specific::create_boxed(g, s, t),
            ActorType::FanRight => fan::Specific::create_boxed(g, s, t),
            ActorType::BrokenWallBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::StoneBackground => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::Teleporter1 => {
                teleporter::Specific::create_boxed(g, s, t)
            }
            ActorType::Teleporter2 => {
                teleporter::Specific::create_boxed(g, s, t)
            }
            ActorType::FenceBackground => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::StoneWindowBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::WindowLeftBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::WindowRightBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::Screen => {
                placeholder::Specific::create_boxed(g, s, t)
            }
            ActorType::BoxGreyEmpty => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::BoxGreyBoots => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Boots => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyClamps => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Clamps => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyGun => item::Specific::create_boxed(g, s, t),
            ActorType::Gun => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyBomb => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::BoxRedSoda => item::Specific::create_boxed(g, s, t),
            ActorType::BoxRedChicken => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::ChickenSingle => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::ChickenDouble => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::BoxBlueFootball => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Football => item::Specific::create_boxed(g, s, t),
            ActorType::Flag => item::Specific::create_boxed(g, s, t),
            ActorType::BoxBlueJoystick => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Joystick => item::Specific::create_boxed(g, s, t),
            ActorType::BoxBlueDisk => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Disk => item::Specific::create_boxed(g, s, t),
            ActorType::BoxBlueBalloon => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Balloon => balloon::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyGlove => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Glove => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyFullLife => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::FullLife => item::Specific::create_boxed(g, s, t),
            ActorType::BoxBlueFlag => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::BlueFlag => item::Specific::create_boxed(g, s, t),
            ActorType::BoxBlueRadio => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::Radio => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyAccessCard => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::AccessCard => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyLetterD => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::LetterD => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyLetterU => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::LetterU => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyLetterK => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::LetterK => item::Specific::create_boxed(g, s, t),
            ActorType::BoxGreyLetterE => {
                item::Specific::create_boxed(g, s, t)
            }
            ActorType::LetterE => item::Specific::create_boxed(g, s, t),
            ActorType::AccessCardSlot => {
                accesscard_slot::Specific::create_boxed(g, s, t)
            }
            ActorType::GloveSlot => {
                glove_slot::Specific::create_boxed(g, s, t)
            }
            ActorType::KeyRed => key::Specific::create_boxed(g, s, t),
            ActorType::KeyholeRed => {
                keyhole::Specific::create_boxed(g, s, t)
            }
            ActorType::DoorRed => door::Specific::create_boxed(g, s, t),
            ActorType::KeyBlue => key::Specific::create_boxed(g, s, t),
            ActorType::KeyholeBlue => {
                keyhole::Specific::create_boxed(g, s, t)
            }
            ActorType::DoorBlue => door::Specific::create_boxed(g, s, t),
            ActorType::KeyPink => key::Specific::create_boxed(g, s, t),
            ActorType::KeyholePink => {
                keyhole::Specific::create_boxed(g, s, t)
            }
            ActorType::DoorPink => door::Specific::create_boxed(g, s, t),
            ActorType::KeyGreen => key::Specific::create_boxed(g, s, t),
            ActorType::KeyholeGreen => {
                keyhole::Specific::create_boxed(g, s, t)
            }
            ActorType::DoorGreen => door::Specific::create_boxed(g, s, t),
            ActorType::ShootableWall => {
                shootable_wall::Specific::create_boxed(g, s, t)
            }
            ActorType::Lift => elevator::Specific::create_boxed(g, s, t),
            ActorType::Acme => acme::Specific::create_boxed(g, s, t),
            ActorType::FireRight => fire::Specific::create_boxed(g, s, t),
            ActorType::FireLeft => fire::Specific::create_boxed(g, s, t),
            ActorType::Mill => mill::Specific::create_boxed(g, s, t),
            ActorType::ElectricArc => {
                electric_arc::Specific::create_boxed(g, s, t)
            }
            ActorType::AccessCardDoor => {
                accesscard_door::Specific::create_boxed(g, s, t)
            }
            ActorType::SpikesUp => spikes::Specific::create_boxed(g, s, t),
            ActorType::SpikesDown => {
                spikes::Specific::create_boxed(g, s, t)
            }
            ActorType::Spike => spikes::Specific::create_boxed(g, s, t),
            ActorType::Score100 => score::Specific::create_boxed(g, s, t),
            ActorType::Score200 => score::Specific::create_boxed(g, s, t),
            ActorType::Score500 => score::Specific::create_boxed(g, s, t),
            ActorType::Score1000 => score::Specific::create_boxed(g, s, t),
            ActorType::Score2000 => score::Specific::create_boxed(g, s, t),
            ActorType::Score5000 => score::Specific::create_boxed(g, s, t),
            ActorType::Score10000 => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus1Left => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus1Right => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus2Left => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus2Right => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus3Left => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus3Right => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus4Left => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus4Right => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus5Left => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus5Right => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus6Left => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus6Right => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus7Left => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::ScoreBonus7Right => {
                score::Specific::create_boxed(g, s, t)
            }
            ActorType::BlueLightBackground1 => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::BlueLightBackground2 => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::BlueLightBackground3 => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::BlueLightBackground4 => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::TextOnScreenBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::HighVoltageFlashBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::RedFlashlightBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::BlueFlashlightBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::KeypanelBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::RedRotationLightBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::UpArrowBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::GreenPoisonBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
            ActorType::LavaBackground => {
                simpleanimation::Specific::create_boxed(g, s, t)
            }
        }
    }
}

#[derive(Debug)]
pub struct ActorData {
    pub actor_type: ActorType,
    pub position: Rect,
    pub is_in_foreground: bool,
    pub hurts_hero: bool,
    pub is_alive: bool,
    pub touches_hero: bool,
    pub is_visible: bool,
    pub acts_while_invisible: bool,
}

impl ActorData {
    pub fn new(actor_type: ActorType) -> Self {
        ActorData {
            actor_type,
            position: Rect::new(0, 0, 0, 0),
            is_in_foreground: true,
            hurts_hero: false,
            is_alive: true,
            touches_hero: false,
            is_visible: false,
            acts_while_invisible: false,
        }
    }
}

pub struct ActorQueueItem {
    pub actor_type: ActorType,
    pub pos: Point,
}

pub trait ActorAdder {
    fn add_actor(&mut self, actor_type: ActorType, pos: Point);

    fn add_particle_firework(&mut self, pos: Point, count: usize) {
        for i in 0..count {
            let actor_type = match i % 4 {
                0 => ActorType::ParticlePink,
                1 => ActorType::ParticleBlue,
                2 => ActorType::ParticleWhite,
                3 => ActorType::ParticleGreen,
                _ => unreachable!(),
            };
            self.add_actor(actor_type, pos);
        }
    }
}

#[derive(Default)]
pub struct ActorQueue {
    pub actors: Vec<ActorQueueItem>,
}

impl ActorAdder for ActorQueue {
    fn add_actor(&mut self, actor_type: ActorType, pos: Point) {
        self.push_back(actor_type, pos);
    }
}

impl ActorQueue {
    pub fn new() -> Self {
        Self { actors: Vec::new() }
    }

    pub fn push_back(&mut self, actor_type: ActorType, pos: Point) {
        self.actors.push(ActorQueueItem { actor_type, pos });
    }

    pub(crate) fn process(&mut self, destination: &mut dyn ActorAdder) {
        for ActorQueueItem { actor_type, pos } in self.actors.drain(..) {
            destination.add_actor(actor_type, pos);
        }
    }
}

pub struct LevelActorAdder<'a> {
    pub solids: &'a mut LevelSolids,
    pub tiles: &'a mut LevelTiles,
    pub actors: &'a mut ActorsList,
}
impl<'a> ActorAdder for LevelActorAdder<'a> {
    fn add_actor(&mut self, actor_type: ActorType, pos: Point) {
        let mut general = ActorData::new(actor_type);
        general.position.reposition(pos);
        let specific = actor_type.create_actor_interface(
            &mut general,
            self.solids,
            self.tiles,
        );
        self.actors.actors.push(Actor { general, specific });
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ActorMessageType {
    OpenDoor,
    Teleport,
    Expand,
    Remove,
}

pub struct ActorMessage {
    pub receivers: ActorType,
    pub message: ActorMessageType,
}

#[derive(Default)]
pub struct ActorMessageQueue {
    pub messages: Vec<ActorMessage>,
}

impl ActorMessageQueue {
    pub fn new() -> Self {
        ActorMessageQueue {
            messages: Vec::new(),
        }
    }

    pub fn push_back(
        &mut self,
        receivers: ActorType,
        message: ActorMessageType,
    ) {
        self.messages.push(ActorMessage { receivers, message });
    }
}

pub(crate) trait ActorCreateInterface: Sized {
    fn create(
        general: &mut ActorData,
        solids: &mut LevelSolids,
        tiles: &mut LevelTiles,
    ) -> Self;

    fn create_boxed(
        general: &mut ActorData,
        solids: &mut LevelSolids,
        tiles: &mut LevelTiles,
    ) -> Box<Self> {
        Box::new(Self::create(general, solids, tiles))
    }
}

pub struct ActParameters<'a> {
    pub general: &'a mut ActorData,
    pub solids: &'a mut LevelSolids,
    pub tiles: &'a mut LevelTiles,
    pub hero_data: &'a mut HeroData,
    pub actor_adder: &'a mut dyn ActorAdder,
    pub play_state: &'a mut PlayState,
}

pub struct ShotParameters<'a> {
    pub general: &'a mut ActorData,
    pub solids: &'a mut LevelSolids,
    pub tiles: &'a mut LevelTiles,
    pub actor_adder: &'a mut dyn ActorAdder,
    pub hero_data: &'a mut HeroData,
    pub actor_message_queue: &'a mut ActorMessageQueue,
}

pub struct RenderParameters<'a> {
    pub general: &'a mut ActorData,
    pub renderer: &'a mut dyn Renderer,
}

pub struct ReceiveMessageParameters<'a> {
    pub general: &'a mut ActorData,
    pub message: ActorMessageType,
    pub hero_data: &'a mut HeroData,
    pub solids: &'a mut LevelSolids,
}

pub struct HeroInteractStartParameters<'a> {
    pub general: &'a mut ActorData,
    pub play_state: &'a mut PlayState,
    pub hero_data: &'a mut HeroData,
    pub info_message_queue: &'a mut InfoMessageQueue,
    pub actor_message_queue: &'a mut ActorMessageQueue,
}

pub struct HeroInteractEndParameters<'a> {
    pub general: &'a mut ActorData,
    pub hero_data: &'a mut HeroData,
    pub play_state: &'a mut PlayState,
}

pub struct HeroTouchStartParameters<'a> {
    pub general: &'a mut ActorData,
    pub hero_data: &'a mut HeroData,
    pub actor_adder: &'a mut dyn ActorAdder,
}

pub struct HeroTouchEndParameters<'a> {
    pub general: &'a mut ActorData,
    pub hero_data: &'a mut HeroData,
}

pub(crate) trait ActorInterface: std::fmt::Debug {
    fn hero_touch_start(&mut self, _p: HeroTouchStartParameters) {}

    fn hero_touch_end(&mut self, _p: HeroTouchEndParameters) {}

    fn hero_can_interact(&self) -> bool {
        false
    }

    fn hero_interact_start(&mut self, _p: HeroInteractStartParameters) {}

    fn hero_interact_end(&mut self, _p: HeroInteractEndParameters) {}

    fn act(&mut self, p: ActParameters);

    fn render(&mut self, p: RenderParameters) -> Result<()>;

    fn can_get_shot(&self, _general: &ActorData) -> bool {
        false
    }

    fn shot(&mut self, _p: ShotParameters) -> ShotProcessing {
        ShotProcessing::Ignore
    }

    fn receive_message(&mut self, _p: ReceiveMessageParameters) {}
}