summaryrefslogtreecommitdiff
path: root/src/cat
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-02-05 16:37:36 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-02-05 16:37:36 (GMT)
commit7ae5eaab79a41f31b633ca6f1bfb0dbae2fccb90 (patch)
treeb76fc14cca5e19c5482209f34131973ad1f80e6a /src/cat
parent8024caec55ff1b93eefe50663d4ddf63934f8d63 (diff)
downloadpowder-7ae5eaab79a41f31b633ca6f1bfb0dbae2fccb90.zip
powder-7ae5eaab79a41f31b633ca6f1bfb0dbae2fccb90.tar.gz
Started intrepreter for tpt script and various things for console
Diffstat (limited to 'src/cat')
-rw-r--r--src/cat/CommandInterface.cpp107
-rw-r--r--src/cat/CommandInterface.h32
-rw-r--r--src/cat/TPTSTypes.cpp112
-rw-r--r--src/cat/TPTSTypes.h80
-rw-r--r--src/cat/TPTScriptInterface.cpp314
-rw-r--r--src/cat/TPTScriptInterface.h27
6 files changed, 672 insertions, 0 deletions
diff --git a/src/cat/CommandInterface.cpp b/src/cat/CommandInterface.cpp
new file mode 100644
index 0000000..485a029
--- /dev/null
+++ b/src/cat/CommandInterface.cpp
@@ -0,0 +1,107 @@
+/*
+ * Kitty.cpp
+ *
+ * Created on: Feb 2, 2012
+ * Author: Simon
+ */
+
+#include <iostream>
+#include <string>
+#include <string.h>
+#include "CommandInterface.h"
+#include "game/GameModel.h"
+
+CommandInterface::CommandInterface() {
+}
+
+void CommandInterface::AttachGameModel(GameModel * m)
+{
+ this->m = m;
+}
+
+int CommandInterface::Command(std::string command)
+{
+ lastError = "No interpreter";
+ return -1;
+}
+
+std::string CommandInterface::FormatCommand(std::string command)
+{
+ return command;
+}
+
+int CommandInterface::GetPropertyOffset(std::string key_, FormatType & format)
+{
+ char * key = (char *)key_.c_str();
+ int offset;
+ if (strcmp(key, "type")==0){
+ offset = offsetof(Particle, type);
+ format = FormatInt;
+ } else if (strcmp(key, "life")==0){
+ offset = offsetof(Particle, life);
+ format = FormatInt;
+ } else if (strcmp(key, "ctype")==0){
+ offset = offsetof(Particle, ctype);
+ format = FormatInt;
+ } else if (strcmp(key, "temp")==0){
+ offset = offsetof(Particle, temp);
+ format = FormatFloat;
+ } else if (strcmp(key, "tmp")==0){
+ offset = offsetof(Particle, tmp);
+ format = FormatInt;
+ } else if (strcmp(key, "tmp2")==0){
+ offset = offsetof(Particle, tmp2);
+ format = FormatInt;
+ } else if (strcmp(key, "vy")==0){
+ offset = offsetof(Particle, vy);
+ format = FormatFloat;
+ } else if (strcmp(key, "vx")==0){
+ offset = offsetof(Particle, vx);
+ format = FormatFloat;
+ } else if (strcmp(key, "x")==0){
+ offset = offsetof(Particle, x);
+ format = FormatFloat;
+ } else if (strcmp(key, "y")==0){
+ offset = offsetof(Particle, y);
+ format = FormatFloat;
+ } else if (strcmp(key, "dcolour")==0){
+ offset = offsetof(Particle, dcolour);
+ format = FormatInt;
+ } else if (strcmp(key, "dcolor")==0){
+ offset = offsetof(Particle, dcolour);
+ format = FormatInt;
+ } else {
+ offset = -1;
+ }
+ return offset;
+}
+
+int CommandInterface::GetParticleType(std::string type)
+{
+ int i = -1;
+ char * txt = (char*)type.c_str();
+
+ //Scope
+ part_type *ptypes = m->GetSimulation()->ptypes;
+
+ // alternative names for some elements
+ if (strcasecmp(txt,"C4")==0) i = PT_PLEX;
+ else if (strcasecmp(txt,"C5")==0) i = PT_C5;
+ else if (strcasecmp(txt,"NONE")==0) i = PT_NONE;
+ for (i=1; i<PT_NUM; i++) {
+ if (strcasecmp(txt, ptypes[i].name)==0 && ptypes[i].enabled)
+ {
+ return i;
+ }
+ }
+ return i;
+}
+
+std::string CommandInterface::GetLastError()
+{
+ return lastError;
+}
+
+CommandInterface::~CommandInterface() {
+}
+
diff --git a/src/cat/CommandInterface.h b/src/cat/CommandInterface.h
new file mode 100644
index 0000000..4f25600
--- /dev/null
+++ b/src/cat/CommandInterface.h
@@ -0,0 +1,32 @@
+/*
+ * Kitty.h
+ *
+ * Created on: Feb 2, 2012
+ * Author: Simon
+ */
+
+#ifndef KITTY_H_
+#define KITTY_H_
+
+#include <string>
+//#include "game/GameModel.h"
+
+class GameModel;
+class CommandInterface {
+protected:
+ enum FormatType { FormatInt, FormatString, FormatChar, FormatFloat };
+ std::string lastError;
+ GameModel * m;
+public:
+ CommandInterface();
+ int GetPropertyOffset(std::string key_, FormatType & format);
+ int GetParticleType(std::string type);
+ void AttachGameModel(GameModel * m);
+ virtual void Tick() {}
+ virtual int Command(std::string command);
+ virtual std::string FormatCommand(std::string command);
+ std::string GetLastError();
+ virtual ~CommandInterface();
+};
+
+#endif /* KITTY_H_ */
diff --git a/src/cat/TPTSTypes.cpp b/src/cat/TPTSTypes.cpp
new file mode 100644
index 0000000..7b39cc9
--- /dev/null
+++ b/src/cat/TPTSTypes.cpp
@@ -0,0 +1,112 @@
+/*
+ * TPTSTypes.cpp
+ *
+ * Created on: Feb 4, 2012
+ * Author: Simon
+ */
+
+#include <iostream>
+#include <sstream>
+#include "TPTSTypes.h"
+
+AnyType::AnyType(ValueType type_, void * value_):
+ type(type_),
+ value(value_)
+{
+}
+
+ValueType AnyType::GetType()
+{
+ return type;
+}
+
+AnyType::AnyType(const AnyType & v):
+ type(v.type),
+ value(v.value)
+{
+ if(type == TypeString)
+ {
+ value = new std::string(*((std::string*)value));
+ }
+ else if(type == TypePoint)
+ {
+ value = new ui::Point(*((ui::Point*)value));
+ }
+}
+
+AnyType::operator NumberType()
+{
+ if(type != TypeNumber)
+ throw InvalidConversionException(type, TypeNumber);
+ else
+ return NumberType((int)value);
+}
+
+AnyType::operator StringType()
+{
+ if(type == TypeNumber)
+ {
+ std::stringstream numberStream;
+ numberStream << ((NumberType*)this)->Value();
+ return StringType(numberStream.str());
+ }
+ else if(type == TypeString && value)
+ {
+ return StringType(*((std::string*)value));
+ }
+ else
+ throw InvalidConversionException(type, TypeString);
+
+}
+
+AnyType::operator PointType()
+{
+ if(type == TypePoint)
+ {
+ return PointType(*((ui::Point*)value));
+ }
+ else if(type == TypeString)
+ {
+ ui::Point thisPoint = *((ui::Point*)value);
+ std::stringstream pointStream;
+ pointStream << thisPoint.X << "," << thisPoint.Y;
+ return StringType(pointStream.str());
+ }
+ else
+ throw InvalidConversionException(type, TypePoint);
+}
+
+AnyType::~AnyType()
+{
+ if(type == TypeString || type == TypePoint)
+ delete value;
+}
+
+//Number Type
+
+NumberType::NumberType(int number): AnyType(TypeNumber, (void*)number) { }
+
+int NumberType::Value()
+{
+ return (int)value;
+}
+
+//String type
+
+StringType::StringType(std::string string): AnyType(TypeString, new std::string(string)) { }
+
+std::string StringType::Value()
+{
+ return std::string(*((std::string*)value));
+}
+
+//Point type
+
+PointType::PointType(ui::Point point): AnyType(TypePoint, new ui::Point(point)) { }
+
+PointType::PointType(int pointX, int pointY): AnyType(TypePoint, new ui::Point(pointX, pointY)) { }
+
+ui::Point PointType::Value()
+{
+ return ui::Point(*((ui::Point*)value));
+}
diff --git a/src/cat/TPTSTypes.h b/src/cat/TPTSTypes.h
new file mode 100644
index 0000000..ad08f0b
--- /dev/null
+++ b/src/cat/TPTSTypes.h
@@ -0,0 +1,80 @@
+/*
+ * TPTSTypes.h
+ *
+ * Created on: Feb 4, 2012
+ * Author: Simon
+ */
+
+#ifndef TPTSTYPES_H_
+#define TPTSTYPES_H_
+
+#include <string>
+#include "interface/Point.h"
+
+enum ValueType { TypeNumber, TypePoint, TypeString, TypeNull, TypeFunction };
+
+class GeneralException
+{
+protected:
+ std::string exception;
+public:
+ GeneralException(std::string message){
+ exception = message;
+ }
+ std::string GetExceptionMessage() {
+ return exception;
+ }
+};
+
+class InvalidConversionException: public GeneralException
+{
+private:
+ ValueType from;
+ ValueType to;
+public:
+ InvalidConversionException(ValueType from_, ValueType to_): GeneralException("Invalid conversion"), from(from_), to(to_) {
+ }
+};
+
+class NumberType;
+class StringType;
+class PointType;
+
+class AnyType
+{
+protected:
+ ValueType type;
+ void * value;
+public:
+ AnyType(ValueType type_, void * value_);
+ AnyType(const AnyType & v);
+ operator NumberType();
+ operator StringType();
+ operator PointType();
+ ValueType GetType();
+ ~AnyType();
+};
+
+class NumberType: public AnyType
+{
+public:
+ NumberType(int number);
+ int Value();
+};
+
+class StringType: public AnyType
+{
+public:
+ StringType(std::string string);
+ std::string Value();
+};
+
+class PointType: public AnyType
+{
+public:
+ PointType(ui::Point point);
+ PointType(int pointX, int pointY);
+ ui::Point Value();
+};
+
+#endif /* TPTSTYPES_H_ */
diff --git a/src/cat/TPTScriptInterface.cpp b/src/cat/TPTScriptInterface.cpp
new file mode 100644
index 0000000..7298be5
--- /dev/null
+++ b/src/cat/TPTScriptInterface.cpp
@@ -0,0 +1,314 @@
+/*
+ * TPTScriptInterface.cpp
+ *
+ * Created on: Feb 5, 2012
+ * Author: Simon
+ */
+
+#include <stack>
+#include <iostream>
+#include <string>
+#include <deque>
+#include <string.h>
+#include <stdlib.h>
+#include "TPTScriptInterface.h"
+#include "game/GameModel.h"
+
+TPTScriptInterface::TPTScriptInterface() {
+}
+
+int TPTScriptInterface::Command(std::string command)
+{
+ lastError = "";
+ std::deque<std::string> words;
+ std::deque<AnyType> commandWords;
+ int retCode;
+
+ //Split command into words, put them on the stack
+ char * rawCommand;
+ rawCommand = (char*)calloc(command.length()+1, 1);
+ memcpy(rawCommand, (char*)command.c_str(), command.length());
+ char * currentWord = rawCommand;
+ char * currentCommand = rawCommand;
+ while((currentCommand = strchr(currentCommand, ' ')))
+ {
+ currentCommand[0] = 0;
+ words.push_back(std::string(currentWord));
+ currentWord = ++currentCommand;
+ }
+ words.push_back(std::string(currentWord));
+ while(!words.empty())
+ {
+ try
+ {
+ commandWords.push_back(eval(&words));
+ }
+ catch (GeneralException & e)
+ {
+ retCode = -1;
+ lastError = e.GetExceptionMessage();
+ break;
+ }
+ }
+ free(rawCommand);
+ if(commandWords.size())
+ {
+ retCode = 0;
+ lastError = ((StringType)commandWords.front()).Value();
+ }
+
+ //Evaluate
+ return 0;
+}
+
+ValueType TPTScriptInterface::testType(std::string word)
+{
+ int i = 0;
+ char * rawWord = (char *)word.c_str();
+ //Function
+ if(word == "set")
+ return TypeFunction;
+ //Basic type
+ parseNumber:
+ for(i = 0; i < word.length(); i++)
+ if(!(rawWord[i] >= '0' && rawWord[i] <= '9'))
+ {
+ if(rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
+ goto parsePoint;
+ else
+ goto parseString;
+ }
+ return TypeNumber;
+ parsePoint:
+ i++;
+ for(; i < word.length(); i++)
+ if(!(rawWord[i] >= '0' && rawWord[i] <= '9'))
+ {
+ goto parseString;
+ }
+ return TypePoint;
+ parseString:
+ return TypeString;
+}
+
+AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
+{
+ if(words->size() < 1)
+ return AnyType(TypeNull, NULL);
+ std::string word = words->front(); words->pop_front();
+ char * rawWord = (char *)word.c_str();
+ ValueType wordType = testType(word);
+ switch(wordType)
+ {
+ case TypeFunction:
+ if(word == "set")
+ return tptS_set(words);
+ break;
+ case TypeNumber:
+ return NumberType(atoi(rawWord));
+ case TypePoint:
+ {
+ int pointX, pointY;
+ sscanf(rawWord, "%d,%d", &pointX, &pointY);
+ return PointType(pointX, pointY);
+ }
+ case TypeString:
+ return StringType(word);
+ }
+}
+
+std::string TPTScriptInterface::FormatCommand(std::string command)
+{
+ std::deque<std::string> words;
+ std::deque<AnyType> commandWords;
+ std::string outputData;
+
+ //Split command into words, put them on the stack
+ char * rawCommand;
+ rawCommand = (char*)calloc(command.length()+1, 1);
+ memcpy(rawCommand, (char*)command.c_str(), command.length());
+ char * currentWord = rawCommand;
+ char * currentCommand = rawCommand;
+ while((currentCommand = strchr(currentCommand, ' ')))
+ {
+ currentCommand[0] = 0;
+ words.push_back(std::string(currentWord));
+ currentWord = ++currentCommand;
+ }
+ words.push_back(std::string(currentWord));
+
+ while(!words.empty())
+ {
+ ValueType cType = testType(words.front());
+ switch(cType)
+ {
+ case TypeFunction:
+ outputData += "\bt";
+ break;
+ case TypeNumber:
+ case TypePoint:
+ outputData += "\bo";
+ break;
+ case TypeString:
+ outputData += "\bg";
+ break;
+ default:
+ outputData += "\bw";
+ break;
+ }
+ outputData += words.front() + " ";
+ words.pop_front();
+ }
+ /*char * rawText = (char*)command.c_str();
+ char * outputData = (char *)calloc(command.length()*6, 1);
+ int rawTextLoc = 0;
+ int outputDataLoc = 0;
+ std::stack<char> pstack;
+ while(rawText[rawTextLoc])
+ {
+ switch(rawText[rawTextLoc])
+ {
+ case '\\':
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ if(rawText[rawTextLoc])
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ break;
+ case '"':
+ if(pstack.size() && pstack.top() == '"')
+ {
+ pstack.pop();
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ outputData[outputDataLoc++] = '\b';
+ outputData[outputDataLoc++] = 'w';
+ }
+ else
+ {
+ pstack.push('"');
+ outputData[outputDataLoc++] = '\b';
+ outputData[outputDataLoc++] = 'o';
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ }
+ break;
+ default:
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ break;
+ }
+ }*/
+ return outputData;
+}
+
+AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
+{
+ //Arguments from stack
+ StringType property = eval(words);
+ AnyType value = eval(words);
+ AnyType selector = eval(words);
+
+ Simulation * sim = m->GetSimulation();
+
+ int returnValue = 0;
+
+ FormatType propertyFormat;
+ int propertyOffset = GetPropertyOffset(property.Value(), propertyFormat);
+
+ if(propertyOffset==-1)
+ throw GeneralException("Invalid property");
+
+ if(selector.GetType() == TypePoint || selector.GetType() == TypeNumber)
+ {
+ int partIndex = -1;
+ if(selector.GetType() == TypePoint)
+ {
+ ui::Point tempPoint = ((PointType)selector).Value();
+ if(tempPoint.X<0 || tempPoint.Y<0 || tempPoint.Y >= YRES || tempPoint.X >= XRES)
+ throw GeneralException("Invalid particle");
+
+ }
+ else
+ partIndex = ((NumberType)selector).Value();
+ if(partIndex<0 || partIndex>NPART || sim->parts[partIndex].type==0)
+ throw GeneralException("Invalid particle");
+
+ switch(propertyFormat)
+ {
+ case FormatInt:
+ *((int*)(((void*)&sim->parts[partIndex])+propertyOffset)) = ((NumberType)value).Value();
+ break;
+ case FormatFloat:
+ *((float*)(((void*)&sim->parts[partIndex])+propertyOffset)) = ((NumberType)value).Value();
+ break;
+ }
+ returnValue = 1;
+ }
+ else if(selector.GetType() == TypeString && ((StringType)selector).Value() == "all")
+ {
+ switch(propertyFormat)
+ {
+ case FormatInt:
+ {
+ int tempNumber = ((NumberType)value).Value();
+ for(int j = 0; j < NPART; j++)
+ if(sim->parts[j].type)
+ {
+ returnValue++;
+ *((int*)(((void*)&sim->parts[j])+propertyOffset)) = tempNumber;
+ }
+ }
+ break;
+ case FormatFloat:
+ {
+ float tempNumber = ((NumberType)value).Value();
+ for(int j = 0; j < NPART; j++)
+ if(sim->parts[j].type)
+ {
+ returnValue++;
+ *((float*)(((void*)&sim->parts[j])+propertyOffset)) = tempNumber;
+ }
+ }
+ break;
+ }
+ }
+ else if(selector.GetType() == TypeString || selector.GetType() == TypeNumber)
+ {
+ int type;
+ if(selector.GetType() == TypeNumber)
+ type = ((NumberType)selector).Value();
+ else
+ type = GetParticleType(((StringType)selector).Value());
+
+ if(type<0 || type>=PT_NUM)
+ throw GeneralException("Invalid particle type");
+ switch(propertyFormat)
+ {
+ case FormatInt:
+ {
+ int tempNumber = ((NumberType)value).Value();
+ for(int j = 0; j < NPART; j++)
+ if(sim->parts[j].type == type)
+ {
+ returnValue++;
+ *((int*)(((void*)&sim->parts[j])+propertyOffset)) = tempNumber;
+ }
+ }
+ break;
+ case FormatFloat:
+ {
+ float tempNumber = ((NumberType)value).Value();
+ for(int j = 0; j < NPART; j++)
+ if(sim->parts[j].type == type)
+ {
+ returnValue++;
+ *((float*)(((void*)&sim->parts[j])+propertyOffset)) = tempNumber;
+ }
+ }
+ break;
+ }
+ }
+ else
+ throw GeneralException("Invalid selector");
+ return NumberType(returnValue);
+}
+
+TPTScriptInterface::~TPTScriptInterface() {
+}
+
diff --git a/src/cat/TPTScriptInterface.h b/src/cat/TPTScriptInterface.h
new file mode 100644
index 0000000..c1a61de
--- /dev/null
+++ b/src/cat/TPTScriptInterface.h
@@ -0,0 +1,27 @@
+/*
+ * TPTScriptInterface.h
+ *
+ * Created on: Feb 5, 2012
+ * Author: Simon
+ */
+
+#ifndef TPTSCRIPTINTERFACE_H_
+#define TPTSCRIPTINTERFACE_H_
+
+#include "CommandInterface.h"
+#include "TPTSTypes.h"
+
+class TPTScriptInterface: public CommandInterface {
+protected:
+ AnyType eval(std::deque<std::string> * words);
+ AnyType tptS_set(std::deque<std::string> * words);
+ ValueType testType(std::string word);
+public:
+ TPTScriptInterface();
+ virtual void Tick() {}
+ virtual int Command(std::string command);
+ virtual std::string FormatCommand(std::string command);
+ virtual ~TPTScriptInterface();
+};
+
+#endif /* TPTSCRIPTINTERFACE_H_ */