Increment random

hi guys I'm working on a game for my Arduino based game system. Its an RPG similar to games like dragon warrior and final fantasy for NES or SNES. I need to make a function that Checks the room or worldmap the player is in then picks a random number between the number of monsters to display and battle on the screen.

Ive started a for loop but I don't know how to increment it to a random number in the loop...

void pickMonster() {

  for (uint8_t i = 0; i < player.room; monster.ID ++i){ 
    
  }
}

There are several random functions available. Never messed with'em so no advice as to usage.

By the way I'm still learning and this is my first attempt to create a for loop. Can the functions you pointed to be used in the increment part of the for loop?

for (uint8_t i = 0; i < player.room; monster.ID ++i) {   // only one expression per term is allowed

ok I did read the reference pages as well. With using the random function, if I want to add more monsters than 9 for it to cycle through I can just just change the upper bound number right?

If you want to change the range of random numbers you get out of random() then yes, you change the upper and/or lower bounds, just like it says on the reference page.

Whether those numbers have anything to do with monsters is entirely down to you.

Steve

Thank you both for the help and the clarification.

Delta I was asking about the example you gave that picks a number 1 to 9 exclusively. Since that is true for up to 9 objects (monsters), its easy to deduce that I was asking about the example you gave.

I'm not gonna argue with you but its all good. My question was answered.

ok say I have a structure and initialisation like so.....

struct Player
{
  int player_x;
  int player_y;
  int w;
  int h;
  int attack;
  int defense;
  int health;
  int room;
  int cameraX;
  int cameraY;
  int player_direction;
  int player_directionRepeat;
};

  Player player = { 160, 170, 16, 16, 20, 15, 100, 3, -128, -254, 2, 0};

And I want to deduct from the heath integer from random 0 to a max attack integer would I write it like this????

player.health - random(0, ENEMY_MAX_ATTACK); // ENEMY_MAX_ATTACK = 20

Well you might want a "player.health =" in there. Doing calculations and not assigning the result anywhere is not usually a good idea.

Oh and it should probably be ENEMY_MAX_ATTACK+1, like it says on the reference page (have you looked at that reference page yet?).

Steve

Reading the reference yes. But I need to read it a few more times. D I apologize. Lets start from the beginning....

I want to build a role playing game or rpg for my gaming libraries. You can see them if you click the links in my signature. Any way the game is a cross between Pokémon and the first final fantasy games for the NES. I'm now at the point in writing the program that I need a turn based battle system. it should work like....

case 1 Attack is initialized
case 2 monster attacks player, player takes damage. If players health drops to zero, game over
case 3 players choice-- Attack, item or flee.

if attack

case 4 player attacks enemy takes damage

if monsters health drops to zero player gets items and experience added and battle sequence is ended

I have flee and item worked out but not the battle parts. So now I need to make two functions....

Monster attacks player

player attacks monster

The battle menu wont fit in this post but if you need it I can post it in a separate post. Its not really important right now as I just need to make the attack functions.

I have a structure system for my monsters and player that give details like health attack defense placement on screen etc....

struct Player
{
  int player_x;
  int player_y;
  int w;
  int h;
  int attack;
  int defense;
  int health;
  int room;
  int cameraX;
  int cameraY;
  int player_direction;
  int player_directionRepeat;
};

  Player player = { 160, 170, 16, 16, 20, 15, 100, 3, -128, -254, 2, 0};


#define Monster_Cave_spider       1
#define Monster_Desert_bat        2
#define Monster_Desert_hare       3
#define Monster_Desert_scorpion   4
#define Monster_Desert_slig       5
#define Monster_Face_dancer       6
#define Monster_Jacuruto_outcast  7
#define Monster_Kangaroo_mouse    8
#define Monster_Laza_tiger        9
#define Monster_Sandworm          10

struct Monster {
  int x;
  int y;
  int w;
  int h;
  uint8_t MonId;
  uint8_t Attack;
  uint8_t Defense;
  uint8_t Health;
};

Monster monsters[] = {
{143, 150, 34, 24, 1,  10, 5,  20},
{143, 150, 48, 40, 2,  10, 5,  20},
{143, 150, 34, 22, 3,  15, 10, 30},
{143, 150, 44, 34, 4,  20, 15, 40},
{143, 150, 28, 36, 5,  20, 15, 40},
{143, 150, 34, 40, 6,  20, 20, 50},
{143, 150, 34, 32, 7,  20, 30, 60},
{143, 150, 38, 36, 8,  10, 5,  20},
{143, 150, 44, 32, 9,  30, 40, 70},
{143, 150, 24, 24, 10, 40, 50, 80}, 
};

Ok so I have a function that half works. When the function is activated it subtracts random numbers but very rarely are they under 20. I also need the uint8_t hploss so I can print out the loss to the screen though now its set to serial.

void Monsterattk() {
uint8_t hploss = (player.health = (player.health - random(0, 10 + 1)));
Serial.println(hploss);

For now I just want to be able to deduct the players loss properly. Ill work in the monsterid and stuff later.

I don't know what other code I could give besides the graphical functions for Draw battle. All I'm trying to do is figure out how deduct health in random increments from two different structures. Ive given the structures for both player and monsters. Each has values for attack health and defense.

Here is the whole sketch page with everything I have for draw battle. It is pulled up randomly and includes a menu system for attack, item and flee

#ifndef _Battle_H_
#define _Battle_H_

#include "Player.h"
#include "Variables.h"
#include "Bitmaps.h"


#define Monster_Cave_spider       1
#define Monster_Desert_bat        2
#define Monster_Desert_hare       3
#define Monster_Desert_scorpion   4
#define Monster_Desert_slig       5
#define Monster_Face_dancer       6
#define Monster_Jacuruto_outcast  7
#define Monster_Kangaroo_mouse    8
#define Monster_Laza_tiger        9
#define Monster_Sandworm          10

struct Monster {
  int x;
  int y;
  int w;
  int h;
  uint8_t MonId;
  uint8_t Attack;
  uint8_t Defense;
  uint8_t Health;
};

Monster monsters[] = {
{143, 150, 34, 24, 1,  10, 5,  20},
{143, 150, 48, 40, 2,  10, 5,  20},
{143, 150, 34, 22, 3,  15, 10, 30},
{143, 150, 44, 34, 4,  20, 15, 40},
{143, 150, 28, 36, 5,  20, 15, 40},
{143, 150, 34, 40, 6,  20, 20, 50},
{143, 150, 34, 32, 7,  20, 30, 60},
{143, 150, 38, 36, 8,  10, 5,  20},
{143, 150, 44, 32, 9,  30, 40, 70},
{143, 150, 24, 24, 10, 40, 50, 80}, 
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const char * getMonsterName(uint8_t MonId)
{
  switch (MonId)
  {
    case 1: return "Cave Spider";
    case 2: return "Desert Bat";
    case 3: return "Desert Hare";
    case 4: return "Desert Scorpion";
    case 5: return "Deset Slig";
    case 6: return "Facedancer";
    case 7: return "Jacuruto Outcast";
    case 8: return "Kangaroo Mouse";
    case 9: return "Laza Tiger";
    case 10: return "Sandworm";
    
  }
}

void monsterAttk(){
  uint8_t hploss = (player.health = (player.health - random(0, 20+1)));
  Serial.println(hploss);
}


void drawBattle() {

  monsterAttk();
  
  //////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////Palette////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  palette[0] = 0;            palette[8] = BEIGE;
  palette[1] = BLACK;        palette[9] = GREEN;
  palette[2] = BLUE;         palette[a] = DARKGREY;
  palette[3] = BROWN;        palette[b] = LIGHTGREY;
  palette[4] = DARKGREEN;    palette[c] = YELLOW;
  palette[5] = GREY;         palette[d] = PURPLE;
  palette[6] = PINK;         palette[e] = WHITE;
  palette[7] = RED;          palette[f] = ORANGE;
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////

  Rect rectC {24, 138, 33, 7};
  Rect rectD {24, 164, 22, 7};
  Rect rectE {24, 190, 47, 7};
  Rect rectF {24, 216, 23, 7};
  Rect rectG {cursorb.cursorB_x, cursorb.cursorB_y, 32, 32};
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  tft.fillScreen(BLACK);
  tft.fillRect(0, 30, 320, 50, WHITE);  ////// draws a back grounds stripe for the monsters to sit on the length nof the screen
  tft.writeRectNBPP(143, 50, 34, 24, 4, cavespider, palette);
  tft.drawRoundRect(40, 82, 240, 40, 4, WHITE);
  tft.fillRoundRect(41, 83, 237, 36, 4, BLUE);
  tft.setCursor(90, 94);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.println("Cave spider");
  tft.fillRoundRect(10, 130, 119, 106, 4, WHITE);
  tft.fillRoundRect(14, 133, 111, 100, 4, BLUE);
  tft.setCursor(24, 138);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.println("Attack");
  tft.setCursor(24, 164);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.println("Item");
  tft.setCursor(24, 190);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.println("Weirding");
  tft.setCursor(24, 216);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.println("Flee");
  ///////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////
  int y1 = ss1.analogRead(2);
  int x1 = ss1.analogRead(3);
  ///////////////////////////////////////////////////////////////////////////////
  ////////////////////////////Up/////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////
  if (x1 > 600 && last_x < 600) {
    tft.writeRectNBPP(cursorb.cursorB_x, cursorb.cursorB_y, 32, 32, 4, cursor3, palette);
    cursorb.cursorB_y -= 26;
  }
  if (cursorb.cursorB_y <= 136) {
    cursorb.cursorB_y = 136;
  }

  //////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////Down///////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  if (x1 < 400 && last_x > 400) {
    tft.writeRectNBPP(cursorb.cursorB_x, cursorb.cursorB_y, 32, 32, 4, cursor3, palette);
    cursorb.cursorB_y += 26;
  }
  if (cursorb.cursorB_y >= 240) {
    cursorb.cursorB_y = 240;
  }

  last_x = x1;
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (cursorb.cursorB_direction == 1) {
    tft.writeRectNBPP(cursorb.cursorB_x, cursorb.cursorB_y, 32, 32, 4, cursor3, palette);
  }
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (!digitalRead(IRQ_PIN2)) {
    uint32_t buttons = ss2.digitalReadBulk(button_mask2);
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectC.x, rectC.y, rectC.width, rectC.height, rectG.x, rectG.y, rectG.width, rectG.height)))
    {
      tft.fillScreen(GREEN);
    }
    else  if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectD.x, rectD.y, rectD.width, rectD.height, rectG.x, rectG.y, rectG.width, rectG.height)))
    {
      state = STATE_Item_list_bat;
    }
    else  if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectE.x, rectE.y, rectE.width, rectE.height, rectG.x, rectG.y, rectG.width, rectG.height)))
    {
      tft.fillScreen(PURPLE);
    }
    else  if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectF.x, rectF.y, rectF.width, rectF.height, rectG.x, rectG.y, rectG.width, rectG.height)))
    {
      state = STATE_Player;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  }
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
#endif

I use a collision function for the cursor which collides with each menu choice and when a button is pressed it performs the action with in each set of brackets