added timer for update loop
This commit is contained in:
parent
3f03b4bf63
commit
f8e5667b44
@ -32,18 +32,18 @@ extern "C" void generate(Map *mapPtr)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(d == 0){
|
if(d == 0){
|
||||||
map[i][j].type = Pixel::SPAWN;
|
map[i][j].type = SPAWN;
|
||||||
map[i][j].data.nbRes = k;
|
map[i][j].data.nbRes = k;
|
||||||
}else{
|
}else{
|
||||||
int l = (d-20)+(rand()%40);
|
int l = (d-20)+(rand()%40);
|
||||||
if(l > r+15) // mountain
|
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
|
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
|
else // forest
|
||||||
{
|
{
|
||||||
l = rand()%10;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <cstring> // memset
|
#include <cstring> // memset
|
||||||
|
|
||||||
// g++ -shared biome.cpp -o biome.dll -I../src
|
// 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_POWER 10
|
||||||
#define MAX_EPICENTER_BY_TYPE 7
|
#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_biomes(int x, int y);
|
||||||
int check_nears_spawn(Map &map, int x, int y);
|
int check_nears_spawn(Map &map, int x, int y);
|
||||||
int in_radius(int x,int y,t_biome e);
|
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();
|
void init_generator();
|
||||||
|
|
||||||
extern "C" void generate(Map *mapPtr)
|
extern "C" void generate(Map *mapPtr)
|
||||||
@ -88,17 +89,13 @@ extern "C" void generate(Map *mapPtr)
|
|||||||
//génération de la carte
|
//génération de la carte
|
||||||
|
|
||||||
for (i=0;i<width;i++){
|
for (i=0;i<width;i++){
|
||||||
for(j=0;j<height;j++){
|
for(j=0;j<height;j++)
|
||||||
if (i == 0 || j == 0 || i == width-1 || j == height-1)
|
|
||||||
map[i][j].type = Pixel::BEDROCK;
|
|
||||||
else
|
|
||||||
map[i][j].type = generate(i, j);
|
map[i][j].type = generate(i, j);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for(i=0; i<n; ++i)
|
for(i=0; i<n; ++i)
|
||||||
{
|
{
|
||||||
map[map.team(i)].type = Pixel::SPAWN;
|
map[map.team(i)].type = SPAWN;
|
||||||
map[map.team(i)].data.nbRes = 0;
|
map[map.team(i)].data.nbRes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +176,7 @@ int in_radius(int x,int y,t_biome e){
|
|||||||
return d < e.radius ? d : -1;
|
return d < e.radius ? d : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Pixel::Type generate(int x, int y){
|
PixelType generate(int x, int y){
|
||||||
int i, j;
|
int i, j;
|
||||||
int proba[5];
|
int proba[5];
|
||||||
int sum, dist, ratio, val, seuil=0;
|
int sum, dist, ratio, val, seuil=0;
|
||||||
@ -211,17 +208,17 @@ Pixel::Type generate(int x, int y){
|
|||||||
for (i=0;i<5;i++){
|
for (i=0;i<5;i++){
|
||||||
seuil += proba[i];
|
seuil += proba[i];
|
||||||
if(val < seuil)
|
if(val < seuil)
|
||||||
return Pixel::Type(i+1);
|
return PixelType(i+1);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
val = rand()%100;
|
val = rand()%100;
|
||||||
if (val <95){
|
if (val <95){
|
||||||
return Pixel::GRASS;
|
return GRASS;
|
||||||
}else{
|
}else{
|
||||||
return Pixel::TREE;
|
return TREE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Pixel::GRASS;
|
return GRASS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int distance_manhattan(int x1,int y1, int x2, int y2){
|
int distance_manhattan(int x1,int y1, int x2, int y2){
|
||||||
|
@ -46,7 +46,7 @@ struct Action
|
|||||||
struct Info
|
struct Info
|
||||||
{
|
{
|
||||||
virtual bool getSuccess() const = 0;
|
virtual bool getSuccess() const = 0;
|
||||||
virtual int getInventory() const = 0;
|
virtual PixelType getInventory() const = 0;
|
||||||
virtual const Com& getCom() const = 0;
|
virtual const Com& getCom() const = 0;
|
||||||
virtual PixelType getNear(Dir d) const = 0;
|
virtual PixelType getNear(Dir d) const = 0;
|
||||||
virtual int getInfo(Dir d) const = 0;
|
virtual int getInfo(Dir d) const = 0;
|
||||||
|
@ -8,7 +8,7 @@ Dude::Dude(const Coord &_pos, Map *_map, int &_team) :
|
|||||||
m_team(_team),
|
m_team(_team),
|
||||||
m_dead(false),
|
m_dead(false),
|
||||||
m_success(true),
|
m_success(true),
|
||||||
m_inventory(-1)
|
m_inventory(EMPTY)
|
||||||
{
|
{
|
||||||
std::memset(&m_memory, 0, DUDE_MEMORY_SIZE);
|
std::memset(&m_memory, 0, DUDE_MEMORY_SIZE);
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ private:
|
|||||||
int m_team;
|
int m_team;
|
||||||
bool m_dead;
|
bool m_dead;
|
||||||
bool m_success;
|
bool m_success;
|
||||||
int m_inventory;
|
PixelType m_inventory;
|
||||||
char m_memory[DUDE_MEMORY_SIZE];
|
char m_memory[DUDE_MEMORY_SIZE];
|
||||||
Com m_comData;
|
Com m_comData;
|
||||||
|
|
||||||
@ -20,14 +20,14 @@ public:
|
|||||||
char* getMemory();
|
char* getMemory();
|
||||||
int getTeam(){ return m_team; }
|
int getTeam(){ return m_team; }
|
||||||
void setSuccess(bool success) { m_success = success; }
|
void setSuccess(bool success) { m_success = success; }
|
||||||
void setInventory(int item) { m_inventory = item; }
|
void setInventory(PixelType item) { m_inventory = item; }
|
||||||
void trespass() { m_dead = true; }
|
void trespass() { m_dead = true; }
|
||||||
const Coord& getPos() { return m_pos; }
|
const Coord& getPos() { return m_pos; }
|
||||||
void move(Dir d);
|
void move(Dir d);
|
||||||
void receiveComData(const Com &comData);
|
void receiveComData(const Com &comData);
|
||||||
|
|
||||||
virtual bool getSuccess() const { return m_success; }
|
virtual bool getSuccess() const { return m_success; }
|
||||||
virtual int getInventory() const { return m_inventory; }
|
virtual PixelType getInventory() const { return m_inventory; }
|
||||||
virtual const Com& getCom() const { return m_comData; }
|
virtual const Com& getCom() const { return m_comData; }
|
||||||
virtual PixelType getNear(Dir d) const;
|
virtual PixelType getNear(Dir d) const;
|
||||||
virtual int getInfo(Dir d) const;
|
virtual int getInfo(Dir d) const;
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
#include "simulationdialog.h"
|
#include "simulationdialog.h"
|
||||||
#include "simulation.h"
|
#include "simulation.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent) :
|
MainWindow::MainWindow(QWidget *parent) :
|
||||||
QMainWindow(parent),
|
QMainWindow(parent),
|
||||||
@ -25,8 +28,15 @@ void MainWindow::openSimuDialog()
|
|||||||
int ret = dialog->exec();
|
int ret = dialog->exec();
|
||||||
if(ret == QDialog::Accepted)
|
if(ret == QDialog::Accepted)
|
||||||
{
|
{
|
||||||
Simulation *simu = dialog->getSimulation();
|
p_simu = dialog->getSimulation();
|
||||||
ui->drawWidget->startSimulation(simu->getMap());
|
ui->drawWidget->startSimulation(p_simu->getMap());
|
||||||
// TODO set a timer to update the simulation
|
QTimer *timer = new QTimer(this);
|
||||||
|
connect(timer,SIGNAL(timeout()),this, SLOT(updateSimu()));
|
||||||
|
timer->start(2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::updateSimu()
|
||||||
|
{
|
||||||
|
p_simu->update();
|
||||||
|
}
|
||||||
|
@ -7,6 +7,8 @@ namespace Ui {
|
|||||||
class MainWindow;
|
class MainWindow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Simulation;
|
||||||
|
|
||||||
class MainWindow : public QMainWindow
|
class MainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -17,9 +19,11 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
|
Simulation* p_simu;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void openSimuDialog();
|
void openSimuDialog();
|
||||||
|
void updateSimu();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
12
src/pixeltype.cpp
Normal file
12
src/pixeltype.cpp
Normal file
@ -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);
|
||||||
|
}
|
@ -5,7 +5,12 @@ enum PixelType {
|
|||||||
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
||||||
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
||||||
DUDE, DEAD_DUDE, // humans
|
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
|
#endif // PIXELTYPE_H
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#include "simulation.h"
|
#include "simulation.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "dude.h"
|
#include "dude.h"
|
||||||
#include "mapscene.h"
|
#include "mapscene.h"
|
||||||
|
#include "pixeltype.h"
|
||||||
|
|
||||||
Simulation::Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors):
|
Simulation::Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors):
|
||||||
p_map(_map)
|
p_map(_map)
|
||||||
@ -21,25 +23,27 @@ Simulation::Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors
|
|||||||
void Simulation::update()
|
void Simulation::update()
|
||||||
{
|
{
|
||||||
std::random_shuffle(m_dudes.begin(),m_dudes.end());
|
std::random_shuffle(m_dudes.begin(),m_dudes.end());
|
||||||
for (auto dude : m_dudes){
|
//BUG : find segfault origin
|
||||||
auto action = m_teams[dude->getTeam()].update(*dude); //get action for this dude from behavior function in team
|
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);
|
handleAction(action,dude);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// for each team, spawn dude if condition met
|
// for each team, spawn dude if condition met
|
||||||
for(int i=0; i<m_teams.size(); ++i){
|
for(int i=0; i<m_teams.size(); ++i){
|
||||||
Team &t = m_teams[i];
|
Team &t = m_teams[i];
|
||||||
if (t.updateSpawn())
|
if (t.updateSpawn())
|
||||||
{
|
{
|
||||||
Dir randDir = Dir(rand()%4);
|
//TODO: check if target pixel is walkable and has no dude
|
||||||
Coord spawnPos = p_map->team(i) + Coord(randDir);
|
Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4));
|
||||||
m_dudes.push_back(new Dude(spawnPos,p_map,i));
|
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){
|
void Simulation::handleAction(Action *action, Dude *dude){
|
||||||
Coord targetPos(dude->getPos() + Coord(action->dir));
|
Coord targetPos(dude->getPos() + Coord(action->dir));
|
||||||
Pixel target = p_map->getPixel(targetPos);
|
Pixel target = p_map->getPixel(targetPos);
|
||||||
@ -47,17 +51,11 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
|||||||
dude->setSuccess(false);
|
dude->setSuccess(false);
|
||||||
switch(action->type){
|
switch(action->type){
|
||||||
case Action::MOVE:
|
case Action::MOVE:
|
||||||
if (target.type != WALL && target.type != ROCK
|
if (isWalkable(target.type))
|
||||||
&& target.type != BEDROCK && target.type != IRON_ORE
|
|
||||||
&& target.type != TREE && target.type != LIBRARY)
|
|
||||||
NULL; // TODO: move action
|
NULL; // TODO: move action
|
||||||
break;
|
break;
|
||||||
case Action::ATTACK:
|
case Action::ATTACK:
|
||||||
if (target.type == DUDE
|
if (isDestroyable(target.type))
|
||||||
|| target.type == SPAWN
|
|
||||||
|| target.type == WALL
|
|
||||||
|| target.type == LIBRARY
|
|
||||||
|| target.type == ROAD)
|
|
||||||
{
|
{
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
if (target.type == DUDE)
|
if (target.type == DUDE)
|
||||||
@ -88,10 +86,10 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
|||||||
target.data.nbRes = 1;
|
target.data.nbRes = 1;
|
||||||
}else
|
}else
|
||||||
target.data.nbRes++;
|
target.data.nbRes++;
|
||||||
dude->setInventory(-1);
|
dude->setInventory(EMPTY);
|
||||||
// TODO: change pixel to new type
|
// TODO: change pixel to new type
|
||||||
}else if(target.type == SPAWN && dude->getInventory() == FOOD){
|
}else if(target.type == SPAWN && dude->getInventory() == FOOD){
|
||||||
dude->setInventory(-1);
|
dude->setInventory(EMPTY);
|
||||||
m_teams[target.data.nbRes].addFood();
|
m_teams[target.data.nbRes].addFood();
|
||||||
}else{
|
}else{
|
||||||
// printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
|
// printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
|
||||||
|
@ -29,9 +29,11 @@ void Team::destroySpawn(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Action* Team::update(Dude dude)
|
Action* Team::update(Dude *dude)
|
||||||
{
|
{
|
||||||
Action* action;
|
Action* action;
|
||||||
m_behavior(action, dude.getMemory(), &dude);
|
m_behavior(action,
|
||||||
|
dude->getMemory(),
|
||||||
|
(Info*) dude);
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ public:
|
|||||||
bool updateSpawn();
|
bool updateSpawn();
|
||||||
void addFood() {m_foodQuantity++; }
|
void addFood() {m_foodQuantity++; }
|
||||||
void destroySpawn();
|
void destroySpawn();
|
||||||
Action* update(Dude);
|
Action* update(Dude*);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TEAM_H
|
#endif // TEAM_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user