| 1 |
{%MainUnit castlebehaviors.pas} |
| 2 |
{ |
| 3 |
Copyright 2006-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ Behavior that allows the creature to move around, |
| 21 |
chasing the enemy, attacking the enemy (by short-range or long-range attack), |
| 22 |
running away from danger. |
| 23 |
|
| 24 |
Optional dependencies: if present, this behavior will use other behaviors |
| 25 |
on the same parent: |
| 26 |
|
| 27 |
@unorderedList( |
| 28 |
@item(TCastleSoundSource (to play spatial sounds; |
| 29 |
this creature is silent otherwise),) |
| 30 |
|
| 31 |
@item(TCastleAliveBehavior (to account for the fact that current creature |
| 32 |
may be dead, or badly wounded; |
| 33 |
this creature is assumed indestructible otherwise.)) |
| 34 |
) } |
| 35 |
TCastleMoveAttack = class(TCastleBehavior) |
| 36 |
public |
| 37 |
type |
| 38 |
TState = ( |
| 39 |
stateIdle, |
| 40 |
stateMove, |
| 41 |
stateAttack, |
| 42 |
stateFireMissile, |
| 43 |
stateDie, |
| 44 |
stateDieBack, |
| 45 |
stateHurt |
| 46 |
); |
| 47 |
private |
| 48 |
FState: TState; |
| 49 |
FLifeTime: TFloatTime; |
| 50 |
FCurrentAnimation: String; |
| 51 |
|
| 52 |
{ Last State change time, taken from LifeTime. } |
| 53 |
FStateChangeTime: TFloatTime; |
| 54 |
|
| 55 |
{ Time of last State change to stateAttack or stateFireMissile, |
| 56 |
taken from LifeTime. } |
| 57 |
LastAttackTime, LastFireMissileTime: TFloatTime; |
| 58 |
{ Whether Attack or FireMissile was already called within this |
| 59 |
stateAttack or stateFireMissile state. } |
| 60 |
AttackDone, FireMissileDone: Boolean; |
| 61 |
|
| 62 |
HasAlternativeTarget: Boolean; |
| 63 |
AlternativeTarget: TVector3; |
| 64 |
{ Time of last setting HasAlternativeTarget to true and AlternativeTarget |
| 65 |
value, taken from LifeTime. Used to not fall into loop |
| 66 |
when the creature tries to move to AlternativeTarget, and is not |
| 67 |
permanently blocked (so MoveCollision returns true all the time) |
| 68 |
but at the same time the creature can't get close enough to the |
| 69 |
AlternativeTarget. In such case we use this variable to resign from |
| 70 |
AlternativeTarget after some time. } |
| 71 |
AlternativeTargetTime: Single; |
| 72 |
|
| 73 |
WaypointsSaved_Begin: TSector; |
| 74 |
WaypointsSaved_End: TSector; |
| 75 |
WaypointsSaved: TWaypointList; |
| 76 |
InternalMiddleForceBoxTime: Single; |
| 77 |
|
| 78 |
{ Last known information about enemy. } |
| 79 |
HasLastSensedEnemy: Boolean; |
| 80 |
LastSensedEnemy: TVector3; |
| 81 |
LastSensedEnemySector: TSector; |
| 82 |
|
| 83 |
FEnemy: TCastleAliveBehavior; |
| 84 |
FEnemyObserver: TFreeNotificationObserver; |
| 85 |
FSoundAttackHit: String; |
| 86 |
FSoundAttackStart: String; |
| 87 |
FSoundTypeAttackHit: TCastleSound; |
| 88 |
FSoundTypeAttackStart: TCastleSound; |
| 89 |
FAnimationIdle: String; |
| 90 |
FAnimationMove: String; |
| 91 |
FAnimationAttack: String; |
| 92 |
FAnimationFireMissile: String; |
| 93 |
FAnimationDie: String; |
| 94 |
FAnimationDieBack: String; |
| 95 |
FAnimationHurt: String; |
| 96 |
|
| 97 |
property StateChangeTime: TFloatTime read FStateChangeTime; |
| 98 |
procedure SetEnemy(const Value: TCastleAliveBehavior); |
| 99 |
procedure EnemyFreeNotification(const Sender: TFreeNotificationObserver); |
| 100 |
procedure PlaySound(const SoundType: TCastleSound; const SoundHeight: Single); |
| 101 |
procedure SetSoundAttackHit(const Value: String); |
| 102 |
procedure SetSoundAttackStart(const Value: String); |
| 103 |
procedure UpdateAnimation; |
| 104 |
function AnimationIdleStored: Boolean; |
| 105 |
function AnimationMoveStored: Boolean; |
| 106 |
function AnimationAttackStored: Boolean; |
| 107 |
function AnimationFireMissileStored: Boolean; |
| 108 |
function AnimationDieStored: Boolean; |
| 109 |
function AnimationDieBackStored: Boolean; |
| 110 |
function AnimationHurtStored: Boolean; |
| 111 |
function AnimationDuration(const Animation: String): TFloatTime; |
| 112 |
protected |
| 113 |
procedure SetState(const Value: TState); virtual; |
| 114 |
procedure ParentAfterAttach; override; |
| 115 |
function CanAttachToParent(const NewParent: TCastleTransform; |
| 116 |
out ReasonWhyCannot: String): Boolean; override; |
| 117 |
|
| 118 |
{ Actually do the attack indicated by AnimationAttack |
| 119 |
and AttackTime and other AttackXxx properties. |
| 120 |
This happens in the middle of AnimationAttack, at the time see AttackTime. |
| 121 |
|
| 122 |
This can happen only if you defined AnimationAttack (it is <> ''). |
| 123 |
|
| 124 |
The default implementation here performs a short range attack, |
| 125 |
if enemy is still within reach (AttackMaxDistance; even if it was within |
| 126 |
reach at the start of stateAttack state, the enemy could step back, |
| 127 |
so we need to check AttackMaxDistance again). |
| 128 |
The damage and knockback are defined by TCreatureResource.AttackDamageConst, |
| 129 |
TCreatureResource.AttackDamageRandom, TCreatureResource.AttackHurtStrength. } |
| 130 |
procedure Attack; virtual; |
| 131 |
|
| 132 |
{ Actually do the attack indicated by AnimationFireMissile |
| 133 |
and FireMissileTime and other FireMissileXxx properties. |
| 134 |
This happens in the middle of AnimationFireMissile, at the time |
| 135 |
FireMissileTime. |
| 136 |
|
| 137 |
This can happen only if you defined AnimationFireMissile (it is <> ''). |
| 138 |
|
| 139 |
The default implementation here creates a new creature with resource |
| 140 |
defined by FireMissileName, if FireMissileName is not empty. } |
| 141 |
procedure FireMissile; virtual; |
| 142 |
public |
| 143 |
const |
| 144 |
DefaultAnimationIdle = 'idle'; |
| 145 |
DefaultAnimationMove = 'move'; |
| 146 |
DefaultAnimationAttack = ''; //< Default value of AnimationAttack, empty because it is an optional animation. |
| 147 |
DefaultAnimationFireMissile = ''; //< Default value of AnimationFireMissile, empty because it is an optional animation. |
| 148 |
DefaultAnimationDie = 'die'; |
| 149 |
DefaultAnimationDieBack = ''; //< Default value of AnimationDieBack, empty because it is an optional animation. |
| 150 |
DefaultAnimationHurt = 'hurt'; |
| 151 |
|
| 152 |
constructor Create(AOwner: TComponent); override; |
| 153 |
destructor Destroy; override; |
| 154 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 155 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 156 |
|
| 157 |
{ Current state of the creature, automatically changing. } |
| 158 |
property State: TState read FState default stateIdle; |
| 159 |
|
| 160 |
{ Life, in seconds. } |
| 161 |
property LifeTime: TFloatTime read FLifeTime; |
| 162 |
published |
| 163 |
{ Enemy that is being chased and attacked by this creature. } |
| 164 |
property Enemy: TCastleAliveBehavior read FEnemy write SetEnemy; |
| 165 |
|
| 166 |
{ Sound played when short-range attack hits. |
| 167 |
|
| 168 |
This is a sound name from sound repository (see https://castle-engine.io/manual_sound.php). |
| 169 |
None by default. } |
| 170 |
property SoundAttackHit: String read FSoundAttackHit write SetSoundAttackHit; |
| 171 |
|
| 172 |
{ Played at the start of attack animation, |
| 173 |
that is when entering stateAttack state. |
| 174 |
To play a sound when the actual hit happens (at AttackTime) |
| 175 |
see SoundAttackHit. |
| 176 |
|
| 177 |
This is a sound name from sound repository (see https://castle-engine.io/manual_sound.php). |
| 178 |
None by default. } |
| 179 |
property SoundAttackStart: String read FSoundAttackStart write SetSoundAttackStart; |
| 180 |
|
| 181 |
{ An animation of being idle (standing / floating in place). |
| 182 |
Will be played in a loop. } |
| 183 |
property AnimationIdle: String read FAnimationIdle write FAnimationIdle stored AnimationIdleStored nodefault; |
| 184 |
|
| 185 |
{ Animation of moving, will be played in a loop. } |
| 186 |
property AnimationMove: String read FAnimationMove write FAnimationMove stored AnimationMoveStored nodefault; |
| 187 |
|
| 188 |
{ An animation of short-range attacking. |
| 189 |
Optional, it may be '' if creature doesn't make short-range attack. } |
| 190 |
property AnimationAttack: String read FAnimationAttack write FAnimationAttack stored AnimationAttackStored nodefault; |
| 191 |
|
| 192 |
{ Firing missile animation. |
| 193 |
Optional, it may be '' if creature doesn't fire missiles. |
| 194 |
|
| 195 |
You can always override TCastleMoveAttack.FireMissile to do pretty |
| 196 |
much anything you want, and this way treat this as an "alternate attack", |
| 197 |
not necessarily firing a missile. |
| 198 |
It's not really required to actually fire a missile --- it's only what |
| 199 |
happens at the default TCastleMoveAttack.FireMissile implementation. } |
| 200 |
property AnimationFireMissile: String read FAnimationFireMissile write FAnimationFireMissile stored AnimationFireMissileStored nodefault; |
| 201 |
|
| 202 |
{ An animation of dying. |
| 203 |
|
| 204 |
Dying animation is not displayed in a loop, after it runs |
| 205 |
it's duration we constantly show the final frame. |
| 206 |
Unless you set RemoveDead to @true, then the dead creature |
| 207 |
will be completely removed from the level. } |
| 208 |
property AnimationDie: String read FAnimationDie write FAnimationDie stored AnimationDieStored nodefault; |
| 209 |
|
| 210 |
{ Alternative optional (may be '') dying animation, used when the creature is killed |
| 211 |
by hitting it in the back. This may be useful if you want your |
| 212 |
creature to fall face-down when killed from the back or face-up |
| 213 |
when killed from the front. If this is defined, then AnimationDie |
| 214 |
is only used when creature is killed by hitting it from the front. |
| 215 |
The direction of last hit is taken from LastHurtDirection. } |
| 216 |
property AnimationDieBack: String read FAnimationDieBack write FAnimationDieBack stored AnimationDieBackStored nodefault; |
| 217 |
|
| 218 |
{ Animation when the creature will be hurt. } |
| 219 |
property AnimationHurt: String read FAnimationHurt write FAnimationHurt stored AnimationHurtStored nodefault; |
| 220 |
end; |
| 221 |
|
| 222 |
{$endif read_interface} |
| 223 |
|
| 224 |
{$ifdef read_implementation} |
| 225 |
|
| 226 |
{ TCastleMoveAttack -------------------------------------------------------- } |
| 227 |
|
| 228 |
constructor TCastleMoveAttack.Create(AOwner: TComponent); |
| 229 |
begin |
| 230 |
inherited; |
| 231 |
FEnemyObserver := TFreeNotificationObserver.Create(Self); |
| 232 |
FEnemyObserver.OnFreeNotification := @EnemyFreeNotification; |
| 233 |
FAnimationIdle := DefaultAnimationIdle; |
| 234 |
FAnimationMove := DefaultAnimationMove; |
| 235 |
FAnimationAttack := DefaultAnimationAttack; |
| 236 |
FAnimationFireMissile := DefaultAnimationFireMissile; |
| 237 |
FAnimationDie := DefaultAnimationDie; |
| 238 |
FAnimationDieBack := DefaultAnimationDieBack; |
| 239 |
FAnimationHurt := DefaultAnimationHurt; |
| 240 |
end; |
| 241 |
|
| 242 |
procedure TCastleMoveAttack.EnemyFreeNotification( |
| 243 |
const Sender: TFreeNotificationObserver); |
| 244 |
begin |
| 245 |
Enemy := nil; |
| 246 |
end; |
| 247 |
|
| 248 |
procedure TCastleMoveAttack.SetEnemy(const Value: TCastleAliveBehavior); |
| 249 |
begin |
| 250 |
if FEnemy <> Value then |
| 251 |
begin |
| 252 |
FEnemyObserver.Observed := Value; |
| 253 |
FEnemy := Value; |
| 254 |
end; |
| 255 |
end; |
| 256 |
|
| 257 |
function TCastleMoveAttack.AnimationDuration(const Animation: String): TFloatTime; |
| 258 |
begin |
| 259 |
Result := (Parent as TCastleScene).AnimationDuration(Animation); |
| 260 |
end; |
| 261 |
|
| 262 |
function TCastleMoveAttack.CanAttachToParent(const NewParent: TCastleTransform; |
| 263 |
out ReasonWhyCannot: String): Boolean; |
| 264 |
begin |
| 265 |
Result := inherited; |
| 266 |
if not Result then Exit; |
| 267 |
|
| 268 |
if NewParent.FindBehavior(TCastleMoveAttack) <> nil then |
| 269 |
begin |
| 270 |
ReasonWhyCannot := 'Only one TCastleMoveAttack behavior can be added to a given TCastleTransform'; |
| 271 |
Result := false; |
| 272 |
end; |
| 273 |
end; |
| 274 |
|
| 275 |
procedure TCastleMoveAttack.ParentAfterAttach; |
| 276 |
var |
| 277 |
Alive: TCastleAliveBehavior; |
| 278 |
begin |
| 279 |
inherited; |
| 280 |
|
| 281 |
// Our PlayAnimation and AnimationDuration requires you attach this to TCastleScene |
| 282 |
if not (Parent is TCastleScene) then |
| 283 |
raise Exception.CreateFmt('%s can only be attached to a TCastleScene', [ClassName]); |
| 284 |
|
| 285 |
{ Note that we don't automatically create TCastleAliveBehavior, |
| 286 |
this would make it complicated when something adds another |
| 287 |
instance of TCastleAliveBehavior -- adding it before or after |
| 288 |
adding TCastleMoveAttack would have different effect. } |
| 289 |
Alive := Parent.FindBehavior(TCastleAliveBehavior) as TCastleAliveBehavior; |
| 290 |
|
| 291 |
if (Alive = nil) or (Alive.MaxLife > 0) then |
| 292 |
begin |
| 293 |
FState := stateIdle; |
| 294 |
FStateChangeTime := 0; |
| 295 |
end else |
| 296 |
begin |
| 297 |
{ This means that the creature is created already in dead state... |
| 298 |
So we start with stateDie state and set FStateChangeTime to fake |
| 299 |
the fact that creature was killed long time ago. |
| 300 |
|
| 301 |
This way the creature is created as a dead corpse, without making |
| 302 |
any kind of dying (or wounded) sound or animation. } |
| 303 |
FState := stateDie; |
| 304 |
FStateChangeTime := -1000; |
| 305 |
end; |
| 306 |
|
| 307 |
UpdateAnimation; |
| 308 |
|
| 309 |
WaypointsSaved := TWaypointList.Create(false); |
| 310 |
end; |
| 311 |
|
| 312 |
destructor TCastleMoveAttack.Destroy; |
| 313 |
begin |
| 314 |
FreeAndNil(WaypointsSaved); |
| 315 |
inherited; |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TCastleMoveAttack.SetSoundAttackHit(const Value: String); |
| 319 |
begin |
| 320 |
if FSoundAttackHit <> Value then |
| 321 |
begin |
| 322 |
FSoundAttackHit := Value; |
| 323 |
FSoundTypeAttackHit := SoundEngine.SoundFromName(Value); |
| 324 |
end; |
| 325 |
end; |
| 326 |
|
| 327 |
procedure TCastleMoveAttack.SetSoundAttackStart(const Value: String); |
| 328 |
begin |
| 329 |
if FSoundAttackStart <> Value then |
| 330 |
begin |
| 331 |
FSoundAttackStart := Value; |
| 332 |
FSoundTypeAttackStart := SoundEngine.SoundFromName(Value); |
| 333 |
end; |
| 334 |
end; |
| 335 |
|
| 336 |
// TODO do not attack dead Enemy |
| 337 |
|
| 338 |
procedure TCastleMoveAttack.SetState(const Value: TState); |
| 339 |
begin |
| 340 |
if FState <> Value then |
| 341 |
begin |
| 342 |
{ Force old box value for Middle and PreferredHeight calculation, |
| 343 |
for a fraction of a second. |
| 344 |
|
| 345 |
This is crucial for TCastleMoveAttack.Update logic |
| 346 |
that could otherwise sometimes get stuck and continuously switching |
| 347 |
between move/idle states, because in idle state Middle indicates |
| 348 |
that we should move (e.g. distance or angle to enemy is not good enough), |
| 349 |
but right after switching to move the LocalBoundingBox changes |
| 350 |
(because 1st move animation frame is suddenly different) |
| 351 |
and the distance/angle seems Ok and we switch back to idle and so on, |
| 352 |
in a loop. Once this unfortunate situation is reached, the creature |
| 353 |
is stuck, blinking between two animation frames and two states, |
| 354 |
and never moving (until something else, like enemy (player) |
| 355 |
position changes). |
| 356 |
|
| 357 |
Safeguards: |
| 358 |
|
| 359 |
- Don't set to "forced" when it's already forced, as then it could |
| 360 |
cause InternalMiddleForceBoxValue change after each SetState to the box |
| 361 |
from previous state, and we'll be in a similar trouble |
| 362 |
(but with box values always from previous state). |
| 363 |
Trouble (without this safeguard) is reproducible on fps_game |
| 364 |
with knight flying. |
| 365 |
|
| 366 |
- Don't set to "forced" when switching between other states than idle/move, |
| 367 |
as the other states logic doesn't allow for such switching |
| 368 |
(states like attack, fireMissile, hurt, die generally continue until |
| 369 |
their time finished; there are no decisions). |
| 370 |
(I didn't actually observed a need for this safeguard so far, |
| 371 |
but it seems reasonable to limit this hack only to idle/move situation.) |
| 372 |
} |
| 373 |
if (not Parent.InternalMiddleForceBox) and |
| 374 |
( ((FState = stateIdle) and (Value = stateMove)) or |
| 375 |
((FState = stateMove) and (Value = stateIdle)) ) then |
| 376 |
begin |
| 377 |
Parent.InternalMiddleForceBox := true; |
| 378 |
Parent.InternalMiddleForceBoxValue := Parent.LocalBoundingBox; |
| 379 |
InternalMiddleForceBoxTime := LifeTime + 0.1; |
| 380 |
end; |
| 381 |
|
| 382 |
{ Some states require special finalization here. } |
| 383 |
case FState of |
| 384 |
stateAttack: |
| 385 |
{ In case we didn't reach AttackTime, make sure to fire the Attack now. } |
| 386 |
if not AttackDone then |
| 387 |
begin |
| 388 |
AttackDone := true; |
| 389 |
Attack; |
| 390 |
end; |
| 391 |
end; |
| 392 |
|
| 393 |
FState := Value; |
| 394 |
FStateChangeTime := LifeTime; |
| 395 |
|
| 396 |
UpdateAnimation; |
| 397 |
|
| 398 |
{ Some states require special initialization here. } |
| 399 |
case FState of |
| 400 |
stateAttack: |
| 401 |
begin |
| 402 |
PlaySound(FSoundTypeAttackStart, 1.0); |
| 403 |
LastAttackTime := StateChangeTime; |
| 404 |
AttackDone := false; |
| 405 |
end; |
| 406 |
stateFireMissile: |
| 407 |
begin |
| 408 |
LastFireMissileTime := LifeTime; |
| 409 |
FireMissileDone := false; |
| 410 |
end; |
| 411 |
end; |
| 412 |
end; |
| 413 |
end; |
| 414 |
|
| 415 |
procedure TCastleMoveAttack.PlaySound(const SoundType: TCastleSound; const SoundHeight: Single); |
| 416 |
var |
| 417 |
SoundSource: TCastleSoundSource; |
| 418 |
PlayingSound: TCastlePlayingSoundSource; |
| 419 |
begin |
| 420 |
if SoundType = nil then Exit; |
| 421 |
|
| 422 |
SoundSource := Parent.FindBehavior(TCastleSoundSource) as TCastleSoundSource; |
| 423 |
if SoundBehavior <> nil then |
| 424 |
begin |
| 425 |
PlayingSound := TCastlePlayingSoundSource.Create(nil); |
| 426 |
PlayingSound.Sound := SoundType; |
| 427 |
PlayingSound.FreeOnStop := true; |
| 428 |
PlayingSound.SoundHeight := SoundHeight; |
| 429 |
SoundSource.Play(PlayingSound); |
| 430 |
end; |
| 431 |
end; |
| 432 |
|
| 433 |
procedure TCastleMoveAttack.UpdateAnimation; |
| 434 |
var |
| 435 |
Loop: Boolean; |
| 436 |
AnimationName: String; |
| 437 |
begin |
| 438 |
case FState of |
| 439 |
stateIdle : AnimationName := AnimationIdle; |
| 440 |
stateMove : AnimationName := AnimationMove; |
| 441 |
stateAttack : AnimationName := AnimationAttack; |
| 442 |
stateFireMissile: AnimationName := AnimationFireMissile; |
| 443 |
stateDie : AnimationName := AnimationDie; |
| 444 |
stateDieBack : AnimationName := AnimationDieBack; |
| 445 |
stateHurt : AnimationName := AnimationHurt; |
| 446 |
else raise EInternalError.Create('FState ?'); |
| 447 |
end; |
| 448 |
|
| 449 |
Loop := FState in [stateIdle, stateMove]; |
| 450 |
|
| 451 |
if FCurrentAnimation <> AnimationName then |
| 452 |
begin |
| 453 |
FCurrentAnimation := AnimationName; |
| 454 |
if not (Parent as TCastleScene).PlayAnimation(FCurrentAnimation, Loop) then |
| 455 |
WritelnWarning('Cannot find animation "%s" on %s', [FCurrentAnimation, Name]); |
| 456 |
end; |
| 457 |
end; |
| 458 |
|
| 459 |
procedure TCastleMoveAttack.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 460 |
var |
| 461 |
EnemySensedNow: boolean; |
| 462 |
SqrDistanceToLastSensedEnemy: Single; |
| 463 |
|
| 464 |
function ActionAllowed(const Animation: String; |
| 465 |
const LastTime, MinDelay, MaxDistance, MaxAngle: Single): boolean; |
| 466 |
var |
| 467 |
AngleBetweenTheDirectionToEnemy: Single; |
| 468 |
begin |
| 469 |
Result := EnemySensedNow and |
| 470 |
(Animation <> '') and |
| 471 |
(LifeTime - LastTime > MinDelay) and |
| 472 |
(SqrDistanceToLastSensedEnemy <= Sqr(MaxDistance)); |
| 473 |
|
| 474 |
if Result then |
| 475 |
begin |
| 476 |
{ Calculate and check AngleBetweenTheDirectionToEnemy. } |
| 477 |
AngleBetweenTheDirectionToEnemy := AngleRadBetweenVectors( |
| 478 |
LastSensedEnemy - Parent.Middle, Parent.Direction); |
| 479 |
Result := AngleBetweenTheDirectionToEnemy <= MaxAngle; |
| 480 |
end; |
| 481 |
end; |
| 482 |
|
| 483 |
function AttackAllowed: boolean; |
| 484 |
begin |
| 485 |
Result := ActionAllowed(AnimationAttack, LastAttackTime, |
| 486 |
Resource.AttackMinDelay, Resource.AttackMaxDistance, Resource.AttackMaxAngle); |
| 487 |
end; |
| 488 |
|
| 489 |
function FireMissileAllowed: boolean; |
| 490 |
begin |
| 491 |
Result := ActionAllowed(AnimationFireMissile, LastFireMissileTime, |
| 492 |
Resource.FireMissileMinDelay, Resource.FireMissileMaxDistance, Resource.FireMissileMaxAngle); |
| 493 |
end; |
| 494 |
|
| 495 |
procedure CalculateDirectionToTarget( |
| 496 |
const Target: TVector3; |
| 497 |
out DirectionToTarget: TVector3; |
| 498 |
out AngleBetweenDirectionToTarget: Single); |
| 499 |
begin |
| 500 |
{ calculate DirectionToTarget } |
| 501 |
DirectionToTarget := Target - Parent.Middle; |
| 502 |
if Parent.Gravity then |
| 503 |
MakeVectorsOrthoOnTheirPlane(DirectionToTarget, World.GravityUp); |
| 504 |
|
| 505 |
{ calculate AngleBetweenDirectionToTarget } |
| 506 |
AngleBetweenDirectionToTarget := |
| 507 |
AngleRadBetweenVectors(DirectionToTarget, Parent.Direction); |
| 508 |
end; |
| 509 |
|
| 510 |
{ Call this only when HasLastSensedEnemy } |
| 511 |
procedure CalculateDirectionToEnemy(out DirectionToEnemy: TVector3; |
| 512 |
out AngleBetweenDirectionToEnemy: Single); |
| 513 |
begin |
| 514 |
CalculateDirectionToTarget(LastSensedEnemy, |
| 515 |
DirectionToEnemy, AngleBetweenDirectionToEnemy); |
| 516 |
end; |
| 517 |
|
| 518 |
procedure CalculateDirectionFromEnemy( |
| 519 |
var DirectionFromEnemy: TVector3; |
| 520 |
var AngleBetweenDirectionFromEnemy: Single); |
| 521 |
begin |
| 522 |
CalculateDirectionToEnemy( |
| 523 |
DirectionFromEnemy, AngleBetweenDirectionFromEnemy); |
| 524 |
DirectionFromEnemy := -DirectionFromEnemy; |
| 525 |
AngleBetweenDirectionFromEnemy := |
| 526 |
Pi - AngleBetweenDirectionFromEnemy; |
| 527 |
end; |
| 528 |
|
| 529 |
{ This changes Direction to be closer to DirectionToTarget. |
| 530 |
Note that it requires the value of AngleBetweenDirectionToTarget |
| 531 |
effectively } |
| 532 |
procedure RotateDirectionToFaceTarget(const DirectionToTarget: TVector3; |
| 533 |
const AngleBetweenDirectionToTarget: Single); |
| 534 |
const |
| 535 |
AngleChangeSpeed = 5.0; |
| 536 |
var |
| 537 |
AngleChange: Single; |
| 538 |
NewDirection: TVector3; |
| 539 |
begin |
| 540 |
if not VectorsParallel(DirectionToTarget, Parent.Direction) then |
| 541 |
begin |
| 542 |
{ Rotate Direction, to be closer to DirectionToTarget } |
| 543 |
|
| 544 |
{ calculate AngleChange } |
| 545 |
AngleChange := AngleChangeSpeed * SecondsPassed; |
| 546 |
MinVar(AngleChange, AngleBetweenDirectionToTarget); |
| 547 |
|
| 548 |
NewDirection := RotatePointAroundAxisRad(AngleChange, Parent.Direction, |
| 549 |
TVector3.CrossProduct(Parent.Direction, DirectionToTarget)); |
| 550 |
|
| 551 |
{ Make sure direction for non-flying creatures is orthogonal to GravityUp. } |
| 552 |
if Parent.Gravity then |
| 553 |
MakeVectorsOrthoOnTheirPlane(NewDirection, Parent.World.GravityUp); |
| 554 |
Parent.Direction := NewDirection; |
| 555 |
end; |
| 556 |
end; |
| 557 |
|
| 558 |
function CloseEnoughToTarget(const Target: TVector3): boolean; |
| 559 |
const |
| 560 |
MinDistanceToTarget = 0.1; |
| 561 |
var |
| 562 |
SqrDistanceToTarget: Single; |
| 563 |
begin |
| 564 |
if not Parent.Gravity then |
| 565 |
SqrDistanceToTarget := PointsDistanceSqr(Parent.Middle, Target) else |
| 566 |
SqrDistanceToTarget := PointsDistance2DSqr(Parent.Middle, Target, Parent.World.GravityCoordinate); |
| 567 |
Result := |
| 568 |
{ If creature is ideally at the target |
| 569 |
(for not Flying creatures, this means "ideally under/above the target"), |
| 570 |
then there is no way to get closer to the target. |
| 571 |
|
| 572 |
We check this with some "epsilon" (MinDistanceToTarget), as usual, to |
| 573 |
|
| 574 |
1. Avoid the unnecessary moving when Middle is in fact |
| 575 |
close enough to the target, but lack of floating precision |
| 576 |
can't move it really ideally to Target. |
| 577 |
|
| 578 |
2. In fact, it's not desirable to get exactly at (or under/above) |
| 579 |
the target, because this could cause undesirable rotations |
| 580 |
of the creature Direction (we usually try to make it in |
| 581 |
the Target direction, so when we stand (almost) exactly |
| 582 |
at Target, creature could try to stupidly rotate around itself). } |
| 583 |
SqrDistanceToTarget <= Sqr(MinDistanceToTarget); |
| 584 |
end; |
| 585 |
|
| 586 |
{ Assuming that I want to move in DesiredDirection direction, |
| 587 |
is it sensible to do this by moving along current Direction ? } |
| 588 |
function WantToMoveInDesiredDirection( |
| 589 |
const AngleBetweenDesiredDirection: Single): boolean; |
| 590 |
const |
| 591 |
MaxAngleToMoveForward = Pi * 60 / 180; |
| 592 |
begin |
| 593 |
Result := |
| 594 |
{ If AngleBetweenDesiredDirection is too large, there is not much point |
| 595 |
in moving in given direction anyway. We should just change our Direction. } |
| 596 |
(AngleBetweenDesiredDirection <= MaxAngleToMoveForward); |
| 597 |
end; |
| 598 |
|
| 599 |
{ Assuming that I want to get to Target position, is it sensible |
| 600 |
to do this by moving along current Direction ? |
| 601 |
This checks whether current Direction points roughly in the |
| 602 |
direction of the Target, and if were not already as close as possible |
| 603 |
to Target. } |
| 604 |
function WantToMoveToTarget( |
| 605 |
const Target: TVector3; |
| 606 |
const AngleBetweenDirectionToTarget: Single): boolean; |
| 607 |
begin |
| 608 |
Result := |
| 609 |
WantToMoveInDesiredDirection(AngleBetweenDirectionToTarget) and |
| 610 |
{ See comments in CloseEnoughToTarget for reasoning why this is needed. } |
| 611 |
(not CloseEnoughToTarget(Target)); |
| 612 |
end; |
| 613 |
|
| 614 |
{ This doesn't take into account current Direction, |
| 615 |
it only looks at our and enemy Middle positions, |
| 616 |
and asks "do I want to get closer" ? |
| 617 |
Use only if HasLastSensedEnemy. } |
| 618 |
function WantToShortenDistanceToEnemy: boolean; |
| 619 |
begin |
| 620 |
{ Is it wanted to get closer to the LastSensedEnemy? |
| 621 |
Yes, if it will help make AttackAllowed from false to true. |
| 622 |
See AttackAllowed implementation for conditions. } |
| 623 |
Result := |
| 624 |
{ There is no point in trying to get closer, |
| 625 |
if we would activate WantToRunAway *before* we reach a point from which |
| 626 |
we can attack. } |
| 627 |
(not |
| 628 |
( (Life <= MaxLife * Resource.RunAwayLife) and |
| 629 |
( (AnimationFireMissile = '') or (Resource.FireMissileMaxDistance < Resource.RunAwayDistance) ) and |
| 630 |
( (AnimationAttack = '') or (Resource. AttackMaxDistance < Resource.RunAwayDistance) ) |
| 631 |
) |
| 632 |
) and |
| 633 |
( |
| 634 |
{ Try to see enemy by moving to last known enemy position. } |
| 635 |
(not EnemySensedNow) or |
| 636 |
|
| 637 |
{ If EnemySensedNow and SqrDistanceToLastSensedEnemy is small enough, |
| 638 |
there's no point in getting closer to the enemy. In fact, it would |
| 639 |
be bad to get closer to enemy in this case, as this would allow |
| 640 |
enemy to easier attack (shorter distance --- easier to reach with |
| 641 |
short-range weapon, or easier to aim with long-range weapon). } |
| 642 |
(SqrDistanceToLastSensedEnemy > Sqr(Resource.PreferredDistance)) |
| 643 |
); |
| 644 |
end; |
| 645 |
|
| 646 |
{ Is it wanted to get closer to the LastSensedEnemy ? |
| 647 |
And (if it's wanted) is it sensible to do this by moving |
| 648 |
along current Direction ? |
| 649 |
Call this only if HasLastSensedEnemy. } |
| 650 |
function WantToMoveToEnemy( |
| 651 |
const AngleBetweenDirectionToEnemy: Single): boolean; |
| 652 |
begin |
| 653 |
Result := WantToShortenDistanceToEnemy and |
| 654 |
WantToMoveToTarget(LastSensedEnemy, AngleBetweenDirectionToEnemy); |
| 655 |
end; |
| 656 |
|
| 657 |
function WantToRunAway: boolean; |
| 658 |
begin |
| 659 |
{ We want to run away whenever HasLastSensedEnemy, not just when EnemySensedNow. |
| 660 |
Otherwise creature that tries to run away could easily get into a loop |
| 661 |
(flickering state), caused by small VisibilityAngle: |
| 662 |
in DoMove creature would rotate to face away from enemy, |
| 663 |
but after such rotation enemy becomes invisible, |
| 664 |
so WantToRunAway would become false, and we would switch to idle, |
| 665 |
and in DoIdle creature would rotate back toward the enemy. |
| 666 |
|
| 667 |
So we run away even when we do not see enemy *now*, it's only enough |
| 668 |
to know last enemy position. This actually makes sense in Real World too. } |
| 669 |
Result := HasLastSensedEnemy and |
| 670 |
(Life <= MaxLife * Resource.RunAwayLife) and |
| 671 |
(SqrDistanceToLastSensedEnemy < Sqr(Resource.RunAwayDistance)); |
| 672 |
end; |
| 673 |
|
| 674 |
procedure InitAlternativeTarget; |
| 675 |
var |
| 676 |
Distance: Single; |
| 677 |
I: Integer; |
| 678 |
begin |
| 679 |
Distance := Resource.RandomMoveDistance; |
| 680 |
|
| 681 |
AlternativeTarget := Parent.Middle; |
| 682 |
{ Add random values to the AlternativeTarget, but only on the components |
| 683 |
where creature can reliably move. Creature that cannot fly cannot |
| 684 |
move in gravity (UpIndex) direction. } |
| 685 |
for I := 0 to 2 do |
| 686 |
if (not Parent.Gravity) or (I <> Parent.World.GravityCoordinate) then |
| 687 |
AlternativeTarget.Data[I] := AlternativeTarget.Data[I] + (Random * Distance * 2 - Distance); |
| 688 |
|
| 689 |
HasAlternativeTarget := true; |
| 690 |
|
| 691 |
AlternativeTargetTime := LifeTime; |
| 692 |
end; |
| 693 |
|
| 694 |
{ Assuming current state is idle (the actual State value ignored), |
| 695 |
- perform "idle" AI |
| 696 |
- and tell whether you changed the state to something else (returns @true if remained idle). } |
| 697 |
function DoIdle: Boolean; |
| 698 |
var |
| 699 |
DirectionToEnemy: TVector3; |
| 700 |
AngleBetweenDirectionToEnemy: Single; |
| 701 |
begin |
| 702 |
Result := true; |
| 703 |
if HasLastSensedEnemy then |
| 704 |
begin |
| 705 |
CalculateDirectionToEnemy(DirectionToEnemy, AngleBetweenDirectionToEnemy); |
| 706 |
|
| 707 |
if FireMissileAllowed then |
| 708 |
begin |
| 709 |
SetState(stateFireMissile); |
| 710 |
Result := false; |
| 711 |
end else |
| 712 |
if AttackAllowed then |
| 713 |
begin |
| 714 |
SetState(stateAttack); |
| 715 |
Result := false; |
| 716 |
end else |
| 717 |
if WantToRunAway or |
| 718 |
WantToMoveToEnemy(AngleBetweenDirectionToEnemy) then |
| 719 |
begin |
| 720 |
SetState(stateMove); |
| 721 |
Result := false; |
| 722 |
end else |
| 723 |
if Parent.Gravity and |
| 724 |
(AngleBetweenDirectionToEnemy < 0.01) and |
| 725 |
Parent.BoundingBox.Contains2D(LastSensedEnemy, Parent.World.GravityCoordinate) then |
| 726 |
begin |
| 727 |
{ Then the enemy (or it's last known position) is right above or below us. |
| 728 |
Since we can't fly, we can't get there. Standing in place |
| 729 |
is one possibility, but it's not really good |
| 730 |
- We become easier target to shoot for enemy with the bow. |
| 731 |
- Most importantly, this way enemy (like player) can stand on our head |
| 732 |
and slash us with a sword without any risk. (This was almost |
| 733 |
a standard technique of killing Werewolf or SpiderQueen bosses). |
| 734 |
So we move a little --- just for the sake of moving. } |
| 735 |
SetState(stateMove); |
| 736 |
InitAlternativeTarget; |
| 737 |
Result := false; |
| 738 |
end else |
| 739 |
begin |
| 740 |
{ Continue stateIdle state } |
| 741 |
RotateDirectionToFaceTarget(DirectionToEnemy, |
| 742 |
AngleBetweenDirectionToEnemy); |
| 743 |
end; |
| 744 |
end; |
| 745 |
end; |
| 746 |
|
| 747 |
procedure DoMove; |
| 748 |
|
| 749 |
{ This performs the real move, which means that it changes Position |
| 750 |
and Middle along the Direction vector. |
| 751 |
|
| 752 |
This doesn't check whether this is a sensible move, so use this |
| 753 |
only if you know that the creature really wants to go in this Direction. |
| 754 |
|
| 755 |
This checks only the basic (i.e. always wanted) things: |
| 756 |
- Collision detection (with level and other collidable stuff like |
| 757 |
player and other creatures) |
| 758 |
- For not Flying creatures, also the check to not fall down from high |
| 759 |
is done. } |
| 760 |
function MoveAlongTheDirection: boolean; |
| 761 |
|
| 762 |
{ Don't be stupid, and don't move where you see you will fall down. } |
| 763 |
function TooHighAboveTheGround(const NewMiddle: TVector3): |
| 764 |
boolean; |
| 765 |
var |
| 766 |
AboveHeight: Single; |
| 767 |
begin |
| 768 |
Result := false; |
| 769 |
if Parent.Gravity then |
| 770 |
begin |
| 771 |
Parent.Height(NewMiddle, AboveHeight); |
| 772 |
if AboveHeight > Resource.MaxHeightAcceptableToFall + Parent.PreferredHeight then |
| 773 |
Result := true; |
| 774 |
end; |
| 775 |
end; |
| 776 |
|
| 777 |
begin |
| 778 |
Result := |
| 779 |
{ First check to not step into some deep fall. |
| 780 |
Note that I'm not using here NewMiddle |
| 781 |
(that will be calculated later by Move) |
| 782 |
because they are too close to Middle to be good to test against. |
| 783 |
I'm calculating here where I would get after 0.2 second. } |
| 784 |
(not TooHighAboveTheGround(Parent.Middle + Parent.Direction * (Resource.MoveSpeed * 0.2))) and |
| 785 |
|
| 786 |
{ Use Move without wall-sliding here. |
| 787 |
Things using MoveAlongTheDirection depend on the fact that |
| 788 |
MoveAlongTheDirection will return false |
| 789 |
if no further way is possible (and wall-sliding would try instead |
| 790 |
to return true and correct target position). |
| 791 |
|
| 792 |
Our trick with "AlternativeTarget" should handle |
| 793 |
eventual problems with the track of creature, so wall-sliding |
| 794 |
should not be needed. } |
| 795 |
Move(Parent.Direction * (Resource.MoveSpeed * SecondsPassed), false, false); |
| 796 |
end; |
| 797 |
|
| 798 |
{ Go the way to LastSensedEnemy, *not* by using waypoints. |
| 799 |
Assumes HasLastSensedEnemy. } |
| 800 |
procedure MoveNormal; |
| 801 |
var |
| 802 |
DirectionToTarget: TVector3; |
| 803 |
AngleBetweenDirectionToTarget: Single; |
| 804 |
begin |
| 805 |
CalculateDirectionToEnemy(DirectionToTarget, |
| 806 |
AngleBetweenDirectionToTarget); |
| 807 |
|
| 808 |
if WantToMoveToEnemy(AngleBetweenDirectionToTarget) then |
| 809 |
begin |
| 810 |
if not MoveAlongTheDirection then |
| 811 |
begin |
| 812 |
{ Not able to get to enemy this way ? Maybe there exists |
| 813 |
some alternative way, not straight. Lets try. } |
| 814 |
InitAlternativeTarget; |
| 815 |
Exit; |
| 816 |
end; |
| 817 |
end else |
| 818 |
begin |
| 819 |
{ I don't want to move anymore. So just stand stil. } |
| 820 |
SetState(stateIdle); |
| 821 |
Exit; |
| 822 |
end; |
| 823 |
|
| 824 |
RotateDirectionToFaceTarget(DirectionToTarget, |
| 825 |
AngleBetweenDirectionToTarget); |
| 826 |
end; |
| 827 |
|
| 828 |
procedure MoveToWaypoint(const Target: TVector3); |
| 829 |
var |
| 830 |
DirectionToTarget: TVector3; |
| 831 |
AngleBetweenDirectionToTarget: Single; |
| 832 |
begin |
| 833 |
CalculateDirectionToTarget(Target, DirectionToTarget, |
| 834 |
AngleBetweenDirectionToTarget); |
| 835 |
|
| 836 |
if WantToShortenDistanceToEnemy then |
| 837 |
begin |
| 838 |
if not MoveAlongTheDirection then |
| 839 |
begin |
| 840 |
{ Not able to get to waypoint this way ? Maybe there exists |
| 841 |
some alternative way, not straight. Lets try. } |
| 842 |
InitAlternativeTarget; |
| 843 |
Exit; |
| 844 |
end; |
| 845 |
end else |
| 846 |
begin |
| 847 |
{ I don't want to move anymore. So just stand stil. } |
| 848 |
SetState(stateIdle); |
| 849 |
Exit; |
| 850 |
end; |
| 851 |
|
| 852 |
RotateDirectionToFaceTarget(DirectionToTarget, |
| 853 |
AngleBetweenDirectionToTarget); |
| 854 |
end; |
| 855 |
|
| 856 |
const |
| 857 |
ProbabilityToTryAnotherAlternativeTarget = 0.5; |
| 858 |
AngleBetweenDirectionToTargetToResign = Pi * 1 / 180; |
| 859 |
MaxTimeForAlternativeTarget = 5.0; |
| 860 |
var |
| 861 |
DirectionToTarget: TVector3; |
| 862 |
AngleBetweenDirectionToTarget: Single; |
| 863 |
SectorNow: TSector; |
| 864 |
UseMoveNormal: boolean; |
| 865 |
begin |
| 866 |
if HasAlternativeTarget then |
| 867 |
begin |
| 868 |
if CloseEnoughToTarget(AlternativeTarget) or |
| 869 |
(LifeTime - AlternativeTargetTime > MaxTimeForAlternativeTarget) then |
| 870 |
begin |
| 871 |
HasAlternativeTarget := false; |
| 872 |
Exit; |
| 873 |
end; |
| 874 |
|
| 875 |
CalculateDirectionToTarget(AlternativeTarget, |
| 876 |
DirectionToTarget, AngleBetweenDirectionToTarget); |
| 877 |
|
| 878 |
if WantToMoveToTarget(AlternativeTarget, |
| 879 |
AngleBetweenDirectionToTarget) then |
| 880 |
begin |
| 881 |
{ Note that MoveAlongTheDirection returns false when |
| 882 |
moving along the current Direction is not good. |
| 883 |
But maybe moving along the DirectionToTarget is possible ? |
| 884 |
So we shouldn't just resign from current AlternativeTarget |
| 885 |
so fast --- maybe it's good, but we have to adjust |
| 886 |
our Direction a little more. That's why I use |
| 887 |
AngleBetweenDirectionToTargetToResign. |
| 888 |
|
| 889 |
Note that for normal moving (i.e. toward LastSensedEnemy, |
| 890 |
not AlternativeTarget) we in this case just change state |
| 891 |
to stateIdle, and this allows creature to rotate in stateIdle |
| 892 |
state. } |
| 893 |
if (not MoveAlongTheDirection) and |
| 894 |
(AngleBetweenDirectionToTarget <= |
| 895 |
AngleBetweenDirectionToTargetToResign) then |
| 896 |
begin |
| 897 |
if Random <= ProbabilityToTryAnotherAlternativeTarget then |
| 898 |
begin |
| 899 |
{ Try yet another alternative way. } |
| 900 |
InitAlternativeTarget; |
| 901 |
Exit; |
| 902 |
end else |
| 903 |
begin |
| 904 |
HasAlternativeTarget := false; |
| 905 |
Exit; |
| 906 |
end; |
| 907 |
end; |
| 908 |
end else |
| 909 |
begin |
| 910 |
{ We know that WantToMoveToTarget may return false only because |
| 911 |
were not directed enough for AlternativeTarget. |
| 912 |
(because we already eliminated CloseEnoughToTarget case above). |
| 913 |
In each DoMove call we will gradually fix this, |
| 914 |
by RotateDirectionToFaceTarget below. |
| 915 |
So do nothing now. Just stay in stateMove mode, |
| 916 |
and do RotateDirectionToFaceTarget below. } |
| 917 |
end; |
| 918 |
|
| 919 |
RotateDirectionToFaceTarget(DirectionToTarget, |
| 920 |
AngleBetweenDirectionToTarget); |
| 921 |
end else |
| 922 |
if WantToRunAway then |
| 923 |
begin |
| 924 |
CalculateDirectionFromEnemy(DirectionToTarget, |
| 925 |
AngleBetweenDirectionToTarget); |
| 926 |
|
| 927 |
if WantToMoveInDesiredDirection(AngleBetweenDirectionToTarget) then |
| 928 |
begin |
| 929 |
if (not MoveAlongTheDirection) and |
| 930 |
(AngleBetweenDirectionToTarget <= |
| 931 |
AngleBetweenDirectionToTargetToResign) then |
| 932 |
begin |
| 933 |
{ Maybe there exists some alternative way, not straight. Lets try. } |
| 934 |
InitAlternativeTarget; |
| 935 |
Exit; |
| 936 |
end; |
| 937 |
end; |
| 938 |
|
| 939 |
RotateDirectionToFaceTarget(DirectionToTarget, |
| 940 |
AngleBetweenDirectionToTarget); |
| 941 |
end else |
| 942 |
begin |
| 943 |
if not HasLastSensedEnemy then |
| 944 |
begin |
| 945 |
{ Nowhere to go; so just stay here. } |
| 946 |
SetState(stateIdle); |
| 947 |
Exit; |
| 948 |
end; |
| 949 |
|
| 950 |
UseMoveNormal := true; |
| 951 |
|
| 952 |
SectorNow := Sector; |
| 953 |
if (SectorNow <> LastSensedEnemySector) and |
| 954 |
(SectorNow <> nil) and |
| 955 |
(LastSensedEnemySector <> nil) then |
| 956 |
begin |
| 957 |
{ The way to LastSensedEnemy is using waypoints. } |
| 958 |
|
| 959 |
{ Recalculate WaypointsSaved. |
| 960 |
Note that I recalculate only when SectorNow or |
| 961 |
LastSensedEnemySector changed. } |
| 962 |
if (SectorNow <> WaypointsSaved_Begin) or |
| 963 |
(LastSensedEnemySector <> WaypointsSaved_End) then |
| 964 |
begin |
| 965 |
WaypointsSaved_Begin := SectorNow; |
| 966 |
WaypointsSaved_End := LastSensedEnemySector; |
| 967 |
TSectorList.FindWay(WaypointsSaved_Begin, WaypointsSaved_End, |
| 968 |
WaypointsSaved); |
| 969 |
end; |
| 970 |
|
| 971 |
if WaypointsSaved.Count <> 0 then |
| 972 |
begin |
| 973 |
{ There is a space around the waypoint that is within |
| 974 |
more than one sector. SectorWithPoint will then answer |
| 975 |
with any (it's not specified which) sector that has |
| 976 |
given position. This is problematic, because this means |
| 977 |
that the creature will be forced to go once again to the same |
| 978 |
waypoint that it's already at... This way there could arise |
| 979 |
a situation when the creature gets stuck at some waypoint, |
| 980 |
because we constantly detect that it must pass through this |
| 981 |
waypoint. The check for CloseEnoughToTarget below prevents this. } |
| 982 |
if CloseEnoughToTarget(WaypointsSaved[0].Position) then |
| 983 |
begin |
| 984 |
if WaypointsSaved.Count > 1 then |
| 985 |
begin |
| 986 |
MoveToWaypoint(WaypointsSaved[1].Position); |
| 987 |
UseMoveNormal := false; |
| 988 |
end; |
| 989 |
end else |
| 990 |
begin |
| 991 |
MoveToWaypoint(WaypointsSaved[0].Position); |
| 992 |
UseMoveNormal := false; |
| 993 |
end; |
| 994 |
end; |
| 995 |
end; |
| 996 |
|
| 997 |
if UseMoveNormal then |
| 998 |
MoveNormal; |
| 999 |
end; |
| 1000 |
|
| 1001 |
if FireMissileAllowed then |
| 1002 |
SetState(stateFireMissile) else |
| 1003 |
if AttackAllowed then |
| 1004 |
SetState(stateAttack); |
| 1005 |
end; |
| 1006 |
|
| 1007 |
{ Go to the default state, like "idle". |
| 1008 |
Doing this instead of SetState(stateIdle) avoids switching to "idle" just for |
| 1009 |
a single frame, which looks bad (animation visibly jumps for 1 frame, |
| 1010 |
and also animation blending is broken by such 1-frame change, |
| 1011 |
since our animation blending now can only transition from last to next animation). } |
| 1012 |
procedure BackToDefaultState; |
| 1013 |
begin |
| 1014 |
if DoIdle then |
| 1015 |
SetState(stateIdle); |
| 1016 |
end; |
| 1017 |
|
| 1018 |
procedure DoAttack; |
| 1019 |
var |
| 1020 |
StateTime: Single; |
| 1021 |
begin |
| 1022 |
StateTime := LifeTime - StateChangeTime; |
| 1023 |
if (not AttackDone) and (StateTime >= Resource.AttackTime) then |
| 1024 |
begin |
| 1025 |
AttackDone := true; |
| 1026 |
Attack; |
| 1027 |
end; |
| 1028 |
if StateTime > AnimationDuration(AnimationAttack) then |
| 1029 |
BackToDefaultState; |
| 1030 |
end; |
| 1031 |
|
| 1032 |
procedure DoFireMissile; |
| 1033 |
var |
| 1034 |
StateTime: Single; |
| 1035 |
begin |
| 1036 |
StateTime := LifeTime - StateChangeTime; |
| 1037 |
if (not FireMissileDone) and (StateTime >= Resource.FireMissileTime) then |
| 1038 |
begin |
| 1039 |
FireMissileDone := true; |
| 1040 |
FireMissile; |
| 1041 |
end; |
| 1042 |
if StateTime > AnimationDuration(AnimationFireMissile) then |
| 1043 |
BackToDefaultState; |
| 1044 |
end; |
| 1045 |
|
| 1046 |
procedure DoHurt; |
| 1047 |
var |
| 1048 |
StateTime: Single; |
| 1049 |
begin |
| 1050 |
StateTime := LifeTime - StateChangeTime; |
| 1051 |
|
| 1052 |
if StateTime > AnimationDuration(AnimationHurt) then |
| 1053 |
begin |
| 1054 |
CancelKnockback; |
| 1055 |
BackToDefaultState; |
| 1056 |
end; |
| 1057 |
end; |
| 1058 |
|
| 1059 |
{ @true if last attack was from the back of the creature, |
| 1060 |
@false if from the front or unknown (when LastHurtDirection is zero). } |
| 1061 |
function WasLastAttackBack: boolean; |
| 1062 |
begin |
| 1063 |
try |
| 1064 |
Result := AngleRadBetweenVectors(LastHurtDirection, Parent.Direction) < Pi/2; |
| 1065 |
except |
| 1066 |
on EVectorInvalidOp do Result := false; |
| 1067 |
end; |
| 1068 |
end; |
| 1069 |
|
| 1070 |
procedure DoDie(const AnimationDuration: Single); |
| 1071 |
begin |
| 1072 |
if Resource.RemoveDead and |
| 1073 |
(LifeTime - StateChangeTime > AnimationDuration) then |
| 1074 |
RemoveMe := rtRemoveAndFree; |
| 1075 |
end; |
| 1076 |
|
| 1077 |
(* TODO: debug visualization |
| 1078 |
procedure UpdateDebugTransform; |
| 1079 |
begin |
| 1080 |
if RenderDebug then |
| 1081 |
begin |
| 1082 |
if FDebugAlternativeTargetAxis = nil then |
| 1083 |
begin |
| 1084 |
FDebugAlternativeTargetAxis := TDebugAxis.Create(Self, BlueRGB); |
| 1085 |
FDebugTransform.ParentSpace.AddChildren(FDebugAlternativeTargetAxis.Root); |
| 1086 |
FDebugTransform.ChangedScene; |
| 1087 |
end; |
| 1088 |
|
| 1089 |
FDebugAlternativeTargetAxis.Render := HasAlternativeTarget; |
| 1090 |
FDebugAlternativeTargetAxis.ScaleFromBox := BoundingBox; |
| 1091 |
FDebugAlternativeTargetAxis.Position := AlternativeTarget; |
| 1092 |
|
| 1093 |
if FDebugLastSensedEnemyAxis = nil then |
| 1094 |
begin |
| 1095 |
FDebugLastSensedEnemyAxis := TDebugAxis.Create(Self, RedRGB); |
| 1096 |
FDebugTransform.ParentSpace.AddChildren(FDebugLastSensedEnemyAxis.Root); |
| 1097 |
FDebugTransform.ChangedScene; |
| 1098 |
end; |
| 1099 |
|
| 1100 |
FDebugLastSensedEnemyAxis.Render := HasLastSensedEnemy; |
| 1101 |
FDebugLastSensedEnemyAxis.ScaleFromBox := BoundingBox; |
| 1102 |
FDebugLastSensedEnemyAxis.Position := LastSensedEnemy; |
| 1103 |
end; |
| 1104 |
end; |
| 1105 |
*) |
| 1106 |
|
| 1107 |
var |
| 1108 |
E: TCastleTransform; |
| 1109 |
begin |
| 1110 |
inherited; |
| 1111 |
if DebugTimeStopForCreatures then Exit; |
| 1112 |
|
| 1113 |
FLifeTime += SecondsPassed; |
| 1114 |
|
| 1115 |
{ eventually turn off InternalMiddleForceBox } |
| 1116 |
Parent.InternalMiddleForceBox := Parent.InternalMiddleForceBox and (LifeTime <= InternalMiddleForceBoxTime); |
| 1117 |
|
| 1118 |
if Dead and not (State in [stateDie, stateDieBack]) then |
| 1119 |
begin |
| 1120 |
if (AnimationDieBack <> '') and WasLastAttackBack then |
| 1121 |
SetState(stateDieBack) |
| 1122 |
else |
| 1123 |
SetState(stateDie); |
| 1124 |
Exit; |
| 1125 |
end; |
| 1126 |
|
| 1127 |
if Enemy <> nil then |
| 1128 |
E := Enemy.Parent |
| 1129 |
else |
| 1130 |
E := nil; |
| 1131 |
EnemySensedNow := (E <> nil) and ( |
| 1132 |
( |
| 1133 |
{ enemy seen } |
| 1134 |
(AngleRadBetweenNormals(E.Middle - Parent.Middle, Parent.Direction) <= |
| 1135 |
Resource.VisibilityAngle / 2) and |
| 1136 |
Parent.LineOfSight(Parent.Middle, E.Middle) |
| 1137 |
) or |
| 1138 |
( |
| 1139 |
{ enemy smelled } |
| 1140 |
PointsDistanceSqr(E.Middle, Parent.Middle) < Sqr(Resource.SmellDistance) |
| 1141 |
) ); |
| 1142 |
if EnemySensedNow then |
| 1143 |
begin |
| 1144 |
HasLastSensedEnemy := true; |
| 1145 |
LastSensedEnemy := E.Middle; |
| 1146 |
LastSensedEnemySector := Sector(E); |
| 1147 |
end; |
| 1148 |
|
| 1149 |
if HasLastSensedEnemy then |
| 1150 |
begin |
| 1151 |
SqrDistanceToLastSensedEnemy := PointsDistanceSqr(LastSensedEnemy, Parent.Middle); |
| 1152 |
end; |
| 1153 |
|
| 1154 |
case FState of |
| 1155 |
stateIdle: DoIdle; |
| 1156 |
stateMove: DoMove; |
| 1157 |
stateAttack: DoAttack; |
| 1158 |
stateFireMissile: DoFireMissile; |
| 1159 |
stateDie : DoDie(AnimationDuration(AnimationDie)); |
| 1160 |
stateDieBack: DoDie(AnimationDuration(AnimationDieBack)); |
| 1161 |
stateHurt: DoHurt; |
| 1162 |
end; |
| 1163 |
|
| 1164 |
{ Flying creatures may change their direction vector freely. |
| 1165 |
However, we want them to keep their sense of up --- they should try |
| 1166 |
to keep straight, so their up vector should try to remain close |
| 1167 |
to GravityUp, not just change wildly. |
| 1168 |
|
| 1169 |
For non-flying, this is not needed, as then Up should always remain equal |
| 1170 |
to initial value, which is GravityUp. } |
| 1171 |
if not Parent.Gravity then |
| 1172 |
Parent.UpPrefer(Parent.World.GravityUp); |
| 1173 |
|
| 1174 |
// UpdateDebugTransform; |
| 1175 |
end; |
| 1176 |
|
| 1177 |
(* TODO: run hurt, listen on TCastleAliveBehavior |
| 1178 |
procedure TCastleMoveAttack.SetLife(const Value: Single); |
| 1179 |
begin |
| 1180 |
if (not Dead) and |
| 1181 |
(Life - Value > Resource.MinLifeLossToHurt * MaxLife) and |
| 1182 |
( (Resource.ChanceToHurt = 1.0) or |
| 1183 |
(Random < Resource.ChanceToHurt) ) then |
| 1184 |
SetState(stateHurt); |
| 1185 |
inherited; |
| 1186 |
end; |
| 1187 |
*) |
| 1188 |
|
| 1189 |
procedure TCastleMoveAttack.Attack; |
| 1190 |
var |
| 1191 |
E: TCastleAliveBehavior; |
| 1192 |
|
| 1193 |
function ShortRangeAttackHits: boolean; |
| 1194 |
var |
| 1195 |
B, EB: TBox3D; |
| 1196 |
DistanceLength, DistanceIncrease: Single; |
| 1197 |
begin |
| 1198 |
if E = nil then Exit(false); { no enemy to hurt } |
| 1199 |
B := BoundingBox; |
| 1200 |
EB := E.BoundingBox; |
| 1201 |
|
| 1202 |
{ We would like to check collision between EB and our B translated |
| 1203 |
by our Direction now, i.e. |
| 1204 |
B.Translate(Direction * ???).Collision(EB) |
| 1205 |
But how much should be scale Direction, i.e. what to put for "???" ? |
| 1206 |
It must be large enough to compensate even large Resource.AttackMaxDistance, |
| 1207 |
it must be small enough so that enemy should not be able to avoid |
| 1208 |
our attacks just by standing very close to the creature. |
| 1209 |
|
| 1210 |
So we have to check a couple of bounding boxes. |
| 1211 |
If we move our boxes by Box3DMinSize(B), we're sure that |
| 1212 |
each box will stick to the previous and next. But maybe |
| 1213 |
there will be some areas around the sticking points ? |
| 1214 |
So B.MinSize / 2 seems safe. } |
| 1215 |
DistanceIncrease := B.MinSize / 2; |
| 1216 |
|
| 1217 |
DistanceLength := DistanceIncrease; |
| 1218 |
while DistanceLength < Resource.AttackMaxDistance do |
| 1219 |
begin |
| 1220 |
if B.Translate(Direction * DistanceLength).Collision(EB) then |
| 1221 |
Exit(true); |
| 1222 |
DistanceLength := DistanceLength + DistanceIncrease; |
| 1223 |
end; |
| 1224 |
|
| 1225 |
{ Check one last time for Resource.AttackMaxDistance } |
| 1226 |
Result := B.Translate(Direction * Resource.AttackMaxDistance).Collision(EB); |
| 1227 |
end; |
| 1228 |
|
| 1229 |
begin |
| 1230 |
E := Enemy; |
| 1231 |
if ShortRangeAttackHits then |
| 1232 |
begin |
| 1233 |
PlaySound(FSoundTypeAttackHit, 1.0); |
| 1234 |
AttackHurt(E); |
| 1235 |
end; |
| 1236 |
end; |
| 1237 |
|
| 1238 |
procedure TCastleMoveAttack.FireMissile; |
| 1239 |
(* TODO: fire missile: |
| 1240 |
var |
| 1241 |
Missile: TCreature; |
| 1242 |
MissilePosition, MissileDirection: TVector3; |
| 1243 |
begin |
| 1244 |
if (Resource.FireMissileName <> '') and HasLastSensedEnemy then |
| 1245 |
begin |
| 1246 |
MissilePosition := LerpLegsMiddle(Resource.FireMissileHeight); |
| 1247 |
MissileDirection := LastSensedEnemy - MissilePosition; |
| 1248 |
Missile := (Resources.FindName(Resource.FireMissileName) as TCreatureResource). |
| 1249 |
CreateCreature(Level, MissilePosition, MissileDirection); |
| 1250 |
Missile.Sound3d(Resource.FireMissileSound, 0.0); |
| 1251 |
end; |
| 1252 |
end; |
| 1253 |
*) |
| 1254 |
begin |
| 1255 |
end; |
| 1256 |
|
| 1257 |
(* TODO: debug visualization |
| 1258 |
procedure TCastleMoveAttack.UpdateDebugCaption(const Lines: TCastleStringList); |
| 1259 |
var |
| 1260 |
StateName: string; |
| 1261 |
begin |
| 1262 |
inherited; |
| 1263 |
|
| 1264 |
case State of |
| 1265 |
stateIdle : StateName := 'Idle'; |
| 1266 |
stateMove : StateName := 'Move'; |
| 1267 |
stateAttack : StateName := 'Attack'; |
| 1268 |
stateFireMissile: StateName := 'FireMissile'; |
| 1269 |
stateDie : StateName := 'Die'; |
| 1270 |
stateDieBack : StateName := 'DieBack'; |
| 1271 |
stateHurt : StateName := 'Hurt'; |
| 1272 |
else StateName := Format('Custom State %d', [State]); |
| 1273 |
end; |
| 1274 |
Lines.Add(StateName); |
| 1275 |
|
| 1276 |
if HasLastSensedEnemy then |
| 1277 |
Lines.Add(Format('Enemy sensed distance: %f', |
| 1278 |
[PointsDistance(LastSensedEnemy, Middle)])); |
| 1279 |
end; |
| 1280 |
*) |
| 1281 |
|
| 1282 |
(* TODO: be notified from TCastleAliveBehavior |
| 1283 |
procedure TCastleMoveAttack.Hurt(const LifeLoss: Single; |
| 1284 |
const HurtDirection: TVector3; |
| 1285 |
const AHurtStrength: Single; const Attacker: TCastleAlive); |
| 1286 |
begin |
| 1287 |
inherited Hurt(LifeLoss, HurtDirection, |
| 1288 |
AHurtStrength * Resource.KnockBackDistance, Attacker); |
| 1289 |
|
| 1290 |
{ If attacked by Enemy, update LastSensedEnemy fields. |
| 1291 |
This way when you attack a creature from the back, it will turn around |
| 1292 |
and fight you. } |
| 1293 |
if (Attacker <> nil) and (Attacker = Enemy) then |
| 1294 |
begin |
| 1295 |
HasLastSensedEnemy := true; |
| 1296 |
LastSensedEnemy := Attacker.Middle; |
| 1297 |
LastSensedEnemySector := Sector(Attacker); |
| 1298 |
end; |
| 1299 |
end; |
| 1300 |
*) |
| 1301 |
|
| 1302 |
function TCastleMoveAttack.PropertySections( |
| 1303 |
const PropertyName: String): TPropertySections; |
| 1304 |
begin |
| 1305 |
case PropertyName of |
| 1306 |
// 'Life', 'MaxLife': // TODO |
| 1307 |
// Result := [psBasic]; |
| 1308 |
// else |
| 1309 |
Result := inherited PropertySections(PropertyName); |
| 1310 |
end; |
| 1311 |
end; |
| 1312 |
|
| 1313 |
function TCastleMoveAttack.AnimationIdleStored: Boolean; |
| 1314 |
begin |
| 1315 |
Result := FAnimationIdle <> DefaultAnimationIdle; |
| 1316 |
end; |
| 1317 |
|
| 1318 |
function TCastleMoveAttack.AnimationMoveStored: Boolean; |
| 1319 |
begin |
| 1320 |
Result := FAnimationMove <> DefaultAnimationMove; |
| 1321 |
end; |
| 1322 |
|
| 1323 |
function TCastleMoveAttack.AnimationAttackStored: Boolean; |
| 1324 |
begin |
| 1325 |
Result := FAnimationAttack <> DefaultAnimationAttack; |
| 1326 |
end; |
| 1327 |
|
| 1328 |
function TCastleMoveAttack.AnimationFireMissileStored: Boolean; |
| 1329 |
begin |
| 1330 |
Result := FAnimationFireMissile <> DefaultAnimationFireMissile; |
| 1331 |
end; |
| 1332 |
|
| 1333 |
function TCastleMoveAttack.AnimationDieStored: Boolean; |
| 1334 |
begin |
| 1335 |
Result := FAnimationDie <> DefaultAnimationDie; |
| 1336 |
end; |
| 1337 |
|
| 1338 |
function TCastleMoveAttack.AnimationDieBackStored: Boolean; |
| 1339 |
begin |
| 1340 |
Result := FAnimationDieBack <> DefaultAnimationDieBack; |
| 1341 |
end; |
| 1342 |
|
| 1343 |
function TCastleMoveAttack.AnimationHurtStored: Boolean; |
| 1344 |
begin |
| 1345 |
Result := FAnimationHurt <> DefaultAnimationHurt; |
| 1346 |
end; |
| 1347 |
|
| 1348 |
{$endif read_implementation} |