1
0
forked from 0ad/0ad
0ad/source/tools/rmgen/simplegroup.cpp

85 lines
1.8 KiB
C++
Raw Normal View History

2005-07-30 23:07:02 +02:00
#include "stdafx.h"
#include "simplegroup.h"
#include "point.h"
#include "random.h"
#include "map.h"
using namespace std;
SimpleGroup::Element::Element(){
}
SimpleGroup::Element::Element(const std::string& t, int c, float d):
type(t), count(c), distance(d)
{
}
SimpleGroup::Element::~Element() {
}
bool SimpleGroup::Element::place(int cx, int cy, Map* m, int player, bool avoidSelf,
Constraint* constr, vector<Object*>& ret) {
2005-07-30 23:07:02 +02:00
int failCount = 0;
for(int i=0; i<count; i++) {
while(true) {
float ang = RandFloat()*2*PI;
float x = cx + 0.5f + distance*cos(ang);
float y = cy + 0.5f + distance*sin(ang);
if(x<0 || y<0 || x>m->size || y>m->size) {
goto bad;
2005-07-30 23:07:02 +02:00
}
if(avoidSelf) {
for(int i=0; i<ret.size(); i++) {
float dx = x - ret[i]->x;
float dy = y - ret[i]->y;
if(dx*dx + dy*dy < 1) {
goto bad;
}
2005-07-30 23:07:02 +02:00
}
}
if(!constr->allows(m, (int)x, (int)y)) {
goto bad;
}
// if we got here, we're good
ret.push_back(new Object(type, player, x, y, RandFloat()*2*PI));
break;
bad: failCount++;
if(failCount > 20) {
return false;
}
2005-07-30 23:07:02 +02:00
}
}
return true;
}
bool SimpleGroup::place(Map* m, int player, Constraint* constr) {
vector<Object*> ret;
2005-07-30 23:07:02 +02:00
for(int i=0; i<elements.size(); i++) {
if(!elements[i]->place(x, y, m, player, avoidSelf, constr, ret)) {
2005-07-30 23:07:02 +02:00
return false;
}
}
for(int i=0; i<ret.size(); i++) {
m->addObject(ret[i]);
if(tileClass != 0) {
tileClass->add((int) ret[i]->x, (int) ret[i]->y);
}
}
2005-07-30 23:07:02 +02:00
return true;
}
SimpleGroup::SimpleGroup(vector<SimpleGroup::Element*>& e, TileClass* tc, bool as, int _x, int _y):
elements(e), x(_x), y(_y), tileClass(tc), avoidSelf(as)
2005-07-30 23:07:02 +02:00
{
}
SimpleGroup::~SimpleGroup(void) {
for(int i=0; i<elements.size(); i++) {
delete elements[i];
}
}