Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(enhancement): Add "on encounter" trigger for NPCs #9049

Merged
merged 18 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion source/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ void Engine::Step(bool isActive)
player.SetPlanet(flagship->GetPlanet());

const System *currentSystem = player.GetSystem();

danaris marked this conversation as resolved.
Show resolved Hide resolved
// Update this here, for thread safety.
if(player.HasTravelPlan() && currentSystem == player.TravelPlan().back())
player.PopTravel();
Expand Down Expand Up @@ -1394,6 +1395,11 @@ void Engine::EnterSystem()
Messages::Add(GameData::HelpMessage("basics 1"), Messages::Importance::High);
Messages::Add(GameData::HelpMessage("basics 2"), Messages::Importance::High);
}

if(flagship->Cloaking() < 1.)
danaris marked this conversation as resolved.
Show resolved Hide resolved
for(const shared_ptr<Ship> &ship : ships)
if(ship->GetSystem() == system && ship->Cloaking() < 1.)
eventQueue.emplace_back(player.FlagshipPtr(), ship, ShipEvent::ENCOUNTER);
}


Expand Down Expand Up @@ -1461,10 +1467,22 @@ void Engine::CalculateStep()

// Keep track of the flagship to see if it jumps or enters a wormhole this turn.
const Ship *flagship = player.Flagship();
bool flagshipWasCloaked = (flagship && flagship->Cloaking() == 1);
bool wasHyperspacing = (flagship && flagship->IsEnteringHyperspace());
// Move all the ships.
// First, move the player's flagship.
MoveShip(player.FlagshipPtr());
// Then, move all the rest of the ships.
danaris marked this conversation as resolved.
Show resolved Hide resolved
for(const shared_ptr<Ship> &it : ships)
{
if(it == player.FlagshipPtr())
continue;
bool wasCloaked = (it->Cloaking() == 1.);
MoveShip(it);
// If we decloaked, *or* the player decloaked, and we're in the same system as the player, they encounter us.
danaris marked this conversation as resolved.
Show resolved Hide resolved
if(((wasCloaked && it->Cloaking() < 1.) || (flagshipWasCloaked && flagship->Cloaking() < 1.))
&& flagship->GetSystem() == it->GetSystem() && flagship->Cloaking() < 1.)
eventQueue.emplace_back(player.FlagshipPtr(), it, ShipEvent::ENCOUNTER);
}
// If the flagship just began jumping, play the appropriate sound.
if(!wasHyperspacing && flagship && flagship->IsEnteringHyperspace())
{
Expand Down Expand Up @@ -1729,6 +1747,9 @@ void Engine::MoveShip(const shared_ptr<Ship> &ship)
else
for(const auto &sound : jumpSounds)
Audio::Play(sound.first, position);

if(flagship->Cloaking() < 1. && ship->Cloaking() < 1.)
danaris marked this conversation as resolved.
Show resolved Hide resolved
eventQueue.emplace_back(player.FlagshipPtr(), ship, ShipEvent::ENCOUNTER);
}
}

Expand Down
13 changes: 9 additions & 4 deletions source/NPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace {
return "on destroy";
case NPC::Trigger::KILL:
return "on kill";
case NPC::Trigger::ENCOUNTER:
return "on encounter";
default:
return "unknown trigger";
}
Expand Down Expand Up @@ -197,7 +199,8 @@ void NPC::Load(const DataNode &node)
{"board", Trigger::BOARD},
{"capture", Trigger::CAPTURE},
{"destroy", Trigger::DESTROY},
{"kill", Trigger::KILL}
{"kill", Trigger::KILL},
{"encounter", Trigger::ENCOUNTER},
};
auto it = trigger.find(child.Token(1));
if(it != trigger.end())
Expand Down Expand Up @@ -747,6 +750,7 @@ void NPC::DoActions(const ShipEvent &event, bool newEvent, PlayerInfo &player, U
{ShipEvent::BOARD, {Trigger::BOARD}},
{ShipEvent::CAPTURE, {Trigger::CAPTURE, Trigger::KILL}},
{ShipEvent::DESTROY, {Trigger::DESTROY, Trigger::KILL}},
{ShipEvent::ENCOUNTER, {Trigger::ENCOUNTER}},
};

int type = event.Type();
Expand Down Expand Up @@ -778,7 +782,8 @@ void NPC::DoActions(const ShipEvent &event, bool newEvent, PlayerInfo &player, U
{Trigger::BOARD, ShipEvent::BOARD},
{Trigger::CAPTURE, ShipEvent::CAPTURE},
{Trigger::DESTROY, ShipEvent::DESTROY},
{Trigger::KILL, ShipEvent::CAPTURE | ShipEvent::DESTROY}
{Trigger::KILL, ShipEvent::CAPTURE | ShipEvent::DESTROY},
{Trigger::ENCOUNTER, ShipEvent::ENCOUNTER},
};

// Some Triggers cannot be met if any of the ships in this NPC have certain events.
Expand All @@ -792,10 +797,10 @@ void NPC::DoActions(const ShipEvent &event, bool newEvent, PlayerInfo &player, U
const auto excludedIt = triggerExclusions.find(trigger);
const int excludedEvents = excludedIt == triggerExclusions.end() ? 0 : excludedIt->second;

// The PROVOKE Trigger only requires a single ship to receive the
// The PROVOKE and ENCOUNTER Triggers only requires a single ship to receive the
// event in order to run. All other Triggers require that all ships
// be affected.
if(trigger == Trigger::PROVOKE || all_of(ships.begin(), ships.end(),
if(trigger == Trigger::ENCOUNTER || trigger == Trigger::PROVOKE || all_of(ships.begin(), ships.end(),
[&](const shared_ptr<Ship> &ship) -> bool
{
auto it = shipEvents.find(ship.get());
Expand Down
3 changes: 2 additions & 1 deletion source/NPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class NPC {
CAPTURE,
DESTROY,
danaris marked this conversation as resolved.
Show resolved Hide resolved
// Can be triggered by either the CAPTURE or DESTROY events.
KILL
KILL,
ENCOUNTER,
danaris marked this conversation as resolved.
Show resolved Hide resolved
danaris marked this conversation as resolved.
Show resolved Hide resolved
};


Expand Down
4 changes: 3 additions & 1 deletion source/ShipEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class ShipEvent {
// you had with the given government, first.
ATROCITY = (1 << 8),
// This ship just jumped into a different system.
JUMP = (1 << 9)
JUMP = (1 << 9),
// This ship just met another ship for the first time
ENCOUNTER = (1 << 10),
};


Expand Down