commit 973cf6cb773411eafd74c7f7eecf8bb34103ea68
parent ceeadcac13d2c190f1e630cee6b1b1cba1caf27b
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Sat, 5 Nov 2011 18:58:56 +0100
Début de l'implémentation de l'algo utilisant les motifs.
Diffstat:
| M | Makefile | | | 33 | ++++++--------------------------- |
| D | hash.c | | | 18 | ------------------ |
| A | hash.cpp | | | 26 | ++++++++++++++++++++++++++ |
| M | hash.h | | | 2 | ++ |
| D | rules.c | | | 12 | ------------ |
| A | rules.cpp | | | 61 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | rules.h | | | 24 | ++++++++++++++++++++++++ |
| M | rules.md | | | 21 | --------------------- |
8 files changed, 119 insertions(+), 78 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,40 +1,19 @@
-CC=gcc
+CXX=g++
# -ansi -pedantic -Wconversion
CCWARN=-Wall -Wextra -Werror
CFLAGS=-O3 $(CCWARN) -g3
.PHONY: all
-all: display roads rules
-
-.PHONY: test
-test: display
- ./display
-
-.PHONY: test
-test-simple-terrain: simple-terrain
- ./simple-terrain | display
-
-.PHONY: testroads
-testroads: roads
- ./roads | display
+all: rules
.PHONY: clean
clean:
- rm simple-terrain display roads rules *.o
-
-simple-terrain: simple-terrain.c
- $(CC) $< -o $@
-
-display: display.o roam.o square.o hash.o
- $(CC) -lGLEW -lSDL -lGLU $^ -o $@
-
-roads: roads.o
- $(CC) -lm $^ -o $@
+ rm rules *.o .*.d
rules: rules.o hash.o
- $(CC) -lm $^ -o $@
+ $(CXX) -lm $^ -o $@
-include .*.d
-%.o: %.c Makefile
- $(CC) -MMD -MF .$(@:.o=.d) -c $< $(CFLAGS) -o $@
+%.o: %.cpp Makefile
+ $(CXX) -MMD -MF .$(@:.o=.d) -c $< $(CFLAGS) -o $@
diff --git a/hash.c b/hash.c
@@ -1,18 +0,0 @@
-// Ce hash donne des bons résultats sur tous les bits de l'entier
-// généré (pas d'artefacts, répartition homogène des 0 et des 1).
-unsigned int hash2(unsigned int a, unsigned int b) {
- unsigned int h = 1;
- int i;
- for (i = 0; i < 32; i+=8) {
- a = a*h + 1;
- b = b*h + 1;
- // marche aussi avec 65521.
- h = (h << 6) + (h << 16) - h + ((a >> i) & 0xff); // h * 65599 + ieme octet de a
- h = (h << 6) + (h << 16) - h + ((b >> i) & 0xff); // h * 65599 + ieme octet de b
- }
- return h;
-}
-
-unsigned int hash3(unsigned int seed, int x, int y) {
- return hash2(seed,hash2(x, y));
-}
diff --git a/hash.cpp b/hash.cpp
@@ -0,0 +1,26 @@
+// Ce hash donne des bons résultats sur tous les bits de l'entier
+// généré (pas d'artefacts, répartition homogène des 0 et des 1).
+unsigned int hash2(unsigned int a, unsigned int b) {
+ unsigned int h = 1;
+ int i;
+ for (i = 0; i < 32; i+=8) {
+ a = a*h + 1;
+ b = b*h + 1;
+ // marche aussi avec 65521.
+ h = (h << 6) + (h << 16) - h + ((a >> i) & 0xff); // h * 65599 + ieme octet de a
+ h = (h << 6) + (h << 16) - h + ((b >> i) & 0xff); // h * 65599 + ieme octet de b
+ }
+ return h;
+}
+
+unsigned int hash3(unsigned int seed, int x, int y) {
+ return hash2(seed,hash2(x, y));
+}
+
+int randomInRange(int seed, int n, int a, int b) {
+ return (hash2(seed, n) % (b - a)) + a;
+}
+
+int newSeed(int seed, int n) {
+ return hash2(seed, n);
+}
diff --git a/hash.h b/hash.h
@@ -1,2 +1,4 @@
unsigned int hash2(unsigned int a, unsigned int b);
unsigned int hash3(unsigned int seed, int x, int y);
+int randomInRange(int seed, int n, int a, int b);
+int newSeed(int seed, int n);
diff --git a/rules.c b/rules.c
@@ -1,12 +0,0 @@
-#include <stdio.h>
-#include "hash.h"
-
-void regle1() {
- // carré(x1,y1,x2,y2) → carré()*4 avec une croix de routes au milieu.
-}
-
-int main() {
- // Générer une tile de base
- // tile.subdivide tant qu'on n'a pas le niveau de détail désiré.
- return 0;
-}
diff --git a/rules.cpp b/rules.cpp
@@ -0,0 +1,61 @@
+#include <iostream>
+#include "rules.h"
+#include "hash.h"
+
+// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
+class RectangleRoutes {
+public:
+ Vertex ne;
+ Vertex so;
+ IO io [4];
+ int seed;
+public:
+ RectangleRoutes(Vertex ne, Vertex so, int seed) : ne(ne), so(so), seed(seed) {}
+ void display() { };
+ int width() { return this->ne.x - this->so.x; }
+ int height() { return this->ne.y - this->so.y; }
+ void subdivide() {
+ Vertex split = {
+ randomInRange(this->seed, 0, this->so.x + this->width()*1/4, this->so.x + this->width()*3/4),
+ randomInRange(this->seed, 1, this->so.y + this->height()*1/4, this->so.y + this->height()*3/4)
+ };
+ split = split;
+ std::cout << this << std::endl;
+ RectangleRoutes rr(ne, so, 42);
+ // std::cout << rr << std::endl;
+ }
+};
+
+std::ostream& operator<<(std::ostream& os, const Vertex& v) {
+ return os << "(" << v.x << "," << v.y << ")";
+}
+
+std::ostream& operator<<(std::ostream& os, const RectangleRoutes& r) {
+ int a = r.ne.x;
+ a = a;
+ return os << "42!";//<< r.ne << "-" << r.so;
+}
+
+
+ /* Carrefour(split + (1,1), split - (1,1)) */
+ /* // routes au NESW du carrefour */
+ /* Route((r.ne.x, split.y) + (0,1)), split + (1,1)) */
+ /* Route((split.x, r.se.y) + (1,0)), split + (-1,1)) */
+ /* Route((r.so.x, split.y) + (0,-1)), split + (-1,-1)) */
+ /* Route((split.x, r.no.y) + (-1,0)), split + (1,-1)) */
+ /* // subrectangles */
+ /* RectangleRoutes(split + (1,1), r.ne, newSeed(r.seed, 2)); */
+ /* RectangleRoutes(split + (1,-1), r.se, newSeed(r.seed, 3)); */
+ /* RectangleRoutes(split + (-1,-1), r.so, newSeed(r.seed, 4)); */
+ /* RectangleRoutes(split + (-1,1), r.no, newSeed(r.seed, 5)); */
+
+int main() {
+ // Générer une tile de base
+ Vertex ne = {100,0};
+ Vertex so = {0,100};
+ RectangleRoutes r(ne, so, 42);
+ r.subdivide();
+ std::cout << r << std::endl;
+ // tile.subdivide tant qu'on n'a pas le niveau de détail désiré.
+ return 0;
+}
diff --git a/rules.h b/rules.h
@@ -0,0 +1,24 @@
+typedef struct Vertex {
+ int x;
+ int y;
+ //int z;
+} Vertex;
+
+typedef enum Cardinaux {
+ N = 0,
+ E = 1,
+ S = 2,
+ O = 3
+} Cardinaux;
+
+typedef enum Diagonales {
+ NE = 0,
+ SE = 1,
+ SO = 2,
+ NO = 3
+} Diagonales;
+
+typedef struct IO {
+ int in;
+ int out;
+} IO;
diff --git a/rules.md b/rules.md
@@ -9,28 +9,7 @@ rectangle suffisemment petit, commercial → magasin
// TODO : faire pour des angles entre 70° et 110°.
-// RectangleRoutes(Vertex coins[4]) est un quadrilatère de routes avec des angles aux coins égaux à 90°.
// TODO : distinguer à la création les RectangleRoutes avec (all sides length > 10) et les autres cas.
-struct RectangleRoutes {
- Vertex ne;
- Vertex se;
- Vertex so;
- Vertex no;
- // TODO : prendre en compte les entrées/sorties.
- IO n;
- IO e;
- IO s;
- IO o;
- int seed;
-}
-
-int randomInRange(int seed, int n, int a, int b) {
- return (hash(seed, n) % (b - a)) + a;
-}
-
-int newSeed(int seed, int n) {
- return hash(seed, n);
-}
RectangleRoutes r (all sides length > 10) {
Vertex split = { .x = randomInRange(r.seed, 0, r.no.x+5, r.ne.x-5), .y = randomInRange(r.seed, 1, r.no.x+5, r.ne.x-5) };