about summary refs log tree commit diff stats
path: root/Source/Randomizer.cpp
blob: 47bb8c3490161cdbab8a72ab4499a557ec0a3611 (plain) (blame)
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
/*
Things to do for V2:
- Better interface design. It's way too simplistic, take (some) notes from talos.
  - Seed: [   ] (Randomize)
  ?? Challenge

  - [] Prevent speedrun snipes // Shadows, Swamp, Town, Quarry stairs
  - [] Speed up various autoscrollers // Swamp platforms, Desert/Mountain elevators, Desert rotating panels

  (Additional required panels)
  - [] Desert 8
  - [] Pond 5
  - [] Both keep halves
  - [] Town lattice requires stars door // and stars door will be randomized

  (Debug version only)
  - [] Noclip
  - [] Noclip speed


- Really randomize panels. Sorted by ROI
  - Random with style
    - Tutorial
    - Mountain 1 orange, green, blue, purple
    - Mountain 2 multipanel
    - Mountain 3 pillars
    - Laser areas (Glass Factory, Symmetry, Quarry, Treehouse, Swamp, Keep pressure plates, Town 25 dots)
    - (low) Discarded panels
    - (low) Tutorials // Dots, Stones, Swamp

  - Keep Hedges become like hedges 4, intersection between path and panel
  - Keep Pressure plates: Random with style

  - No idea how to randomize:
    - Symmetry transparent
    - Desert
    - Shadows
    - Town (lattice, RGB area, snipes, triple)
    - Monastery
    - Jungle
    - Bunker
    - UTM
    - Mountain 2 rainbow
    - Challenge

- Any RNG rerolls should be based on previous seed so that everyone can go to next seed easily

- Stability. Duh. I need to clearly define the ownership between the randomizer and the game.

- Challenge should have some way to 'reroll every run'
- Challenge should not turn off after time limit?
- Challenge triangles should not turn off




*/






/*
 * Try to wire up both keep halves
 * Wire up both halves of symmetry laser
 * Turn off floating panel in desert
 * Try randomizing default-on for pitches & bunker


 * Speed up *everything* ? Or maybe we'll just stop using this setting entirely.

 * Add setting for "Don't reset the challenge seed on new challenge"
 * Don't rerandomize anything outside of challenge on re-click
 * Change re-randomization prevention?


 * BUGS:
 * Shipwreck vault is solved reversed? -> Not reversed, just "half", you can normally solve orange. Seems to need pattern name.
 * Tutorial sounds don't always play -> Unsure. Not controlled by pattern name.
 * Rainbow seems to be not copying background?
 ** Rainbow 1 <-> Green 3 (the poly one) worked
 ** Rainbow 2 <-> Treehouse Right Orange 1 didn't
 * FEATURES:
 * Start the game if it isn't running?
 * Randomize audio logs -- Hard, seem to be unloaded some times?
 * Swap sounds in jungle (along with panels) -- maybe impossible
 * Make orange 7 (all of oranges?) hard. Like big = hard. (See: HARD_MODE)
 * Try turning on first half of wire in shadows once tutorial is done
 * It might be possible to remove the texture on top of rainbow 5 (so that any panel can be placed there)
 * 20 challenges with 20 consecutive seeds
 * Random *rotation* of desert laser redirect?
*/
#include "pch.h"
#include "Randomizer.h"
#include "Panels.h"
#include "Random.h"


template <class T>
int find(const std::vector<T> &data, T search, size_t startIndex = 0) {
    for (size_t i=startIndex ; i<data.size(); i++) {
        if (data[i] == search) return static_cast<int>(i);
    }
    std::cout << "Couldn't find " << search << " in data!" << std::endl;
    throw std::exception("Couldn't find value in data!");
}

Randomizer::Randomizer(const std::shared_ptr<Memory>& memory) : _memory(memory) {}

void Randomizer::Randomize() {
    // reveal_exit_hall - Prevent actually ending the game (EEE)
    _memory->AddSigScan({0x45, 0x8B, 0xF7, 0x48, 0x8B, 0x4D}, [&](int index){
        _memory->WriteData<byte>({index + 0x15}, {0xEB}); // jz -> jmp
    });

    // begin_endgame_1 - Prevent actually ending the game (Wonkavator)
    _memory->AddSigScan({0x83, 0x7C, 0x01, 0xD0, 0x04}, [&](int index){
        if (Memory::GLOBALS == 0x5B28C0) { // Version differences.
            index += 0x75;
        } else if (Memory::GLOBALS == 0x62D0A0) {
            index += 0x86;
        }
        _memory->WriteData<byte>({index}, {0xEB}); // jz -> jmp
    });

    _memory->ExecuteSigScans();

    // Glass Factory Entry (for Mountain Blue 1 and 2)
    Tutorialise(0x33AF5, 0x01A54);
    Tutorialise(0x33AF7, 0x01A54);

    // Tutorial Bend
    for (int panel : utmPerspective) {
        Tutorialise(panel, 0x00182);
    }
    // Tutorial Straight
    for (int panel : squarePanels) {
        Tutorialise(panel, 0x00064);
    }

    // Disable tutorial cursor speed modifications (not working?)
    _memory->WriteEntityData<float>(0x00295, CURSOR_SPEED_SCALE, { 1.0 });
    _memory->WriteEntityData<float>(0x0C373, CURSOR_SPEED_SCALE, { 1.0 });
    _memory->WriteEntityData<float>(0x00293, CURSOR_SPEED_SCALE, { 1.0 });
    _memory->WriteEntityData<float>(0x002C2, CURSOR_SPEED_SCALE, { 1.0 });

    // Ensure that whatever pivot panels we have are flagged as "pivotable"
    // @Bug: Can return {}, be careful!
    int panelFlags = _memory->ReadEntityData<int>(0x17DD1, STYLE_FLAGS, 1)[0];
    _memory->WriteEntityData<int>(0x17DD1, STYLE_FLAGS, { panelFlags | 0x8000 });
    panelFlags = _memory->ReadEntityData<int>(0x17CE3, STYLE_FLAGS, 1)[0];
    _memory->WriteEntityData<int>(0x17CE3, STYLE_FLAGS, { panelFlags | 0x8000 });
    panelFlags = _memory->ReadEntityData<int>(0x17DB7, STYLE_FLAGS, 1)[0];
    _memory->WriteEntityData<int>(0x17DB7, STYLE_FLAGS, { panelFlags | 0x8000 });
    panelFlags = _memory->ReadEntityData<int>(0x17E52, STYLE_FLAGS, 1)[0];
    _memory->WriteEntityData<int>(0x17E52, STYLE_FLAGS, { panelFlags | 0x8000 });
}

void Randomizer::Tutorialise(int panel1, int tutorialStraight) {
    //const int tutorialStraight = 0x00064;
    
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, PATH_COLOR, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, REFLECTION_PATH_COLOR, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, DOT_COLOR, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, ACTIVE_COLOR, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, BACKGROUND_REGION_COLOR, 12);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SUCCESS_COLOR_A, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SUCCESS_COLOR_B, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, STROBE_COLOR_A, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, STROBE_COLOR_B, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, ERROR_COLOR, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, PATTERN_POINT_COLOR, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, PATTERN_POINT_COLOR_A, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, PATTERN_POINT_COLOR_B, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SYMBOL_A, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SYMBOL_B, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SYMBOL_C, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SYMBOL_D, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SYMBOL_E, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, PUSH_SYMBOL_COLORS, sizeof(int));
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, OUTER_BACKGROUND, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, OUTER_BACKGROUND_MODE, sizeof(int));
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, NUM_COLORED_REGIONS, sizeof(int));
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, COLORED_REGIONS, NUM_COLORED_REGIONS);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, TRACED_EDGES, 16);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, PATH_WIDTH_SCALE, sizeof(float));
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, STARTPOINT_SCALE, sizeof(float));
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, NUM_DOTS, sizeof(int));
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, NUM_CONNECTIONS, sizeof(int));
    _memory->CopyArray<float>(tutorialStraight, panel1, DOT_POSITIONS, _memory->ReadEntityData<int>(tutorialStraight, NUM_DOTS, sizeof(int))[0]*2);
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DOT_FLAGS, NUM_DOTS);
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DOT_CONNECTION_A, NUM_CONNECTIONS);
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DOT_CONNECTION_B, NUM_CONNECTIONS);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, NUM_DECORATIONS, sizeof(int));
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DECORATIONS, NUM_DECORATIONS);
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DECORATION_FLAGS, NUM_DECORATIONS);
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DECORATION_COLORS, NUM_DECORATIONS);
    if (_memory->ReadPanelData<int>(tutorialStraight, REFLECTION_DATA)) {
        _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, REFLECTION_DATA, NUM_DOTS);
    }
    else {
        _memory->WritePanelData<long long>(panel1, REFLECTION_DATA, { 0 });
    }
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, SEQUENCE_LEN, sizeof(int));
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, SEQUENCE, SEQUENCE_LEN);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, DOT_SEQUENCE_LEN, sizeof(int));
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DOT_SEQUENCE, DOT_SEQUENCE_LEN);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, DOT_SEQUENCE_LEN_REFLECTION, sizeof(int));
    _memory->CopyArrayDynamicSize<int>(tutorialStraight, panel1, DOT_SEQUENCE_REFLECTION, DOT_SEQUENCE_LEN_REFLECTION);
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, GRID_SIZE_X, sizeof(int));
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, GRID_SIZE_Y, sizeof(int));
    _memory->CopyEntityData<byte>(tutorialStraight, panel1, STYLE_FLAGS, sizeof(int));
    _memory->WritePanelData<byte>(panel1, RANDOMISE_ON_POWER_ON, { 0 });


        //arrays.push_back(AUDIO_PREFIX);
//        offsets[IS_CYLINDER] = sizeof(int);
//        offsets[CYLINDER_Z0] = sizeof(float);
//        offsets[CYLINDER_Z1] = sizeof(float);
//        offsets[CYLINDER_RADIUS] = sizeof(float);

        //arrays.push_back(PANEL_TARGET);
        //arrays.push_back(SPECULAR_TEXTURE);

}