mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-03 10:23:38 +02:00
map::agent_startpos(): Introduce, abstract out from main loop
This commit is contained in:
parent
ea4f18b642
commit
ec3897b29b
3 changed files with 16 additions and 6 deletions
6
main.cc
6
main.cc
|
@ -66,11 +66,7 @@ next_agent:
|
||||||
int cfd = accept(lfd, NULL, NULL);
|
int cfd = accept(lfd, NULL, NULL);
|
||||||
if (cfd >= 0) {
|
if (cfd >= 0) {
|
||||||
class connection *conn = new class connection(cfd);
|
class connection *conn = new class connection(cfd);
|
||||||
class tile *agentpos;
|
agents.push_back(new class agent(agents.size(), map.agent_startpos(), conn));
|
||||||
do {
|
|
||||||
agentpos = &map.tile_at(random() % map.w, random() % map.h);
|
|
||||||
} while (agentpos->agent);
|
|
||||||
agents.push_back(new class agent(agents.size(), *agentpos, conn));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Run on_tick everywhere. */
|
/* Run on_tick everywhere. */
|
||||||
|
|
14
map.cc
14
map.cc
|
@ -1,5 +1,6 @@
|
||||||
#include <iostream>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "agent.h"
|
#include "agent.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
|
@ -47,6 +48,17 @@ tile::tile_in_dir(int dir_x, int dir_y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class tile &
|
||||||
|
map::agent_startpos(void)
|
||||||
|
{
|
||||||
|
/* Find a random starting tile that is not occupied yet. */
|
||||||
|
class tile *tile;
|
||||||
|
do {
|
||||||
|
tile = &tile_at(random() % w, random() % h);
|
||||||
|
} while (tile->agent);
|
||||||
|
return *tile;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
map::on_tick(void)
|
map::on_tick(void)
|
||||||
{
|
{
|
||||||
|
|
2
map.h
2
map.h
|
@ -55,6 +55,8 @@ public:
|
||||||
return *tiles[y * w + x];
|
return *tiles[y * w + x];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class tile &agent_startpos(void);
|
||||||
|
|
||||||
void on_tick(void);
|
void on_tick(void);
|
||||||
|
|
||||||
void print_map(void);
|
void print_map(void);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue