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 Jade {
41 
42 struct ScriptName {
44  const char *name;
45 };
46 
47 static const ScriptName kScriptNames[] = {
48  {kScriptOnBlocked , "OnBlocked"},
49  {kScriptOnClick , "OnClicked"},
50  {kScriptOnClose , "OnClose"},
51  {kScriptOnCombatEnd , "OnCombatEnd"},
52  {kScriptOnConversation , "OnConversation"},
53  {kScriptOnCreate , "OnCreate"},
54  {kScriptOnDamage , "OnDamaged"},
55  {kScriptOnDead , "OnDead"},
56  {kScriptOnDeath , "OnDeath"},
57  {kScriptOnEnter , "OnEnter"},
58  {kScriptOnExit , "OnExit"},
59  {kScriptOnHeartbeat , "OnHeartbeat"},
60  {kScriptOnHeartbeat , "OnHeartBeat"},
61  {kScriptOnPerceptionAppeared , "OnPerception"},
62  {kScriptOnPerceptionVanished , "OnPerceptionVan"},
63  {kScriptOnSpawn , "OnSpawn"},
64  {kScriptOnSubCombatEnd , "OnSubCombatEnd"},
65  {kScriptOnSubCombatStart , "OnSubCombatStart"},
66  {kScriptOnUse , "OnUse"},
67  {kScriptOnUserdefined , "OnUserDefined"}
68 };
69 
71 }
72 
74 }
75 
77  assert((script >= 0) && (script < kScriptMAX));
78 
79  return _scripts[script];
80 }
81 
82 bool ScriptContainer::hasScript(Script script) const {
83  return !getScript(script).empty();
84 }
85 
87  for (size_t i = 0; i < kScriptMAX; i++)
88  _scripts[i].clear();
89 }
90 
92  clearScripts();
93 
94  if (gff.hasField("Scripts")) {
95  const Aurora::GFF3Struct &scripts = gff.getStruct("Scripts");
96  for (size_t i = 0; i < ARRAYSIZE(kScriptNames); i++) {
97  const Script script = kScriptNames[i].script;
98  const char *name = kScriptNames[i].name;
99 
100  _scripts[script] = scripts.getString(name, _scripts[script]);
101  }
102  }
103 }
104 
106  for (size_t i = 0; i < kScriptMAX; i++)
107  _scripts[i] = container._scripts[i];
108 }
109 
112  const Aurora::NWScript::ObjectReference triggerer) {
113  return runScript(getScript(script), owner, triggerer);
114 }
115 
118  const Aurora::NWScript::ObjectReference triggerer) {
119 
120  return runScript(script, Aurora::NWScript::NCSFile::getEmptyState(), owner, triggerer);
121 }
122 
124  const Aurora::NWScript::ScriptState &state,
126  const Aurora::NWScript::ObjectReference triggerer) {
127  if (script.empty())
128  return true;
129 
130  try {
131  Aurora::NWScript::NCSFile ncs(script);
132 
133  const Aurora::NWScript::Variable &retVal = ncs.run(state, owner, triggerer);
134  if (retVal.getType() == Aurora::NWScript::kTypeInt)
135  return retVal.getInt() != 0;
136  if (retVal.getType() == Aurora::NWScript::kTypeFloat)
137  return retVal.getFloat() != 0.0f;
138 
139  return true;
140 
141  } catch (...) {
142  Common::exceptionDispatcherWarning("Failed running script \"%s\"", script.c_str());
143  return false;
144  }
145 
146  return true;
147 }
148 
149 } // End of namespace Jade
150 
151 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
NWScript variable.
An object containing scripts.
A class holding an UTF-8 string.
Definition: ustring.h:48
static const ScriptName kScriptNames[]
Definition: container.cpp:47
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:91
bool hasScript(Script script) const
Definition: container.cpp:82
An NCS, BioWare&#39;s NWN Compile Script.
Definition: ncsfile.h:86
const Common::UString & getScript(Script script) const
Definition: container.cpp:76
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:110
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.
A struct within a GFF3.
Definition: gff3file.h:164
const GFF3Struct & getStruct(const Common::UString &field) const
Definition: gff3file.cpp:728
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Common::UString _scripts[kScriptMAX]
Definition: container.h:75
NWScript types.