xoreos  0.0.5
container.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/util.h"
28 #include "src/common/error.h"
29 
30 #include "src/aurora/gff3file.h"
31 
33 
37 
39 
40 namespace Engines {
41 
42 namespace NWN {
43 
44 struct ScriptName {
46  const char *name;
47 };
48 
49 static const ScriptName kScriptNames[] = {
50  {kScriptAcquireItem , "Mod_OnAcquirItem"},
51  {kScriptUnacquireItem , "Mod_OnUnAqreItem"},
52  {kScriptActivateItem , "Mod_OnActvtItem" },
53  {kScriptEnter , "Mod_OnClientEntr"},
54  {kScriptEnter , "OnEnter" },
55  {kScriptEnter , "ScriptOnEnter" },
56  {kScriptExit , "Mod_OnClientLeav"},
57  {kScriptExit , "OnExit" },
58  {kScriptExit , "ScriptOnExit" },
59  {kScriptCutsceneAbort , "Mod_OnCutsnAbort"},
60  {kScriptHeartbeat , "Mod_OnHeartbeat" },
61  {kScriptHeartbeat , "OnHeartbeat" },
62  {kScriptHeartbeat , "ScriptHeartbeat" },
63  {kScriptModuleLoad , "Mod_OnModLoad" },
64  {kScriptModuleStart , "Mod_OnModStart" },
65  {kScriptPlayerChat , "Mod_OnPlrChat" },
66  {kScriptPlayerDeath , "Mod_OnPlrDeath" },
67  {kScriptPlayerDying , "Mod_OnPlrDying" },
68  {kScriptPlayerEquipItem , "Mod_OnPlrEqItm" },
69  {kScriptPlayerUnequipItem, "Mod_OnPlrUnEqItm"},
70  {kScriptPlayerLevelUp , "Mod_OnPlrLvlUp" },
71  {kScriptPlayerRest , "Mod_OnPlrRest" },
72  {kScriptPlayerRespawn , "Mod_OnSpawnBtnDn"},
73  {kScriptUserdefined , "Mod_OnUsrDefined"},
74  {kScriptUserdefined , "OnUserDefined" },
75  {kScriptUserdefined , "ScriptUserDefine"},
76  {kScriptUsed , "OnUsed" },
77  {kScriptClick , "OnClick" },
78  {kScriptOpen , "OnOpen" },
79  {kScriptClosed , "OnClosed" },
80  {kScriptDamaged , "OnDamaged" },
81  {kScriptDamaged , "ScriptDamaged" },
82  {kScriptDeath , "OnDeath" },
83  {kScriptDeath , "ScriptDeath" },
84  {kScriptDisarm , "OnDisarm" },
85  {kScriptLock , "OnLock" },
86  {kScriptUnlock , "OnUnlock" },
87  {kScriptAttacked , "OnMeleeAttacked" },
88  {kScriptAttacked , "ScriptAttacked" },
89  {kScriptSpellCastAt , "OnSpellCastAt" },
90  {kScriptSpellCastAt , "ScriptSpellAt" },
91  {kScriptTrapTriggered , "OnTrapTriggered" },
92  {kScriptDialogue , "ScriptDialogue" },
93  {kScriptDisturbed , "ScriptDisturbed" },
94  {kScriptEndRound , "ScriptEndRound" },
95  {kScriptBlocked , "ScriptOnBlocked" },
96  {kScriptNotice , "ScriptOnNotice" },
97  {kScriptRested , "ScriptRested" },
98  {kScriptSpawn , "ScriptSpawn" },
99  {kScriptFailToOpen , "OnFailToOpen" }
100 };
101 
103 }
104 
106 }
107 
109  assert((script >= 0) && (script < kScriptMAX));
110 
111  return _scripts[script];
112 }
113 
114 bool ScriptContainer::hasScript(Script script) const {
115  return !getScript(script).empty();
116 }
117 
119  for (size_t i = 0; i < kScriptMAX; i++)
120  _scripts[i].clear();
121 }
122 
124  clearScripts();
125 
126  for (size_t i = 0; i < ARRAYSIZE(kScriptNames); i++) {
127  const Script script = kScriptNames[i].script;
128  const char *name = kScriptNames[i].name;
129 
130  _scripts[script] = gff.getString(name, _scripts[script]);
131  }
132 }
133 
135  for (size_t i = 0; i < kScriptMAX; i++)
136  _scripts[i] = container._scripts[i];
137 }
138 
141  const Aurora::NWScript::ObjectReference triggerer) {
142  return runScript(getScript(script), owner, triggerer);
143 }
144 
147  const Aurora::NWScript::ObjectReference triggerer) {
148 
149  return runScript(script, Aurora::NWScript::NCSFile::getEmptyState(), owner, triggerer);
150 }
151 
153  const Aurora::NWScript::ScriptState &state,
155  const Aurora::NWScript::ObjectReference triggerer) {
156  if (script.empty())
157  return true;
158 
159  try {
160  Aurora::NWScript::NCSFile ncs(script);
161 
162  const Aurora::NWScript::Variable &retVal = ncs.run(state, owner, triggerer);
163  if (retVal.getType() == Aurora::NWScript::kTypeInt)
164  return retVal.getInt() != 0;
165  if (retVal.getType() == Aurora::NWScript::kTypeFloat)
166  return retVal.getFloat() != 0.0f;
167 
168  return true;
169 
170  } catch (...) {
171  Common::exceptionDispatcherWarning("Failed running script \"%s\"", script.c_str());
172  return false;
173  }
174 
175  return true;
176 }
177 
178 } // End of namespace NWN
179 
180 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
bool hasScript(Script script) const
Definition: container.cpp:114
NWScript variable.
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:123
A class holding an UTF-8 string.
Definition: ustring.h:48
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
Common::UString _scripts[kScriptMAX]
Definition: container.h:75
An NCS, BioWare&#39;s NWN Compile Script.
Definition: ncsfile.h:86
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
const Variable & run(Object *owner=0, Object *triggerer=0)
Run the current script, from start to finish.
Definition: ncsfile.cpp:350
Utility templates and functions.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
static ScriptState getEmptyState()
Definition: ncsfile.cpp:306
Handling BioWare&#39;s NWN Compiled Scripts.
Reference to an NWScript object.
A struct within a GFF3.
Definition: gff3file.h:164
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
const Common::UString & getScript(Script script) const
Definition: container.cpp:108
An object containing scripts.
NWScript types.
static const ScriptName kScriptNames[]
Definition: container.cpp:49
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:139