From 7ece405b8b2c8fd263a55233c1493ed4b6602486 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 6 May 2018 21:41:33 +0200 Subject: [PATCH] improve random spawning --- achterbahn.ino | 113 +++++++++++++----- .../achterbahnconfig/achterbahnconfig.pde | 11 +- wagon.cpp | 59 +++++++-- 3 files changed, 143 insertions(+), 40 deletions(-) diff --git a/achterbahn.ino b/achterbahn.ino index d602b64..a79f9d6 100644 --- a/achterbahn.ino +++ b/achterbahn.ino @@ -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_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;i0) { 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,19 +398,44 @@ void loop_achterbahn(){ if (lastRoutineUpdate+ROUTINEUPDATETIME::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=_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]; } @@ -160,8 +181,24 @@ void Wagon::updateGraphics() _r*=featherbrightness; _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 } }