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 Witcher {
41 
42 struct ScriptName {
44  const char *name;
45 };
46 
47 static const ScriptName kScriptNames[] = {
48  {kScriptAttackBegin, "ScriptAtkBegin" },
49  {kScriptAttacked, "OnMeleeAttacked" },
50  {kScriptAttacked, "ScriptAttacked" },
51  {kScriptBattleBegin, "OnBattleBegin" },
52  {kScriptBlocked, "ScriptOnBlocked" },
53  {kScriptCastSpellAt, "ScriptSpellAt" },
54  {kScriptClick, "OnClick" },
55  {kScriptClosed, "OnClosed" },
56  {kScriptDamaged, "OnDamaged" },
57  {kScriptDamaged, "ScriptDamaged" },
58  {kScriptDeath, "OnDeath" },
59  {kScriptDeath, "ScriptDeath" },
60  {kScriptDialogue, "ScriptDialogue" },
61  {kScriptDisarm, "OnDisarm" },
62  {kScriptDisturbed, "OnInvDisturbed" },
63  {kScriptDisturbed, "ScriptDisturbed" },
64  {kScriptEndDialogue, "ScriptEndConvers"},
65  {kScriptEndRound, "ScriptEndRound" },
66  {kScriptEnter, "Mod_OnClientEntr"},
67  {kScriptEnter, "OnEnter" },
68  {kScriptEnter, "ScriptOnEnter" },
69  {kScriptExit, "Mod_OnClientLeav"},
70  {kScriptExit, "OnExit" },
71  {kScriptExit, "ScriptOnExit" },
72  {kScriptFailToOpen, "OnFailToOpen" },
73  {kScriptHeartbeat, "Mod_OnHeartbeat" },
74  {kScriptHeartbeat, "OnHeartbeat" },
75  {kScriptHeartbeat, "ScriptHeartbeat" },
76  {kScriptInternal, "ScriptInternal" },
77  {kScriptIsUsable, "OnIsUsable" },
78  {kScriptItemSold, "OnItemSold" },
79  {kScriptJoinAttempt, "OnJoinAttempt" },
80  {kScriptLock, "OnLock" },
81  {kScriptModuleLoad, "Mod_OnModLoad" },
82  {kScriptModuleStart, "Mod_OnModStart" },
83  {kScriptNotice, "ScriptOnNotice" },
84  {kScriptOpen, "OnOpen" },
85  {kScriptOpenStore, "OnOpenStore" },
86  {kScriptOutOfAmmo, "ScriptOutOfAmmo" },
87  {kScriptPlayerDeath, "Mod_OnPlrDeath" },
88  {kScriptPlayerDying, "Mod_OnPlrDying" },
89  {kScriptPlayerLevelUp, "Mod_OnPlrLvlUp" },
90  {kScriptPlayerRespawn, "Mod_OnSpawnBtnDn"},
91  {kScriptPlayerRest, "Mod_OnPlrRest" },
92  {kScriptPutIntoInventory, "OnPutIntoInv" },
93  {kScriptRemoveFromInventory, "OnRemoveFromInv" },
94  {kScriptRest, "ScriptRested" },
95  {kScriptSpawn, "ScriptSpawn" },
96  {kScriptSpellCastAt, "OnSpellCastAt" },
97  {kScriptTrapTriggered, "OnTrapTriggered" },
98  {kScriptUnlock, "OnUnlock" },
99  {kScriptUnlock, "OnUnlocked" },
100  {kScriptUsed, "OnUsed" },
101  {kScriptUserdefine, "ScriptUserDefine"},
102  {kScriptUserdefined, "Mod_OnUsrDefined"},
103  {kScriptUserdefined, "OnUserDefined" },
104  {kScriptUserdefined, "OnUserdefined" },
105  {kScriptWitness, "ScriptOnWitness" }
106 };
107 
109 }
110 
112 }
113 
115  assert((script >= 0) && (script < kScriptMAX));
116 
117  return _scripts[script];
118 }
119 
120 bool ScriptContainer::hasScript(Script script) const {
121  return !getScript(script).empty();
122 }
123 
125  for (size_t i = 0; i < kScriptMAX; i++)
126  _scripts[i].clear();
127 }
128 
130  clearScripts();
131 
132  for (size_t i = 0; i < ARRAYSIZE(kScriptNames); i++) {
133  const Script script = kScriptNames[i].script;
134  const char *name = kScriptNames[i].name;
135 
136  _scripts[script] = gff.getString(name, _scripts[script]);
137  }
138 }
139 
141  for (size_t i = 0; i < kScriptMAX; i++)
142  _scripts[i] = container._scripts[i];
143 }
144 
147  const Aurora::NWScript::ObjectReference triggerer) {
148  return runScript(getScript(script), owner, triggerer);
149 }
150 
153  const Aurora::NWScript::ObjectReference triggerer) {
154 
155  return runScript(script, Aurora::NWScript::NCSFile::getEmptyState(), owner, triggerer);
156 }
157 
159  const Aurora::NWScript::ScriptState &state,
161  const Aurora::NWScript::ObjectReference triggerer) {
162  if (script.empty())
163  return true;
164 
165  try {
166  Aurora::NWScript::NCSFile ncs(script);
167 
168  const Aurora::NWScript::Variable &retVal = ncs.run(state, owner, triggerer);
169  if (retVal.getType() == Aurora::NWScript::kTypeInt)
170  return retVal.getInt() != 0;
171  if (retVal.getType() == Aurora::NWScript::kTypeFloat)
172  return retVal.getFloat() != 0.0f;
173 
174  return true;
175 
176  } catch (...) {
177  Common::exceptionDispatcherWarning("Failed running script \"%s\"", script.c_str());
178  return false;
179  }
180 
181  return true;
182 }
183 
184 } // End of namespace Witcher
185 
186 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
static const ScriptName kScriptNames[]
Definition: container.cpp:47
bool hasScript(Script script) const
Definition: container.cpp:120
NWScript variable.
A class holding an UTF-8 string.
Definition: ustring.h:48
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:129
#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
const Common::UString & getScript(Script script) const
Definition: container.cpp:114
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:145
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
An object containing scripts.
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
Common::UString _scripts[kScriptMAX]
Definition: container.h:75
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 getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
NWScript types.