added timer for update loop

This commit is contained in:
Lendemor 2016-05-23 23:37:45 +02:00
parent 3f03b4bf63
commit f8e5667b44
12 changed files with 75 additions and 47 deletions

View File

@ -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;
}
}
}

View File

@ -3,6 +3,7 @@
#include <cstring> // 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<width;i++){
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);
}
for(j=0;j<height;j++)
map[i][j].type = generate(i, j);
}
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;
}
@ -179,7 +176,7 @@ int in_radius(int x,int y,t_biome e){
return d < e.radius ? d : -1;
}
Pixel::Type generate(int x, int y){
PixelType generate(int x, int y){
int i, j;
int proba[5];
int sum, dist, ratio, val, seuil=0;
@ -211,17 +208,17 @@ Pixel::Type generate(int x, int y){
for (i=0;i<5;i++){
seuil += proba[i];
if(val < seuil)
return Pixel::Type(i+1);
return PixelType(i+1);
}
}else{
val = rand()%100;
if (val <95){
return Pixel::GRASS;
return GRASS;
}else{
return Pixel::TREE;
return TREE;
}
}
return Pixel::GRASS;
return GRASS;
}
int distance_manhattan(int x1,int y1, int x2, int y2){

View File

@ -46,7 +46,7 @@ struct Action
struct Info
{
virtual bool getSuccess() const = 0;
virtual int getInventory() const = 0;
virtual PixelType getInventory() const = 0;
virtual const Com& getCom() const = 0;
virtual PixelType getNear(Dir d) const = 0;
virtual int getInfo(Dir d) const = 0;

View File

@ -8,7 +8,7 @@ Dude::Dude(const Coord &_pos, Map *_map, int &_team) :
m_team(_team),
m_dead(false),
m_success(true),
m_inventory(-1)
m_inventory(EMPTY)
{
std::memset(&m_memory, 0, DUDE_MEMORY_SIZE);
}

View File

@ -11,7 +11,7 @@ private:
int m_team;
bool m_dead;
bool m_success;
int m_inventory;
PixelType m_inventory;
char m_memory[DUDE_MEMORY_SIZE];
Com m_comData;
@ -20,14 +20,14 @@ public:
char* getMemory();
int getTeam(){ return m_team; }
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; }
const Coord& getPos() { return m_pos; }
void move(Dir d);
void receiveComData(const Com &comData);
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 PixelType getNear(Dir d) const;
virtual int getInfo(Dir d) const;

View File

@ -3,6 +3,9 @@
#include "ui_mainwindow.h"
#include "simulationdialog.h"
#include "simulation.h"
#include <iostream>
#include <chrono>
#include <QTimer>
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();
}

View File

@ -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

12
src/pixeltype.cpp Normal file
View 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);
}

View File

@ -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

View File

@ -1,9 +1,11 @@
#include "simulation.h"
#include <algorithm>
#include <iostream>
#include "map.h"
#include "dude.h"
#include "mapscene.h"
#include "pixeltype.h"
Simulation::Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors):
p_map(_map)
@ -21,25 +23,27 @@ Simulation::Simulation(MapScene *_map, std::vector<BehaviorFunction> &_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; i<m_teams.size(); ++i){
Team &t = m_teams[i];
if (t.updateSpawn())
{
Dir randDir = Dir(rand()%4);
Coord spawnPos = p_map->team(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);

View File

@ -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;
}

View File

@ -24,7 +24,7 @@ public:
bool updateSpawn();
void addFood() {m_foodQuantity++; }
void destroySpawn();
Action* update(Dude);
Action* update(Dude*);
};
#endif // TEAM_H