1/*
2 * LegacyClonk
3 *
4 * Copyright (c) 1998-2000, Matthes Bender (RedWolf Design)
5 * Copyright (c) 2017-2022, The LegacyClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16
17/* Component host for CAulScript */
18
19#pragma once
20
21#include <C4ComponentHost.h>
22
23#include <C4Aul.h>
24
25// generic script host for objects
26class C4ScriptHost : public C4AulScript, public C4ComponentHost
27{
28public:
29 C4ScriptHost();
30 ~C4ScriptHost();
31 bool Delete() override { return true; }
32
33public:
34 void Default();
35 void Clear();
36 void Close() override;
37 bool Load(const char *szName, C4Group &hGroup, const char *szFilename,
38 const char *szLanguage, C4Def *pDef, class C4LangStringTable *pLocalTable, bool fLoadTable = false);
39 const char *GetControlDesc(const char *szFunctionFormat, int32_t iCom, C4ID *pidImage = nullptr, int32_t *piImagePhase = nullptr);
40 void GetControlMethodMask(std::format_string<const char *> functionFormat, int32_t &first, int32_t &second);
41 int32_t GetControlMethod(int32_t com, int32_t first, int32_t second);
42
43 C4Value ObjectCall(C4Object *pCaller, C4Object *pObj, const char *szFunction, const C4AulParSet &pPars = C4AulParSet{}, bool fPassError = false, bool convertNilToIntBool = true)
44 {
45 if (!szFunction) return C4VNull;
46 return FunctionCall(pCaller, szFunction, pObj, pPars, fPrivateCall: false, fPassError, convertNilToIntBool);
47 }
48
49 C4Value Call(const char *szFunction, const C4AulParSet &pPars = C4AulParSet{}, bool fPassError = false, bool convertNilToIntBool = true)
50 {
51 if (!szFunction) return C4VNull;
52 return FunctionCall(pCaller: nullptr, szFunction, pObj: nullptr, pPars, fPrivateCall: false, fPassError, convertNilToIntBool);
53 }
54
55protected:
56 class C4LangStringTable *pStringTable;
57 void MakeScript();
58 C4Value FunctionCall(C4Object *pCaller, const char *szFunction, C4Object *pObj, const C4AulParSet &pPars = C4AulParSet{}, bool fPrivateCall = false, bool fPassError = false, bool convertNilToIntBool = true);
59 bool ReloadScript(const char *szPath) override;
60};
61
62// script host for defs
63class C4DefScriptHost : public C4ScriptHost
64{
65public:
66 C4DefScriptHost() : C4ScriptHost() { Default(); }
67
68 void Default();
69
70 bool Delete() override { return false; } // do NOT delete this - it's just a class member!
71
72protected:
73 void AfterLink() override; // get common funcs
74
75public:
76 C4AulScriptFunc *SFn_CalcValue; // get object value
77 C4AulScriptFunc *SFn_SellTo; // player par(0) sold the object
78 C4AulScriptFunc *SFn_ControlTransfer; // object par(0) tries to get to par(1)/par(2)
79 C4AulScriptFunc *SFn_CustomComponents; // PSF_GetCustomComponents
80 int32_t ControlMethod[2], ContainedControlMethod[2], ActivationControlMethod[2];
81};
82
83// script host for scenario scripts
84class C4GameScriptHost : public C4ScriptHost
85{
86public:
87 C4GameScriptHost();
88 ~C4GameScriptHost();
89 bool Delete() override { return false; } // do NOT delete this - it's just a class member!
90 void Default();
91 C4Value GRBroadcast(const char *szFunction, const C4AulParSet &pPars = C4AulParSet{}, bool fPassError = false, bool fRejectTest = false, bool convertNilToIntBool = true); // call function in scenario script and all goals/rules/environment objects
92
93 // Global script data
94 // FIXME: Move to C4AulScriptEngine
95 int32_t Counter;
96 bool Go;
97 bool Execute();
98
99 // Compile scenario script data
100 void CompileFunc(StdCompiler *pComp);
101};
102