From: Daniel Karbach <daniel.karbach@localhorst.tv>
Date: Sat, 13 Oct 2012 18:10:49 +0000 (+0200)
Subject: first scripting implementation
X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=6e88a625710c7936f87b38ecf6094472f3a49f4f;p=l2e.git

first scripting implementation
---

diff --git a/Debug/src/common/subdir.mk b/Debug/src/common/subdir.mk
index e0e6bd3..ee57f9b 100644
--- a/Debug/src/common/subdir.mk
+++ b/Debug/src/common/subdir.mk
@@ -10,6 +10,8 @@ CPP_SRCS += \
 ../src/common/Ikari.cpp \
 ../src/common/Inventory.cpp \
 ../src/common/Item.cpp \
+../src/common/Script.cpp \
+../src/common/ScriptRunner.cpp \
 ../src/common/Spell.cpp \
 ../src/common/Stats.cpp \
 ../src/common/TargetingMode.cpp 
@@ -21,6 +23,8 @@ OBJS += \
 ./src/common/Ikari.o \
 ./src/common/Inventory.o \
 ./src/common/Item.o \
+./src/common/Script.o \
+./src/common/ScriptRunner.o \
 ./src/common/Spell.o \
 ./src/common/Stats.o \
 ./src/common/TargetingMode.o 
@@ -32,6 +36,8 @@ CPP_DEPS += \
 ./src/common/Ikari.d \
 ./src/common/Inventory.d \
 ./src/common/Item.d \
+./src/common/Script.d \
+./src/common/ScriptRunner.d \
 ./src/common/Spell.d \
 ./src/common/Stats.d \
 ./src/common/TargetingMode.d 
diff --git a/Release/src/common/subdir.mk b/Release/src/common/subdir.mk
index cc64cb3..c0683d3 100644
--- a/Release/src/common/subdir.mk
+++ b/Release/src/common/subdir.mk
@@ -10,6 +10,8 @@ CPP_SRCS += \
 ../src/common/Ikari.cpp \
 ../src/common/Inventory.cpp \
 ../src/common/Item.cpp \
+../src/common/Script.cpp \
+../src/common/ScriptRunner.cpp \
 ../src/common/Spell.cpp \
 ../src/common/Stats.cpp \
 ../src/common/TargetingMode.cpp 
@@ -21,6 +23,8 @@ OBJS += \
 ./src/common/Ikari.o \
 ./src/common/Inventory.o \
 ./src/common/Item.o \
+./src/common/Script.o \
+./src/common/ScriptRunner.o \
 ./src/common/Spell.o \
 ./src/common/Stats.o \
 ./src/common/TargetingMode.o 
@@ -32,6 +36,8 @@ CPP_DEPS += \
 ./src/common/Ikari.d \
 ./src/common/Inventory.d \
 ./src/common/Item.d \
+./src/common/Script.d \
+./src/common/ScriptRunner.d \
 ./src/common/Spell.d \
 ./src/common/Stats.d \
 ./src/common/TargetingMode.d 
diff --git a/src/common/Script.cpp b/src/common/Script.cpp
new file mode 100644
index 0000000..1797c55
--- /dev/null
+++ b/src/common/Script.cpp
@@ -0,0 +1,22 @@
+/*
+ * Script.cpp
+ *
+ *  Created on: Oct 13, 2012
+ *      Author: holy
+ */
+
+#include "Script.h"
+
+namespace common {
+
+Script::Script()
+: text(0)
+, textlen(0) {
+
+}
+
+Script::~Script() {
+
+}
+
+}
diff --git a/src/common/Script.h b/src/common/Script.h
new file mode 100644
index 0000000..93bc531
--- /dev/null
+++ b/src/common/Script.h
@@ -0,0 +1,46 @@
+/*
+ * Script.h
+ *
+ *  Created on: Oct 13, 2012
+ *      Author: holy
+ */
+
+#ifndef COMMON_SCRIPT_H_
+#define COMMON_SCRIPT_H_
+
+namespace common {
+
+class Script {
+
+public:
+	Script();
+	~Script();
+
+	enum Code {
+		CODE_MOVE_A0,
+		CODE_MOVE_A1,
+		CODE_MOVE_I0,
+		CODE_MOVE_I1,
+		CODE_MOVE_V0,
+		CODE_MOVE_V1,
+
+		CODE_ADD_I0,
+		CODE_ADD_I1,
+		CODE_ADD_V0,
+		CODE_ADD_V1,
+
+		CODE_RAND_I0,
+		CODE_RAND_I1,
+
+		CODE_SYSCALL,
+	};
+
+public:
+	const unsigned char *text;
+	int textlen;
+
+};
+
+}
+
+#endif /* COMMON_SCRIPT_H_ */
diff --git a/src/common/ScriptHost.h b/src/common/ScriptHost.h
new file mode 100644
index 0000000..5b44b7e
--- /dev/null
+++ b/src/common/ScriptHost.h
@@ -0,0 +1,27 @@
+/*
+ * ScriptHost.h
+ *
+ *  Created on: Oct 13, 2012
+ *      Author: holy
+ */
+
+#ifndef COMMON_SCRIPTHOST_H_
+#define COMMON_SCRIPTHOST_H_
+
+#include "fwd.h"
+
+namespace common {
+
+class ScriptHost {
+
+public:
+	virtual ~ScriptHost() { }
+
+public:
+	virtual void HandleSyscall(ScriptRunner &) = 0;
+
+};
+
+}
+
+#endif /* COMMON_SCRIPTHOST_H_ */
diff --git a/src/common/ScriptRunner.cpp b/src/common/ScriptRunner.cpp
new file mode 100644
index 0000000..110b702
--- /dev/null
+++ b/src/common/ScriptRunner.cpp
@@ -0,0 +1,126 @@
+/*
+ * ScriptRunner.cpp
+ *
+ *  Created on: Oct 13, 2012
+ *      Author: holy
+ */
+
+#include "ScriptRunner.h"
+
+#include "Script.h"
+#include "ScriptHost.h"
+
+#include <cstdlib>
+
+using geometry::Vector;
+
+namespace common {
+
+ScriptRunner::ScriptRunner()
+: host(0)
+, script(0)
+, cursor(0)
+, address0(0)
+, address1(0)
+, integer0(0)
+, integer1(0)
+, vector0(0, 0)
+, vector1(0, 0) {
+
+}
+
+ScriptRunner::~ScriptRunner() {
+
+}
+
+
+bool ScriptRunner::Running() const {
+	return script && cursor < script->textlen;
+}
+
+void ScriptRunner::Run(ScriptHost &h, const Script &s) {
+	host = &h;
+	script = &s;
+	Reset();
+	while (cursor < script->textlen) {
+		unsigned char code(script->text[cursor]);
+		++cursor;
+		Exec(code);
+	}
+	host = 0;
+	script = 0;
+}
+
+void ScriptRunner::Reset() {
+	cursor = 0;
+	address0 = 0;
+	address1 = 0;
+	integer0 = 0;
+	integer1 = 0;
+	vector0 = Vector<int>(0, 0);
+	vector1 = Vector<int>(0, 0);
+}
+
+void ScriptRunner::Exec(unsigned char code) {
+	switch (code) {
+		case Script::CODE_MOVE_A0:
+			address0 = PopAddress();
+			break;
+		case Script::CODE_MOVE_A1:
+			address1 = PopAddress();
+			break;
+		case Script::CODE_MOVE_I0:
+			integer0 = PopInt();
+			break;
+		case Script::CODE_MOVE_I1:
+			integer1 = PopInt();
+			break;
+		case Script::CODE_MOVE_V0:
+			vector0 = PopVector();
+			break;
+		case Script::CODE_MOVE_V1:
+			vector1 = PopVector();
+			break;
+		case Script::CODE_ADD_I0:
+			integer0 += PopInt();
+			break;
+		case Script::CODE_ADD_I1:
+			integer1 += PopInt();
+			break;
+		case Script::CODE_ADD_V0:
+			vector0 += PopVector();
+			break;
+		case Script::CODE_ADD_V1:
+			vector1 += PopVector();
+			break;
+		case Script::CODE_RAND_I0:
+			integer0 = std::rand();
+			break;
+		case Script::CODE_RAND_I1:
+			integer1 = std::rand();
+			break;
+		case Script::CODE_SYSCALL:
+			host->HandleSyscall(*this);
+			break;
+	}
+}
+
+void *ScriptRunner::PopAddress() {
+	void *const *addr(reinterpret_cast<void *const *>(script->text + cursor));
+	cursor += sizeof(void *);
+	return *addr;
+}
+
+int ScriptRunner::PopInt() {
+	const int *i(reinterpret_cast<const int *>(script->text + cursor));
+	cursor += sizeof(int);
+	return *i;
+}
+
+const Vector<int> &ScriptRunner::PopVector() {
+	const Vector<int> *vec(reinterpret_cast<const Vector<int> *>(script->text + cursor));
+	cursor += sizeof(Vector<int>);
+	return *vec;
+}
+
+}
diff --git a/src/common/ScriptRunner.h b/src/common/ScriptRunner.h
new file mode 100644
index 0000000..4751908
--- /dev/null
+++ b/src/common/ScriptRunner.h
@@ -0,0 +1,57 @@
+/*
+ * ScriptRunner.h
+ *
+ *  Created on: Oct 13, 2012
+ *      Author: holy
+ */
+
+#ifndef COMMON_SCRIPTRUNNER_H_
+#define COMMON_SCRIPTRUNNER_H_
+
+#include "fwd.h"
+#include "../geometry/Vector.h"
+
+namespace common {
+
+class ScriptRunner {
+
+public:
+	ScriptRunner();
+	~ScriptRunner();
+
+public:
+	void Run(ScriptHost &, const Script &);
+	bool Running() const;
+
+	void *Address0() const { return address0; }
+	void *Address1() const { return address1; }
+	int Integer0() const { return integer0; }
+	int Integer1() const { return integer1; }
+	const geometry::Vector<int> &Vector0() const { return vector0; }
+	const geometry::Vector<int> &Vector1() const { return vector1; }
+
+private:
+	void Reset();
+	void Exec(unsigned char code);
+
+	void *PopAddress();
+	int PopInt();
+	const geometry::Vector<int> &PopVector();
+
+private:
+	ScriptHost *host;
+	const Script *script;
+	int cursor;
+
+	void *address0;
+	void *address1;
+	int integer0;
+	int integer1;
+	geometry::Vector<int> vector0;
+	geometry::Vector<int> vector1;
+
+};
+
+}
+
+#endif /* COMMON_SCRIPTRUNNER_H_ */
diff --git a/src/common/fwd.h b/src/common/fwd.h
index 053307e..1cd0671 100644
--- a/src/common/fwd.h
+++ b/src/common/fwd.h
@@ -17,6 +17,9 @@ class HeroGroup;
 class Ikari;
 class Inventory;
 class Item;
+class Script;
+class ScriptHost;
+class ScriptRunner;
 class Spell;
 class Stats;
 class TargetingMode;