Use the random generator instead of rand()

This commit is contained in:
Linus_Probert 2018-02-15 14:45:20 +01:00
parent f4867551a1
commit a26c7be122
6 changed files with 29 additions and 24 deletions

View File

@ -6,6 +6,7 @@
#include "util.h"
#include "gui.h"
#include "mixer.h"
#include "random.h"
static ItemBuilder *builder = NULL;
@ -92,7 +93,7 @@ create_treasure(int current_level)
unsigned int highest_treasure;
unsigned int value;
amt = (unsigned int) (rand() + (5*current_level)) % 40;
amt = (unsigned int) get_random(5*current_level) % 40;
if (current_level > 9) {
highest_treasure = TREASURE_COUNT;
@ -102,7 +103,7 @@ create_treasure(int current_level)
highest_treasure = GOLD;
}
value = rand() % highest_treasure;
value = get_random(highest_treasure);
SDL_Rect clip = { 0, 0, 16, 16 };
switch (value) {
@ -157,13 +158,13 @@ item_builder_build_item(ItemKey key, int level)
item = create_item(path_flesh,
(SDL_Rect) { 0, 0, 16, 16 },
&eat_flesh);
item->value = 1;
item->value = get_random(level);
break;
case HEALTH:
item = create_item(path_potion,
(SDL_Rect) { 0, 0, 16, 16 },
&drink_health);
item->value = 1 + (rand() % 2);
item->value = 1 + get_random(level*2);
break;
default:
fatal("in item_builder_build() : Unhandled item key %d", key);

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
@ -22,6 +23,7 @@
#include "menu.h"
#include "keyboard.h"
#include "mixer.h"
#include "random.h"
static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL;
@ -167,7 +169,7 @@ startGame(void *unused)
if (gPlayer)
player_destroy(gPlayer);
gPlayer = player_create(WARRIOR, gRenderer);
mixer_play_music(GAME_MUSIC0 + (rand() % 3));
mixer_play_music(GAME_MUSIC0 + get_random(2));
resetGame();
}
@ -381,7 +383,7 @@ check_next_level(void)
}
if (tile->levelExit) {
mixer_play_effect(NEXT_LEVEL);
mixer_play_music(GAME_MUSIC0 + (rand() % 3));
mixer_play_music(GAME_MUSIC0 + get_random(2));
++cLevel;
resetGame();
}

View File

@ -259,19 +259,19 @@ monster_update_stats_for_level(Monster *m, unsigned int level)
void
monster_drop_loot(Monster *monster, Map *map)
{
static unsigned int item_drop_chance = 3;
static unsigned int treasure_drop_chance = 2;
static unsigned int item_drop_chance = 1;
static unsigned int treasure_drop_chance = 1;
Item *item;
Item *items[2];
unsigned int item_count = 0;
if ((rand() % treasure_drop_chance) == 0) {
if (get_random(treasure_drop_chance) == 0) {
item = item_builder_build_item(TREASURE, map->level);
item->sprite->pos = monster->sprite->pos;
items[item_count++] = item;
}
if ((rand() % item_drop_chance) == 0) {
item = item_builder_build_item(rand() % TREASURE, map->level);
if (get_random(item_drop_chance) == 0) {
item = item_builder_build_item(get_random(TREASURE - 1), map->level);
item->sprite->pos = monster->sprite->pos;
items[item_count++] = item;
}

View File

@ -4,6 +4,7 @@
#include "util.h"
#include "defines.h"
#include "vector2d.h"
#include "random.h"
typedef struct Particle_t {
Position pos;
@ -50,17 +51,17 @@ particle_engine_bloodspray(Position pos, Dimension dim, unsigned int count)
unsigned int mt, lt;
Particle *p;
x = (rand() % dim.width) + pos.x;
y = (rand() % dim.height) + pos.y;
x = get_random(dim.width) + pos.x;
y = get_random(dim.height) + pos.y;
xv = (rand() % 200) - 100;
yv = (rand() % 200) - 100;
xv = get_random(200) - 100;
yv = get_random(200) - 100;
mt = (rand() % 10) + 10;
lt = (rand() % 120) + 60;
mt = get_random(10) + 10;
lt = get_random(120) + 60;
w = (rand() % 3) + 2;
h = (rand() % 3) + 2;
w = get_random(3) + 2;
h = get_random(3) + 2;
p = ec_malloc(sizeof(Particle));
p->pos = (Position) { x, y };

View File

@ -10,6 +10,7 @@
#include "particle_engine.h"
#include "keyboard.h"
#include "mixer.h"
#include "random.h"
#define ENGINEER_STATS { 12, 12, 5, 7, 2, 1, 1 }
#define MAGE_STATS { 12, 12, 5, 7, 2, 1, 1 }
@ -78,7 +79,7 @@ has_collided(Player *player, RoomMatrix *matrix)
unsigned int hit = stats_fight(&player->stats,
&space->monster->stats);
mixer_play_effect(SWING0 + (rand() % 3));
mixer_play_effect(SWING0 + get_random(2));
monster_hit(space->monster, hit);
@ -180,7 +181,7 @@ sip_health(Player *player)
if (player->potion_sips > 0) {
--player->potion_sips;
++player->stats.hp;
mixer_play_effect(BUBBLE0 + (rand() % 3));
mixer_play_effect(BUBBLE0 + get_random(2));
gui_log("You take a sip of health potion");
} else {
gui_log("You have nothing to sip");

View File

@ -25,9 +25,9 @@ stats_fight(Stats *attacker, Stats *defender)
debug("Attacking: %d Defending: %d", atkRoll, defRoll);
if (atkRoll > defRoll) {
dmgRoll = (rand() % attacker->dmg) + 1;
defender->hp -= dmgRoll;
dmgRoll = get_random(attacker->dmg - 1) + 1;
if (critical)
dmgRoll = dmgRoll * 2;
defender->hp -= dmgRoll;
}