Skip to content

Random Bonuses

noooway edited this page May 20, 2017 · 53 revisions

Currently bonuses generated on bricks destruction are predefined in the level description. In this part I want to add random bonus generation into the game.

screenshot

First, in the level map some way to denote that a bonus to create on brick destruction should be random is necessary. It's possible to reserve any number or a string for this, but I simply use 00.

function bonuses.bonustype_denotes_random( bonustype )
   return bonustype == 0
end

Bonuses are generated on bricks destruction by bonus.generate_bonus function.

function bricks.brick_hit_by_ball( i, brick, shift_ball, bonuses )
   if bricks.is_simple( brick ) then
      bonuses.generate_bonus(
         vector( brick.position.x + brick.width / 2,
                 brick.position.y + brick.height / 2 ),
         brick.bonustype )
      table.remove( bricks.current_level_bricks, i )
   .....
   end
end

In this function, a bonustype associated with the brick is checked. If it is one of the previously defined "valid" bonustypes (from 11 to 18), a corresponding bonus is generated. Check on random bonustype also should be placed in this function. To explicitly tell, that no bonus should be associated with the brick, it is enough to provide any "invalid" bonustype, such as -1.

function bonuses.generate_bonus( position, bonustype )
   if bonuses.bonustype_denotes_random( bonustype ) then
      bonustype = bonuses.random_bonustype()
   end
   if bonuses.valid_bonustype( bonustype ) then
      bonuses.add_bonus( bonuses.new_bonus( position, bonustype ) )
   end
end

function bonuses.valid_bonustype( bonustype )
   if bonustype and bonustype > 10 and bonustype < 19 then
      return true
   else
      return false
   end
end

Random bonus generation is done under following scheme. There are valuable bonuses and common bonuses; valuable are "Add Life" and "Next Level", common are everything else. I want a common bonus to appear approximately each 10 brick, and a valuable bonus - approximately once in 5 levels. There are maximum 8*11 ~ 90 bricks in each level. Since usually there are some unbreakable and missing bricks, there are, say, roughly 80 bricks per level or 400 bricks in 5 levels. So, on brick destruction, a common bonus (of randomly decided type) should drop with probability 1/10 and one of the valuable bonuses - with probability 1/400.

If a random number in interval [1,400] is generated, probability to get a certain predefined number, say 293 or 400, is exactly 1/400. To obtain an event with probability of 1/10, it is necessary to agree on 40 such numbers ( 1/400 * 40 = 1/10), say an interval [359, 399].

It is possible to generate a bonus with two rolls of dice: on first roll decide whether a valuable bonus should be generated, a common, or no bonus at all. Then in former two cases decide an exact type with a second roll.

First a random number in the range [1, 400] is generated. It equal a certain predefined number, say 400, with probability 1/400. If this happens, generate a random number again to decide which valuable bonus to create. A 1/10 probability in the interval [1, 399] is ~ 40, so we need to agree on 40 numbers in which case to drop a common bonus.Let's say it is an interval [360, 399]. If the random nubmer falls in that range, it is necessary to decide on which common bonus to generate.

Instead of generating bonus in two calls to random number generator, it is possible to do it in a single call. To simplify arithmetics a bit, I generate a random number in a range [1,402]. Let's agree, if the number equal to 402, "Add Life" is generated, if it equal to 401 - "Next Level" (probability of both events is 1/402 ~ 1/400). A common bonus should be generated if the number falls in range [360, 400]. We can split in 6 intervals and assign a certain common bonustype to each. E.g. "Slowdown" is in the range [360, 366], "Accelerate] is in the range [367, 342] and so on. This assignment can be done automatically with the following construct:

   elseif prob > 360 then
      bonustype = common_bonuses[ math.ceil( (prob - 360) / 7 ) ]
   else

figure with axis with numbers and probs

I don't want to make any distinction in bonus probability between simple and armored bricks. To generate random bonustype first it is necessary to initialize random number generator.

The whole code is

local bonustype_rng = love.math.newRandomGenerator( os.time() )

function bonuses.random_bonustype()
   -- once in 5 levels (~400 blocks): L or N
   -- once in 10 blocks - any others with roughly equal prob
   local bonustype
   local common_bonuses = { 11, 12, 13, 14, 15, 16 }
   local prob = bonustype_rng:random( 402 )
   if prob == 402 then
      bonustype = 18
   elseif prob == 401 then
      bonustype = 17
   elseif prob > 360 then
      bonustype = common_bonuses[ math.ceil( (prob - 360) / 7 ) ]
   else
      bonustype = nil
   end
   return bonustype
end

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. Game Over

    Appendix C: Stricter Modules (draft)
    Appendix D-1: Intro to Classes (draft)
    Appendix D-2: Chapter 2 Using Classes.

Chapter 3 (deprecated): Details

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally