Skip to content

Commit

Permalink
- wrap all personality decision actions in VM functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed May 28, 2023
1 parent 540f964 commit f12cc01
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 354 deletions.
8 changes: 7 additions & 1 deletion source/games/sw/src/actor.cpp
Expand Up @@ -1101,9 +1101,15 @@ void DSWActor::callAction()
void DSWActor::callStateAction()
{
if (user.__legacyState.State && user.__legacyState.State->Animator)
callFunction(*user.__legacyState.State->Animator);
}

void DSWActor::callFunction(VMFunction* func)
{
if (func)
{
VMValue param[] = { this };
VMCall(*user.__legacyState.State->Animator, param, 1, nullptr, 0);
VMCall(func, param, 1, nullptr, 0);
}
}

Expand Down
67 changes: 31 additions & 36 deletions source/games/sw/src/ai.cpp
Expand Up @@ -43,7 +43,7 @@ BEGIN_SW_NS
ANIMATOR InitActorRunToward;
bool DropAhead(DSWActor* actor, double min_height);

ANIMATOR* ChooseAction(DECISION decision[]);
VMFunction* ChooseAction(DECISION decision[]);


#define CHOOSE2(value) (RANDOM_P2(1024) < (value))
Expand All @@ -68,11 +68,7 @@ bool ActorMoveHitReact(DSWActor* actor)
// if you ran into a player - call close range functions
DoActorPickClosePlayer(actor);
auto action = ChooseAction(actor->user.__legacyState.Personality->TouchTarget);
if (action)
{
(*action)(actor);
return true;
}
actor->callFunction(action);
}
}
return false;
Expand Down Expand Up @@ -126,7 +122,7 @@ void DoActorSetSpeed(DSWActor* actor, uint8_t speed)
*/
//---------------------------------------------------------------------------

ANIMATOR* ChooseAction(DECISION decision[])
VMFunction* ChooseAction(DECISION decision[])
{
// !JIM! Here is an opportunity for some AI, instead of randomness!
int random_value = RANDOM_P2(1024<<5)>>5;
Expand All @@ -137,7 +133,7 @@ ANIMATOR* ChooseAction(DECISION decision[])

if (random_value <= decision[i].range)
{
return decision[i].action;
return *decision[i].action;
}
}
}
Expand Down Expand Up @@ -167,50 +163,50 @@ int ChooseActionNumber(int16_t decision[])
//
//---------------------------------------------------------------------------

int DoActorNoise(ANIMATOR* Action, DSWActor* actor)
int DoActorNoise(VMFunction* Action, DSWActor* actor)
{
if (Action == InitActorAmbientNoise)
if (Action == *AF(InitActorAmbientNoise))
{
PlaySpriteSound(actor, attr_ambient, v3df_follow);
}
else if (Action == InitActorAlertNoise)
else if (Action == *AF(InitActorAlertNoise))
{
if (actor->hasU() && !actor->user.DidAlert) // This only allowed once
PlaySpriteSound(actor, attr_alert, v3df_follow);
}
else if (Action == InitActorAttackNoise)
else if (Action == *AF(InitActorAttackNoise))
{
PlaySpriteSound(actor, attr_attack, v3df_follow);
}
else if (Action == InitActorPainNoise)
else if (Action == *AF(InitActorPainNoise))
{
PlaySpriteSound(actor, attr_pain, v3df_follow);
}
else if (Action == InitActorDieNoise)
else if (Action == *AF(InitActorDieNoise))
{
PlaySpriteSound(actor, attr_die, v3df_none);
}
else if (Action == InitActorExtra1Noise)
else if (Action == *AF(InitActorExtra1Noise))
{
PlaySpriteSound(actor, attr_extra1, v3df_follow);
}
else if (Action == InitActorExtra2Noise)
else if (Action == *AF(InitActorExtra2Noise))
{
PlaySpriteSound(actor, attr_extra2, v3df_follow);
}
else if (Action == InitActorExtra3Noise)
else if (Action == *AF(InitActorExtra3Noise))
{
PlaySpriteSound(actor, attr_extra3, v3df_follow);
}
else if (Action == InitActorExtra4Noise)
else if (Action == *AF(InitActorExtra4Noise))
{
PlaySpriteSound(actor, attr_extra4, v3df_follow);
}
else if (Action == InitActorExtra5Noise)
else if (Action == *AF(InitActorExtra5Noise))
{
PlaySpriteSound(actor, attr_extra5, v3df_follow);
}
else if (Action == InitActorExtra6Noise)
else if (Action == *AF(InitActorExtra6Noise))
{
PlaySpriteSound(actor, attr_extra6, v3df_follow);
}
Expand Down Expand Up @@ -455,9 +451,9 @@ int DoActorOperate(DSWActor* actor)

DECISION GenericFlaming[] =
{
{30, InitActorAttack},
{512, InitActorRunToward},
{1024, InitActorRunAway},
{30, AF(InitActorAttack)},
{512, AF(InitActorRunToward)},
{1024, AF(InitActorRunAway)},
};

/*
Expand All @@ -471,9 +467,9 @@ DECISION GenericFlaming[] =
do anymore and then this routine is called again.
*/

ANIMATOR* DoActorActionDecide(DSWActor* actor)
VMFunction* DoActorActionDecide(DSWActor* actor)
{
ANIMATOR* action;
VMFunction* action;
bool ICanSee=false;

// REMINDER: This function is not even called if SpriteControl doesn't let
Expand All @@ -482,7 +478,7 @@ ANIMATOR* DoActorActionDecide(DSWActor* actor)
ASSERT(actor->user.__legacyState.Personality);

actor->user.Dist = 0;
action = InitActorDecide;
action = *AF(InitActorDecide);

// target is gone.
if (actor->user.targetActor == nullptr)
Expand Down Expand Up @@ -555,7 +551,7 @@ ANIMATOR* DoActorActionDecide(DSWActor* actor)
// knows, its not a
// target any more
if (actor->hasState(NAME_Duck) && RANDOM_P2(1024<<8)>>8 < 100)
action = InitActorDuck;
action = *AF(InitActorDuck);
else
{
if ((actor->user.ID == COOLG_RUN_R0 && (actor->spr.cstat & CSTAT_SPRITE_TRANSLUCENT)) || (actor->spr.cstat & CSTAT_SPRITE_INVISIBLE))
Expand Down Expand Up @@ -618,7 +614,7 @@ ANIMATOR* DoActorActionDecide(DSWActor* actor)
//CON_Message("Surprised");
if (!actor->user.DidAlert && ICanSee)
{
DoActorNoise(InitActorAlertNoise, actor);
DoActorNoise(*AF(InitActorAlertNoise), actor);
actor->user.DidAlert = true;
}
return action;
Expand All @@ -635,7 +631,7 @@ ANIMATOR* DoActorActionDecide(DSWActor* actor)
}
}

//CON_Message("Couldn't resolve decide, InitActorDecide");
//CON_Message("Couldn't resolve decide, AF(InitActorDecide)");
return action;
}

Expand All @@ -659,21 +655,22 @@ int InitActorDecide(DSWActor* actor)

int DoActorDecide(DSWActor* actor)
{
ANIMATOR* actor_action;
VMFunction* actor_action;

// See what to do next

actor_action = DoActorActionDecide(actor);

// Fix for the GenericFlaming bug for actors that don't have attack states
if (actor_action == InitActorAttack && actor->user.WeaponNum == 0)
if (actor_action == *AF(InitActorAttack) && actor->user.WeaponNum == 0)
return 0; // Just let the actor do as it was doing before in this case

// Target is gone.
if (actor->user.targetActor == nullptr)
return 0;

// zombie is attacking a player
if (actor_action == InitActorAttack && actor->user.ID == ZOMBIE_RUN_R0 && actor->user.targetActor->user.PlayerP)
if (actor_action == *AF(InitActorAttack) && actor->user.ID == ZOMBIE_RUN_R0 && actor->user.targetActor->user.PlayerP)
{
// Don't let zombies shoot at master
if (GetOwner(actor) == actor->user.targetActor)
Expand All @@ -686,17 +683,15 @@ int DoActorDecide(DSWActor* actor)

ASSERT(actor_action != nullptr);

if (actor_action != InitActorDecide)
if (actor_action != *AF(InitActorDecide))
{
// NOT staying put
(*actor_action)(actor);
//CON_Message("DoActorDecide: NOT Staying put");
actor->callFunction(actor_action);
}
else
{
// Actually staying put
actor->setStateGroup(NAME_Stand);
//CON_Message("DoActorDecide: Staying put");
}

return 0;
Expand Down
23 changes: 1 addition & 22 deletions source/games/sw/src/ai.h
Expand Up @@ -32,32 +32,11 @@ class VMFunction;
BEGIN_SW_NS


// Call functions based on a random range value
struct Decision
{
int range;
VMFunction* action;
};

// Personality structure
struct Personality
{
Decision* Battle;
Decision* Offense;
Decision* Broadcast;
Decision* Surprised;
Decision* Evasive;
Decision* LostTarget;
Decision* CloseRange;
Decision* TouchTarget;
};


// Call functions based on a random range value
struct DECISION
{
int range;
ANIMATOR* action;
VMNativeFunction** action;
};

// Personality structure
Expand Down
41 changes: 20 additions & 21 deletions source/games/sw/src/bunny.cpp
Expand Up @@ -43,55 +43,54 @@ int Bunny_Count = 0;

DECISION BunnyBattle[] =
{
{748, InitActorMoveCloser},
{750, InitActorAlertNoise},
{760, InitActorAttackNoise},
{1024, InitActorMoveCloser}
{748, AF(InitActorMoveCloser)},
{750, AF(InitActorAlertNoise)},
{760, AF(InitActorAttackNoise)},
{1024, AF(InitActorMoveCloser)}
};

DECISION BunnyOffense[] =
{
{600, InitActorMoveCloser},
{700, InitActorAlertNoise},
{1024, InitActorMoveCloser}
{600, AF(InitActorMoveCloser)},
{700, AF(InitActorAlertNoise)},
{1024, AF(InitActorMoveCloser)}
};

DECISION BunnyBroadcast[] =
{
{21, InitActorAlertNoise},
{51, InitActorAmbientNoise},
{1024, InitActorDecide}
{21, AF(InitActorAlertNoise)},
{51, AF(InitActorAmbientNoise)},
{1024, AF(InitActorDecide)}
};

DECISION BunnySurprised[] =
{
{500, InitActorRunAway},
{701, InitActorMoveCloser},
{1024, InitActorDecide}
{500, AF(InitActorRunAway)},
{701, AF(InitActorMoveCloser)},
{1024, AF(InitActorDecide)}
};

DECISION BunnyEvasive[] =
{
{500, InitActorWanderAround},
{1020, InitActorRunAway},
{1024, InitActorAmbientNoise}
{500, AF(InitActorWanderAround)},
{1020, AF(InitActorRunAway)},
{1024, AF(InitActorAmbientNoise)}
};

DECISION BunnyLostTarget[] =
{
{900, InitActorFindPlayer},
{1024, InitActorWanderAround}
{900, AF(InitActorFindPlayer)},
{1024, AF(InitActorWanderAround)}
};

DECISION BunnyCloseRange[] =
{
{1024, InitActorAttack },
// {1024, InitActorReposition }
{1024, AF(InitActorAttack) },
};

DECISION BunnyWander[] =
{
{1024, InitActorReposition}
{1024, AF(InitActorReposition)}
};

PERSONALITY WhiteBunnyPersonality =
Expand Down

0 comments on commit f12cc01

Please sign in to comment.