59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
basicmonster(int type, int yaw, int state, int trigger, int move)
{
if (type >= NUMMONSTERTYPES) {
conoutf(@"warning: unknown monster in spawn: %d", type);
type = 0;
}
DynamicEntity *m = newdynent();
monstertype *t = &monstertypes[(m.mtype = type)];
m.eyeheight = 2.0f;
m.aboveeye = 1.9f;
m.radius *= t->bscale / 10.0f;
m.eyeheight *= t->bscale / 10.0f;
m.aboveeye *= t->bscale / 10.0f;
m.monsterstate = state;
if (state != M_SLEEP)
|
|
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
basicmonster(int type, int yaw, int state, int trigger, int move)
{
if (type >= NUMMONSTERTYPES) {
conoutf(@"warning: unknown monster in spawn: %d", type);
type = 0;
}
DynamicEntity *m = newdynent();
struct monstertype *t = &monstertypes[(m.mtype = type)];
m.eyeheight = 2.0f;
m.aboveeye = 1.9f;
m.radius *= t->bscale / 10.0f;
m.eyeheight *= t->bscale / 10.0f;
m.aboveeye *= t->bscale / 10.0f;
m.monsterstate = state;
if (state != M_SLEEP)
|
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
|
monstertotal++;
}
}
}
// height-correct line of sight for monster shooting/seeing
bool
los(float lx, float ly, float lz, float bx, float by, float bz, OFVector3D &v)
{
if (OUTBORD((int)lx, (int)ly) || OUTBORD((int)bx, (int)by))
return false;
float dx = bx - lx;
float dy = by - ly;
int steps = (int)(sqrt(dx * dx + dy * dy) / 0.9);
if (!steps)
return false;
float x = lx;
float y = ly;
int i = 0;
for (;;) {
sqr *s = S(fast_f2nat(x), fast_f2nat(y));
if (SOLID(s))
break;
float floor = s->floor;
if (s->type == FHF)
floor -= s->vdelta / 4.0f;
float ceil = s->ceil;
if (s->type == CHF)
ceil += s->vdelta / 4.0f;
float rz = lz - ((lz - bz) * (i / (float)steps));
if (rz < floor || rz > ceil)
break;
v.x = x;
v.y = y;
v.z = rz;
x += dx / (float)steps;
y += dy / (float)steps;
i++;
}
return i >= steps;
}
bool
enemylos(DynamicEntity *m, OFVector3D &v)
{
v = m.o;
return los(
m.o.x, m.o.y, m.o.z, m.enemy.o.x, m.enemy.o.y, m.enemy.o.z, v);
}
// monster AI is sequenced using transitions: they are in a particular state
// where they execute a particular behaviour until the trigger time is hit, and
// then they reevaluate their situation based on the current state, the
|
|
|
|
|
|
|
|
|
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
|
monstertotal++;
}
}
}
// height-correct line of sight for monster shooting/seeing
bool
los(float lx, float ly, float lz, float bx, float by, float bz, OFVector3D *v)
{
if (OUTBORD((int)lx, (int)ly) || OUTBORD((int)bx, (int)by))
return false;
float dx = bx - lx;
float dy = by - ly;
int steps = (int)(sqrt(dx * dx + dy * dy) / 0.9);
if (!steps)
return false;
float x = lx;
float y = ly;
int i = 0;
for (;;) {
struct sqr *s = S(fast_f2nat(x), fast_f2nat(y));
if (SOLID(s))
break;
float floor = s->floor;
if (s->type == FHF)
floor -= s->vdelta / 4.0f;
float ceil = s->ceil;
if (s->type == CHF)
ceil += s->vdelta / 4.0f;
float rz = lz - ((lz - bz) * (i / (float)steps));
if (rz < floor || rz > ceil)
break;
v->x = x;
v->y = y;
v->z = rz;
x += dx / (float)steps;
y += dy / (float)steps;
i++;
}
return i >= steps;
}
bool
enemylos(DynamicEntity *m, OFVector3D *v)
{
*v = m.o;
return los(
m.o.x, m.o.y, m.o.z, m.enemy.o.x, m.enemy.o.y, m.enemy.o.z, v);
}
// monster AI is sequenced using transitions: they are in a particular state
// where they execute a particular behaviour until the trigger time is hit, and
// then they reevaluate their situation based on the current state, the
|
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
transition(m, M_HOME, 1, 100, 200);
break;
case M_SLEEP: // state classic sp monster start in, wait for visual
// contact
{
OFVector3D target;
if (editmode || !enemylos(m, target))
return; // skip running physics
normalise(m, enemyyaw);
float angle = (float)fabs(enemyyaw - m.yaw);
if (disttoenemy < 8 // the better the angle to the player, the
// further the monster can see/hear
|| (disttoenemy < 16 && angle < 135) ||
(disttoenemy < 32 && angle < 90) ||
|
|
|
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
transition(m, M_HOME, 1, 100, 200);
break;
case M_SLEEP: // state classic sp monster start in, wait for visual
// contact
{
OFVector3D target;
if (editmode || !enemylos(m, &target))
return; // skip running physics
normalise(m, enemyyaw);
float angle = (float)fabs(enemyyaw - m.yaw);
if (disttoenemy < 8 // the better the angle to the player, the
// further the monster can see/hear
|| (disttoenemy < 16 && angle < 135) ||
(disttoenemy < 32 && angle < 90) ||
|
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
case M_HOME:
// monster has visual contact, heads straight for player and
// may want to shoot at any time
m.targetyaw = enemyyaw;
if (m.trigger < lastmillis) {
OFVector3D target;
if (!enemylos(m, target)) {
// no visual contact anymore, let monster get
// as close as possible then search for player
transition(m, M_HOME, 1, 800, 500);
} else {
// the closer the monster is the more likely he
// wants to shoot
if (!rnd((int)disttoenemy / 3 + 1) &&
|
|
|
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
case M_HOME:
// monster has visual contact, heads straight for player and
// may want to shoot at any time
m.targetyaw = enemyyaw;
if (m.trigger < lastmillis) {
OFVector3D target;
if (!enemylos(m, &target)) {
// no visual contact anymore, let monster get
// as close as possible then search for player
transition(m, M_HOME, 1, 800, 500);
} else {
// the closer the monster is the more likely he
// wants to shoot
if (!rnd((int)disttoenemy / 3 + 1) &&
|