summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-05-12 17:11:20 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-05-12 17:11:20 (GMT)
commitc5798c745675e4866a44228ddf161258e85d39a7 (patch)
tree6788dbb3c89632f8a1f2acc4038a93faa26b6324
parent16d3895e9c054e908ca8b230719f4294e824a4a2 (diff)
downloadpowder-c5798c745675e4866a44228ddf161258e85d39a7.zip
powder-c5798c745675e4866a44228ddf161258e85d39a7.tar.gz
Tools implemented in a similar way to elements
-rw-r--r--generator.py209
-rw-r--r--src/game/GameModel.cpp7
-rw-r--r--src/game/Tool.h12
-rw-r--r--src/simulation/Simulation.cpp109
-rw-r--r--src/simulation/Simulation.h8
-rw-r--r--src/simulation/SimulationData.h3
-rw-r--r--src/simulation/Tools.h7
-rw-r--r--src/simulation/tools/Cool.cpp19
-rw-r--r--src/simulation/tools/Heat.cpp19
-rw-r--r--src/simulation/tools/SimTool.cpp10
-rw-r--r--src/simulation/tools/SimTool.h23
11 files changed, 357 insertions, 69 deletions
diff --git a/generator.py b/generator.py
index dc97594..55737d5 100644
--- a/generator.py
+++ b/generator.py
@@ -4,75 +4,154 @@ if os.path.isdir("generated/"):
shutil.rmtree("generated/")
os.mkdir("generated")
-elementClasses = dict()
+def generateElements():
+ elementClasses = dict()
-elementHeader = """#ifndef ELEMENTCLASSES_H
-#define ELEMENTCLASSES_H
-#include <vector>
-#include "simulation/Element.h"
-#include "simulation/elements/Element.h"
-"""
+ elementHeader = """#ifndef ELEMENTCLASSES_H
+ #define ELEMENTCLASSES_H
+ #include <vector>
+ #include "simulation/Element.h"
+ #include "simulation/elements/Element.h"
+ """
-directives = []
+ directives = []
-elementFiles = os.listdir("src/simulation/Elements")
-for elementFile in elementFiles:
- f = open("src/simulation/Elements/"+elementFile, "r")
- fileData = f.read()
- f.close()
+ elementFiles = os.listdir("src/simulation/elements")
+ for elementFile in elementFiles:
+ f = open("src/simulation/elements/"+elementFile, "r")
+ fileData = f.read()
+ f.close()
+
+ directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)'
+ matcher = re.compile(directiveMatcher)
+ directiveMatches = matcher.findall(fileData)
+
+ for match in directiveMatches:
+ directives.append(match.split(" "))
+
+ classDirectives = []
+ for d in directives:
+ if d[0] == "ElementClass":
+ elementClasses[d[1]] = []
+ elementHeader += "#define %s %s\n" % (d[2], d[3])
+ d[3] = string.atoi(d[3])
+ classDirectives.append(d)
+
+ for d in directives:
+ if d[0] == "ElementHeader":
+ elementClasses[d[1]].append(string.join(d[2:], " ")+";")
+
+ for className, classMembers in elementClasses.items():
+ elementHeader += """class {0}: public Element
+ {{
+ public:
+ {0}();
+ virtual ~{0}();
+ {1}
+ }};
+ """.format(className, string.join(classMembers, "\n"))
+
+ elementHeader += """std::vector<Element> GetElements();
+ #endif
+ """
+
+ elementContent = """#include "ElementClasses.h"
+ std::vector<Element> GetElements()
+ {
+ std::vector<Element> elements;
+ """;
- directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)'
- matcher = re.compile(directiveMatcher)
- directiveMatches = matcher.findall(fileData)
+ elementIDs = sorted(classDirectives, key=lambda directive: directive[3])
+ for d in elementIDs:
+ elementContent += """ elements.push_back(%s());
+ """ % (d[1])
- for match in directiveMatches:
- directives.append(match.split(" "))
+ elementContent += """ return elements;
+ }
+ """;
-classDirectives = []
-for d in directives:
- if d[0] == "ElementClass":
- elementClasses[d[1]] = []
- elementHeader += "#define %s %s\n" % (d[2], d[3])
- d[3] = string.atoi(d[3])
- classDirectives.append(d)
+ f = open("generated/ElementClasses.h", "w")
+ f.write(elementHeader)
+ f.close()
+
+ f = open("generated/ElementClasses.cpp", "w")
+ f.write(elementContent)
+ f.close()
+
+def generateTools():
+ toolClasses = dict()
+
+ toolHeader = """#ifndef TOOLCLASSES_H
+ #define TOOLCLASSES_H
+ #include <vector>
+ #include "simulation/Tools.h"
+ #include "simulation/tools/SimTool.h"
+ """
+
+ directives = []
-for d in directives:
- if d[0] == "ElementHeader":
- elementClasses[d[1]].append(string.join(d[2:], " ")+";")
-
-for className, classMembers in elementClasses.items():
- elementHeader += """class {0}: public Element
-{{
- public:
- {0}();
- virtual ~{0}();
- {1}
-}};
-""".format(className, string.join(classMembers, "\n"))
-
-elementHeader += """std::vector<Element> GetElements();
-#endif
-"""
-
-elementContent = """#include "ElementClasses.h"
-std::vector<Element> GetElements()
-{
- std::vector<Element> elements;
-""";
-
-elementIDs = sorted(classDirectives, key=lambda directive: directive[3])
-for d in elementIDs:
- elementContent += """ elements.push_back(%s());
-""" % (d[1])
-
-elementContent += """ return elements;
-}
-""";
-
-f = open("generated/ElementClasses.h", "w")
-f.write(elementHeader)
-f.close()
-
-f = open("generated/ElementClasses.cpp", "w")
-f.write(elementContent)
-f.close() \ No newline at end of file
+ toolFiles = os.listdir("src/simulation/tools")
+ for toolFile in toolFiles:
+ f = open("src/simulation/tools/"+toolFile, "r")
+ fileData = f.read()
+ f.close()
+
+ directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)'
+ matcher = re.compile(directiveMatcher)
+ directiveMatches = matcher.findall(fileData)
+
+ for match in directiveMatches:
+ directives.append(match.split(" "))
+
+ classDirectives = []
+ for d in directives:
+ if d[0] == "ToolClass":
+ toolClasses[d[1]] = []
+ toolHeader += "#define %s %s\n" % (d[2], d[3])
+ d[3] = string.atoi(d[3])
+ classDirectives.append(d)
+
+ for d in directives:
+ if d[0] == "ToolHeader":
+ toolClasses[d[1]].append(string.join(d[2:], " ")+";")
+
+ for className, classMembers in toolClasses.items():
+ toolHeader += """class {0}: public SimTool
+ {{
+ public:
+ {0}();
+ virtual ~{0}();
+ virtual int Perform(Simulation * sim, Particle * cpart, int x, int y, float strength);
+ {1}
+ }};
+ """.format(className, string.join(classMembers, "\n"))
+
+ toolHeader += """std::vector<SimTool*> GetTools();
+ #endif
+ """
+
+ toolContent = """#include "ToolClasses.h"
+ std::vector<SimTool*> GetTools()
+ {
+ std::vector<SimTool*> tools;
+ """;
+
+ toolIDs = sorted(classDirectives, key=lambda directive: directive[3])
+ for d in toolIDs:
+ toolContent += """ tools.push_back(new %s());
+ """ % (d[1])
+
+ toolContent += """ return tools;
+ }
+ """;
+
+ f = open("generated/ToolClasses.h", "w")
+ f.write(toolHeader)
+ f.close()
+
+ f = open("generated/ToolClasses.cpp", "w")
+ f.write(toolContent)
+ f.close()
+
+generateElements()
+generateTools() \ No newline at end of file
diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp
index 453232f..c040566 100644
--- a/src/game/GameModel.cpp
+++ b/src/game/GameModel.cpp
@@ -92,6 +92,13 @@ GameModel::GameModel():
menuList[SC_WALL]->AddTool(tempTool);
//sim->wtypes[i]
}
+
+ //Build menu for simtools
+ for(int i = 0; i < sim->tools.size(); i++)
+ {
+ Tool * tempTool = new Tool(i, sim->tools[i]->Name, PIXR(sim->tools[i]->Colour), PIXG(sim->tools[i]->Colour), PIXB(sim->tools[i]->Colour));
+ menuList[SC_TOOL]->AddTool(tempTool);
+ }
//Add decoration tools to menu
menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendAdd, "ADD", 0, 0, 0));
diff --git a/src/game/Tool.h b/src/game/Tool.h
index 107c8cc..9f88188 100644
--- a/src/game/Tool.h
+++ b/src/game/Tool.h
@@ -28,9 +28,15 @@ public:
}
string GetName() { return toolName; }
virtual ~Tool() {}
- virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {}
- virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {}
- virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {}
+ virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {
+ sim->ToolBrush(position.X, position.Y, toolID, brush);
+ }
+ virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {
+ sim->ToolLine(position1.X, position1.Y, position2.X, position2.Y, toolID, brush);
+ }
+ virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {
+ sim->ToolBox(position1.X, position1.Y, position2.X, position2.Y, toolID, brush);
+ }
virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) {};
int colRed, colBlue, colGreen;
};
diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp
index 354c759..914df7e 100644
--- a/src/simulation/Simulation.cpp
+++ b/src/simulation/Simulation.cpp
@@ -608,6 +608,113 @@ void Simulation::ApplyDecorationLine(int x1, int y1, int x2, int y2, int rx, int
}
}
+int Simulation::Tool(int x, int y, int tool, float strength)
+{
+ if(tools[tool])
+ {
+ Particle * cpart = NULL;
+ int r;
+ if(r = pmap[y][x])
+ cpart = &(parts[r>>8]);
+ else if(r = photons[y][x])
+ cpart = &(parts[r>>8]);
+ return tools[tool]->Perform(this, cpart, x, y, strength);
+ }
+ return 0;
+}
+
+int Simulation::ToolBrush(int x, int y, int tool, Brush * cBrush)
+{
+ int rx, ry, j, i;
+ if(!cBrush)
+ return 0;
+ rx = cBrush->GetRadius().X;
+ ry = cBrush->GetRadius().Y;
+ unsigned char *bitmap = cBrush->GetBitmap();
+ for (j=-ry; j<=ry; j++)
+ for (i=-rx; i<=rx; i++)
+ if(bitmap[(j+ry)*((rx*2)+1)+(i+rx+1)])
+ {
+ if ( x+i<0 || y+j<0 || x+i>=XRES || y+j>=YRES)
+ continue;
+ Tool(x+i, y+j, tool, 1.0f);
+ }
+}
+
+void Simulation::ToolLine(int x1, int y1, int x2, int y2, int tool, Brush * cBrush)
+{
+ int cp=abs(y2-y1)>abs(x2-x1), x, y, dx, dy, sy, rx, ry;
+ float e, de;
+ rx = cBrush->GetRadius().X;
+ ry = cBrush->GetRadius().Y;
+ if (cp)
+ {
+ y = x1;
+ x1 = y1;
+ y1 = y;
+ y = x2;
+ x2 = y2;
+ y2 = y;
+ }
+ if (x1 > x2)
+ {
+ y = x1;
+ x1 = x2;
+ x2 = y;
+ y = y1;
+ y1 = y2;
+ y2 = y;
+ }
+ dx = x2 - x1;
+ dy = abs(y2 - y1);
+ e = 0.0f;
+ if (dx)
+ de = dy/(float)dx;
+ else
+ de = 0.0f;
+ y = y1;
+ sy = (y1<y2) ? 1 : -1;
+ for (x=x1; x<=x2; x++)
+ {
+ if (cp)
+ ToolBrush(y, x, tool, cBrush);
+ else
+ ToolBrush(x, y, tool, cBrush);
+ e += de;
+ if (e >= 0.5f)
+ {
+ y += sy;
+ if ((!(rx+ry)) && ((y1<y2) ? (y<=y2) : (y>=y2)))
+ {
+ if (cp)
+ ToolBrush(y, x, tool, cBrush);
+ else
+ ToolBrush(x, y, tool, cBrush);
+ }
+ e -= 1.0f;
+ }
+ }
+}
+void Simulation::ToolBox(int x1, int y1, int x2, int y2, int tool, Brush * cBrush)
+{
+ int i, j;
+ if (x1>x2)
+ {
+ i = x2;
+ x2 = x1;
+ x1 = i;
+ }
+ if (y1>y2)
+ {
+ j = y2;
+ y2 = y1;
+ y1 = j;
+ }
+ for (j=y1; j<=y2; j++)
+ for (i=x1; i<=x2; i++)
+ ToolBrush(i, j, tool, cBrush);
+}
+
//this creates particles from a brush, don't use if you want to create one particle
int Simulation::CreateParts(int x, int y, int rx, int ry, int c, int flags, Brush * cBrush)
{
@@ -3762,6 +3869,8 @@ Simulation::Simulation():
{
elements[i] = elementList[i];
}
+
+ tools = GetTools();
int golRulesCount;
int * golRulesT = LoadGOLRules(golRulesCount);
diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h
index 9ed763a..71f233d 100644
--- a/src/simulation/Simulation.h
+++ b/src/simulation/Simulation.h
@@ -12,11 +12,11 @@
#include "Renderer.h"
#include "Graphics.h"
#include "Elements.h"
+#include "Tools.h"
#include "Misc.h"
#include "game/Brush.h"
#include "Gravity.h"
#include "SimulationData.h"
-//#include "ElementFunctions.h"
#define CHANNELS ((int)(MAX_TEMP-73)/100+2)
@@ -140,6 +140,7 @@ public:
Air * air;
Element * elements;
+ vector<SimTool*> tools;
unsigned int * platent;
wall_type wtypes[UI_WALLCOUNT];
gol_menu gmenu[NGOL];
@@ -244,6 +245,11 @@ public:
void update_particles();
void rotate_area(int area_x, int area_y, int area_w, int area_h, int invert);
void clear_area(int area_x, int area_y, int area_w, int area_h);
+
+ int Tool(int x, int y, int tool, float strength);
+ int ToolBrush(int x, int y, int tool, Brush * cBrush);
+ void ToolLine(int x1, int y1, int x2, int y2, int tool, Brush * cBrush);
+ void ToolBox(int x1, int y1, int x2, int y2, int tool, Brush * cBrush);
void CreateBox(int x1, int y1, int x2, int y2, int c, int flags);
int FloodParts(int x, int y, int c, int cm, int bm, int flags);
diff --git a/src/simulation/SimulationData.h b/src/simulation/SimulationData.h
index 5e48e94..c24ce39 100644
--- a/src/simulation/SimulationData.h
+++ b/src/simulation/SimulationData.h
@@ -156,7 +156,10 @@ struct menu_section;
struct wall_type;
+class SimTool;
+
class Element;
+
std::vector<Element*> GetDefaultElements();
gol_menu * LoadGOLMenu(int & golMenuCount);
diff --git a/src/simulation/Tools.h b/src/simulation/Tools.h
new file mode 100644
index 0000000..525701e
--- /dev/null
+++ b/src/simulation/Tools.h
@@ -0,0 +1,7 @@
+#ifndef TOOLS_H_
+#define TOOLS_H_
+
+#include "ToolClasses.h"
+
+
+#endif
diff --git a/src/simulation/tools/Cool.cpp b/src/simulation/tools/Cool.cpp
new file mode 100644
index 0000000..12a6b28
--- /dev/null
+++ b/src/simulation/tools/Cool.cpp
@@ -0,0 +1,19 @@
+#include "simulation/Tools.h"
+//#TPT-Directive ToolClass Tool_Cool TOOL_COOL 1
+Tool_Cool::Tool_Cool()
+{
+ Identifier = "DEFAULT_TOOL_COOL";
+ Name = "COOL";
+ Colour = PIXPACK(0x00DDFF);
+ Description = "Cools particles";
+}
+
+int Tool_Cool::Perform(Simulation * sim, Particle * cpart, int x, int y, float strength)
+{
+ if(!cpart)
+ return 0;
+ cpart->temp -= strength;
+ return 1;
+}
+
+Tool_Cool::~Tool_Cool() {} \ No newline at end of file
diff --git a/src/simulation/tools/Heat.cpp b/src/simulation/tools/Heat.cpp
new file mode 100644
index 0000000..a2c500c
--- /dev/null
+++ b/src/simulation/tools/Heat.cpp
@@ -0,0 +1,19 @@
+#include "simulation/Tools.h"
+//#TPT-Directive ToolClass Tool_Heat TOOL_HEAT 0
+Tool_Heat::Tool_Heat()
+{
+ Identifier = "DEFAULT_TOOL_HEAT";
+ Name = "HEAT";
+ Colour = PIXPACK(0xFFDD00);
+ Description = "Heats particles";
+}
+
+int Tool_Heat::Perform(Simulation * sim, Particle * cpart, int x, int y, float strength)
+{
+ if(!cpart)
+ return 0;
+ cpart->temp += strength;
+ return 1;
+}
+
+Tool_Heat::~Tool_Heat() {} \ No newline at end of file
diff --git a/src/simulation/tools/SimTool.cpp b/src/simulation/tools/SimTool.cpp
new file mode 100644
index 0000000..d7015fa
--- /dev/null
+++ b/src/simulation/tools/SimTool.cpp
@@ -0,0 +1,10 @@
+#include "simulation/Element.h"
+#include "simulation/Tools.h"
+
+SimTool::SimTool():
+Identifier("DEFAULT_TOOL_INVALID"),
+Name(""),
+Colour(PIXPACK(0xFFFFFF)),
+Description("NULL Tool, does NOTHING")
+{
+} \ No newline at end of file
diff --git a/src/simulation/tools/SimTool.h b/src/simulation/tools/SimTool.h
new file mode 100644
index 0000000..0fbcba4
--- /dev/null
+++ b/src/simulation/tools/SimTool.h
@@ -0,0 +1,23 @@
+#ifndef SIMTOOL_H
+#define SIMTOOL_H
+
+#include "simulation/Simulation.h"
+#include "Renderer.h"
+#include "simulation/Elements.h"
+
+class Simulation;
+struct Particle;
+class SimTool
+{
+public:
+ char *Identifier;
+ char *Name;
+ pixel Colour;
+ char *Description;
+
+ SimTool();
+ virtual ~SimTool() {}
+ virtual int Perform(Simulation * sim, Particle * cpart, int x, int y, float strength) {}
+};
+
+#endif \ No newline at end of file