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 
35 
37 
38 namespace Engines {
39 
40 namespace NWN2 {
41 
42 struct ScriptName {
44  const char *name;
45 };
46 
47 static const ScriptName kScriptNames[] = {
48  {kScriptAcquireItem , "Mod_OnAcquirItem"},
49  {kScriptActiveItem , "Mod_OnActvtItem" },
50  {kScriptAttacked , "OnMeleeAttacked" },
51  {kScriptAttacked , "ScriptAttacked" },
52  {kScriptBlocked , "ScriptOnBlocked" },
53  {kScriptClick , "OnLeftClick" },
54  {kScriptClient , "OnClick" },
55  {kScriptClosed , "OnClosed" },
56  {kScriptCutsceneAbort , "Mod_OnCutsnAbort"},
57  {kScriptDamaged , "OnDamaged" },
58  {kScriptDamaged , "ScriptDamaged" },
59  {kScriptDeath , "OnDeath" },
60  {kScriptDeath , "ScriptDeath" },
61  {kScriptDialogue , "OnDialog" },
62  {kScriptDialogue , "ScriptDialogue" },
63  {kScriptDisarm , "OnDisarm" },
64  {kScriptDisturbed , "OnInvDisturbed" },
65  {kScriptDisturbed , "ScriptDisturbed" },
66  {kScriptEndRound , "ScriptEndRound" },
67  {kScriptEnter , "Mod_OnClientEntr"},
68  {kScriptEnter , "OnClientEnter" },
69  {kScriptEnter , "OnEnter" },
70  {kScriptEnter , "OnEntered" },
71  {kScriptEnter , "ScriptOnEnter" },
72  {kScriptExhausted , "OnExhausted" },
73  {kScriptExit , "Mod_OnClientLeav"},
74  {kScriptExit , "OnExit" },
75  {kScriptExit , "ScriptOnExit" },
76  {kScriptFailToOpen , "OnFailToOpen" },
77  {kScriptHeartbeat , "Mod_OnHeartbeat" },
78  {kScriptHeartbeat , "OnHeartbeat" },
79  {kScriptHeartbeat , "ScriptHeartbeat" },
80  {kScriptLock , "OnLock" },
81  {kScriptModuleLoad , "Mod_OnModLoad" },
82  {kScriptModuleStart , "Mod_OnModStart" },
83  {kScriptNotice , "ScriptOnNotice" },
84  {kScriptOpen , "OnOpen" },
85  {kScriptPCLoaded , "Mod_OnPCLoaded" },
86  {kScriptPlayerChat , "Mod_OnChat" },
87  {kScriptPlayerDeath , "Mod_OnPlrDeath" },
88  {kScriptPlayerDying , "Mod_OnPlrDying" },
89  {kScriptPlayerEquipItem , "Mod_OnPlrEqItm" },
90  {kScriptPlayerLevelUp , "Mod_OnPlrLvlUp" },
91  {kScriptPlayerRespanw , "Mod_OnSpawnBtnDn"},
92  {kScriptPlayerRest , "Mod_OnPlrRest" },
93  {kScriptPlayerUnequipItem, "Mod_OnPlrUnEqItm"},
94  {kScriptRested , "ScriptRested" },
95  {kScriptShout , "ScriptOnShout" },
96  {kScriptSpawn , "ScriptSpawn" },
97  {kScriptSpellCastAt , "OnSpellCastAt" },
98  {kScriptSpellCastAt , "ScriptSpellAt" },
99  {kScriptStoreClose , "OnStoreClosed" },
100  {kScriptStoreOpen , "OnOpenStore" },
101  {kScriptTrapTriggered , "OnTrapTriggered" },
102  {kScriptUnaquireItem , "Mod_OnUnAqreItem"},
103  {kScriptUnlock , "OnUnlock" },
104  {kScriptUsed , "OnUsed" },
105  {kScriptUserdefined , "Mod_OnUsrDefined"},
106  {kScriptUserdefined , "OnUserDefined" },
107  {kScriptUserdefined , "ScriptUserDefine"}
108 };
109 
111 }
112 
114 }
115 
117  assert((script >= 0) && (script < kScriptMAX));
118 
119  return _scripts[script];
120 }
121 
122 bool ScriptContainer::hasScript(Script script) const {
123  return !getScript(script).empty();
124 }
125 
127  for (size_t i = 0; i < kScriptMAX; i++)
128  _scripts[i].clear();
129 }
130 
132  clearScripts();
133 
134  for (size_t i = 0; i < ARRAYSIZE(kScriptNames); i++) {
135  const Script script = kScriptNames[i].script;
136  const char *name = kScriptNames[i].name;
137 
138  _scripts[script] = gff.getString(name, _scripts[script]);
139  }
140 }
141 
143  for (size_t i = 0; i < kScriptMAX; i++)
144  _scripts[i] = container._scripts[i];
145 }
146 
149  const Aurora::NWScript::ObjectReference triggerer) {
150  return runScript(getScript(script), owner, triggerer);
151 }
152 
155  const Aurora::NWScript::ObjectReference triggerer) {
156 
157  return runScript(script, Aurora::NWScript::NCSFile::getEmptyState(), owner, triggerer);
158 }
159 
161  const Aurora::NWScript::ScriptState &state,
163  const Aurora::NWScript::ObjectReference triggerer) {
164  if (script.empty())
165  return true;
166 
167  try {
168  Aurora::NWScript::NCSFile ncs(script);
169 
170  const Aurora::NWScript::Variable &retVal = ncs.run(state, owner, triggerer);
171  if (retVal.getType() == Aurora::NWScript::kTypeInt)
172  return retVal.getInt() != 0;
173  if (retVal.getType() == Aurora::NWScript::kTypeFloat)
174  return retVal.getFloat() != 0.0f;
175 
176  return true;
177 
178  } catch (...) {
179  Common::exceptionDispatcherWarning("Failed running script \"%s\"", script.c_str());
180  return false;
181  }
182 
183  return true;
184 }
185 
186 } // End of namespace NWN2
187 
188 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
NWScript variable.
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
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
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:131
Utility templates and functions.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
const Common::UString & getScript(Script script) const
Definition: container.cpp:116
static const ScriptName kScriptNames[]
Definition: container.cpp:47
static ScriptState getEmptyState()
Definition: ncsfile.cpp:306
Handling BioWare&#39;s NWN Compiled Scripts.
A struct within a GFF3.
Definition: gff3file.h:164
Common::UString _scripts[kScriptMAX]
Definition: container.h:75
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:147
An object containing scripts.
NWScript types.
bool hasScript(Script script) const
Definition: container.cpp:122