Skip to content

Commit

Permalink
NWN2: Implement CreateTrapOnObject script call
Browse files Browse the repository at this point in the history
  • Loading branch information
rjshae authored and DrMcCoy committed Feb 24, 2019
1 parent 6a62149 commit 89c8265
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/engines/nwn2/door.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ void Door::setLocked(bool locked) {
_linkedDoor->setLocked(locked);
}

void Door::createTrap(uint8 trapType, uint32 faction,
const Common::UString &disarm,
const Common::UString &triggered) {
Trap::createTrap(trapType, faction, disarm, triggered);
_faction = faction; // Overriding door faction

setScript(kScriptDisarm, disarm);
if (triggered != "")
setScript(kScriptTrapTriggered, triggered);
}

bool Door::click(Object *triggerer) {
_lastUsedBy = triggerer;

Expand Down
4 changes: 4 additions & 0 deletions src/engines/nwn2/door.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class Door : public Situated, public Trap {

/** Lock/Unlock the door. */
void setLocked(bool locked);
/** Create a trap on the door. */
void createTrap(uint8 trapType, uint32 faction,
const Common::UString &disarm,
const Common::UString &triggered);

// Object/Cursor interactions

Expand Down
12 changes: 12 additions & 0 deletions src/engines/nwn2/placeable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ bool Placeable::isActivated() const {
return isOpen();
}

void Placeable::createTrap(uint8 trapType, uint32 faction,
const Common::UString &disarm,
const Common::UString &triggered) {
Trap::createTrap(trapType, faction, disarm, triggered);
_faction = faction; // Overriding placeable faction

setScript(kScriptDisarm, disarm);
if (triggered != "")
setScript(kScriptTrapTriggered, triggered);
}


bool Placeable::click(Object *triggerer) {
// If the placeable is locked, just play the appropriate sound and bail
if (isLocked()) {
Expand Down
5 changes: 5 additions & 0 deletions src/engines/nwn2/placeable.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Placeable : public Situated, public Trap {
/** Is the placeable activated? */
bool isActivated() const;

/** Create a trap on the placeable. */
void createTrap(uint8 trapType, uint32 faction,
const Common::UString &disarm,
const Common::UString &triggered);

/** The opener object opens this placeable. */
bool open(Object *opener);
/** The closer object closes this placeable. */
Expand Down
6 changes: 6 additions & 0 deletions src/engines/nwn2/script/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ const Common::UString &ScriptContainer::getScript(Script script) const {
return _scripts[script];
}

void ScriptContainer::setScript(Script script, const Common::UString &name) {
assert((script >= 0) && (script < kScriptMAX));

_scripts[script] = name;
}

bool ScriptContainer::hasScript(Script script) const {
return !getScript(script).empty();
}
Expand Down
2 changes: 2 additions & 0 deletions src/engines/nwn2/script/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class ScriptContainer {

const Common::UString &getScript(Script script) const;

void setScript(Script script, const Common::UString &name);

bool hasScript(Script script) const;

bool runScript(Script script,
Expand Down
2 changes: 1 addition & 1 deletion src/engines/nwn2/script/function_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 966, "SetTrapKeyTag" , 0 },
{ 967, "SetTrapOneShot" , 0 },
{ 968, "CreateTrapAtLocation" , 0 },
{ 969, "CreateTrapOnObject" , 0 },
{ 969, "CreateTrapOnObject" , &Functions::createTrapOnObject },
{ 970, "GetAreaSize" , 0 },
{ 971, "GetTrapRecoverable" , 0 },
{ 972, "SetTrapRecoverable" , 0 },
Expand Down
2 changes: 2 additions & 0 deletions src/engines/nwn2/script/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ class Functions {

void setTrapActive(Aurora::NWScript::FunctionContext &ctx);

void createTrapOnObject(Aurora::NWScript::FunctionContext &ctx);

void jumpToLocation(Aurora::NWScript::FunctionContext &ctx);
void jumpToObject (Aurora::NWScript::FunctionContext &ctx);

Expand Down
15 changes: 15 additions & 0 deletions src/engines/nwn2/script/functions_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,21 @@ void Functions::setTrapActive(Aurora::NWScript::FunctionContext &ctx) {
trigger->setTrapActive(active);
}

void Functions::createTrapOnObject(Aurora::NWScript::FunctionContext &ctx) {
NWN2::Object *object = NWN2::ObjectContainer::toObject(getParamObject(ctx, 1));
if (!object)
return;

const uint8 trapType = (uint8)(ctx.getParams()[0].getInt());
const uint32 faction = (uint32)(ctx.getParams()[2].getInt());
const Common::UString &disarm = ctx.getParams()[3].getString();
const Common::UString &triggered = ctx.getParams()[4].getString();

Trigger *trigger = NWN2::ObjectContainer::toTrigger(object);
if (trigger)
trigger->createTrap(trapType, faction, disarm, triggered);
}

void Functions::jumpToLocation(Aurora::NWScript::FunctionContext &ctx) {
NWN2::Object *object = NWN2::ObjectContainer::toObject(ctx.getCaller());
NWN2::Location *moveTo = NWN2::ObjectContainer::toLocation(ctx.getParams()[0].getEngineType());
Expand Down
47 changes: 32 additions & 15 deletions src/engines/nwn2/trap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ void Trap::setTrapKeyTag(const Common::UString &keyTag) {
_keyTag = keyTag;
}

/** Create a new trap type using the 'trapType' row data in 'traps.2da' */
void Trap::createTrapBaseType(uint8 trapType) {
const Aurora::TwoDAFile &twoDA = TwoDAReg.get2DA("traps");
const size_t count = twoDA.getRowCount();
if (trapType >= count)
return;

// Load the trap row
const Aurora::TwoDARow &row = twoDA.getRow(trapType);

// Set this up as a new trap
_detectDC = row.getInt("DetectDCMod");
_disarmDC = row.getInt("DisarmDCMod");
_itemResRef = row.getString("ResRef");
_trapType = trapType;
_isTrap = true;
_isFlagged = false;
_detectedBy = 0;
_createdBy = 0;
}

void Trap::createTrap(uint8 trapType, uint32 UNUSED(faction),
const Common::UString &UNUSED(disarm),
const Common::UString &UNUSED(triggered)) {
createTrapBaseType(trapType);
}

/*
* The agent is attempting to detect the trap using
* the search skill and all applicable modifiers.
Expand Down Expand Up @@ -319,12 +346,10 @@ void Trap::triggeredTrap() {

/** Load the trap information from the struct */
void Trap::load(const Aurora::GFF3Struct &gff) {
_isTrapActive = true; // Default for a trigger trap

// Load the traps.2da information
_trapType = gff.getUint("TrapType", _trapType);
loadTrap2da(TwoDAReg.get2DA("traps"), _trapType);
// Initialize using the 'traps.2da' information
createTrapBaseType(gff.getUint("TrapType", _trapType));

_isTrapActive = true; // Default for a trigger trap
_isTrap = gff.getBool("TrapFlag", _isTrap);
_isDetectable = gff.getBool("TrapDetectable", _isDetectable);
_isDisarmable = gff.getBool("TrapDisarmable", _isDisarmable);
Expand All @@ -340,20 +365,12 @@ void Trap::load(const Aurora::GFF3Struct &gff) {
_keyTag = gff.getString("KeyName", _keyTag);
}

/** Load the trap information from a traps.2da row */
/** Load the trap information from a 'traps.2da' row */
void Trap::load(const uint8 type, const Creature *creator) {
_trapType = type;
loadTrap2da(TwoDAReg.get2DA("traps"), _trapType);
createTrapBaseType(type);
_createdBy = creator->getID();
}

/** Load base trap information from the traps.2da file */
void Trap::loadTrap2da(const Aurora::TwoDAFile &twoda, uint32 id) {
_detectDC = twoda.getRow(id).getInt("DetectDCMod");
_disarmDC = twoda.getRow(id).getInt("DisarmDCMod");
_itemResRef = twoda.getRow(id).getString("ResRef");
}

} // End of namespace NWN2

} // End of namespace Engines
8 changes: 6 additions & 2 deletions src/engines/nwn2/trap.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ class Trap {

Trap(const Aurora::GFF3Struct &trap);
Trap(const uint8 type, const Creature *creator);
~Trap();
virtual ~Trap();

void createTrapBaseType(uint8 trapType);
virtual void createTrap(uint8 trapType, uint32 faction,
const Common::UString &disarm,
const Common::UString &triggered);

bool isTriggeredBy(Object *triggerBy) const;

Expand Down Expand Up @@ -110,7 +115,6 @@ class Trap {

void load(const Aurora::GFF3Struct &trap);
void load(const uint8 type, const Creature *creator);
void loadTrap2da(const Aurora::TwoDAFile &twoda, uint32 id);
};

} // End of namespace NWN2
Expand Down
11 changes: 11 additions & 0 deletions src/engines/nwn2/trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ void Trigger::hide() {
void Trigger::hideSoft() {
}

void Trigger::createTrap(uint8 trapType, uint32 faction,
const Common::UString &disarm,
const Common::UString &triggered) {
Trap::createTrap(trapType, faction, disarm, triggered);
_faction = faction; // Overriding trigger faction

setScript(kScriptDisarm, disarm);
if (triggered != "")
setScript(kScriptTrapTriggered, triggered);
}

bool Trigger::isVisible() const {
return Renderable::isVisible();
}
Expand Down
4 changes: 4 additions & 0 deletions src/engines/nwn2/trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class Trigger : public ::Engines::Trigger, public Object, public Trap {

/** Get the reputation of the trigger with the source. */
uint8 getReputation(Object *source) const;
/** Create a trap on the trigger. */
void createTrap(uint8 trapType, uint32 faction,
const Common::UString &disarm,
const Common::UString &triggered);

protected:
void load(const Aurora::GFF3Struct &gff);
Expand Down

0 comments on commit 89c8265

Please sign in to comment.