improve random spawning

This commit is contained in:
interfisch 2018-05-06 21:41:33 +02:00
parent d96d2e9b55
commit 7ece405b8b
3 changed files with 143 additions and 40 deletions

View File

@ -15,6 +15,10 @@ long lastPixelUpdate=0;
#define PIXELUPDATETIME 20
long lastRoutineUpdate=0;
#define ROUTINEUPDATETIME 20
long lastCheckspawn=0;
#define CHECKSPAWNDELAY 4000 //delay in ms to check random spawn
#define SPAWNCHANCE 30 //1 out of x times wagon will spawn
#define SPAWNCHANCEDOUBLE 5 //change of spawning a two trains simultaneously
long loopmillis=0;
@ -25,9 +29,15 @@ uint8_t heightraw[NUMPIXELS]; //uninterpolated values
std::vector <Wagon> wagon_arr;
uint8_t maxid=0;
bool configmode=true;
bool configmode=false;
int selectedpixel=-1; //-1 = none
uint8_t wagoncount=0;
//define config
//#define RESPAWNWAGON
#define MAXWAGONS 5 //maximum number of wagons
void setup() {
@ -40,27 +50,33 @@ void setup() {
resetHeightmap();
//Temporaer
/*height[0]=46;
height[28]=46;
height[60]=3;
height[63]=3;
height[70]=4;
height[100]=0;
height[122]=0;
height[137]=12;
height[138]=12;
height[152]=0;
height[183]=0;
height[200]=21;
height[209]=26;
height[225]=26;
height[233]=23;
height[240]=23;
height[250]=21;
height[255]=20;
height[274]=46;
height[NUMPIXELS-1]=46;*/
//fixed heightmap
heightraw[0]=254;
heightraw[43]=254;
heightraw[69]=200;
heightraw[95]=149;
heightraw[114]=132;
heightraw[137]=128;
heightraw[195]=128;
heightraw[226]=150;
heightraw[276]=139;
heightraw[303]=150;
heightraw[337]=131;
heightraw[354]=129;
heightraw[368]=131;
heightraw[405]=172;
heightraw[419]=147;
heightraw[435]=117;
heightraw[446]=105;
heightraw[458]=96;
heightraw[472]=77;
heightraw[503]=35;
heightraw[523]=0;
heightraw[554]=0;
heightraw[562]=8;
heightraw[577]=34;
heightraw[599]=67;
interpolateHeightValues();
@ -79,7 +95,7 @@ void setup() {
//previewHeightmap(2000);
spawnWagon();
//spawnWagon();
//spawnWagon();
}
@ -92,6 +108,19 @@ void resetHeightmap(){
heightraw[NUMPIXELS-1]=0;
}
void printHeightmapRaw() {
Serial.println();
for (int i=0;i<NUMPIXELS;i++){
if (heightraw[i]!=255){
Serial.print("heightraw[");
Serial.print(i);
Serial.print("]=");
Serial.print(heightraw[i]);
Serial.println(";");
}
}
}
void interpolateHeightValues(){
for (int i=0;i<NUMPIXELS;i++){ //copy heightraw to height
height[i]=heightraw[i];
@ -174,7 +203,8 @@ void previewHeightmap(int waittime){
void spawnWagon(){
//Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, 35, 6, 0.5,0); //spawn new wagon
// pos, wagonlength, startvel, startacc, trainmass, wagoncolor
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,20), random(0.2, 50)/10.0, 0 , random(5,100) , Wheel(random(0,255))); //spawn new wagon
//Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,20), random(0.2, 50)/10.0, 0 , random(5,100) , Wheel(random(0,256))); //spawn new wagon
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,40), random(1, 70)/10.0, 0 , random(50,200) , Wheel(random(0,256))); //spawn new wagon
wagon_arr.push_back(tmpr);
Serial.println("Spawned Wagon");
@ -223,10 +253,14 @@ void checkSerial(){
if (serialstring.length()>0) {
Serial.println("String:"+serialstring);
if (serialstring.equals("run")){
configmode=false;
}else if (serialstring.equals("debug")){
configmode=true;
}else if (serialstring.equals("print")){
printHeightmapRaw();
}else if (serialstring.equals("remove")){
removeAllWagons();
}else if (serialstring.equals("clear")){
@ -321,10 +355,10 @@ void loop_configmode(){
previewHeightmap(0);
if (selectedpixel>0){
if (selectedpixel>=0){
uint32_t c=strip.Color(255,255,255);
strip.setPixelColor(selectedpixel,c);
if (selectedpixel>1){
if (selectedpixel>=1){
uint32_t c=strip.Color(0,0,0);
strip.setPixelColor(selectedpixel-1,c);
}
@ -364,20 +398,45 @@ void loop_achterbahn(){
if (lastRoutineUpdate+ROUTINEUPDATETIME<loopmillis){
lastRoutineUpdate=loopmillis;
wagoncount=0;
for (std::vector<Wagon>::iterator it = wagon_arr.begin(); it != wagon_arr.end(); ++it) //all wagons
{
Wagon & w = *it;
w.updatePhysics(ROUTINEUPDATETIME);
if (!w.alive())
{
it = wagon_arr.erase(it); // After erasing, it is now pointing the next element.
--it;
spawnWagon(); //spawn new one
#ifdef RESPAWNWAGON
spawnWagon(); //spawn new one
#endif
}else{ //wagon is alive
wagoncount++;
}
}
}
//Check spawning
if (lastCheckspawn+CHECKSPAWNDELAY<loopmillis) {
lastCheckspawn=loopmillis;
Serial.print("Checking Spawning, wagons ");
Serial.println(wagoncount);
if (random(0,SPAWNCHANCE)==0 && wagoncount<MAXWAGONS) { //by chance, exclusive SPAWNCHANCE
spawnWagon();
if (random(0,SPAWNCHANCEDOUBLE)==0){
spawnWagon();
}
}else{
Serial.println("no spawn");
}
}
}

View File

@ -90,10 +90,10 @@ void setup() {
.setSize(50, 50);
slSpawnPos = cp5.addSlider("spawnpos")
.setRange(0,maxpixelvalue)
.setRange(0,numpixels-1)
.setValue(0)
.setPosition(220,80)
.setSize(maxpixelvalue,10);
.setSize(300,10);
slSpawnLength = cp5.addSlider("spawnlength")
.setRange(0,maxspawnlength)
@ -170,6 +170,13 @@ void Spawn() {
myPort.write(writeserial);
}
void SpawnRandom() {
//String writeserial="spawn\n"; //random
String writeserial="spawn\n";
myPort.write(writeserial);
}
void draw() {

View File

@ -1,6 +1,9 @@
#include "wagon.h"
#define SLOWVELOCITY 0.05
#define SLOWVELOCITY 0.1
#define EDGE_WALL
//#define EDGE_WRAP
Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,float pos, float trainlength,float startvel,float startacc, float wagonmass, uint32_t wagoncolor)
{
@ -106,11 +109,21 @@ void Wagon::updatePhysics(float updatedelayms)
}
if (_pos>=_numpixels){ //Wrap around edges
_pos-=_numpixels;
if (_pos>=_numpixels){
#ifdef EDGE_WRAP
_pos-=_numpixels; //Wrap around edges
#endif
#ifdef EDGE_WALL
_vel*=-1; //wall at edges
#endif
}
if (_pos<0){ //warp around edges
_pos=_numpixels+_pos;
if (_pos<0){
#ifdef EDGE_WRAP
_pos=_numpixels+_pos; //warp around edges
#endif
#ifdef EDGE_WALL
_vel*=-1;; //wall at edges
#endif
}
}
@ -119,11 +132,19 @@ float Wagon::getHeight(int p){
if (p<0){
p=0; //straight edges
//return (-p)*10; //edges as wall
#ifdef EDGE_WRAP
p=numpixels+p; //wrap edge
#endif
#ifdef EDGE_WALL
return _height[0]+p*-10; //edges as wall
#endif
}else if(p>=_numpixels){
p=_numpixels-1; //straight edges
return (p-_numpixels)*10; //edges as wall
#ifdef EDGE_WRAP
p=p-numpixels; //wrap edge
#endif
#ifdef EDGE_WALL
return _height[_numpixels-1]+(p-_numpixels)*10; //edges as wall
#endif
}
return _height[p];
}
@ -161,7 +182,23 @@ void Wagon::updateGraphics()
_g*=featherbrightness;
_b*=featherbrightness;
_strip->setPixelColor(i,_r,_g,_b);
uint32_t _pxcolor=_strip->getPixelColor(i); //get current color of that pixel
uint8_t _pxr = _pxcolor >> 16;
uint8_t _pxg = _pxcolor >> 8;
uint8_t _pxb = _pxcolor;
uint16_t _tmpr=_pxr+_r; //add colors
uint16_t _tmpg=_pxg+_g;
uint16_t _tmpb=_pxb+_b;
if (_tmpr>255){ //clamp
_tmpr=255;
}
if (_tmpg>255){
_tmpg=255;
}
if (_tmpb>255){
_tmpb=255;
}
_strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel
}
}