Author Topic: good random numbers  (Read 10964 times)

0 Members and 1 Guest are viewing this topic.

Offline kevinkore3

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +0/-0
    • View Profile
Re: good random numbers
« Reply #15 on: June 21, 2014, 02:13:03 pm »
Sorry for all the posts, but I have another problem :L
Code: [Select]
player.position_x=player.position_x_temp-player.drillcount/player.drillmax;
doesn't do anything because drillmax>drillcount, so drillcount/drillmax=0, but
Code: [Select]
player.position_x=player.position_x_temp-20*player.drillcount/player.drillmax;
moves the player 40 pixels to the left (which is very strange because it should be at most 20)

I tested
Code: [Select]
player.position_x=player.position_x_temp-1.1*player.drillcount/player.drillmax;and it also moves the player 40 pixels to the left ???
Code: [Select]
#include <os.h>
#include <SDL/SDL.h>
#include <ctime>
#include <cmath>
#include "blocks.cpp"
#include "player.cpp"
#define score_color 0x8000
#define spacing 12
uint32_t *money=0;
uint16_t tempint;
uint16_t chance[18];//for ore generation
uint8_t getlength(int score)
{
   uint8_t scorelength;
   while(1)
   {
      if(score>9)
      {
         if(score>99)
         {
            if(score>999)
            {
               if(score>9999)
               {
                  if(score>99999)
                  {
                     scorelength=6; //gl reaching 7 digits
                     break;
                  }
                  scorelength=5;
                  break;
               }
               scorelength=4;
               break;
            }
            scorelength=3;
            break;
         }
         scorelength=2;
         break;
      }
      scorelength=1;
      break;
   }
   return scorelength;
}
void shownumber(SDL_Surface *screen,uint32_t score,uint8_t scorelength)
{
   uint8_t tempscore;
   score*=10;
   for(int i=0;i<scorelength;i++)
   {
      score/=10;
      tempscore=score%10;
      switch(tempscore)
      {
         case 0:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=1;z<11;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            break;
         case 1:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,310-spacing*i,z,score_color);
            }
            break;
         case 2:
            for(int z=0;z<11;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<8;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
               nSDL_SetPixel(screen,305-spacing*i,7+z,score_color);
            }
            break;
         case 3:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            break;
         case 4:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
            }
            for(int z=0;z<7;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
            }
            break;
         case 5:
            for(int z=0;z<7;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
               nSDL_SetPixel(screen,315-spacing*i,7+z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            break;
         case 6:
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
            }
            for(int z=0;z<8;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,7+z,score_color);
            }
            break;
         case 7:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
            }
            break;
         case 8:
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            break;
         case 9:
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<8;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
            }
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            break;
      };
   }
}
enum blocktype
{
   nothing=0,
   dirt=1,
   coal=2,
   iron=3,
   tin=4,
   copper=5,
   silver=6,
   gold=7,
   platinum=8,
   topaz=9,
   sapphire=10,
   emerald=11,
   ruby=12,
   opal=13,
   jade=14,
   diamond=15,
   fossil=16,
   artifact=17,
   sigsegv=18,
   lava=19,//visible but does lots of damage
   rock=20,//you have to blow these up with explosives
   gas=21//invisible and blows up if you run into it
};
blocktype map[600][40];//motherload is something like 12 feet a block-1 block is 20 pixels
//less than 100kB anyway, no point using push_back so often
enum currentdirection
{
   down=0,
   right=1,
   left=2,
   up=3
};
class theplayer
{
public:
   bool is_flying;
   SDL_Rect rect_player_centered;
   SDL_Rect rect_player_animation_source;
   uint16_t drillmax;
   uint16_t drillcount;
   uint8_t animationcount;
   bool is_active;
   currentdirection direction;
   int8_t acceleration;
   int16_t position_x;//in pixels
   int16_t position_x_temp;
   int16_t depth;//in pixels
   int16_t depth_temp;
   uint8_t hull;
   uint8_t hullhealth;
   uint8_t drillspeed;
   uint8_t drill;
   uint16_t fueltank;
   uint16_t fuel;//every loop-1L=100 loops=2 seconds at 50FPS
   uint8_t engine;
   uint8_t enginepower;//max pixels/second lift
   uint8_t cargobay;
   uint16_t cargocapacity;//in cubic feet
   uint8_t cargo[18];//each element represents how many of a certain type
   //for example, element 0 represents coal, which is the second item in the enum
   theplayer()
   {
      acceleration=0;
      depth_temp=0;
      drillmax=0;
      drillcount=0;
      animationcount=0;
      is_active=0;
      direction=right;
      depth=0;
      position_x=100;
      hull=1;
      hullhealth=10;
      drill=1;
      drillspeed=10;
      fueltank=1;
      fuel=150;
      engine=1;
      enginepower=3;
      cargobay=1;
      cargocapacity=10;
      rect_player_centered.x=156;//player's position drawn on the screen, always centered
      rect_player_centered.y=106;
      rect_player_centered.w=28;
      rect_player_centered.h=28;
      rect_player_animation_source.x=0;
      rect_player_animation_source.y=0;
      rect_player_animation_source.w=28;
      rect_player_animation_source.h=28;
      for(int i=0;i<15;i++)
      {
         cargo[i]=0;
      }
   }
   void sell_cargo()
   {
      *money+=30*cargo[0];//coal is worth 30
      *money+=50*cargo[1];//iron is worth 50
      *money+=80*cargo[2];//tin is worth 80
      *money+=120*cargo[3];//copper
      *money+=200*cargo[4];//silver
      *money+=400*cargo[5];//gold
      *money+=750*cargo[6];//platinum
      *money+=1000*cargo[7];//topaz
      *money+=1200*cargo[8];//sapphire
      *money+=2000*cargo[9];//emerald
      *money+=3500*cargo[10];//ruby
      *money+=5000*cargo[11];//opal
      *money+=8000*cargo[12];//jade
      *money+=20000*cargo[13];//diamond
      *money+=10000*cargo[14];//fossils
      *money+=15000*cargo[15];//artifacts
      *money+=100000*cargo[16];//sigsegv lol
      for(int i=0;i<15;i++)
      {
         cargo[i]=0;
      }
   }
};
void newrow(uint16_t row)
{
   chance[0]=1000/(abs(pow(row-10,1.1))+50);//1000/(abs(pow(row-row_where_it_is_most_common,range_of_how_common_it_is))+100/percent_of_blocks_in_most_common_row)
   //coal is found with a frequency of 100/50=2% of blocks in the row in which it is most commonly found
   //coal is most commonly found in row 10 (actually 11 in the array, but 1 more or less doesn't make a huge difference)
   //coal is commonly found in a large range of blocks, note the low exponent
   //note that 0 in this array corresponds to 2 in blocktype
   chance[1]=1000/(abs(pow(row-30,1.1))+60);//iron is found in 1.667% or 16/1000 blocks in the generator at max, and is also very common
   chance[2]=1000/(abs(pow(row-50,1.1))+80);
   chance[3]=1000/(abs(pow(row-80,1.1))+20);
   chance[4]=1000/(abs(pow(row-110,1.3))+100);
   chance[5]=1000/(abs(pow(row-150,1.5))+150);
   for(int i=0;i<40;i++)
   {
      tempint=rand()%1000;
      for(int z=0;z<7;z++)
      {
         if(z==6)
         {
            if(rand()%8)
            {
               map[row][i]=dirt;
               break;
            }
            map[row][i]=nothing;
            break;
         }
         if(chance[z]>tempint)
         {
            map[row][i]=(static_cast<blocktype>(z+2));
            break;
         }
         tempint-=chance[z];
      }   
   }
}
int main()
{
   srand(time(NULL));
   uint8_t intlength;
   SDL_Init(SDL_INIT_VIDEO);
   SDL_Surface *screen=SDL_SetVideoMode(320,240,16,SDL_SWSURFACE);
   nSDL_Font *font=nSDL_LoadFont(NSDL_FONT_VGA,0,64,0);
   SDL_Surface *sprite_blocks=nSDL_LoadImage(image_blocks);
   SDL_Surface *sprite_player=nSDL_LoadImage(image_player);
   SDL_SetColorKey(sprite_blocks,SDL_SRCCOLORKEY,SDL_MapRGB(sprite_blocks->format,0xff,0xff,0xff));
   SDL_SetColorKey(sprite_player,SDL_SRCCOLORKEY,SDL_MapRGB(sprite_blocks->format,0xff,0xff,0xff));
   SDL_Rect rect_fullscreen{0,0,320,240};
   SDL_Rect rect_sky{0,0,320,0};
   theplayer player;
   SDL_Rect sprite_blocks_source{0,0,20,20};
   SDL_Rect rect_blocks;
   rect_blocks.w=20;
   rect_blocks.h=20;
   SDL_Rect rect_hud1{0,0,320,20};
   SDL_Rect rect_hud2{0,20,20,220};
   SDL_Rect rect_blank{0,0,20,20};
   for(int i=0;i<600;i++)
   {
      newrow(i);
   }
   while(1)
   {
      if(!player.is_active)
      {
         if(isKeyPressed(KEY_NSPIRE_UP))
         {
            player.direction=up;
            player.is_active=1;
         }
         else if(isKeyPressed(KEY_NSPIRE_LEFT) && player.position_x>19)
         {
            player.direction=left;
            player.is_active=1;
            player.position_x_temp=player.position_x;
            player.drillmax=player.depth/20+200;
         }
         else if(isKeyPressed(KEY_NSPIRE_DOWN) && player.depth>-1)
         {
            player.depth_temp=player.depth;
            player.direction=down;
            player.is_active=1;
            player.drillmax=player.depth/20+200;
         }
         else if(isKeyPressed(KEY_NSPIRE_RIGHT) && player.position_x<619)
         {
            player.direction=right;
            player.position_x_temp=player.position_x;
            player.is_active=1;
            player.drillmax=player.depth/20+200;
         }
      }
      else
      {
         if(player.direction==down)
         {
            player.depth=player.depth_temp+20*player.drillcount/player.drillmax;
         }
         else if(player.direction==right)
         {
            player.position_x=player.position_x_temp-player.drillcount/player.drillmax;
         }
         else if(player.direction==left)
         {
            player.position_x=player.position_x_temp+20*player.drillcount/player.drillmax;
         }
         player.animationcount++;
         player.animationcount%=4;
         player.rect_player_animation_source.x=static_cast<currentdirection>(player.direction)*112+player.animationcount*28;
         player.drillcount+=player.drillspeed;
         if(player.drillcount>=player.drillmax)
         {
            if(player.direction==left)
            {
               player.position_x=player.position_x_temp-20;
            }
            else if(player.direction==right)
            {
               player.position_x=player.position_x_temp+20;
            }
            map[player.depth_temp/20-1][player.position_x/20+15]=nothing;
            if(player.direction==down)
            {
               player.depth=player.depth_temp+20;
            }
            player.drillcount=0;
            player.is_active=0;
         }
      }
      if(player.direction==up)
      {
         if(!isKeyPressed(KEY_NSPIRE_UP))
         {
            if(player.acceleration!=0)
            {
               player.acceleration--;
            }
            else
            {
               player.is_active=0;
            }
         }
         else if(player.acceleration!=player.enginepower)
         {
            player.acceleration++;
         }
         player.depth-=player.acceleration;
      }
      if(isKeyPressed(KEY_NSPIRE_ESC))
      {
         SDL_Quit();
         return 0;
      }
      SDL_FillRect(screen,&rect_fullscreen,0x7b08);
      if(player.depth<140)
      {
         rect_sky.h=140-player.depth;
         SDL_FillRect(screen,&rect_sky,0x9edd);
      }
      for(int z=0;z<13;z++)
      {
         if(player.depth+20*z<120)
         {
            continue;
         }
         for(int i=0;i<17;i++)
         {
            if(player.position_x/20+i<0)
            {
               continue;
            }
            rect_blocks.y=20*z-player.depth%20+10;
            rect_blocks.x=player.position_x%20+20*i;
            if(static_cast<blocktype>(map[player.depth/20+z-7][player.position_x/20+i+7])>1)
            {
               sprite_blocks_source.x=20;
               SDL_BlitSurface(sprite_blocks,&sprite_blocks_source,screen,&rect_blocks);
            }
            sprite_blocks_source.x=20*(static_cast<blocktype>(map[player.depth/20+z-7][player.position_x/20+i+7]));
            SDL_BlitSurface(sprite_blocks,&sprite_blocks_source,screen,&rect_blocks);
         }
      }
      SDL_BlitSurface(sprite_player,&player.rect_player_animation_source,screen,&player.rect_player_centered);
      //SDL_FillRect(screen,&rect_fullscreen,35362);//brown in case you were wondering-crude graphics for dirt
      SDL_FillRect(screen,&rect_hud1,0xffff);
      SDL_FillRect(screen,&rect_hud2,0xffff);
      intlength=getlength(abs(player.depth));
      shownumber(screen,abs(player.depth),intlength);
      if(player.depth<0)
      {
         for(int i=0;i<10;i++)
         {
            nSDL_SetPixel(screen,305-intlength*12+i,7,score_color);
         }
      }
      SDL_Flip(screen);
   }
   return 0;
}
(code still needs to be cleaned up a lot)
Probably some other derp fail, but was wondering
You can view the glitch in the attachment




(gcc occasionally glitches for me in codeblocks (the exact same code compiles to different things at different times) because my computer is weird, but I've tried this lots of times)
« Last Edit: June 21, 2014, 02:16:59 pm by kevinkore3 »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: good random numbers
« Reply #16 on: June 25, 2014, 11:39:33 am »
:o To be honest, your source code is awful:
  • DON'T include .cpp files EVER!
  • DON'T mix defines and variables. They need to be distinguishable, e.g. ALL-CAPS
  • A while(1) with an if-chain and break statements? You know you can return at any point in a function?
  • Don't do it like getlength, use floor(log10(score))+1
  • Don't hardcode font drawing. Use an array.
  • Use "enum class" instead of enum, otherwise the enum values will have the same scope as the enum itself
  • Use initialization lists
  • References instead of pointers
  • Loop + array lookup in sell_cargo
  • Why is money a pointer to a uint32_t?
  • (u)int(8/16/32)_t doesn't have a fractional part. Multiplying it by 1.1 is the same as *1.

I'd advise you to read a good c++ book or online tutorials, but it'll take a while.
« Last Edit: June 25, 2014, 11:41:17 am by Vogtinator »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: good random numbers
« Reply #17 on: June 25, 2014, 11:40:55 am »
Why shouldn't one include cpp files?

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: good random numbers
« Reply #18 on: June 25, 2014, 11:43:14 am »
They will also get picked up by the linker and there may be some "duplicate definition of symbol" errors.
Also, you wouldn't be able to use it in two seperate files, as the symbols in it would then cause the same issue.
If there aren't any symbols in it, it doesn't need to get compiled -> header file.
« Last Edit: June 25, 2014, 11:46:14 am by Vogtinator »

Offline kevinkore3

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +0/-0
    • View Profile
Re: good random numbers
« Reply #19 on: June 25, 2014, 09:48:13 pm »
I know my source code kind of sucks. I used getlength because people said logs were lots slower (dunno if that's true, but it had lots of thumbs ups and I'm not taking risks at 132mhz) I know there are lots of bigger things to optimize, but this was an easier one to do
As for the 1* and 1.1*, I was also thinking that it should give the same result, but it didn't. As for the money thingy,  I have no idea why I put it there :P  probably wasn't thinking (do I need any pointers?)
For the cargo thingy, there is no pattern to how much each item costs, so I decided against a loop.
I will clean up the source after I finish.
Anyone have any idea about the weird multiplying glitch? If I do +=40 it gets jerky.
I'm happy that it runs and gets decent fps at least  :D
Thanks for the suggestions
« Last Edit: June 25, 2014, 09:58:52 pm by kevinkore3 »