commit 96d8b12b04019cf642b2e955b40c90d975d27d79
parent 673770fd7429d2cc6beae419fb6d31df69a8f851
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Thu, 24 Nov 2011 00:44:24 +0100
Une grande partie du code de base des règles.
Diffstat:
16 files changed, 250 insertions(+), 37 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,9 +1,10 @@
CXX=g++
# -ansi -pedantic -Wconversion
CCWARN=-Wall -Wextra -Werror
-CFLAGS=-O3 $(CCWARN) -g3
+# -flto (nécessite GCC 4.5) -m32 ou -m64
+CFLAGS=-O3 -g3 -I. $(CCWARN)
-OBJECTS = main.o hash.o segment.o vertex.o io.o rules/rectangleroutes.o rules/route.o rules/carrefour.o
+OBJECTS = main.o hash.o segment.o vertex.o triangle.o io.o rules/chose.o rules/rectangleroutes.o rules/route.o rules/carrefour.o rules/batiment.o
EXECUTABLE = city
.PHONY: test
diff --git a/all_includes.hh b/all_includes.hh
@@ -0,0 +1,26 @@
+#ifndef _ALL_INCLUDES_HH_
+#define _ALL_INCLUDES_HH_
+
+#include <iostream>
+#include <math.h>
+
+#include "vertex.hh"
+#include "segment.hh"
+#include "triangle.hh"
+#include "directions.hh"
+#include "io.hh"
+#include "hash.hh"
+
+class Chose;
+// class Batiment;
+// class Carrefour;
+// class Route;
+// class RectangleRoutes;
+
+#include "rules/chose.hh"
+#include "rules/batiment.hh"
+#include "rules/carrefour.hh"
+#include "rules/route.hh"
+#include "rules/rectangleroutes.hh"
+
+#endif
diff --git a/bugs/open/2011-11-24-0045-todo b/bugs/open/2011-11-24-0045-todo
@@ -0,0 +1,2 @@
+* Route et Carrefour à mettre à jour pour qu'ils soient sous-classe de Chose.
+* Vertex en 3D
diff --git a/main.cpp b/main.cpp
@@ -1,12 +1,17 @@
-#include "vertex.hh"
-#include "hash.hh"
-#include "rules/rectangleroutes.hh"
+#include "all_includes.hh"
+
+// TODO : bâtiments.
+// TODO : split route en 2 triangles.
+// TODO : split bâtiment en faces, puis en triangles.
+// TODO : probabilités des différents types de bâtiments.
+// TODO : midpoint displacement sur les probabilités des différents types de bâtiments.
+// TODO : largeur des routes : ???
int main() {
// Générer une tile de base
Vertex ne(100, 100);
Vertex sw(0, 0);
- RectangleRoutes r(ne, sw, random());
+ RectangleRoutes r(ne, sw);
r.subdivide();
// tile.subdivide tant qu'on n'a pas le niveau de détail désiré.
return 0;
diff --git a/rules/batiment.cpp b/rules/batiment.cpp
@@ -0,0 +1,43 @@
+#include "all_includes.hh"
+
+Batiment::Batiment(Vertex ne, Vertex sw) : ne(ne), sw(sw) {
+ std::cout << this << std::endl;
+}
+
+int Batiment::width() { return this->ne.x - this->sw.x; }
+
+int Batiment::height() { return this->ne.y - this->sw.y; }
+
+void Batiment::subdivide() {
+ // TODO : rien ?
+}
+
+void Batiment::triangulation() {
+ // // abcd sont les quatre coins du bâtiment.
+ // int h = 6;
+ // Vertex ah = a + Vertex(0,0,h);
+ // Vertex bh = b + Vertex(0,0,h);
+ // Vertex ch = c + Vertex(0,0,h);
+ // Vertex dh = d + Vertex(0,0,h);
+ // Vertex toit = (ah + bh + ch + dh) / 4 + Vertex(0,0,h/5);
+
+ // // 4 Murs
+ // new Triangle(a,bh,ah); new Triangle(a,b,bh); // a-b-bh-ah
+ // new Triangle(b,dh,bh); new Triangle(b,d,dh); // b-d-dh-bh
+ // new Triangle(d,ch,dh); new Triangle(d,c,ch); // d-c-ch-dh
+ // new Triangle(c,ah,ch); new Triangle(c,a,ah); // c-a-ah-ch
+
+ // // 1 Toit
+ // new Triangle(ah,toit,bh);
+ // new Triangle(bh,toit,dh);
+ // new Triangle(dh,toit,ch);
+ // new Triangle(ch,toit,ah);
+}
+
+std::ostream& operator<<(std::ostream& os, const Batiment* r) {
+ return os << *r;
+}
+
+std::ostream& operator<<(std::ostream& os, const Batiment& r) {
+ return os << "Batiment " << r.ne << "-" << r.sw;
+}
diff --git a/rules/batiment.hh b/rules/batiment.hh
@@ -0,0 +1,23 @@
+#ifndef _RULES_BATIMENT_HH_
+#define _RULES_BATIMENT_HH_
+
+#include "all_includes.hh"
+
+// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
+class Batiment : public Chose {
+public:
+ Vertex ne;
+ Vertex sw;
+ int seed;
+public:
+ Batiment(Vertex ne, Vertex sw);
+ int width();
+ int height();
+ virtual void subdivide();
+ virtual void triangulation();
+};
+
+std::ostream& operator<<(std::ostream& os, const Batiment& r);
+std::ostream& operator<<(std::ostream& os, const Batiment* r);
+
+#endif
diff --git a/rules/chose.cpp b/rules/chose.cpp
@@ -0,0 +1,12 @@
+#include "all_includes.hh"
+
+Chose::Chose() : seed(initialSeed) {}
+
+std::ostream& operator<<(std::ostream& os, const Chose* r) {
+ return os << *r;
+}
+
+std::ostream& operator<<(std::ostream& os, const Chose& r) {
+ (void)r; // unused
+ return os << "Chose";
+}
diff --git a/rules/chose.hh b/rules/chose.hh
@@ -0,0 +1,28 @@
+#ifndef _RULES_CHOSE_HH_
+#define _RULES_CHOSE_HH_
+
+#include "all_includes.hh"
+
+// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
+class Chose {
+public:
+ static const unsigned int initialSeed = 42;
+ unsigned int seed;
+public:
+ Chose();
+ inline void addEntropy(unsigned int x1) { seed = hash2(seed, x1); }
+ inline void addEntropy(unsigned int x1, unsigned int x2) { addEntropy(x1); addEntropy(x2); }
+ inline void addEntropy(unsigned int x1, unsigned int x2, unsigned int x3) { addEntropy(x1, x2); addEntropy(x3); }
+ inline void addEntropy(unsigned int x1, unsigned int x2, unsigned int x3, unsigned int x4) { addEntropy(x1, x2); addEntropy(x3, x4); }
+ inline void addEntropy(Vertex v1) { addEntropy(v1.x, v1.y); }
+ inline void addEntropy(Vertex v1, Vertex v2) { addEntropy(v1); addEntropy(v2); }
+ inline void addEntropy(Vertex v1, Vertex v2, Vertex v3) { addEntropy(v1, v2); addEntropy(v3); }
+ inline void addEntropy(Vertex v1, Vertex v2, Vertex v3, Vertex v4) { addEntropy(v1, v2); addEntropy(v3, v4); }
+ virtual void subdivide() = 0;
+ virtual void triangulation() = 0;
+};
+
+std::ostream& operator<<(std::ostream& os, const Chose& r);
+std::ostream& operator<<(std::ostream& os, const Chose* r);
+
+#endif
diff --git a/rules/rectangleroutes.cpp b/rules/rectangleroutes.cpp
@@ -1,12 +1,8 @@
-#include "rectangleroutes.hh"
-#include "../vertex.hh"
-#include "../directions.hh"
-#include "../hash.hh"
+#include "all_includes.hh"
-#include "carrefour.hh"
-#include "route.hh"
-
-RectangleRoutes::RectangleRoutes(Vertex ne, Vertex sw, int seed) : ne(ne), sw(sw), seed(seed) {
+RectangleRoutes::RectangleRoutes(Vertex ne, Vertex sw) : ne(ne), sw(sw) {
+ addEntropy(ne);
+ addEntropy(sw);
std::cout << this << std::endl;
}
@@ -30,12 +26,31 @@ void RectangleRoutes::subdivide() {
Route rs(roadEndS.add(+1,0), roadEndS.add(-1,0), split.add(-1,-1), split.add(+1,-1));
Route rw(roadEndW.add(0,-1), roadEndW.add(0,+1), split.add(-1,+1), split.add(-1,-1));
// Sous-quartiers
- RectangleRoutes rrne(this->ne, re.corners[NW], newSeed(this->seed, 2));
- RectangleRoutes rrse(re.corners[SE], rs.corners[SE], newSeed(this->seed, 3));
- RectangleRoutes rrsw(rs.corners[NW], this->sw, newSeed(this->seed, 4));
- RectangleRoutes rrnw(Vertex(this->sw.x, this->ne.y), rn.corners[SW], newSeed(this->seed, 5));
- // Entrées/sorties par mètre carré en fonction du type de terrain ?
- // rrnw.io[N].in += …;
+ Chose* rrne = sub(this->ne, re.corners[NW]);
+ Chose* rrse = sub(re.corners[SE], rs.corners[SE]);
+ Chose* rrsw = sub(rs.corners[NW], this->sw);
+ Chose* rrnw = sub(Vertex(this->sw.x, this->ne.y), rn.corners[SW]);
+ // TODO : stocker ces objets quelque part.
+ (void)rrne;
+ (void)rrse;
+ (void)rrsw;
+ (void)rrnw;
+}
+
+void RectangleRoutes::triangulation() {
+ Vertex nw(this->sw.x, this->ne.y);
+ Vertex se(this->ne.x, this->sw.y);
+ new Triangle(this->sw, nw, this->ne);
+ new Triangle(this->sw, se, this->ne);
+}
+
+Chose* RectangleRoutes::sub(Vertex ne, Vertex sw) {
+ Segment rect = Segment(ne,sw);
+ if (rect.width() < 10 || rect.height() < 10) {
+ return new Batiment(ne, sw);
+ } else {
+ return new RectangleRoutes(ne, sw);
+ }
}
std::ostream& operator<<(std::ostream& os, const RectangleRoutes* r) {
diff --git a/rules/rectangleroutes.hh b/rules/rectangleroutes.hh
@@ -1,26 +1,25 @@
#ifndef _RULES_RECTANGLEROUTES_HH_
#define _RULES_RECTANGLEROUTES_HH_
-#include <iostream>
-#include "../vertex.hh"
-#include "../directions.hh"
-#include "../io.hh"
+#include "all_includes.hh"
// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
-class RectangleRoutes {
+class RectangleRoutes : Chose {
public:
Vertex ne;
Vertex sw;
- IO io [4];
- int seed;
public:
- RectangleRoutes(Vertex ne, Vertex sw, int seed);
+ RectangleRoutes(Vertex ne, Vertex sw);
int width();
int height();
- void subdivide();
+ virtual void subdivide();
+ virtual void triangulation();
+private:
+ Chose* sub(Vertex ne, Vertex sw);
+public:
+ friend std::ostream& operator<<(std::ostream& os, const RectangleRoutes& r);
+ friend std::ostream& operator<<(std::ostream& os, const RectangleRoutes* r);
};
-std::ostream& operator<<(std::ostream& os, const RectangleRoutes& r);
-std::ostream& operator<<(std::ostream& os, const RectangleRoutes* r);
#endif
diff --git a/segment.cpp b/segment.cpp
@@ -1,6 +1,4 @@
-#include "segment.hh"
-#include "vertex.hh"
-#include <math.h>
+#include "all_includes.hh"
Segment::Segment(Vertex u, Vertex v): u(u), v(v) {}
@@ -9,3 +7,11 @@ int Segment::length() {
int y = u.y - v.y;
return sqrt(x*x + y*y);
}
+
+int Segment::width() {
+ return u.x - v.x;
+}
+
+int Segment::height() {
+ return u.y - v.y;
+}
diff --git a/segment.hh b/segment.hh
@@ -1,7 +1,7 @@
#ifndef _SEGMENT_HH_
#define _SEGMENT_HH_
-#include "vertex.hh"
+#include "all_includes.hh"
class Segment {
public:
@@ -10,6 +10,8 @@ public:
public:
Segment(Vertex u, Vertex v);
int length();
+ int width();
+ int height();
};
#endif
diff --git a/triangle.cpp b/triangle.cpp
@@ -0,0 +1,9 @@
+#include "all_includes.hh"
+
+Triangle::Triangle(Vertex a, Vertex b, Vertex c): a(a), b(b), c(c) {
+ std::cout << this << std::endl;
+}
+
+std::ostream& operator<<(std::ostream& os, const Triangle& t) {
+ return os << "Triangle " << t.a << "--" << t.b << "--" << t.c << "-- cycle";
+}
diff --git a/triangle.hh b/triangle.hh
@@ -0,0 +1,17 @@
+#ifndef _TRIANGLE_HH_
+#define _TRIANGLE_HH_
+
+#include "all_includes.hh"
+
+class Triangle {
+public:
+ Vertex a;
+ Vertex b;
+ Vertex c;
+public:
+ Triangle(Vertex a, Vertex b, Vertex c);
+public:
+ friend std::ostream& operator<<(std::ostream& os, const Triangle& t);
+};
+
+#endif
diff --git a/vertex.cpp b/vertex.cpp
@@ -14,3 +14,23 @@ Vertex Vertex::add(int dx, int dy) {
std::ostream& operator<<(std::ostream& os, const Vertex& v) {
return os << "(" << v.x << "," << v.y << ")";
}
+
+Vertex operator+(const Vertex& u, const Vertex& v) {
+ return Vertex(u.x + v.x, u.y + v.y);
+}
+
+Vertex operator-(const Vertex& u, const Vertex& v) {
+ return Vertex(u.x + v.x, u.y + v.y);
+}
+
+Vertex operator-(const Vertex& v) {
+ return Vertex(-v.x, -v.y);
+}
+
+Vertex operator*(const Vertex& v, const int n) {
+ return Vertex(v.x * n, v.y * n);
+}
+
+Vertex operator/(const Vertex& v, const int n) {
+ return Vertex(v.x / n, v.y / n);
+}
diff --git a/vertex.hh b/vertex.hh
@@ -12,8 +12,13 @@ public:
Vertex();
Vertex(int x, int y);
Vertex add(int dx, int dy);
+public:
+ friend std::ostream& operator<<(std::ostream& os, const Vertex& v);
+ friend Vertex operator+(const Vertex& u, const Vertex& v);
+ friend Vertex operator-(const Vertex& u, const Vertex& v);
+ friend Vertex operator-(const Vertex& v);
+ friend Vertex operator*(const Vertex& v, const int n);
+ friend Vertex operator/(const Vertex& v, const int n);
};
-std::ostream& operator<<(std::ostream& os, const Vertex& v);
-
#endif