Skip to content

Commit

Permalink
Rework monster decision code #109: PoC introduce Behavior tree
Browse files Browse the repository at this point in the history
follower ai - follows the attacker, work only along straight lines
  • Loading branch information
demoth committed Jan 16, 2024
1 parent 21ea49e commit d00c3f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions game/src/main/java/jake2/game/SV.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ of the License, or (at your option) any later version.
/**
* SV
*/
final class SV {
final public class SV {

private final cvar_t sv_maxvelocity;

Expand Down Expand Up @@ -1069,7 +1069,7 @@ && SV_StepDirection(actor, turnaround, dist, gameExports))
* SV_CloseEnough - returns true if distance between 2 ents is smaller than
* given dist.
*/
static boolean SV_CloseEnough(SubgameEntity ent, edict_t goal, float dist) {
public static boolean SV_CloseEnough(SubgameEntity ent, edict_t goal, float dist) {
int i;

for (i = 0; i < 3; i++) {
Expand Down
49 changes: 32 additions & 17 deletions game/src/main/kotlin/jake2/game/character/GameCharacter.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package jake2.game.character

import jake2.game.GameDefines
import jake2.game.GameExportsImpl
import jake2.game.M
import jake2.game.SubgameEntity
import jake2.game.*
import jake2.game.adapters.SuperAdapter.Companion.registerThink
import jake2.game.components.ThinkComponent
import jake2.qcommon.Com
import jake2.qcommon.Defines
import jake2.qcommon.math.Vector3f
import jake2.qcommon.util.Math3D
import kotlin.random.Random

Expand Down Expand Up @@ -148,20 +145,25 @@ class GameCharacter(
// these commands are called either by AI or a Player.
// could be called continuously
//
fun aim(to: Vector3f) {
TODO()
// todo: all these actions should check if character is not dead or somehow disabled
// usually it's verified by the state machine, but when it's not used (like in the aim() method) - should be checked explicitly

fun aim(enemyYaw: Float) {
if (notDisabled()) {
self.ideal_yaw = enemyYaw
M.rotateToIdealYaw(self)
}
}


fun walk() {
if (stateMachine.attemptStateChange("walk")) {
M.rotateToIdealYaw(self)
M.M_walkmove(self, self.ideal_yaw, 5f, game)
}
}

fun run() {
if (stateMachine.attemptStateChange("run")) {
M.rotateToIdealYaw(self)
M.M_walkmove(self, self.ideal_yaw, 15f, game)
}
}
Expand All @@ -174,7 +176,7 @@ class GameCharacter(
fun jump() {
if (stateMachine.attemptStateChange("jump")) {
// GameLogic.tossCharacter(...)
// transitions to "stand" once hit the ground // todo: where is this code?
// transitions to "stand" once hit the ground // todo: where is this code? in the M_CheckGround?
}
}

Expand All @@ -192,13 +194,16 @@ class GameCharacter(
stateMachine.attemptStateChange("dead")
}

private fun notDisabled() =
stateMachine.currentState.type != StateType.PAIN && stateMachine.currentState.type != StateType.DEAD

private fun sound(soundIndex: Int,
channel: Int = Defines.CHAN_VOICE,
volume: Float = 1f,
attenuation: Float = Defines.ATTN_IDLE.toFloat(),
timeOffset: Float = 0f) {
timeOffset: Float = 0f) =
game.gameImports.sound(self, channel, soundIndex, volume, attenuation, timeOffset)
}


}

Expand All @@ -225,19 +230,29 @@ fun spawnNewMonster(self: SubgameEntity, game: GameExportsImpl) {

self.controller = selector(
sequence(
// should hunt the enemy?
node { self.enemy != null },
node {
// rotate towards the enemy
finish {
val distance = floatArrayOf(0f, 0f, 0f)
Math3D.VectorSubtract(self.enemy.s.origin, self.s.origin, distance)
val enemyYaw = Math3D.vectoyaw(distance)
self.ideal_yaw = enemyYaw
true

self.character.aim(enemyYaw)
},
// move towards the enemy
finish {
if (self.character.health >= 50)
if (SV.SV_CloseEnough(self, self.enemy, 50f)) {
// idle
Com.dprintln("SV_CloseEnough: idle")
self.character.idle()
} else if (SV.SV_CloseEnough(self, self.enemy, 200f)) {
Com.dprintln("SV_CloseEnough: walk")
self.character.walk()
else
} else {
Com.dprintln("SV_CloseEnough: run")
self.character.run()
}
}
),
sequence(
Expand Down

0 comments on commit d00c3f9

Please sign in to comment.