From: Daniel Karbach <daniel.karbach@localhorst.tv>
Date: Sun, 14 Oct 2012 21:07:07 +0000 (+0200)
Subject: added compare and jump codes for scripts
X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=8060b9ee74a24fe33a874732035dc8b74003439f;p=l2e.git

added compare and jump codes for scripts
---

diff --git a/src/common/Script.h b/src/common/Script.h
index 0dbaf4d..c203f40 100644
--- a/src/common/Script.h
+++ b/src/common/Script.h
@@ -35,6 +35,18 @@ public:
 		CODE_RAND_I0,
 		CODE_RAND_I1,
 
+		CODE_CMP_I0,
+		CODE_CMP_I1,
+		CODE_CMP_I0_I1,
+
+		CODE_JUMP,
+		CODE_JUMP_EQUAL,
+		CODE_JUMP_NOT_EQUAL,
+		CODE_JUMP_LESS,
+		CODE_JUMP_LESS_EQUAL,
+		CODE_JUMP_GREATER,
+		CODE_JUMP_GREATER_EQUAL,
+
 		CODE_SYSCALL,
 	};
 
diff --git a/src/common/ScriptRunner.cpp b/src/common/ScriptRunner.cpp
index c1022c4..98fd4fb 100644
--- a/src/common/ScriptRunner.cpp
+++ b/src/common/ScriptRunner.cpp
@@ -25,7 +25,8 @@ ScriptRunner::ScriptRunner()
 , integer0(0)
 , integer1(0)
 , vector0(0, 0)
-, vector1(0, 0) {
+, vector1(0, 0)
+, compare(0) {
 
 }
 
@@ -105,6 +106,60 @@ void ScriptRunner::Exec(unsigned char code) {
 		case Script::CODE_RAND_I1:
 			integer1 = std::rand();
 			break;
+		case Script::CODE_CMP_I0:
+			Compare(integer0, PopInt());
+			break;
+		case Script::CODE_CMP_I1:
+			Compare(integer1, PopInt());
+			break;
+		case Script::CODE_CMP_I0_I1:
+			Compare(integer0, integer1);
+			break;
+		case Script::CODE_JUMP:
+			cursor = PopInt();
+			break;
+		case Script::CODE_JUMP_EQUAL: {
+			int addr(PopInt());
+			if (compare == COMPARE_EQUAL) {
+				cursor = addr;
+			}
+			break;
+		}
+		case Script::CODE_JUMP_NOT_EQUAL: {
+			int addr(PopInt());
+			if (compare != COMPARE_EQUAL) {
+				cursor = addr;
+			}
+			break;
+		}
+		case Script::CODE_JUMP_LESS: {
+			int addr(PopInt());
+			if (compare == COMPARE_LESS) {
+				cursor = addr;
+			}
+			break;
+		}
+		case Script::CODE_JUMP_LESS_EQUAL: {
+			int addr(PopInt());
+			if (compare != COMPARE_GREATER) {
+				cursor = addr;
+			}
+			break;
+		}
+		case Script::CODE_JUMP_GREATER: {
+			int addr(PopInt());
+			if (compare == COMPARE_GREATER) {
+				cursor = addr;
+			}
+			break;
+		}
+		case Script::CODE_JUMP_GREATER_EQUAL: {
+			int addr(PopInt());
+			if (compare != COMPARE_LESS) {
+				cursor = addr;
+			}
+			break;
+		}
 		case Script::CODE_SYSCALL:
 			host->HandleSyscall(*this);
 			break;
@@ -129,4 +184,14 @@ const Vector<int> &ScriptRunner::PopVector() {
 	return *vec;
 }
 
+void ScriptRunner::Compare(int lhs, int rhs) {
+	if (lhs < rhs) {
+		compare = COMPARE_LESS;
+	} else if (lhs > rhs) {
+		compare = COMPARE_GREATER;
+	} else {
+		compare = COMPARE_EQUAL;
+	}
+}
+
 }
diff --git a/src/common/ScriptRunner.h b/src/common/ScriptRunner.h
index 4751908..9b0f1ac 100644
--- a/src/common/ScriptRunner.h
+++ b/src/common/ScriptRunner.h
@@ -11,6 +11,8 @@
 #include "fwd.h"
 #include "../geometry/Vector.h"
 
+#include <SDL.h>
+
 namespace common {
 
 class ScriptRunner {
@@ -38,6 +40,14 @@ private:
 	int PopInt();
 	const geometry::Vector<int> &PopVector();
 
+	void Compare(int, int);
+
+	enum CompareFlags {
+		COMPARE_EQUAL = 0,
+		COMPARE_LESS = 1,
+		COMPARE_GREATER = 2,
+	};
+
 private:
 	ScriptHost *host;
 	const Script *script;
@@ -50,6 +60,8 @@ private:
 	geometry::Vector<int> vector0;
 	geometry::Vector<int> vector1;
 
+	Uint8 compare;
+
 };
 
 }