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

Trigger improvements #636

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 18 additions & 10 deletions game/world/triggers/abstracttrigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ AbstractTrigger::AbstractTrigger(Vob* parent, World &world, const zenkit::Virtua
data.type == VirtualObjectType::oCTriggerScript || data.type == VirtualObjectType::zCMover ||
data.type == VirtualObjectType::oCTriggerChangeLevel || data.type == VirtualObjectType::oCCSTrigger) {
auto& trigger = reinterpret_cast<const zenkit::VTrigger&>(data);
fireDelay = uint64_t(trigger.fire_delay_sec*1000.f);
retriggerDelay = uint64_t(trigger.retrigger_delay_sec*1000.f);
maxActivationCount = (data.type==VirtualObjectType::zCMover && trigger.max_activation_count!=0) ? uint32_t(-1) : uint32_t(trigger.max_activation_count);
if(data.type == VirtualObjectType::zCMover) {
maxActivationCount = (trigger.max_activation_count==0) ? 0 : uint32_t(-1);
} else {
fireDelay = uint64_t(trigger.fire_delay_sec*1000.f);
retriggerDelay = uint64_t(trigger.retrigger_delay_sec*1000.f);
maxActivationCount = uint32_t(trigger.max_activation_count);
sendUntrigger = trigger.send_untrigger;
}
target = trigger.target;
disabled = !trigger.start_enabled;
sendUntrigger = trigger.send_untrigger;
reactToOnTrigger = trigger.react_to_on_trigger;
reactToOnTouch = trigger.react_to_on_touch;
respondToNpc = trigger.respond_to_npc;
Expand Down Expand Up @@ -78,6 +82,7 @@ void AbstractTrigger::processEvent(const TriggerEvent& evt) {
if(fireDelay>0) {
TriggerEvent ex(evt.target, evt.emitter, world.tickCount() + fireDelay, evt.type);
delayedEvent = std::move(ex);
world.enableDefTrigger(*this);
return;
}
implProcessEvent(evt);
Expand Down Expand Up @@ -136,15 +141,15 @@ void AbstractTrigger::moveEvent() {
}

void AbstractTrigger::onIntersect(Npc& n) {
if(vobType==zenkit::VirtualObjectType::zCMover || vobType==zenkit::VirtualObjectType::zCCSCamera)
return;
if((n.isPlayer() ? !respondToPlayer : !respondToNpc) || !reactToOnTouch)
return;

if(!isEnabled())
return;

if(boxNpc.intersections().size()==1) {
if(boxNpc.intersections().size()>0) {
// enableTicks();
TriggerEvent e("","",TriggerEvent::T_Touch);
TriggerEvent e(target,vobName,TriggerEvent::T_Touch);
processEvent(e);
}
}
Expand All @@ -168,8 +173,11 @@ void AbstractTrigger::load(Serialize& fin) {
fin.read(emitCount,disabled);
fin.read(emitTimeLast);

if(fin.version()>=47)
if(fin.version()>=47) {
delayedEvent.load(fin);
if(hasDelayedEvents())
world.enableDefTrigger(*this);
}
if(fin.version()>=48) {
fin.read(ticksEnabled);
if(ticksEnabled)
Expand Down Expand Up @@ -204,7 +212,7 @@ void AbstractTrigger::Cb::onCollide(DynamicWorld::BulletBody& b) {
return;
if(b.isSpell())
return;
TriggerEvent ex(tg->vobName,tg->vobName,tg->world.tickCount(),TriggerEvent::T_Touch);
TriggerEvent ex(tg->target,tg->vobName,tg->world.tickCount(),TriggerEvent::T_Touch);
tg->processEvent(ex);
}

Expand Down
4 changes: 4 additions & 0 deletions game/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ void World::execTriggerEvent(const TriggerEvent& e) {
}
}

void World::enableDefTrigger(AbstractTrigger& t) {
wobj.enableDefTrigger(t);
}

void World::enableTicks(AbstractTrigger& t) {
wobj.enableTicks(t);
}
Expand Down
1 change: 1 addition & 0 deletions game/world/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class World final {
void triggerEvent(const TriggerEvent& e);
void triggerChangeWorld(std::string_view world, std::string_view wayPoint);
void execTriggerEvent(const TriggerEvent& e);
void enableDefTrigger(AbstractTrigger& t);
void enableTicks (AbstractTrigger& t);
void disableTicks(AbstractTrigger& t);
void setCurrentCs(CsCamera* cs);
Expand Down
15 changes: 7 additions & 8 deletions game/world/worldobjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ void WorldObjects::load(Serialize &fin) {
for(auto& i:rootVobs)
i->loadVobTree(fin);

for(auto& i:triggers)
if(i->hasDelayedEvents())
triggersDef.push_back(i);

fin.setEntry("worlds/",fin.worldName(),"/triggerEvents");
fin.read(sz);
triggerEvents.resize(sz);
Expand Down Expand Up @@ -400,11 +396,7 @@ bool WorldObjects::execTriggerEvent(const TriggerEvent& e) {
auto& t = *i;
if(t.name()!=e.target)
continue; // NOTE: trigger name is not unique - more then one trigger can be activated

const bool hadDelayedEvt = t.hasDelayedEvents();
t.processEvent(e);
if(!hadDelayedEvt && t.hasDelayedEvents())
Try marked this conversation as resolved.
Show resolved Hide resolved
triggersDef.push_back(&t);
emitted = true;
}

Expand Down Expand Up @@ -503,6 +495,13 @@ void WorldObjects::addTrigger(AbstractTrigger* tg) {
triggers.emplace_back(tg);
}

void WorldObjects::enableDefTrigger(AbstractTrigger& t) {
for(auto& i:triggersDef)
if(i==&t)
return;
triggersDef.push_back(&t);
Try marked this conversation as resolved.
Show resolved Hide resolved
}

bool WorldObjects::triggerOnStart(bool firstTime) {
bool ret = false;
TriggerEvent evt("","",firstTime ? TriggerEvent::T_StartupFirstTime : TriggerEvent::T_Startup);
Expand Down
1 change: 1 addition & 0 deletions game/world/worldobjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class WorldObjects final {
CsCamera* currentCs() const;

void addTrigger(AbstractTrigger* trigger);
void enableDefTrigger(AbstractTrigger& trigger);
void triggerEvent(const TriggerEvent& e);
bool triggerOnStart(bool firstTime);
void execDelayedEvents();
Expand Down