-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
121 lines (96 loc) · 4.41 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
ACS Virtual Machine (ACSVM)
ACS VM library and standalone interpreter. Intended to be suitable for use in
Doom-based video game engines to implement Hexen ACS or ZDoom ACS bytecode
execution. It is focused on being usable for implementing existing extensions
and new functions (whether through new instructions or CallFunc indexes) while
having performance suitable to the complex ACS-based mods that have come into
existence.
===============================================================================
Integrating ACSVM
===============================================================================
Although it can be used as an external library, ACSVM is also written so that
it can be integrated directly into the repository of projects using it without
needing to change any of the ACSVM files.
It is enough to copy ACSVM's root into the root of the target repository, such
that acsvm/CMakeLists.txt exists. Only the subdirectories of desired components
(usually just ACSVM) need to be included. The CMakeLists.txt will automatically
disable the omitted components.
In your root CMakeLists.txt, all that is needed is:
set(ACSVM_NOFLAGS ON)
set(ACSVM_SHARED OFF)
add_subdirectory(acsvm)
And the enabled components (again, usually just acsvm) will be available for
use in target_link_libraries.
===============================================================================
Usage Overview
===============================================================================
===========================================================
Getting Started
===========================================================
To use ACSVM, you will need to define a class that inherits from
ACSVM::Environment. By overriding the various virtuals you can configure the
different aspects of ACS loading and interpretation. But the absolute minimal
usage only requires overriding loadModule:
class Env : public ACSVM::Environment
{
protected:
virtual void loadModule(ACSVM::Module *module);
};
Which is implemented by using the module's name to locate the corresponding
bytecode and passing that to module->readBytecode. The default behavior of
getModuleName is to just set the ModuleName's string. This can be used to
implement bytecode directly from files:
void Env::loadModule(ACSVM::Module *module)
{
std::ifstream in{module->name.s->str,
std::ios_base::in | std::ios_base::binary};
if(!in) throw ACSVM::ReadError("file open failure");
std::vector<ACSVM::Byte> data;
for(int c; c = in.get(), in;)
data.push_back(c);
module->readBytecode(data.data(), data.size());
}
In a Doom engine, this would most likely use lumps, instead. Either by doing
the lookup in loadModule, or by overriding getModuleName to turn the input
string into a lump number.
To actually initialize the environment and load some modules, you can use:
void EnvInit(Environment &env, char const *const *namev, std::size_t namec)
{
// Load modules.
std::vector<ACSVM::Module *> modules;
for(std::size_t i = 1; i < namec; ++i)
modules.push_back(env.getModule(env.getModuleName(namev[i])));
// Create and activate scopes.
ACSVM::GlobalScope *global = env.getGlobalScope(0); global->active = true;
ACSVM::HubScope *hub = global->getHubScope(0); hub ->active = true;
ACSVM::MapScope *map = hub->getMapScope(0); map ->active = true;
// Register modules with map scope.
map->addModules(modules.data(), modules.size());
// Start Open scripts.
map->scriptStartType(1, {});
}
And then a simple interpreter loop:
while(env.hasActiveThread())
{
std::chrono::duration<double> rate{1.0 / 35};
auto time = std::chrono::steady_clock::now() + rate;
env.exec();
std::this_thread::sleep_until(time);
}
Note that if you already have a game loop, you only need to call env.exec once
per simulation frame.
Finally, you will need to register instruction and callfunc functions to
actually interface with the larger environment. At the least, it is useful to
implement the EndPrint (86) instruction:
bool CF_EndPrint(ACSVM::Thread *thread, ACSVM::Word const *, ACSVM::Word)
{
std::cout << thread->printBuf.data() << '\n';
thread->printBuf.drop();
return false;
}
Environment::Environment()
{
addCodeDataACS0(86, {"", 0, addCallFunc(CF_EndPrint)});
}
Most of the other ACS printing logic is already handled by ACSVM, so this is
enough to display simple Print messages.