]> git.localhorst.tv Git - l2e.git/commitdiff
first scripting implementation
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 13 Oct 2012 18:10:49 +0000 (20:10 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 13 Oct 2012 18:40:28 +0000 (20:40 +0200)
Debug/src/common/subdir.mk
Release/src/common/subdir.mk
src/common/Script.cpp [new file with mode: 0644]
src/common/Script.h [new file with mode: 0644]
src/common/ScriptHost.h [new file with mode: 0644]
src/common/ScriptRunner.cpp [new file with mode: 0644]
src/common/ScriptRunner.h [new file with mode: 0644]
src/common/fwd.h

index e0e6bd3e3b2b4718b9ed2a1048210497167a6b39..ee57f9b30b753e8509e5e28173d4f20eaf11bcbc 100644 (file)
@@ -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 
index cc64cb3866efa83e5b99f40152965aeb11d0a706..c0683d392d6893bd12f84d8ca3f7ec9f35e8c50b 100644 (file)
@@ -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 (file)
index 0000000..1797c55
--- /dev/null
@@ -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 (file)
index 0000000..93bc531
--- /dev/null
@@ -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 (file)
index 0000000..5b44b7e
--- /dev/null
@@ -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 (file)
index 0000000..110b702
--- /dev/null
@@ -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 (file)
index 0000000..4751908
--- /dev/null
@@ -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_ */
index 053307e4f9db84ed2f049a9c214f349394cec6ac..1cd06715cea3bb927d82c02b49e0b96932f296d4 100644 (file)
@@ -17,6 +17,9 @@ class HeroGroup;
 class Ikari;
 class Inventory;
 class Item;
+class Script;
+class ScriptHost;
+class ScriptRunner;
 class Spell;
 class Stats;
 class TargetingMode;