-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpc.cpp
230 lines (204 loc) · 5.79 KB
/
npc.cpp
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
#include "npc.h"
NPC::NPC(
LCG& global_lcg,
RAM& global_ram,
int npc_adr,
int pos_adr,
int npc_num,
float startX,
float startY
) :
lcg(global_lcg),
ram(global_ram) {
npc_address = npc_adr;
pos_address = pos_adr;
npc_number = npc_num;
startingX = startX;
startingY = startY;
currentX = startX;
currentY = startY;
wait_time = 0.0;
walk_speed = 0.0;
nextX = 0.0;
nextY = 0.0;
destX = 0.0;
destY = 0.0;
state = WAITING;
step_count = 0;
first_step = true;
debug = false;
//write constants to ram
ram.write_int(npc_address + 4, npc_address);
ram.write_int(npc_address + 8, pos_address);
ram.write_int(npc_address + 40, 0x0000002d);
ram.write_int(npc_address + 44, npc_number);
ram.write_int(npc_address + 52, 0x3e94a529);
ram.write_single(npc_address + 116, startingX);
ram.write_int(npc_address + 120, 0x00000000);
ram.write_single(npc_address + 124, startingY);
ram.write_int(npc_address + 128, 0x41700000);
ram.write_int(pos_address, 0x00000000); //unused
ram.write_int(pos_address + 28, 0x00000000);
ram.write_int(pos_address + 36, 0x00000000);
ram.write_int(pos_address + 40, 0x00000000); //unused
ram.write_int(pos_address + 44, 0x00000000);
ram.write_int(pos_address + 160, 0x41f00000); //constant?
};
void NPC::step() {
// simulates one frame of NPC action
if (state == WALKING) {
// calc the next position in the walk
pair <float, float> calced;
if (step_count == 0) {
calced = calc_walk(currentX, currentY);
nextX = calced.first;
nextY = calced.second;
step_count++;
}
calced = calc_walk(nextX, nextY);
float calcedX = calced.first;
float calcedY = calced.second;
step_count++;
// check if we would pass the destination next step and if so start stopping
if ((nextX <= destX and destX <= calcedX) or
(nextX >= destX and destX >= calcedX)) {
set_wait_time();
state = STOPPING;
}
currentX = nextX;
currentY = nextY;
nextX = calcedX;
nextY = calcedY;
}
else if (state == FIRST_WALK) {
// calc the next position in the walk
pair <float, float> calced = calc_walk(currentX, currentY);
float calcedX = calced.first;
float calcedY = calced.second;
step_count++;
// check if we will pass the destination now and if so start stopping
if ((currentX <= destX and destX <= calcedX) or
(currentX >= destX and destX >= calcedX)) {
set_wait_time();
state = STOPPING;
}
currentX = calcedX;
currentY = calcedY;
}
else if (state == STOPPING) {
currentX = destX;
currentY = destY;
step_count = 0;
wait();
state = WAITING;
}
else if (state == WAITING) {
if (wait_time <= 0.0) {
set_walk_params();
if (first_step) {
state = FIRST_WALK;
first_step = false;
}
else {
state = WALKING;
}
}
wait();
}
}
void NPC::set_wait_time() {
//simulates logic for selecting a pseudo random wait time
//cout << "npc" << hex << npc_number << " called lcg in wait" << endl;
//cout << "npc" << hex << npc_number << " called lcg in wait" << endl;
double prn1 = lcg.generate();
double prn2 = lcg.generate();
double rand = (prn1 + prn2) - 1.0; // constant from rtoc
wait_time = (float)((3.0 * rand) + 5.0); //constants from r31
}
void NPC::wait() {
//simulates waiting one frame
double val = (double)wait_time;
val -= 0.03333333507180214; //0x3fa1111120000000
if (val >= 0) {
wait_time = (float)val;
}
else {
wait_time = 0.0;
}
}
void NPC::set_walk_params() {
//executes logic for selecting a pseudo random walk destination and speed
unordered_map<string, int> starting_int_registers = {
{"sp", 0x8048e5e0},
{"r13", 0x80480820},
{"r30", npc_address},
{"r31", npc_address},
};
//cout << "npc" << hex << npc_number << " called lcg set_walk_params" << endl;
double prn = lcg.generate();
unordered_map<string, double> starting_double_registers = {
{"f1", prn},
};
ram.write_single(pos_address + 24, currentX);
ram.write_single(pos_address + 32, currentY);
PPC_exc exc = PPC_exc(ram, 0x80184e64, starting_int_registers, starting_double_registers);
exc.execute();
walk_speed = ram.read_single(npc_address + 64);
destX = ram.read_single(npc_address + 92);
destY = ram.read_single(npc_address + 100);
}
pair <float, float> NPC::calc_walk(float givenX, float givenY) {
//calculates the next walk position from a given x and y
unordered_map<string, int> starting_int_registers = {
{"lr", 0x80185afc},
{"sp", 0x80185afc},
{"r13", 0x80480820},
{"r30", 0x0000002d},
{"r31", npc_number},
};
unordered_map<string, double> starting_double_registers = {
{"f30", 1.0},
};
ram.write_single(npc_address + 64, walk_speed);
ram.write_single(pos_address + 24, givenX);
ram.write_single(pos_address + 32, givenY);
//strange out of bounds value
//worried this may be incorrect off emulator
if (state == FIRST_WALK) {
if (step_count == 0){
ram.write_int(0xe0000054, 0x00000001);
}
else if (step_count == 1) {
ram.write_int(0xe0000054, 0x00000004);
}
else if (step_count == 2) {
ram.write_int(0xe0000054, 0x00000001);
}
else {
ram.write_int(0xe0000054, 0x00000002);
}
}
else {
ram.write_int(0xe0000054, 0x00000002);
}
PPC_exc exc = PPC_exc(ram, 0x8018824c, starting_int_registers, starting_double_registers);
exc.execute();
float calcedX = ram.read_single(exc.int_registers.at("sp") + 0x10);
float calcedY = ram.read_single(exc.int_registers.at("sp") + 0x18);
return make_pair(calcedX, calcedY);
}
void NPC::print_state() {
int val;
memcpy(&val, &destX, 4);
cout << "destX: " << hex << val << endl;
memcpy(&val, &destY, 4);
cout << "destY: " << hex << val << endl;
memcpy(&val, &walk_speed, 4);
cout << "speed: " << hex << val << endl;
memcpy(&val, ¤tX, 4);
cout << "currentX: " << hex << val << endl;
memcpy(&val, ¤tY, 4);
cout << "currentY: " << hex << val << endl;
memcpy(&val, &wait_time, 4);
cout << "wait_time: " << hex << val << endl;
}