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 KotOR2 {
41 
42 struct ScriptName {
44  const char *name;
45 };
46 
47 static const ScriptName kScriptNames[] = {
48  {kScriptAccelerate, "OnAccelerate" },
49  {kScriptActivateItem, "Mod_OnActvtItem" },
50  {kScriptAnimate, "OnAnimEvent" },
51  {kScriptAquireItem, "Mod_OnAcquirItem"},
52  {kScriptAttacked, "OnMeleeAttacked" },
53  {kScriptAttacked, "ScriptAttacked" },
54  {kScriptBlocked, "ScriptOnBlocked" },
55  {kScriptBreak, "OnBrake" },
56  {kScriptClick, "OnClick" },
57  {kScriptClosed, "OnClosed" },
58  {kScriptCreate, "OnCreate" },
59  {kScriptDamaged, "OnDamage" },
60  {kScriptDamaged, "OnDamaged" },
61  {kScriptDamaged, "ScriptDamaged" },
62  {kScriptDeath, "OnDeath" },
63  {kScriptDeath, "ScriptDeath" },
64  {kScriptDialogue, "ScriptDialogue" },
65  {kScriptDisarm, "OnDisarm" },
66  {kScriptDisturbed, "OnInvDisturbed" },
67  {kScriptDisturbed, "ScriptDisturbed" },
68  {kScriptEndDialogue, "OnEndDialogue" },
69  {kScriptEndDialogue, "ScriptEndDialogu"},
70  {kScriptEndRound, "ScriptEndRound" },
71  {kScriptEnter, "Mod_OnClientEntr"},
72  {kScriptEnter, "OnEnter" },
73  {kScriptEnter, "OnEntered" },
74  {kScriptEnter, "ScriptOnEnter" },
75  {kScriptExhausted, "OnExhausted" },
76  {kScriptExit, "Mod_OnClientLeav"},
77  {kScriptExit, "OnExit" },
78  {kScriptExit, "ScriptOnExit" },
79  {kScriptFailToOpen, "OnFailToOpen" },
80  {kScriptFire, "OnFire" },
81  {kScriptHeartbeart, "ScriptHeartbeat" },
82  {kScriptHeartbeat, "Mod_OnHeartbeat" },
83  {kScriptHeartbeat, "OnHeartbeat" },
84  {kScriptHitBullet, "OnHitBullet" },
85  {kScriptHitFollower, "OnHitFollower" },
86  {kScriptHitObstacle, "OnHitObstacle" },
87  {kScriptHitWorld, "OnHitWorld" },
88  {kScriptLock, "OnLock" },
89  {kScriptModuleLoad, "Mod_OnModLoad" },
90  {kScriptModuleStart, "Mod_OnModStart" },
91  {kScriptNotice, "ScriptOnNotice" },
92  {kScriptOpen, "OnOpen" },
93  {kScriptOpenStore, "OnOpenStore" },
94  {kScriptPlayerDeath, "Mod_OnPlrDeath" },
95  {kScriptPlayerDying, "Mod_OnPlrDying" },
96  {kScriptPlayerLevelUp, "Mod_OnPlrLvlUp" },
97  {kScriptPlayerRespawn, "Mod_OnSpawnBtnDn"},
98  {kScriptPlayerRest, "Mod_OnPlrRest" },
99  {kScriptRest, "ScriptRested" },
100  {kScriptSpawn, "ScriptSpawn" },
101  {kScriptSpellCastAt, "OnSpellCastAt" },
102  {kScriptSpellCastAt, "ScriptSpellAt" },
103  {kScriptTrackLoop, "OnTrackLoop" },
104  {kScriptTrapTriggered, "OnTrapTriggered" },
105  {kScriptUnaquireItem, "Mod_OnUnAqreItem"},
106  {kScriptUnlock, "OnUnlock" },
107  {kScriptUsed, "OnUsed" },
108  {kScriptUserdefined, "Mod_OnUsrDefined"},
109  {kScriptUserdefined, "OnUserDefined" },
110  {kScriptUserdefined, "ScriptUserDefine"}
111 };
112 
114 }
115 
117 }
118 
120  assert((script >= 0) && (script < kScriptMAX));
121 
122  return _scripts[script];
123 }
124 
125 bool ScriptContainer::hasScript(Script script) const {
126  return !getScript(script).empty();
127 }
128 
130  for (size_t i = 0; i < kScriptMAX; i++)
131  _scripts[i].clear();
132 }
133 
135  clearScripts();
136 
137  for (size_t i = 0; i < ARRAYSIZE(kScriptNames); i++) {
138  const Script script = kScriptNames[i].script;
139  const char *name = kScriptNames[i].name;
140 
141  _scripts[script] = gff.getString(name, _scripts[script]);
142  }
143 }
144 
146  for (size_t i = 0; i < kScriptMAX; i++)
147  _scripts[i] = container._scripts[i];
148 }
149 
152  const Aurora::NWScript::ObjectReference triggerer) {
153  return runScript(getScript(script), owner, triggerer);
154 }
155 
158  const Aurora::NWScript::ObjectReference triggerer) {
159 
160  return runScript(script, Aurora::NWScript::NCSFile::getEmptyState(), owner, triggerer);
161 }
162 
164  const Aurora::NWScript::ScriptState &state,
166  const Aurora::NWScript::ObjectReference triggerer) {
167  if (script.empty())
168  return true;
169 
170  try {
171  Aurora::NWScript::NCSFile ncs(script);
172 
173  const Aurora::NWScript::Variable &retVal = ncs.run(state, owner, triggerer);
174  if (retVal.getType() == Aurora::NWScript::kTypeInt)
175  return retVal.getInt() != 0;
176  if (retVal.getType() == Aurora::NWScript::kTypeFloat)
177  return retVal.getFloat() != 0.0f;
178 
179  return true;
180 
181  } catch (...) {
182  Common::exceptionDispatcherWarning("Failed running script \"%s\"", script.c_str());
183  return false;
184  }
185 
186  return true;
187 }
188 
189 } // End of namespace KotOR2
190 
191 } // 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 object containing scripts.
const Common::UString & getScript(Script script) const
Definition: container.cpp:119
An NCS, BioWare&#39;s NWN Compile Script.
Definition: ncsfile.h:86
static const ScriptName kScriptNames[]
Definition: container.cpp:47
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 runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:150
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.
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
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:134
bool hasScript(Script script) const
Definition: container.cpp:125
NWScript types.
Common::UString _scripts[kScriptMAX]
Definition: container.h:75