From 35f41d01c97f79d52c0813cebace57bc4ed8ca64 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 20 Mar 2010 16:18:11 +0000 Subject: [PATCH] Add smoothing tool --- images/icons/smooth.png | Bin 0 -> 602 bytes include/IMap.hpp | 3 ++ include/Maths.hpp | 7 ++++ layouts/editor.xml | 1 + maps/uphill/uphill.bin | Bin 4364 -> 4364 bytes maps/uphill/uphill.xml | 82 ++++++++++++++++++++++++++++------------ src/Editor.cpp | 7 +++- src/Engine.cpp | 15 ++++++-- src/Map.cpp | 67 ++++++++++++++++++++++++++++++++ 9 files changed, 153 insertions(+), 29 deletions(-) create mode 100644 images/icons/smooth.png diff --git a/images/icons/smooth.png b/images/icons/smooth.png new file mode 100644 index 0000000000000000000000000000000000000000..89b5535aa521b7eb755064a7a091c7da1a9405e0 GIT binary patch literal 602 zcmV-g0;TPx#24YJ`L;(K){{a7>y{D4^000SaNLh0L01FcU01FcV0GgZ_00007bV*G`2igM^ z4Felz9HgZLd06Ne?flPpWXTHYNIa#FFVh;P+1I%~NkuZRjvG5uH1Z4?I zL=l28##O2=o-I6LJ-LA^HHy`{H6{*_umBB!58z!~7-A>1N!7*Aa~Ifs*kg`bpbcrv zN;`wlO&vigh?~)vqvEzAG!_iU5HgeTRCvOz7RUX2QmA;%DX0gc6odW&cgC<@h#sZ; zM-%@96Y&Biq7d)mfDeEMSZNE!g2BoKc=&u&@r}eJ06-+M!Wsq;f$)42HlhjFRy6TcpX(398Q5bEL3D oZNqKEd%5j>to3Ww0%$hC7yEUqG&MMk2LJ#707*qoM6N<$g8sDxNB{r; literal 0 HcmV?d00001 diff --git a/include/IMap.hpp b/include/IMap.hpp index 89db4aa..8bf7f9b 100644 --- a/include/IMap.hpp +++ b/include/IMap.hpp @@ -102,6 +102,9 @@ public: // Make all tiles in the area the same height virtual void levelArea(Point aStartPos, Point aFinishPos) = 0; + // Smooth the gradient along a strip + virtual void smoothArea(Point start, Point finish) = 0; + // Create a new station covering this area or extend an existing station virtual IStationPtr extendStation(Point aStartPos, Point aFinishPos) = 0; diff --git a/include/Maths.hpp b/include/Maths.hpp index 90a0a6b..e04d8cf 100644 --- a/include/Maths.hpp +++ b/include/Maths.hpp @@ -178,6 +178,13 @@ struct Point { return Point(x + rhs.x, y + rhs.y); } + Point& operator+=(const Point& rhs) + { + x += rhs.x; + y += rhs.y; + return *this; + } + bool operator<(const Point& rhs) const { return x < rhs.x || y < rhs.y; diff --git a/layouts/editor.xml b/layouts/editor.xml index 42aa9bd..bcc69c3 100644 --- a/layouts/editor.xml +++ b/layouts/editor.xml @@ -15,6 +15,7 @@ + diff --git a/maps/uphill/uphill.bin b/maps/uphill/uphill.bin index 241d1a6b5eeb14876bf69ed0b1264cae453d1b82..26e62a856d397d82752dc149e33f16012af5c3cf 100644 GIT binary patch literal 4364 zcmc&%ZD^HM9DnW&wpbBHQ&?{a^c&+`s?i;FEFIC1Iky>w&LI_zU z`tAP1JUzb8?{8P9jZq^eF)tdRhht)O~Akv2@-f&(;{XUyH z!~~e^-GDd2xc~&Y9rUwvQ@sxk_?|@~!#O<-_IZ1Tke`4%Q_Rgdnq4nf3w~?Sn)CI7 zig_MAB7V0y7>@r`8nXK~oZEu){gMQb#{jfUuE9v;{R$yy$Ry}`T9NOUP|SNrEvl@x zaGvJscjH_QF|U;OQ)>BVs^RZ+6FslB)q$F@sPapTz)O>fjpGit(24!Jy}Nct#cGw< zS*D7v7OSC#xLUB`XSt)|D_L~4TQ1npBsV?xwoE-*uHxPa^|D?{%f;)oZF02iLtuKrU~1 zoKM44u|O`i>Pr^b_lx>*+PCpDT2-`hFCyD?z0$k zoF($D{$i(%cdG4I?iZ5SdcWQGwI83z@I%L&#dQ`@Yq_}~FB6%w;PPRvG!*=_jR40t zSsUhd{qXZa%xt~myGqNrcSfbtWz?+ipjQ2d+T+LgTs}Y#i%$C19Hnl*kCEj55yIIG zua&Rwc(2@|6o4`F){P@mw|xJC1X}bl%X`|SxJ~kzKBzwOms7boPT!f>THqmZotB9o z=#(j+k%on}D-y#_ks|2Q98ZY#(7ssg6s6t z1P_MH7HZNb=+E>J*UVl!hZ;40_M&Jf%sCD1zZ5C}<0`Ru93AZSF{=ajlGCu;uc3e1 zpP8Tm{=AGP!1^BQL@hubf*yOrdmwF3>5Ipx0tmqk&Og84b`fxY*uy>Dt@=UgqCX6GIPV%vpr(G#5r=mF?QiTQqY-pcSB z%)K^;VTS+jZ5k8C=2Yg#TkB2O6WwDzGA|)9{`FqFT$Ubez}~?(9bkOs(1iHRez>e3 zkpF!-nU&-jlXyN{lLKjMISA)REx5K;YB^YI>o&v&>Su?W);9IXVOdoMK%eVU2hzEZ zx(#rmZYtAxnsy%t+KS)3kh8n&z}kiRZ4Rs#;FNNJ{eyoJA8n67ALCPUSXOJXcZwJH zYBu)GK!VQ{93-CMt_DA}?V}I-_r-gVx5$BciyFl`b!k87IBW-F!8OK8$p^V_$NO8? z4}XEfI_!s^_QkY*z&_QT4`X5C8xG diff --git a/maps/uphill/uphill.xml b/maps/uphill/uphill.xml index 5275488..e43047e 100644 --- a/maps/uphill/uphill.xml +++ b/maps/uphill/uphill.xml @@ -2,7 +2,7 @@ No Name - + uphill.bin @@ -32,7 +32,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -96,7 +96,7 @@ - + @@ -120,7 +120,7 @@ - + @@ -168,7 +168,7 @@ - + @@ -188,7 +188,7 @@ - + @@ -224,7 +224,7 @@ - + @@ -236,11 +236,11 @@ - + - + @@ -248,7 +248,7 @@ - + @@ -272,7 +272,7 @@ - + @@ -284,11 +284,11 @@ - + - + @@ -304,7 +304,7 @@ - + @@ -320,7 +320,7 @@ - + @@ -336,7 +336,7 @@ - + @@ -352,7 +352,7 @@ - + @@ -368,7 +368,7 @@ - + @@ -380,11 +380,11 @@ - + - + @@ -396,7 +396,11 @@ - + + + + + @@ -404,7 +408,11 @@ - + + + + + @@ -415,16 +423,40 @@ + + + + + + + + - + - + + + + + + + + + + + + + + + + + diff --git a/src/Editor.cpp b/src/Editor.cpp index 4440578..cba0431 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -53,7 +53,7 @@ public: enum Tool { TRACK_TOOL, RAISE_TOOL, LOWER_TOOL, DELETE_TOOL, LEVEL_TOOL, START_TOOL, STATION_TOOL, BUILDING_TOOL, - TREE_TOOL + TREE_TOOL, SMOOTH_TOOL }; void setTool(Tool aTool) { myTool = aTool; } @@ -132,6 +132,8 @@ void Editor::buildGUI() bind(&Editor::setTool, this, BUILDING_TOOL)); layout->get("/tool_wnd/tools/tree").connect(gui::Widget::SIG_CLICK, bind(&Editor::setTool, this, TREE_TOOL)); + layout->get("/tool_wnd/tools/smooth").connect(gui::Widget::SIG_CLICK, + bind(&Editor::setTool, this, SMOOTH_TOOL)); layout->get("/lower/action_wnd/save").connect(gui::Widget::SIG_CLICK, bind(&Editor::save, this)); @@ -607,6 +609,9 @@ void Editor::onMouseRelease(IPickBufferPtr pickBuffer, int x, int y, case TREE_TOOL: plantTrees(); break; + case SMOOTH_TOOL: + map->smoothArea(dragBegin, dragEnd); + break; } amDragging = false; diff --git a/src/Engine.cpp b/src/Engine.cpp index ce13a4e..2d64809 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -205,13 +205,22 @@ void Engine::update(int delta, float gradient) const double P = tractiveEffort(); const double Q = resistance(); const double B = isBrakeOn ? brakeForce() : 0.0; - const double G = gravity(gradient); + const double G = 0;//gravity(gradient); +#if 0 + if (gradient * P > 0.001f) + debug() << "D"; + else if (gradient * P < -0.001f) + debug() << "U"; + else + debug() << "F"; +#endif + // The applied tractive effort is controlled by the throttle const double netP = P * static_cast(myThrottle) / 10.0; const double deltaSeconds = delta / 1000.0f; - const double a = ((netP - Q - B - G) / myMass) * deltaSeconds; + const double a = ((netP - Q - B + G) / myMass) * deltaSeconds; // mySpeed = max(mySpeed + a, 0.0); mySpeed += a; @@ -223,7 +232,7 @@ void Engine::update(int delta, float gradient) else haveStopped = false; -#if 1 +#if 0 debug() << "P=" << netP << ", Q=" << Q << ", B=" << B << ", G=" << G diff --git a/src/Map.cpp b/src/Map.cpp index 21d5ea3..cb960a4 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -104,6 +104,7 @@ public: void lowerArea(const Point& aStartPos, const Point& aFinishPos); void levelArea(Point aStartPos, Point aFinishPos); + void smoothArea(Point start, Point finish); void save(); @@ -1110,6 +1111,72 @@ void Map::levelArea(Point aStartPos, Point aFinishPos) } } +void Map::smoothArea(Point start, Point finish) +{ + const int xmin = min(start.x, finish.x); + const int xmax = max(start.x, finish.x); + + const int ymin = min(start.y, finish.y); + const int ymax = max(start.y, finish.y); + + Point step; + if (xmin == xmax) + step = makePoint(0, 1); + else if (ymin == ymax) + step = makePoint(1, 0); + else { + warn() << "Can only smooth strips"; + return; + } + + const Point absStart = makePoint(xmin, ymin); + const Point absFinish = makePoint(xmax, ymax); + + const float heightStart = heightAt(absStart); + const float heightFinish = heightAt(absFinish); + + setTileHeight(absStart.x, absStart.y, heightStart); + setTileHeight(absFinish.x, absFinish.y, heightFinish); + + const float drop = + (heightStart - heightFinish) / (xmax + ymax - xmin - ymin); + debug() << "drop=" << drop; + + int i = 0; + for (Point it = absStart; it != absFinish; i++, it += step) { + const bool trackAffected = raiseWillCoverTrack(it.x, it.y); + + int indexes[4]; + tileVertices(it.x, it.y, indexes); + + int targets[2]; + if (xmin == xmax) { + targets[0] = 0; + targets[1] = 1; + } + else { + targets[0] = 1; + targets[1] = 2; + } + + for (int j = 0; j < 2; j++) { + const float newHeight = heightStart - (i * drop); + + if (trackAffected + && abs(heightMap[indexes[targets[j]]].pos.y - newHeight) > 0.01f) { + warn() << "Cannot change terrain under track"; + return; + } + else + heightMap[indexes[targets[j]]].pos.y = newHeight; + } + + fixNormals(it.x, it.y); + dirtyTile(it.x, it.y); + } + +} + void Map::raiseArea(const Point& aStartPos, const Point& aFinishPos) { -- 2.39.2