︙ | | | ︙ | |
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
|
m->state = CS_ALIVE;
m->anger = 0;
@autoreleasepool {
strcpy_s(m->name, t->name.UTF8String);
}
monsters.add(m);
return m;
};
void
spawnmonster() // spawn a random monster according to freq distribution in DMSP
{
int n = rnd(TOTMFREQ), type;
for (int i = 0;; i++)
if ((n -= monstertypes[i].freq) < 0) {
type = i;
break;
};
basicmonster(type, rnd(360), M_SEARCH, 1000, 1);
};
void
monsterclear() // called after map start of when toggling edit mode to
// reset/spawn all monsters to initial state
{
loopv(monsters) free(monsters[i]);
monsters.setsize(0);
|
<
>
<
>
|
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
|
m->state = CS_ALIVE;
m->anger = 0;
@autoreleasepool {
strcpy_s(m->name, t->name.UTF8String);
}
monsters.add(m);
return m;
}
void
spawnmonster() // spawn a random monster according to freq distribution in DMSP
{
int n = rnd(TOTMFREQ), type;
for (int i = 0;; i++)
if ((n -= monstertypes[i].freq) < 0) {
type = i;
break;
};
basicmonster(type, rnd(360), M_SEARCH, 1000, 1);
}
void
monsterclear() // called after map start of when toggling edit mode to
// reset/spawn all monsters to initial state
{
loopv(monsters) free(monsters[i]);
monsters.setsize(0);
|
︙ | | | ︙ | |
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
dynent *m = basicmonster(
ents[i].attr2, ents[i].attr1, M_SLEEP, 100, 0);
m->o.x = ents[i].x;
m->o.y = ents[i].y;
m->o.z = ents[i].z;
entinmap(m);
monstertotal++;
};
};
};
bool
los(float lx, float ly, float lz, float bx, float by, float bz,
OFVector3D &v) // height-correct line of sight for monster shooting/seeing
{
if (OUTBORD((int)lx, (int)ly) || OUTBORD((int)bx, (int)by))
return false;
|
>
|
<
<
>
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
dynent *m = basicmonster(
ents[i].attr2, ents[i].attr1, M_SLEEP, 100, 0);
m->o.x = ents[i].x;
m->o.y = ents[i].y;
m->o.z = ents[i].z;
entinmap(m);
monstertotal++;
}
};
}
bool
los(float lx, float ly, float lz, float bx, float by, float bz,
OFVector3D &v) // height-correct line of sight for monster shooting/seeing
{
if (OUTBORD((int)lx, (int)ly) || OUTBORD((int)bx, (int)by))
return false;
|
︙ | | | ︙ | |
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
|
v.y = y;
v.z = rz;
x += dx / (float)steps;
y += dy / (float)steps;
i++;
};
return i >= steps;
};
bool
enemylos(dynent *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
// environment etc., and transition to the next state. Transition timeframes are
// parametrized by difficulty level (skill), faster transitions means quicker
// decision making means tougher AI.
void
transition(dynent *m, int state, int moving, int n,
int r) // n = at skill 0, n/2 = at skill 10, r = added random factor
{
m->monsterstate = state;
m->move = moving;
n = n * 130 / 100;
m->trigger = lastmillis + n - skill * (n / 16) + rnd(r + 1);
};
void
normalise(dynent *m, float angle)
{
while (m->yaw < angle - 180.0f)
m->yaw += 360.0f;
while (m->yaw > angle + 180.0f)
m->yaw -= 360.0f;
};
void
monsteraction(
dynent *m) // main AI thinking routine, called every frame for every monster
{
if (m->enemy->state == CS_DEAD) {
m->enemy = player1;
|
<
>
<
>
<
>
<
>
|
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
|
v.y = y;
v.z = rz;
x += dx / (float)steps;
y += dy / (float)steps;
i++;
};
return i >= steps;
}
bool
enemylos(dynent *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
// environment etc., and transition to the next state. Transition timeframes are
// parametrized by difficulty level (skill), faster transitions means quicker
// decision making means tougher AI.
void
transition(dynent *m, int state, int moving, int n,
int r) // n = at skill 0, n/2 = at skill 10, r = added random factor
{
m->monsterstate = state;
m->move = moving;
n = n * 130 / 100;
m->trigger = lastmillis + n - skill * (n / 16) + rnd(r + 1);
}
void
normalise(dynent *m, float angle)
{
while (m->yaw < angle - 180.0f)
m->yaw += 360.0f;
while (m->yaw > angle + 180.0f)
m->yaw -= 360.0f;
}
void
monsteraction(
dynent *m) // main AI thinking routine, called every frame for every monster
{
if (m->enemy->state == CS_DEAD) {
m->enemy = player1;
|
︙ | | | ︙ | |
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
vdist(disttoenemy, vectoenemy, m->o, m->enemy->o);
m->pitch = atan2(m->enemy->o.z - m->o.z, disttoenemy) * 180 / PI;
if (m->blocked) // special case: if we run into scenery
{
m->blocked = false;
if (!rnd(20000 /
monstertypes[m->mtype]
.speed)) // try to jump over obstackle (rare)
{
m->jumpnext = true;
} else if (m->trigger < lastmillis &&
(m->monsterstate != M_HOME ||
!rnd(5))) // search for a way around (common)
{
m->targetyaw +=
180 + rnd(180); // patented "random walk" AI
// pathfinding (tm) ;)
transition(m, M_SEARCH, 1, 400, 1000);
};
};
|
|
|
|
|
|
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
vdist(disttoenemy, vectoenemy, m->o, m->enemy->o);
m->pitch = atan2(m->enemy->o.z - m->o.z, disttoenemy) * 180 / PI;
if (m->blocked) // special case: if we run into scenery
{
m->blocked = false;
if (!rnd(20000 /
monstertypes[m->mtype]
.speed)) // try to jump over obstackle (rare)
{
m->jumpnext = true;
} else if (m->trigger < lastmillis &&
(m->monsterstate != M_HOME ||
!rnd(5))) // search for a way around (common)
{
m->targetyaw +=
180 + rnd(180); // patented "random walk" AI
// pathfinding (tm) ;)
transition(m, M_SEARCH, 1, 400, 1000);
};
};
|
︙ | | | ︙ | |
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
};
};
};
break;
};
moveplayer(m, 1, false); // use physics to move monster
};
void
monsterpain(dynent *m, int damage, dynent *d)
{
if (d->monsterstate) // a monster hit us
{
if (m != d) // guard for RL guys shooting themselves :)
|
<
>
|
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
};
};
};
break;
};
moveplayer(m, 1, false); // use physics to move monster
}
void
monsterpain(dynent *m, int damage, dynent *d)
{
if (d->monsterstate) // a monster hit us
{
if (m != d) // guard for RL guys shooting themselves :)
|
︙ | | | ︙ | |
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
playsound(monstertypes[m->mtype].diesound, &m->o);
int remain = monstertotal - numkilled;
if (remain > 0 && remain <= 5)
conoutf(@"only %d monster(s) remaining", remain);
} else {
playsound(monstertypes[m->mtype].painsound, &m->o);
}
};
void
endsp(bool allkilled)
{
conoutf(allkilled ? @"you have cleared the map!"
: @"you reached the exit!");
conoutf(@"score: %d kills in %d seconds", numkilled,
|
<
>
|
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
playsound(monstertypes[m->mtype].diesound, &m->o);
int remain = monstertotal - numkilled;
if (remain > 0 && remain <= 5)
conoutf(@"only %d monster(s) remaining", remain);
} else {
playsound(monstertypes[m->mtype].painsound, &m->o);
}
}
void
endsp(bool allkilled)
{
conoutf(allkilled ? @"you have cleared the map!"
: @"you reached the exit!");
conoutf(@"score: %d kills in %d seconds", numkilled,
|
︙ | | | ︙ | |
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
else
{
v.z += monsters[i]->eyeheight;
vdist(dist, t, monsters[i]->o, v);
v.z -= monsters[i]->eyeheight;
if (dist < 4)
teleport((int)(&e - &ents[0]), monsters[i]);
};
};
loopv(monsters) if (monsters[i]->state == CS_ALIVE)
monsteraction(monsters[i]);
}
void
monsterrender()
{
loopv(monsters) renderclient(monsters[i], false,
monstertypes[monsters[i]->mtype].mdlname, monsters[i]->mtype == 5,
monstertypes[monsters[i]->mtype].mscale / 10.0f);
}
|
<
<
>
>
|
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
else
{
v.z += monsters[i]->eyeheight;
vdist(dist, t, monsters[i]->o, v);
v.z -= monsters[i]->eyeheight;
if (dist < 4)
teleport((int)(&e - &ents[0]), monsters[i]);
}
}
loopv(monsters) if (monsters[i]->state == CS_ALIVE)
monsteraction(monsters[i]);
}
void
monsterrender()
{
loopv(monsters) renderclient(monsters[i], false,
monstertypes[monsters[i]->mtype].mdlname, monsters[i]->mtype == 5,
monstertypes[monsters[i]->mtype].mscale / 10.0f);
}
|