diff --git a/generators/anselme.cpp b/generators/anselme.cpp index faa5567..a79fd4f 100644 --- a/generators/anselme.cpp +++ b/generators/anselme.cpp @@ -32,18 +32,18 @@ extern "C" void generate(Map *mapPtr) break; } if(d == 0){ - map[i][j].type = Pixel::SPAWN; + map[i][j].type = SPAWN; map[i][j].data.nbRes = k; }else{ int l = (d-20)+(rand()%40); if(l > r+15) // mountain - map[i][j].type = rand()%8 ? Pixel::ROCK : Pixel::IRON_ORE; + map[i][j].type = rand()%8 ? ROCK : IRON_ORE; else if(l < r-15) // plains - map[i][j].type = rand()%15 ? Pixel::GRASS : Pixel::BERRIES; + map[i][j].type = rand()%15 ? GRASS : BERRIES; else // forest { l = rand()%10; - map[i][j].type = l > 5 ? Pixel::TREE : l ? Pixel::GRASS : Pixel::BERRIES; + map[i][j].type = l > 5 ? TREE : l ? GRASS : BERRIES; } } } diff --git a/generators/biome.cpp b/generators/biome.cpp index d6f3f17..5f3417a 100644 --- a/generators/biome.cpp +++ b/generators/biome.cpp @@ -3,6 +3,7 @@ #include // memset // g++ -shared biome.cpp -o biome.dll -I../src +// g++ -shared biome.cpp -o biome.so -I../src -fPIC #define MAX_POWER 10 #define MAX_EPICENTER_BY_TYPE 7 @@ -47,7 +48,7 @@ void create_biome_random(Map &map, int type); int check_nears_biomes(int x, int y); int check_nears_spawn(Map &map, int x, int y); int in_radius(int x,int y,t_biome e); -Pixel::Type generate(int x, int y); +PixelType generate(int x, int y); void init_generator(); extern "C" void generate(Map *mapPtr) @@ -88,17 +89,13 @@ extern "C" void generate(Map *mapPtr) //génération de la carte for (i=0;i +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -25,8 +28,15 @@ void MainWindow::openSimuDialog() int ret = dialog->exec(); if(ret == QDialog::Accepted) { - Simulation *simu = dialog->getSimulation(); - ui->drawWidget->startSimulation(simu->getMap()); - // TODO set a timer to update the simulation + p_simu = dialog->getSimulation(); + ui->drawWidget->startSimulation(p_simu->getMap()); + QTimer *timer = new QTimer(this); + connect(timer,SIGNAL(timeout()),this, SLOT(updateSimu())); + timer->start(2000); } } + +void MainWindow::updateSimu() +{ + p_simu->update(); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index e9820b1..8275422 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -7,6 +7,8 @@ namespace Ui { class MainWindow; } +class Simulation; + class MainWindow : public QMainWindow { Q_OBJECT @@ -17,9 +19,11 @@ public: private: Ui::MainWindow *ui; + Simulation* p_simu; private slots: void openSimuDialog(); + void updateSimu(); }; #endif // MAINWINDOW_H diff --git a/src/pixeltype.cpp b/src/pixeltype.cpp new file mode 100644 index 0000000..ce5ab4d --- /dev/null +++ b/src/pixeltype.cpp @@ -0,0 +1,12 @@ + +#include "pixeltype.h" + +bool isWalkable(PixelType target){ + return target != WALL && target != ROCK && target != BEDROCK + && target != IRON_ORE && target != TREE && target != LIBRARY; +} + +bool isDestroyable(PixelType target){ + return (target == DUDE || target == SPAWN || target == WALL + || target == LIBRARY || target == ROAD); +} diff --git a/src/pixeltype.h b/src/pixeltype.h index e747610..604afa1 100644 --- a/src/pixeltype.h +++ b/src/pixeltype.h @@ -5,7 +5,12 @@ enum PixelType { BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature FOOD, WOOD, STONE, IRON, SWORD, // resources DUDE, DEAD_DUDE, // humans - SPAWN, WALL, ROAD, MARK, LIBRARY // buildings + SPAWN, WALL, ROAD, MARK, LIBRARY, // buildings + EMPTY = -1 }; +//we might have to place these function elsewhere ? +bool isWalkable(PixelType target); +bool isDestroyable(PixelType target); + #endif // PIXELTYPE_H diff --git a/src/simulation.cpp b/src/simulation.cpp index 1a85062..b3e51cd 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -1,9 +1,11 @@ #include "simulation.h" #include +#include #include "map.h" #include "dude.h" #include "mapscene.h" +#include "pixeltype.h" Simulation::Simulation(MapScene *_map, std::vector &_behaviors): p_map(_map) @@ -21,25 +23,27 @@ Simulation::Simulation(MapScene *_map, std::vector &_behaviors void Simulation::update() { std::random_shuffle(m_dudes.begin(),m_dudes.end()); - for (auto dude : m_dudes){ - auto action = m_teams[dude->getTeam()].update(*dude); //get action for this dude from behavior function in team - handleAction(action,dude); + //BUG : find segfault origin + if(m_dudes.empty()){ + for (Dude* dude : m_dudes){ + auto action = m_teams[dude->getTeam()].update(dude); //get action for this dude from behavior function in team + handleAction(action,dude); + } } - // for each team, spawn dude if condition met for(int i=0; iteam(i) + Coord(randDir); + //TODO: check if target pixel is walkable and has no dude + Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4)); m_dudes.push_back(new Dude(spawnPos,p_map,i)); } } +// std::cerr << "Update..." << std::endl; } -// TODO perform action (cf old_code/main.c -> void handleAction(t_dude* dude) ) - +// TODO finish conversion of handleAction() void Simulation::handleAction(Action *action, Dude *dude){ Coord targetPos(dude->getPos() + Coord(action->dir)); Pixel target = p_map->getPixel(targetPos); @@ -47,17 +51,11 @@ void Simulation::handleAction(Action *action, Dude *dude){ dude->setSuccess(false); switch(action->type){ case Action::MOVE: - if (target.type != WALL && target.type != ROCK - && target.type != BEDROCK && target.type != IRON_ORE - && target.type != TREE && target.type != LIBRARY) + if (isWalkable(target.type)) NULL; // TODO: move action break; case Action::ATTACK: - if (target.type == DUDE - || target.type == SPAWN - || target.type == WALL - || target.type == LIBRARY - || target.type == ROAD) + if (isDestroyable(target.type)) { dude->setSuccess(true); if (target.type == DUDE) @@ -88,10 +86,10 @@ void Simulation::handleAction(Action *action, Dude *dude){ target.data.nbRes = 1; }else target.data.nbRes++; - dude->setInventory(-1); + dude->setInventory(EMPTY); // TODO: change pixel to new type }else if(target.type == SPAWN && dude->getInventory() == FOOD){ - dude->setInventory(-1); + dude->setInventory(EMPTY); m_teams[target.data.nbRes].addFood(); }else{ // printf("put failed : trying to put %d in %d\n", dude->inventory, target.type); diff --git a/src/team.cpp b/src/team.cpp index 5797e9e..88f4f42 100644 --- a/src/team.cpp +++ b/src/team.cpp @@ -29,9 +29,11 @@ void Team::destroySpawn(){ } -Action* Team::update(Dude dude) +Action* Team::update(Dude *dude) { Action* action; - m_behavior(action, dude.getMemory(), &dude); + m_behavior(action, + dude->getMemory(), + (Info*) dude); return action; } diff --git a/src/team.h b/src/team.h index 2c23376..e548c76 100644 --- a/src/team.h +++ b/src/team.h @@ -24,7 +24,7 @@ public: bool updateSpawn(); void addFood() {m_foodQuantity++; } void destroySpawn(); - Action* update(Dude); + Action* update(Dude*); }; #endif // TEAM_H