From c5798c745675e4866a44228ddf161258e85d39a7 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 12 May 2012 18:11:20 +0100 Subject: [PATCH] Tools implemented in a similar way to elements --- generator.py | 207 +++++++++++++++++++++---------- src/game/GameModel.cpp | 7 ++ src/game/Tool.h | 12 +- src/simulation/Simulation.cpp | 109 ++++++++++++++++ src/simulation/Simulation.h | 8 +- src/simulation/SimulationData.h | 3 + src/simulation/Tools.h | 7 ++ src/simulation/tools/Cool.cpp | 19 +++ src/simulation/tools/Heat.cpp | 19 +++ src/simulation/tools/SimTool.cpp | 10 ++ src/simulation/tools/SimTool.h | 23 ++++ 11 files changed, 356 insertions(+), 68 deletions(-) create mode 100644 src/simulation/Tools.h create mode 100644 src/simulation/tools/Cool.cpp create mode 100644 src/simulation/tools/Heat.cpp create mode 100644 src/simulation/tools/SimTool.cpp create mode 100644 src/simulation/tools/SimTool.h diff --git a/generator.py b/generator.py index dc975949b..55737d504 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 -#include "simulation/Element.h" -#include "simulation/elements/Element.h" -""" + elementHeader = """#ifndef ELEMENTCLASSES_H + #define ELEMENTCLASSES_H + #include + #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() + 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 GetElements(); + #endif + """ + + elementContent = """#include "ElementClasses.h" + std::vector GetElements() + { + std::vector 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() - directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)' - matcher = re.compile(directiveMatcher) - directiveMatches = matcher.findall(fileData) + f = open("generated/ElementClasses.cpp", "w") + f.write(elementContent) + f.close() - 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) +def generateTools(): + toolClasses = dict() -for d in directives: - if d[0] == "ElementHeader": - elementClasses[d[1]].append(string.join(d[2:], " ")+";") + toolHeader = """#ifndef TOOLCLASSES_H + #define TOOLCLASSES_H + #include + #include "simulation/Tools.h" + #include "simulation/tools/SimTool.h" + """ + + directives = [] + + 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 GetTools(); + #endif + """ + + toolContent = """#include "ToolClasses.h" + std::vector GetTools() + { + std::vector 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() -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 GetElements(); -#endif -""" - -elementContent = """#include "ElementClasses.h" -std::vector GetElements() -{ - std::vector 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 +generateElements() +generateTools() \ No newline at end of file diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 453232fe8..c04056646 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 107c8cc56..9f881889f 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 354c7598d..914df7e7e 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= 0.5f) + { + y += sy; + if ((!(rx+ry)) && ((y1=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 9ed763a44..71f233d3d 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 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 5e48e9499..c24ce398a 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 GetDefaultElements(); gol_menu * LoadGOLMenu(int & golMenuCount); diff --git a/src/simulation/Tools.h b/src/simulation/Tools.h new file mode 100644 index 000000000..525701e67 --- /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 000000000..12a6b2800 --- /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 000000000..a2c500c9c --- /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 000000000..d7015fabf --- /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 000000000..0fbcba46a --- /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