forked from 0ad/0ad
Matei
467b14e928
- Made objects snap to terrain (the game does this automatically for entities but it was a problem with actors) - Small enhancements to the RMS API - New version of cantabrian_highlands This was SVN commit r2688.
85 lines
1.8 KiB
C++
85 lines
1.8 KiB
C++
#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) {
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool SimpleGroup::place(Map* m, int player, Constraint* constr) {
|
|
vector<Object*> ret;
|
|
for(int i=0; i<elements.size(); i++) {
|
|
if(!elements[i]->place(x, y, m, player, avoidSelf, constr, ret)) {
|
|
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);
|
|
}
|
|
}
|
|
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)
|
|
{
|
|
}
|
|
|
|
SimpleGroup::~SimpleGroup(void) {
|
|
for(int i=0; i<elements.size(); i++) {
|
|
delete elements[i];
|
|
}
|
|
} |