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 #define PIXELUPDATETIME 20
long lastRoutineUpdate=0; long lastRoutineUpdate=0;
#define ROUTINEUPDATETIME 20 #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; long loopmillis=0;
@ -25,9 +29,15 @@ uint8_t heightraw[NUMPIXELS]; //uninterpolated values
std::vector <Wagon> wagon_arr; std::vector <Wagon> wagon_arr;
uint8_t maxid=0; uint8_t maxid=0;
bool configmode=true; bool configmode=false;
int selectedpixel=-1; //-1 = none int selectedpixel=-1; //-1 = none
uint8_t wagoncount=0;
//define config
//#define RESPAWNWAGON
#define MAXWAGONS 5 //maximum number of wagons
void setup() { void setup() {
@ -40,27 +50,33 @@ void setup() {
resetHeightmap(); resetHeightmap();
//Temporaer //fixed heightmap
/*height[0]=46; heightraw[0]=254;
height[28]=46; heightraw[43]=254;
height[60]=3; heightraw[69]=200;
height[63]=3; heightraw[95]=149;
height[70]=4; heightraw[114]=132;
height[100]=0; heightraw[137]=128;
height[122]=0; heightraw[195]=128;
height[137]=12; heightraw[226]=150;
height[138]=12; heightraw[276]=139;
height[152]=0; heightraw[303]=150;
height[183]=0; heightraw[337]=131;
height[200]=21; heightraw[354]=129;
height[209]=26; heightraw[368]=131;
height[225]=26; heightraw[405]=172;
height[233]=23; heightraw[419]=147;
height[240]=23; heightraw[435]=117;
height[250]=21; heightraw[446]=105;
height[255]=20; heightraw[458]=96;
height[274]=46; heightraw[472]=77;
height[NUMPIXELS-1]=46;*/ heightraw[503]=35;
heightraw[523]=0;
heightraw[554]=0;
heightraw[562]=8;
heightraw[577]=34;
heightraw[599]=67;
interpolateHeightValues(); interpolateHeightValues();
@ -79,7 +95,7 @@ void setup() {
//previewHeightmap(2000); //previewHeightmap(2000);
spawnWagon(); //spawnWagon();
//spawnWagon(); //spawnWagon();
} }
@ -92,6 +108,19 @@ void resetHeightmap(){
heightraw[NUMPIXELS-1]=0; 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(){ void interpolateHeightValues(){
for (int i=0;i<NUMPIXELS;i++){ //copy heightraw to height for (int i=0;i<NUMPIXELS;i++){ //copy heightraw to height
height[i]=heightraw[i]; height[i]=heightraw[i];
@ -174,7 +203,8 @@ void previewHeightmap(int waittime){
void spawnWagon(){ void spawnWagon(){
//Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, 35, 6, 0.5,0); //spawn new wagon //Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, 35, 6, 0.5,0); //spawn new wagon
// pos, wagonlength, startvel, startacc, trainmass, wagoncolor // 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); wagon_arr.push_back(tmpr);
Serial.println("Spawned Wagon"); Serial.println("Spawned Wagon");
@ -223,10 +253,14 @@ void checkSerial(){
if (serialstring.length()>0) { if (serialstring.length()>0) {
Serial.println("String:"+serialstring); Serial.println("String:"+serialstring);
if (serialstring.equals("run")){ if (serialstring.equals("run")){
configmode=false; configmode=false;
}else if (serialstring.equals("debug")){ }else if (serialstring.equals("debug")){
configmode=true; configmode=true;
}else if (serialstring.equals("print")){
printHeightmapRaw();
}else if (serialstring.equals("remove")){ }else if (serialstring.equals("remove")){
removeAllWagons(); removeAllWagons();
}else if (serialstring.equals("clear")){ }else if (serialstring.equals("clear")){
@ -321,10 +355,10 @@ void loop_configmode(){
previewHeightmap(0); previewHeightmap(0);
if (selectedpixel>0){ if (selectedpixel>=0){
uint32_t c=strip.Color(255,255,255); uint32_t c=strip.Color(255,255,255);
strip.setPixelColor(selectedpixel,c); strip.setPixelColor(selectedpixel,c);
if (selectedpixel>1){ if (selectedpixel>=1){
uint32_t c=strip.Color(0,0,0); uint32_t c=strip.Color(0,0,0);
strip.setPixelColor(selectedpixel-1,c); strip.setPixelColor(selectedpixel-1,c);
} }
@ -364,19 +398,44 @@ void loop_achterbahn(){
if (lastRoutineUpdate+ROUTINEUPDATETIME<loopmillis){ if (lastRoutineUpdate+ROUTINEUPDATETIME<loopmillis){
lastRoutineUpdate=loopmillis; lastRoutineUpdate=loopmillis;
wagoncount=0;
for (std::vector<Wagon>::iterator it = wagon_arr.begin(); it != wagon_arr.end(); ++it) //all wagons for (std::vector<Wagon>::iterator it = wagon_arr.begin(); it != wagon_arr.end(); ++it) //all wagons
{ {
Wagon & w = *it; Wagon & w = *it;
w.updatePhysics(ROUTINEUPDATETIME); w.updatePhysics(ROUTINEUPDATETIME);
if (!w.alive()) if (!w.alive())
{ {
it = wagon_arr.erase(it); // After erasing, it is now pointing the next element. it = wagon_arr.erase(it); // After erasing, it is now pointing the next element.
--it; --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); .setSize(50, 50);
slSpawnPos = cp5.addSlider("spawnpos") slSpawnPos = cp5.addSlider("spawnpos")
.setRange(0,maxpixelvalue) .setRange(0,numpixels-1)
.setValue(0) .setValue(0)
.setPosition(220,80) .setPosition(220,80)
.setSize(maxpixelvalue,10); .setSize(300,10);
slSpawnLength = cp5.addSlider("spawnlength") slSpawnLength = cp5.addSlider("spawnlength")
.setRange(0,maxspawnlength) .setRange(0,maxspawnlength)
@ -169,6 +169,13 @@ void Spawn() {
String writeserial="spawn="+spawnpos+","+spawnlength+","+spawnstartvel+","+spawnstartacc+","+spawnmass+","+spawncolor+"\n"; String writeserial="spawn="+spawnpos+","+spawnlength+","+spawnstartvel+","+spawnstartacc+","+spawnmass+","+spawncolor+"\n";
myPort.write(writeserial); myPort.write(writeserial);
} }
void SpawnRandom() {
//String writeserial="spawn\n"; //random
String writeserial="spawn\n";
myPort.write(writeserial);
}
void draw() { void draw() {

View File

@ -1,6 +1,9 @@
#include "wagon.h" #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) 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 if (_pos>=_numpixels){
_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 if (_pos<0){
_pos=_numpixels+_pos; #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){ if (p<0){
p=0; //straight edges #ifdef EDGE_WRAP
//return (-p)*10; //edges as wall p=numpixels+p; //wrap edge
#endif
#ifdef EDGE_WALL
return _height[0]+p*-10; //edges as wall
#endif
}else if(p>=_numpixels){ }else if(p>=_numpixels){
p=_numpixels-1; //straight edges #ifdef EDGE_WRAP
return (p-_numpixels)*10; //edges as wall p=p-numpixels; //wrap edge
#endif
#ifdef EDGE_WALL
return _height[_numpixels-1]+(p-_numpixels)*10; //edges as wall
#endif
} }
return _height[p]; return _height[p];
} }
@ -160,8 +181,24 @@ void Wagon::updateGraphics()
_r*=featherbrightness; _r*=featherbrightness;
_g*=featherbrightness; _g*=featherbrightness;
_b*=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
} }
} }