From a8bcacc95045102e67f2feabbdddf79535837554 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Thu, 19 Aug 2021 18:46:51 +0200 Subject: forgot to make repo until now --- game/ground.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 game/ground.cpp (limited to 'game/ground.cpp') diff --git a/game/ground.cpp b/game/ground.cpp new file mode 100644 index 0000000..c0ba2ca --- /dev/null +++ b/game/ground.cpp @@ -0,0 +1,82 @@ +#include "ground.h" +#include "gst.h" + +#include +#include + +Ground::Ground (int sx, int sy) { + sizex = sx; sizey = sy; + tiles = new int[sx*sy]; + for (int i=0; i Ground::star (int pos) { + std::vector fs; + int x = pos % sizex, y = pos / sizex; + if (x-1 >= 0) { fs.push_back(at(x-1, y)); } + if (x+1 < sizex) { fs.push_back(at(x+1, y)); } + if (y-1 >= 0) { fs.push_back(at(x, y-1)); } + if (y+1 < sizey) { fs.push_back(at(x, y+1)); } + return fs; +} + +class step { public: + step(int pos, int m) : pos(pos), m(m) {}; + bool operator==(step oth) { return pos==oth.pos && m==oth.m; } + bool operator==(int p) { return pos==p; } + int pos, m; +}; + +std::vector Ground::move_area (Gst &gst, Entity ent) { + std::vector moves; + std::vector visited { at(ent.x, ent.y) }; + std::vector frontier { step { at(ent.x, ent.y), ent.info.move } }; + + int iter=0; + for (; iter<10000; iter++) { + + if (frontier.size() == 0) break; + step maxf {-1, -1}; + for (step t : frontier) { + if (t.m > maxf.m) { + maxf.pos = t.pos; + maxf.m = t.m; + } + } + frontier.erase(std::remove(frontier.begin(), frontier.end(), maxf), + frontier.end()); + auto forward_star = star(maxf.pos); + for (int t : forward_star) { + if (!(std::find(visited.begin(), visited.end(), t) != visited.end()) + && !(std::find(frontier.begin(), frontier.end(), t) != frontier.end())) { + int walkedm = maxf.m - gst.tiles[gst.ground.tiles[t]].move_cost; + bool obstructed = false; + for (Entity &e : gst.entities) { + if (e.owner != ent.owner && at(e.x, e.y) == t) { + obstructed = true; + break; + } + } + if (walkedm >= 0 && !obstructed) { + frontier.emplace_back(t, walkedm); + moves.push_back(t); + } + } + } + visited.push_back(maxf.pos); + } + + std::cout << "iters: " << iter; + + return moves; +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf