2009-05-08 13:28:41 +00:00
|
|
|
/*
|
|
|
|
* OpenClonk, http://www.openclonk.org
|
|
|
|
*
|
2009-06-05 13:41:20 +00:00
|
|
|
* Copyright (c) 1998-2000 Matthes Bender
|
|
|
|
* Copyright (c) 2001-2002, 2005, 2007 Sven Eberhardt
|
|
|
|
* Copyright (c) 2003-2005 Peter Wortmann
|
|
|
|
* Copyright (c) 2006 Armin Burgmeier
|
2011-09-01 14:58:52 +00:00
|
|
|
* Copyright (c) 2006-2007, 2009, 2011 Günther Brammer
|
2010-12-23 00:01:24 +00:00
|
|
|
* Copyright (c) 2009 Nicolas Hake
|
2009-05-08 13:28:41 +00:00
|
|
|
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
|
|
|
|
*
|
|
|
|
* Portions might be copyrighted by other authors who have contributed
|
|
|
|
* to OpenClonk.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
* See isc_license.txt for full license and disclaimer.
|
|
|
|
*
|
|
|
|
* "Clonk" is a registered trademark of Matthes Bender.
|
|
|
|
* See clonk_trademark_license.txt for full license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Handles script file components (calls, inheritance, function maps) */
|
|
|
|
|
|
|
|
#include <C4Include.h>
|
|
|
|
#include <C4ScriptHost.h>
|
|
|
|
|
2012-02-16 23:30:00 +00:00
|
|
|
#include <C4Def.h>
|
2009-06-15 21:47:26 +00:00
|
|
|
#include <C4GameObjects.h>
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
/*--- C4ScriptHost ---*/
|
|
|
|
|
2011-10-13 16:01:02 +00:00
|
|
|
C4ScriptHost::C4ScriptHost()
|
|
|
|
{
|
|
|
|
Script = NULL;
|
|
|
|
Code.clear();
|
|
|
|
LastCode = NULL;
|
|
|
|
stringTable = 0;
|
2012-01-26 23:20:55 +00:00
|
|
|
SourceScripts.push_back(this);
|
2011-10-13 16:01:02 +00:00
|
|
|
}
|
2009-05-08 13:28:41 +00:00
|
|
|
C4ScriptHost::~C4ScriptHost() { Clear(); }
|
|
|
|
|
|
|
|
void C4ScriptHost::Clear()
|
2010-03-28 18:58:01 +00:00
|
|
|
{
|
2009-05-08 13:28:41 +00:00
|
|
|
C4AulScript::Clear();
|
2011-02-06 20:37:19 +00:00
|
|
|
ComponentHost.Clear();
|
2011-10-13 16:01:02 +00:00
|
|
|
Script.Clear();
|
|
|
|
ClearCode();
|
2012-01-26 23:20:55 +00:00
|
|
|
SourceScripts.clear();
|
|
|
|
SourceScripts.push_back(this);
|
2010-03-28 18:58:01 +00:00
|
|
|
}
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2011-02-06 20:31:33 +00:00
|
|
|
bool C4ScriptHost::Load(C4Group &hGroup, const char *szFilename,
|
2011-09-29 01:49:21 +00:00
|
|
|
const char *szLanguage, class C4LangStringTable *pLocalTable)
|
2010-03-28 18:58:01 +00:00
|
|
|
{
|
2009-05-08 13:28:41 +00:00
|
|
|
// Base load
|
2011-02-06 20:37:19 +00:00
|
|
|
bool fSuccess = ComponentHost.Load(hGroup,szFilename,szLanguage);
|
2009-05-08 13:28:41 +00:00
|
|
|
// String Table
|
2009-11-12 15:57:37 +00:00
|
|
|
stringTable = pLocalTable;
|
2009-05-08 13:28:41 +00:00
|
|
|
// set name
|
2011-02-06 20:37:19 +00:00
|
|
|
ScriptName.Ref(ComponentHost.GetFilePath());
|
2009-05-08 13:28:41 +00:00
|
|
|
// preparse script
|
|
|
|
MakeScript();
|
|
|
|
// Success
|
|
|
|
return fSuccess;
|
2010-03-28 18:58:01 +00:00
|
|
|
}
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
void C4ScriptHost::MakeScript()
|
2010-03-28 18:58:01 +00:00
|
|
|
{
|
2009-05-08 13:28:41 +00:00
|
|
|
// clear prev
|
|
|
|
Script.Clear();
|
|
|
|
|
|
|
|
// create script
|
2009-11-12 15:57:37 +00:00
|
|
|
if (stringTable)
|
2009-05-08 13:28:41 +00:00
|
|
|
{
|
2011-03-05 00:05:36 +00:00
|
|
|
stringTable->ReplaceStrings(ComponentHost.GetDataBuf(), Script);
|
2009-05-08 13:28:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-06 20:37:19 +00:00
|
|
|
Script.Ref(ComponentHost.GetDataBuf());
|
2009-05-08 13:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// preparse script
|
|
|
|
Preparse();
|
2010-03-28 18:58:01 +00:00
|
|
|
}
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2011-02-06 20:37:19 +00:00
|
|
|
bool C4ScriptHost::ReloadScript(const char *szPath, const char *szLanguage)
|
2010-03-28 18:58:01 +00:00
|
|
|
{
|
2010-03-27 16:05:02 +00:00
|
|
|
// this?
|
2011-02-06 20:37:19 +00:00
|
|
|
if (SEqualNoCase(szPath, ComponentHost.GetFilePath()) || (stringTable && SEqualNoCase(szPath, stringTable->GetFilePath())))
|
2010-03-27 16:05:02 +00:00
|
|
|
{
|
|
|
|
// try reload
|
|
|
|
char szParentPath[_MAX_PATH + 1]; C4Group ParentGrp;
|
2010-03-28 18:58:01 +00:00
|
|
|
if (GetParentPath(szPath, szParentPath))
|
|
|
|
if (ParentGrp.Open(szParentPath))
|
2011-09-29 01:49:21 +00:00
|
|
|
if (Load(ParentGrp, NULL, szLanguage, stringTable))
|
2010-03-27 16:05:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-25 03:15:39 +00:00
|
|
|
return false;
|
2010-03-28 18:58:01 +00:00
|
|
|
}
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
void C4ScriptHost::SetError(const char *szMessage)
|
2010-03-28 18:58:01 +00:00
|
|
|
{
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2010-03-28 18:58:01 +00:00
|
|
|
}
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2011-10-24 15:02:27 +00:00
|
|
|
/*--- C4ExtraScriptHost ---*/
|
|
|
|
|
|
|
|
C4ExtraScriptHost::C4ExtraScriptHost():
|
2012-06-01 15:27:59 +00:00
|
|
|
ParserPropList(C4PropList::NewAnon(NULL, NULL, NULL))
|
2011-10-24 15:02:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void C4ExtraScriptHost::Clear()
|
|
|
|
{
|
|
|
|
ParserPropList.getPropList()->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
C4PropList * C4ExtraScriptHost::GetPropList()
|
|
|
|
{
|
|
|
|
return ParserPropList.getPropList();
|
|
|
|
}
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
/*--- C4DefScriptHost ---*/
|
|
|
|
|
2011-10-04 20:16:27 +00:00
|
|
|
bool C4DefScriptHost::Load(C4Group & g, const char * f, const char * l, C4LangStringTable * t)
|
|
|
|
{
|
|
|
|
bool r = C4ScriptHost::Load(g, f, l, t);
|
|
|
|
assert(Def);
|
|
|
|
|
|
|
|
// Check category
|
2011-12-29 14:56:51 +00:00
|
|
|
if (!Def->GetPlane() && Def->Category & C4D_SortLimit)
|
2011-10-04 20:16:27 +00:00
|
|
|
{
|
|
|
|
int Plane; bool gotplane = true;
|
2011-12-29 14:56:51 +00:00
|
|
|
switch (Def->Category & C4D_SortLimit)
|
2011-10-04 20:16:27 +00:00
|
|
|
{
|
|
|
|
case C4D_StaticBack: Plane = 100; break;
|
|
|
|
case C4D_Structure: Plane = C4Plane_Structure; break;
|
|
|
|
case C4D_Vehicle: Plane = 300; break;
|
|
|
|
case C4D_Living: Plane = 400; break;
|
|
|
|
case C4D_Object: Plane = 500; break;
|
|
|
|
case C4D_StaticBack | C4D_Background: Plane = -500; break;
|
|
|
|
case C4D_Structure | C4D_Background: Plane = -400; break;
|
|
|
|
case C4D_Vehicle | C4D_Background: Plane = -300; break;
|
|
|
|
case C4D_Living | C4D_Background: Plane = -200; break;
|
|
|
|
case C4D_Object | C4D_Background: Plane = -100; break;
|
|
|
|
case C4D_StaticBack | C4D_Foreground: Plane = 1100; break;
|
|
|
|
case C4D_Structure | C4D_Foreground: Plane = 1200; break;
|
|
|
|
case C4D_Vehicle | C4D_Foreground: Plane = 1300; break;
|
|
|
|
case C4D_Living | C4D_Foreground: Plane = 1400; break;
|
|
|
|
case C4D_Object | C4D_Foreground: Plane = 1500; break;
|
|
|
|
default:
|
|
|
|
DebugLogF("WARNING: Def %s (%s) at %s has invalid category!", Def->GetName(), Def->id.ToString(), g.GetFullName().getData());
|
|
|
|
gotplane = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (gotplane) Def->SetProperty(P_Plane, C4VInt(Plane));
|
|
|
|
}
|
|
|
|
if (!Def->GetPlane())
|
|
|
|
{
|
|
|
|
DebugLogF("WARNING: Def %s (%s) at %s has invalid Plane!", Def->GetName(), Def->id.ToString(), g.GetFullName().getData());
|
|
|
|
Def->SetProperty(P_Plane, C4VInt(1));
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-09-29 01:49:21 +00:00
|
|
|
C4PropList * C4DefScriptHost::GetPropList() { return Def; }
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
/*--- C4GameScriptHost ---*/
|
|
|
|
|
2011-09-25 21:24:29 +00:00
|
|
|
C4GameScriptHost::C4GameScriptHost(): ScenPrototype(0), ScenPropList(0) { }
|
2009-05-08 13:28:41 +00:00
|
|
|
C4GameScriptHost::~C4GameScriptHost() { }
|
|
|
|
|
2012-02-16 23:30:00 +00:00
|
|
|
bool C4GameScriptHost::Load(C4Group & g, const char * f, const char * l, C4LangStringTable * t)
|
2011-09-25 21:24:29 +00:00
|
|
|
{
|
2011-10-04 20:12:45 +00:00
|
|
|
assert(ScriptEngine.GetPropList());
|
2012-06-01 15:27:59 +00:00
|
|
|
C4PropListStatic * pScen = C4PropList::NewAnon(NULL/*ScenPrototype*/, NULL, ::Strings.RegString("Scenario"));
|
|
|
|
ScenPropList.SetPropList(pScen);
|
|
|
|
::ScriptEngine.RegisterGlobalConstant("Scenario", ScenPropList);
|
|
|
|
ScenPrototype.SetPropList(C4PropList::NewAnon(ScriptEngine.GetPropList(), pScen, &::Strings.P[P_Prototype]));
|
|
|
|
ScenPropList._getPropList()->SetProperty(P_Prototype, ScenPrototype);
|
2012-02-17 00:04:05 +00:00
|
|
|
Reg2List(&ScriptEngine);
|
2012-02-16 23:30:00 +00:00
|
|
|
return C4ScriptHost::Load(g, f, l, t);
|
2011-09-25 21:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void C4GameScriptHost::Clear()
|
|
|
|
{
|
2012-06-01 15:27:59 +00:00
|
|
|
ScenPropList.Set0();
|
|
|
|
ScenPrototype.Set0();
|
2011-10-14 23:38:59 +00:00
|
|
|
C4ScriptHost::Clear();
|
2011-09-25 21:24:29 +00:00
|
|
|
}
|
|
|
|
|
2011-09-25 21:25:13 +00:00
|
|
|
C4Value C4GameScriptHost::Call(const char *szFunction, C4AulParSet *Pars, bool fPassError)
|
|
|
|
{
|
2011-10-15 00:05:10 +00:00
|
|
|
// FIXME: Does fPassError make sense?
|
2012-06-01 15:27:59 +00:00
|
|
|
return ScenPropList._getPropList()->Call(szFunction, Pars);
|
2011-09-25 21:25:13 +00:00
|
|
|
}
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
C4Value C4GameScriptHost::GRBroadcast(const char *szFunction, C4AulParSet *pPars, bool fPassError, bool fRejectTest)
|
2010-03-28 18:58:01 +00:00
|
|
|
{
|
2009-05-08 13:28:41 +00:00
|
|
|
// call objects first - scenario script might overwrite hostility, etc...
|
2011-03-05 16:36:16 +00:00
|
|
|
C4Value vResult = ::Objects.GRBroadcast(szFunction, pPars, fPassError, fRejectTest);
|
|
|
|
// rejection tests abort on first nonzero result
|
|
|
|
if (fRejectTest) if (!!vResult) return vResult;
|
2009-05-08 13:28:41 +00:00
|
|
|
// scenario script call
|
2011-09-25 21:25:13 +00:00
|
|
|
return Call(szFunction, pPars, fPassError);
|
2010-03-28 18:58:01 +00:00
|
|
|
}
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2011-01-08 16:04:20 +00:00
|
|
|
C4GameScriptHost GameScript;
|