From c72f09adfa6f776ff5cabd0b8e4b4e0a2e752703 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Fri, 30 Oct 2009 15:59:29 +0100 Subject: Splitting up stuff.cc. New: colour.cc, coord.cc, coordit.cc, random.cc, rng.cc. --- crawl-ref/source/abyss.cc | 4 +- crawl-ref/source/acr.cc | 1 + crawl-ref/source/arena.cc | 1 + crawl-ref/source/artefact.cc | 4 +- crawl-ref/source/beam.cc | 26 +- crawl-ref/source/beam.h | 3 +- crawl-ref/source/cloud.cc | 2 + crawl-ref/source/colour.cc | 378 +++++++++++++++++++ crawl-ref/source/colour.h | 62 +++ crawl-ref/source/command.cc | 3 +- crawl-ref/source/coord.cc | 100 +++++ crawl-ref/source/coord.h | 35 ++ crawl-ref/source/coordit.cc | 189 ++++++++++ crawl-ref/source/coordit.h | 62 +++ crawl-ref/source/database.cc | 1 + crawl-ref/source/delay.cc | 2 + crawl-ref/source/describe.cc | 2 + crawl-ref/source/dgnevent.cc | 3 +- crawl-ref/source/directn.cc | 2 + crawl-ref/source/dungeon.cc | 1 + crawl-ref/source/effects.cc | 1 + crawl-ref/source/exclude.cc | 1 + crawl-ref/source/externs.h | 9 - crawl-ref/source/files.cc | 1 + crawl-ref/source/food.cc | 1 + crawl-ref/source/format.cc | 2 +- crawl-ref/source/ghost.cc | 4 +- crawl-ref/source/godabil.cc | 4 + crawl-ref/source/initfile.cc | 153 ++------ crawl-ref/source/initfile.h | 3 - crawl-ref/source/invent.cc | 1 + crawl-ref/source/item_use.cc | 1 + crawl-ref/source/itemname.cc | 1 + crawl-ref/source/itemprop.cc | 1 + crawl-ref/source/l_crawl.cc | 1 + crawl-ref/source/l_dgn.cc | 3 + crawl-ref/source/l_dgn_bf.cc | 2 +- crawl-ref/source/l_dgnbld.cc | 2 + crawl-ref/source/l_dgngrd.cc | 3 +- crawl-ref/source/l_dgnit.cc | 1 + crawl-ref/source/l_item.cc | 1 + crawl-ref/source/l_los.cc | 1 + crawl-ref/source/l_you.cc | 1 + crawl-ref/source/lev-pand.cc | 2 +- crawl-ref/source/los.cc | 2 + crawl-ref/source/makefile.obj | 5 + crawl-ref/source/makeitem.cc | 4 +- crawl-ref/source/mapdef.cc | 3 + crawl-ref/source/mapdef.h | 1 + crawl-ref/source/maps.cc | 2 +- crawl-ref/source/menu.cc | 11 +- crawl-ref/source/message.cc | 2 + crawl-ref/source/mgrow.cc | 2 +- crawl-ref/source/misc.cc | 1 + crawl-ref/source/mon-util.cc | 2 + crawl-ref/source/monplace.cc | 1 + crawl-ref/source/monplace.h | 1 + crawl-ref/source/monstuff.cc | 1 + crawl-ref/source/mstuff2.cc | 1 + crawl-ref/source/mtransit.cc | 2 +- crawl-ref/source/mutation.cc | 2 +- crawl-ref/source/newgame.cc | 1 + crawl-ref/source/ng-init.cc | 2 +- crawl-ref/source/ouch.cc | 1 + crawl-ref/source/output.cc | 2 + crawl-ref/source/overmap.cc | 2 + crawl-ref/source/player.cc | 3 + crawl-ref/source/random.cc | 263 +++++++++++++ crawl-ref/source/random.h | 76 ++++ crawl-ref/source/rng.cc | 91 +++++ crawl-ref/source/rng.h | 14 + crawl-ref/source/skills.cc | 2 +- crawl-ref/source/species.cc | 2 +- crawl-ref/source/spells1.cc | 2 + crawl-ref/source/spl-cast.cc | 2 + crawl-ref/source/spl-mis.cc | 1 + crawl-ref/source/spl-util.cc | 1 + crawl-ref/source/stash.cc | 1 + crawl-ref/source/stuff.cc | 849 +----------------------------------------- crawl-ref/source/stuff.h | 163 -------- crawl-ref/source/terrain.cc | 1 + crawl-ref/source/tilepick.cc | 3 + crawl-ref/source/tilereg.cc | 1 + crawl-ref/source/tilesdl.cc | 1 + crawl-ref/source/transfor.cc | 1 + crawl-ref/source/traps.cc | 1 + crawl-ref/source/travel.cc | 2 + crawl-ref/source/tutorial.cc | 3 + crawl-ref/source/view.cc | 2 + crawl-ref/source/view.h | 46 --- 90 files changed, 1411 insertions(+), 1252 deletions(-) create mode 100644 crawl-ref/source/colour.cc create mode 100644 crawl-ref/source/colour.h create mode 100644 crawl-ref/source/coord.cc create mode 100644 crawl-ref/source/coord.h create mode 100644 crawl-ref/source/coordit.cc create mode 100644 crawl-ref/source/coordit.h create mode 100644 crawl-ref/source/random.cc create mode 100644 crawl-ref/source/random.h create mode 100644 crawl-ref/source/rng.cc create mode 100644 crawl-ref/source/rng.h (limited to 'crawl-ref/source') diff --git a/crawl-ref/source/abyss.cc b/crawl-ref/source/abyss.cc index 4c2f8b9458..c7d96b3b6d 100644 --- a/crawl-ref/source/abyss.cc +++ b/crawl-ref/source/abyss.cc @@ -11,10 +11,9 @@ #include #include -#include "externs.h" - #include "artefact.h" #include "cloud.h" +#include "colour.h" #include "makeitem.h" #include "mapmark.h" #include "message.h" @@ -28,6 +27,7 @@ #include "l_defs.h" #include "lev-pand.h" #include "los.h" +#include "random.h" #include "religion.h" #include "stuff.h" #include "spells3.h" diff --git a/crawl-ref/source/acr.cc b/crawl-ref/source/acr.cc index 1461423479..bb6c9cbc4c 100644 --- a/crawl-ref/source/acr.cc +++ b/crawl-ref/source/acr.cc @@ -94,6 +94,7 @@ #include "overmap.h" #include "player.h" #include "quiver.h" +#include "random.h" #include "religion.h" #include "shopping.h" #include "skills.h" diff --git a/crawl-ref/source/arena.cc b/crawl-ref/source/arena.cc index 2d03feb352..163946587a 100644 --- a/crawl-ref/source/arena.cc +++ b/crawl-ref/source/arena.cc @@ -9,6 +9,7 @@ #include "arena.h" #include "artefact.h" #include "cio.h" +#include "colour.h" #include "command.h" #include "dungeon.h" #include "initfile.h" diff --git a/crawl-ref/source/artefact.cc b/crawl-ref/source/artefact.cc index feccab1ca1..d1dbdd0450 100644 --- a/crawl-ref/source/artefact.cc +++ b/crawl-ref/source/artefact.cc @@ -15,8 +15,8 @@ #include #include "externs.h" -#include "species.h" +#include "colour.h" #include "database.h" #include "describe.h" #include "itemname.h" @@ -24,7 +24,9 @@ #include "items.h" #include "place.h" #include "player.h" +#include "random.h" #include "religion.h" +#include "species.h" #include "spl-book.h" #include "stuff.h" #include "view.h" // Elemental colours for unrandarts diff --git a/crawl-ref/source/beam.cc b/crawl-ref/source/beam.cc index 95d981a414..c77a23f447 100644 --- a/crawl-ref/source/beam.cc +++ b/crawl-ref/source/beam.cc @@ -26,6 +26,7 @@ #include "cio.h" #include "cloud.h" +#include "colour.h" #include "delay.h" #include "dgnevent.h" #include "effects.h" @@ -288,31 +289,6 @@ bool player_tracer( zap_type ztype, int power, bolt &pbolt, int range) return (true); } -dice_def calc_dice( int num_dice, int max_damage ) -{ - dice_def ret( num_dice, 0 ); - - if (num_dice <= 1) - { - ret.num = 1; - ret.size = max_damage; - } - else if (max_damage <= num_dice) - { - ret.num = max_damage; - ret.size = 1; - } - else - { - // Divide the damage among the dice, and add one - // occasionally to make up for the fractions. -- bwr - ret.size = max_damage / num_dice; - ret.size += x_chance_in_y(max_damage % num_dice, num_dice); - } - - return (ret); -} - template struct power_deducer { diff --git a/crawl-ref/source/beam.h b/crawl-ref/source/beam.h index 9f8779ae40..8b976c3492 100644 --- a/crawl-ref/source/beam.h +++ b/crawl-ref/source/beam.h @@ -10,6 +10,7 @@ #include "externs.h" +#include "random.h" #include "ray.h" class monsters; @@ -274,8 +275,6 @@ public: bool stop_at_statues, bool stop_at_walls); }; -dice_def calc_dice( int num_dice, int max_damage ); - // Test if the to-hit (attack) beats evasion (defence). bool test_beam_hit(int attack, int defence); diff --git a/crawl-ref/source/cloud.cc b/crawl-ref/source/cloud.cc index f9f60541ff..523d7cd8f3 100644 --- a/crawl-ref/source/cloud.cc +++ b/crawl-ref/source/cloud.cc @@ -14,9 +14,11 @@ #include "branch.h" #include "cloud.h" +#include "coord.h" #include "mapmark.h" #include "ouch.h" #include "player.h" +#include "random.h" #include "spells4.h" #include "stuff.h" #include "terrain.h" diff --git a/crawl-ref/source/colour.cc b/crawl-ref/source/colour.cc new file mode 100644 index 0000000000..6a27c04b6a --- /dev/null +++ b/crawl-ref/source/colour.cc @@ -0,0 +1,378 @@ +#include "AppHdr.h" + +#include "colour.h" + +#include "env.h" +#include "player.h" +#include "random.h" + +unsigned char random_colour(void) +{ + return (1 + random2(15)); +} + +unsigned char random_uncommon_colour() +{ + unsigned char result; + + do + result = random_colour(); + while (result == LIGHTCYAN || result == CYAN || result == BROWN); + + return (result); +} + +unsigned char make_low_colour(unsigned char colour) +{ + if (colour >= 8 && colour <= 15) + return (colour - 8); + + return (colour); +} + +unsigned char make_high_colour(unsigned char colour) +{ + if (colour <= 7) + return (colour + 8); + + return (colour); +} + +// returns if a colour is one of the special element colours (ie not regular) +bool is_element_colour( int col ) +{ + // stripping any COLFLAGS (just in case) + return ((col & 0x007f) >= ETC_FIRE); +} + +int element_colour( int element, bool no_random ) +{ + // Doing this so that we don't have to do recursion here at all + // (these were the only cases which had possible double evaluation): + if (element == ETC_FLOOR) + element = env.floor_colour; + else if (element == ETC_ROCK) + element = env.rock_colour; + + // pass regular colours through for safety. + if (!is_element_colour( element )) + return (element); + + int ret = BLACK; + + // Setting no_random to true will get the first colour in the cases + // below. This is potentially useful for calls to this function + // which might want a consistent result. + int tmp_rand = (no_random ? 0 : random2(120)); + + switch (element & 0x007f) // strip COLFLAGs just in case + { + case ETC_FIRE: + ret = (tmp_rand < 40) ? RED : + (tmp_rand < 80) ? YELLOW + : LIGHTRED; + break; + + case ETC_ICE: + ret = (tmp_rand < 40) ? LIGHTBLUE : + (tmp_rand < 80) ? BLUE + : WHITE; + break; + + case ETC_EARTH: + ret = (tmp_rand < 60) ? BROWN : LIGHTRED; + break; + + case ETC_AIR: + ret = (tmp_rand < 60) ? LIGHTGREY : WHITE; + break; + + case ETC_ELECTRICITY: + ret = (tmp_rand < 40) ? LIGHTCYAN : + (tmp_rand < 80) ? LIGHTBLUE + : CYAN; + break; + + case ETC_POISON: + ret = (tmp_rand < 60) ? LIGHTGREEN : GREEN; + break; + + case ETC_WATER: + ret = (tmp_rand < 60) ? BLUE : CYAN; + break; + + case ETC_MAGIC: + ret = (tmp_rand < 30) ? LIGHTMAGENTA : + (tmp_rand < 60) ? LIGHTBLUE : + (tmp_rand < 90) ? MAGENTA + : BLUE; + break; + + case ETC_MUTAGENIC: + case ETC_WARP: + ret = (tmp_rand < 60) ? LIGHTMAGENTA : MAGENTA; + break; + + case ETC_ENCHANT: + ret = (tmp_rand < 60) ? LIGHTBLUE : BLUE; + break; + + case ETC_HEAL: + ret = (tmp_rand < 60) ? LIGHTBLUE : YELLOW; + break; + + case ETC_BLOOD: + ret = (tmp_rand < 60) ? RED : DARKGREY; + break; + + case ETC_DEATH: // assassin + case ETC_NECRO: // necromancer + ret = (tmp_rand < 80) ? DARKGREY : MAGENTA; + break; + + case ETC_UNHOLY: // ie demonology + ret = (tmp_rand < 80) ? DARKGREY : RED; + break; + + case ETC_DARK: + ret = (tmp_rand < 80) ? DARKGREY : LIGHTGREY; + break; + + case ETC_HOLY: + ret = (tmp_rand < 60) ? YELLOW : WHITE; + break; + + case ETC_VEHUMET: + ret = (tmp_rand < 40) ? LIGHTRED : + (tmp_rand < 80) ? LIGHTMAGENTA + : LIGHTBLUE; + break; + + case ETC_BEOGH: + ret = (tmp_rand < 60) ? LIGHTRED // plain Orc colour + : BROWN; // Orcish mines wall/idol colour + break; + + case ETC_CRYSTAL: + ret = (tmp_rand < 40) ? LIGHTGREY : + (tmp_rand < 80) ? GREEN + : LIGHTRED; + break; + + case ETC_SLIME: + ret = (tmp_rand < 40) ? GREEN : + (tmp_rand < 80) ? BROWN + : LIGHTGREEN; + break; + + case ETC_SMOKE: + ret = (tmp_rand < 30) ? LIGHTGREY : + (tmp_rand < 60) ? DARKGREY : + (tmp_rand < 90) ? LIGHTBLUE + : MAGENTA; + break; + + case ETC_JEWEL: + ret = (tmp_rand < 12) ? WHITE : + (tmp_rand < 24) ? YELLOW : + (tmp_rand < 36) ? LIGHTMAGENTA : + (tmp_rand < 48) ? LIGHTRED : + (tmp_rand < 60) ? LIGHTGREEN : + (tmp_rand < 72) ? LIGHTBLUE : + (tmp_rand < 84) ? MAGENTA : + (tmp_rand < 96) ? RED : + (tmp_rand < 108) ? GREEN + : BLUE; + break; + + case ETC_ELVEN: + ret = (tmp_rand < 40) ? LIGHTGREEN : + (tmp_rand < 80) ? GREEN : + (tmp_rand < 100) ? LIGHTBLUE + : BLUE; + break; + + case ETC_DWARVEN: + ret = (tmp_rand < 40) ? BROWN : + (tmp_rand < 80) ? LIGHTRED : + (tmp_rand < 100) ? LIGHTGREY + : CYAN; + break; + + case ETC_ORCISH: + ret = (tmp_rand < 40) ? DARKGREY : + (tmp_rand < 80) ? RED : + (tmp_rand < 100) ? BROWN + : MAGENTA; + break; + + case ETC_GILA: + ret = (tmp_rand < 30) ? LIGHTMAGENTA : + (tmp_rand < 60) ? MAGENTA : + (tmp_rand < 90) ? YELLOW : + (tmp_rand < 105) ? LIGHTRED + : RED; + break; + + case ETC_STONE: + if (player_in_branch( BRANCH_HALL_OF_ZOT )) + ret = env.rock_colour; + else + ret = LIGHTGREY; + break; + + case ETC_MIST: + ret = tmp_rand < 100? CYAN : BLUE; + break; + + case ETC_SHIMMER_BLUE: + ret = random_choose_weighted(80, BLUE, 20, LIGHTBLUE, 5, CYAN, 0); + break; + + case ETC_DECAY: + ret = (tmp_rand < 60) ? BROWN : GREEN; + break; + + case ETC_SILVER: + ret = (tmp_rand < 90) ? LIGHTGREY : WHITE; + break; + + case ETC_GOLD: + ret = (tmp_rand < 60) ? YELLOW : BROWN; + break; + + case ETC_IRON: + ret = (tmp_rand < 40) ? CYAN : + (tmp_rand < 80) ? LIGHTGREY : + DARKGREY; + break; + + case ETC_BONE: + ret = (tmp_rand < 90) ? WHITE : LIGHTGREY; + break; + + case ETC_RANDOM: + ret = 1 + random2(15); // always random + break; + + case ETC_FLOOR: // should already be handled + case ETC_ROCK: // should already be handled + default: + break; + } + + ASSERT( !is_element_colour( ret ) ); + + return ((ret == BLACK) ? GREEN : ret); +} + +#ifdef USE_TILE +static std::string tile_cols[24] = +{ + "black", "darkgrey", "grey", "lightgrey", "white", + "blue", "lightblue", "darkblue", + "green", "lightgreen", "darkgreen", + "cyan", "lightcyan", "darkcyan", + "red", "lightred", "darkred", + "magenta", "lightmagenta", "darkmagenta", + "yellow", "lightyellow", "darkyellow", "brown" +}; + +unsigned int str_to_tile_colour(std::string colour) +{ + if (colour.empty()) + return (0); + + lowercase(colour); + + if (colour == "darkgray") + colour = "darkgrey"; + else if (colour == "gray") + colour = "grey"; + else if (colour == "lightgray") + colour = "lightgrey"; + + for (unsigned int i = 0; i < 24; i++) + { + if (tile_cols[i] == colour) + return (i); + } + return (0); +} +#endif + +const std::string cols[16] = +{ + "black", "blue", "green", "cyan", "red", "magenta", "brown", + "lightgrey", "darkgrey", "lightblue", "lightgreen", "lightcyan", + "lightred", "lightmagenta", "yellow", "white" +}; + +const std::string colour_to_str(unsigned char colour) +{ + if ( colour >= 16 ) + return "lightgrey"; + else + return cols[colour]; +} + +// Returns -1 if unmatched else returns 0-15. +int str_to_colour( const std::string &str, int default_colour, + bool accept_number ) +{ + int ret; + + static const std::string element_cols[] = + { + "fire", "ice", "earth", "electricity", "air", "poison", + "water", "magic", "mutagenic", "warp", "enchant", "heal", + "holy", "dark", "death", "necro", "unholy", "vehumet", + "beogh", "crystal", "blood", "smoke", "slime", "jewel", + "elven", "dwarven", "orcish", "gila", "floor", "rock", + "stone", "mist", "shimmer_blue", "decay", "silver", "gold", + "iron", "bone", "random" + }; + + ASSERT(ARRAYSZ(element_cols) == (ETC_RANDOM - ETC_FIRE) + 1); + for (ret = 0; ret < 16; ++ret) + { + if (str == cols[ret]) + break; + } + + // Check for alternate spellings. + if (ret == 16) + { + if (str == "lightgray") + ret = 7; + else if (str == "darkgray") + ret = 8; + } + + if (ret == 16) + { + // Maybe we have an element colour attribute. + for (unsigned i = 0; i < sizeof(element_cols) / sizeof(*element_cols); + ++i) + { + if (str == element_cols[i]) + { + // Ugh. + ret = element_type(ETC_FIRE + i); + break; + } + } + } + + if (ret == 16 && accept_number) + { + // Check if we have a direct colour index. + const char *s = str.c_str(); + char *es = NULL; + const int ci = static_cast(strtol(s, &es, 10)); + if (s != (const char *) es && es && ci >= 0 && ci < 16) + ret = ci; + } + + return ((ret == 16) ? default_colour : ret); +} diff --git a/crawl-ref/source/colour.h b/crawl-ref/source/colour.h new file mode 100644 index 0000000000..1d8acfe9a4 --- /dev/null +++ b/crawl-ref/source/colour.h @@ -0,0 +1,62 @@ +#ifndef COLOUR_H +#define COLOUR_H + +// various elemental colour schemes... used for abstracting random +// short lists. When adding colours, please also add their names in +// str_to_colour! +enum element_type +{ + ETC_FIRE = 32, // fiery colours (must be first and > highest colour) + ETC_ICE, // icy colours + ETC_EARTH, // earthy colours + ETC_ELECTRICITY, // electrical side of air + ETC_AIR, // non-electric and general air magic + ETC_POISON, // used only for venom mage and stalker stuff + ETC_WATER, // used only for the elemental + ETC_MAGIC, // general magical effect + ETC_MUTAGENIC, // transmute, poly, radiation effects + ETC_WARP, // teleportation and anything similar + ETC_ENCHANT, // magical enhancements + ETC_HEAL, // holy healing (not necromantic stuff) + ETC_HOLY, // general "good" god effects + ETC_DARK, // darkness + ETC_DEATH, // currently only assassin (and equal to ETC_NECRO) + ETC_NECRO, // necromancy stuff + ETC_UNHOLY, // demonology stuff + ETC_VEHUMET, // vehumet's oddball colours + ETC_BEOGH, // Beogh altar colours + ETC_CRYSTAL, // colours of crystal + ETC_BLOOD, // colours of blood + ETC_SMOKE, // colours of smoke + ETC_SLIME, // colours of slime + ETC_JEWEL, // colourful + ETC_ELVEN, // used for colouring elf fabric items + ETC_DWARVEN, // used for colouring dwarf fabric items + ETC_ORCISH, // used for colouring orc fabric items + ETC_GILA, // gila monster colours + ETC_FLOOR, // colour of the area's floor + ETC_ROCK, // colour of the area's rock + ETC_STONE, // colour of the area's stone + ETC_MIST, // colour of mist + ETC_SHIMMER_BLUE, // shimmering colours of blue. + ETC_DECAY, // colour of decay/swamp + ETC_SILVER, // colour of silver + ETC_GOLD, // colour of gold + ETC_IRON, // colour of iron + ETC_BONE, // colour of bone + ETC_RANDOM // any colour (except BLACK) +}; + +int str_to_colour(const std::string &str, int default_colour = -1, + bool accept_number = true); +const std::string colour_to_str(unsigned char colour); +unsigned int str_to_tile_colour(std::string colour); + +unsigned char random_colour(); +unsigned char random_uncommon_colour(); +unsigned char make_low_colour(unsigned char colour); +unsigned char make_high_colour(unsigned char colour); +bool is_element_colour(int col); +int element_colour(int element, bool no_random = false); + +#endif diff --git a/crawl-ref/source/command.cc b/crawl-ref/source/command.cc index e78e45e07e..94b0487508 100644 --- a/crawl-ref/source/command.cc +++ b/crawl-ref/source/command.cc @@ -14,12 +14,12 @@ #include #include "externs.h" -#include "species.h" #include "abl-show.h" #include "branch.h" #include "chardump.h" #include "cio.h" +#include "colour.h" #include "database.h" #include "debug.h" #include "decks.h" @@ -41,6 +41,7 @@ #include "quiver.h" #include "religion.h" #include "skills2.h" +#include "species.h" #include "spl-book.h" #include "spl-cast.h" #include "spl-util.h" diff --git a/crawl-ref/source/coord.cc b/crawl-ref/source/coord.cc new file mode 100644 index 0000000000..59a75cf888 --- /dev/null +++ b/crawl-ref/source/coord.cc @@ -0,0 +1,100 @@ +#include "AppHdr.h" + +#include "coord.h" + +#include "directn.h" +#include "random.h" +#include "state.h" + +////////////////////////////////////////////////////////////////////////// +// coord_def +int coord_def::distance_from(const coord_def &other) const +{ + return (grid_distance(*this, other)); +} + +int grid_distance( const coord_def& p1, const coord_def& p2 ) +{ + return grid_distance(p1.x, p1.y, p2.x, p2.y); +} + +// More accurate than distance() given the actual movement geometry -- bwr +int grid_distance( int x, int y, int x2, int y2 ) +{ + const int dx = abs( x - x2 ); + const int dy = abs( y - y2 ); + + // returns distance in terms of moves: + return ((dx > dy) ? dx : dy); +} + +int distance( const coord_def& p1, const coord_def& p2 ) +{ + return distance(p1.x, p1.y, p2.x, p2.y); +} + +int distance( int x, int y, int x2, int y2 ) +{ + //jmf: now accurate, but remember to only compare vs. pre-squared distances + // thus, next to == (distance(m1.x,m1.y, m2.x,m2.y) <= 2) + const int dx = x - x2; + const int dy = y - y2; + + return ((dx * dx) + (dy * dy)); +} + +bool adjacent( const coord_def& p1, const coord_def& p2 ) +{ + return grid_distance(p1, p2) <= 1; +} + +bool in_bounds_x(int x) +{ + return (x > X_BOUND_1 && x < X_BOUND_2); +} + +bool in_bounds_y(int y) +{ + return (y > Y_BOUND_1 && y < Y_BOUND_2); +} + +// Returns true if inside the area the player can move and dig (ie exclusive). +bool in_bounds(int x, int y) +{ + return (in_bounds_x(x) && in_bounds_y(y)); +} + +bool map_bounds_x(int x) +{ + return (x >= X_BOUND_1 && x <= X_BOUND_2); +} + +bool map_bounds_y(int y) +{ + return (y >= Y_BOUND_1 && y <= Y_BOUND_2); +} + +// Returns true if inside the area the player can map (ie inclusive). +// Note that terrain features should be in_bounds() leaving an outer +// ring of rock to frame the level. +bool map_bounds(int x, int y) +{ + return (map_bounds_x(x) && map_bounds_y(y)); +} + +coord_def random_in_bounds() +{ + if (crawl_state.arena) + { + const coord_def &ul = crawl_view.glos1; // Upper left + const coord_def &lr = crawl_view.glos2; // Lower right + + return coord_def(random_range(ul.x, lr.x - 1), + random_range(ul.y, lr.y - 1)); + } + else + return coord_def(random_range(MAPGEN_BORDER, GXM - MAPGEN_BORDER - 1), + random_range(MAPGEN_BORDER, GYM - MAPGEN_BORDER - 1)); +} + + diff --git a/crawl-ref/source/coord.h b/crawl-ref/source/coord.h new file mode 100644 index 0000000000..685718ca8e --- /dev/null +++ b/crawl-ref/source/coord.h @@ -0,0 +1,35 @@ +#ifndef COORD_H +#define COORD_H + +bool in_bounds_x(int x); +bool in_bounds_y(int y); +bool in_bounds(int x, int y); +bool map_bounds_x(int x); +bool map_bounds_y(int y); +bool map_bounds(int x, int y); +coord_def random_in_bounds(); + +inline bool in_bounds(const coord_def &p) +{ + return in_bounds(p.x, p.y); +} + +inline bool map_bounds(const coord_def &p) +{ + return map_bounds(p.x, p.y); +} + +// Determines if the coordinate is within bounds of an LOS array. +inline bool show_bounds(const coord_def &p) +{ + return (p.x >= 0 && p.x < ENV_SHOW_DIAMETER + && p.y >= 0 && p.y < ENV_SHOW_DIAMETER); +} + +int grid_distance( const coord_def& p1, const coord_def& p2 ); +int grid_distance( int x, int y, int x2, int y2 ); +int distance( const coord_def& p1, const coord_def& p2 ); +int distance( int x, int y, int x2, int y2); +bool adjacent( const coord_def& p1, const coord_def& p2 ); + +#endif diff --git a/crawl-ref/source/coordit.cc b/crawl-ref/source/coordit.cc new file mode 100644 index 0000000000..cfe817a192 --- /dev/null +++ b/crawl-ref/source/coordit.cc @@ -0,0 +1,189 @@ +/* + * File: coordit.h + * Summary: Coordinate iterators. + */ + +#include "AppHdr.h" + +#include "coordit.h" + +#include "coord.h" +#include "los.h" + +rectangle_iterator::rectangle_iterator( const coord_def& corner1, + const coord_def& corner2 ) +{ + topleft.x = std::min(corner1.x, corner2.x); + topleft.y = std::min(corner1.y, corner2.y); // not really necessary + bottomright.x = std::max(corner1.x, corner2.x); + bottomright.y = std::max(corner1.y, corner2.y); + current = topleft; +} + +rectangle_iterator::rectangle_iterator( int x_border_dist, int y_border_dist ) +{ + if (y_border_dist < 0) + y_border_dist = x_border_dist; + + topleft.set(x_border_dist, y_border_dist); + bottomright.set(GXM - x_border_dist - 1, GYM - y_border_dist - 1); + current = topleft; +} + +rectangle_iterator::operator bool() const +{ + return (current.y <= bottomright.y); +} + +coord_def rectangle_iterator::operator *() const +{ + return current; +} + +const coord_def* rectangle_iterator::operator->() const +{ + return ¤t; +} + +rectangle_iterator& rectangle_iterator::operator ++() +{ + if (current.x == bottomright.x) + { + current.x = topleft.x; + current.y++; + } + else + { + current.x++; + } + return *this; +} + +rectangle_iterator rectangle_iterator::operator++( int dummy ) +{ + const rectangle_iterator copy = *this; + ++(*this); + return (copy); +} + +radius_iterator::radius_iterator(const coord_def& _center, int _radius, + bool _roguelike_metric, bool _require_los, + bool _exclude_center, + const env_show_grid* _losgrid) + : center(_center), radius(_radius), roguelike_metric(_roguelike_metric), + require_los(_require_los), exclude_center(_exclude_center), + losgrid(_losgrid), iter_done(false) +{ + reset(); +} + +void radius_iterator::reset() +{ + iter_done = false; + + location.x = center.x - radius; + location.y = center.y - radius; + + if ( !this->on_valid_square() ) + ++(*this); +} + +bool radius_iterator::done() const +{ + return iter_done; +} + +coord_def radius_iterator::operator *() const +{ + return location; +} + +const coord_def* radius_iterator::operator->() const +{ + return &location; +} + +void radius_iterator::step() +{ + const int minx = std::max(X_BOUND_1+1, center.x - radius); + const int maxx = std::min(X_BOUND_2-1, center.x + radius); + const int maxy = std::min(Y_BOUND_2-1, center.y + radius); + + // Sweep L-R, U-D + location.x++; + if (location.x > maxx) + { + location.x = minx; + location.y++; + if (location.y > maxy) + iter_done = true; + } +} + +void radius_iterator::step_back() +{ + const int minx = std::max(X_BOUND_1+1, center.x - radius); + const int maxx = std::min(X_BOUND_2-1, center.x + radius); + const int miny = std::max(Y_BOUND_1+1, center.y - radius); + + location.x--; + if (location.x < minx) + { + location.x = maxx; + location.y--; + if (location.y < miny) + iter_done = true; + } +} + +bool radius_iterator::on_valid_square() const +{ + if (!in_bounds(location)) + return (false); + if (!roguelike_metric && (location - center).abs() > radius*radius) + return (false); + if (require_los) + { + if (!losgrid && !see_cell(location)) + return (false); + if (losgrid && !see_cell(*losgrid, center, location)) + return (false); + } + if (exclude_center && location == center) + return (false); + + return (true); +} + +const radius_iterator& radius_iterator::operator++() +{ + do + this->step(); + while (!this->done() && !this->on_valid_square()); + + return (*this); +} + +const radius_iterator& radius_iterator::operator--() +{ + do + this->step_back(); + while (!this->done() && !this->on_valid_square()); + + return (*this); +} + +radius_iterator radius_iterator::operator++(int dummy) +{ + const radius_iterator copy = *this; + ++(*this); + return (copy); +} + +radius_iterator radius_iterator::operator--(int dummy) +{ + const radius_iterator copy = *this; + --(*this); + return (copy); +} + diff --git a/crawl-ref/source/coordit.h b/crawl-ref/source/coordit.h new file mode 100644 index 0000000000..b7323bbef8 --- /dev/null +++ b/crawl-ref/source/coordit.h @@ -0,0 +1,62 @@ +#ifndef COORDIT_H +#define COORDIT_H + +#include "player.h" + +class rectangle_iterator : + public std::iterator +{ +public: + rectangle_iterator( const coord_def& corner1, const coord_def& corner2 ); + explicit rectangle_iterator( int x_border_dist, int y_border_dist = -1 ); + operator bool() const; + coord_def operator *() const; + const coord_def* operator->() const; + + rectangle_iterator& operator ++ (); + rectangle_iterator operator ++ (int); +private: + coord_def current, topleft, bottomright; +}; + +class radius_iterator : public std::iterator +{ +public: + radius_iterator( const coord_def& center, int radius, + bool roguelike_metric = true, + bool require_los = true, + bool exclude_center = false, + const env_show_grid* losgrid = NULL ); + bool done() const; + void reset(); + operator bool() const { return !done(); } + coord_def operator *() const; + const coord_def* operator->() const; + + const radius_iterator& operator ++ (); + const radius_iterator& operator -- (); + radius_iterator operator ++ (int); + radius_iterator operator -- (int); +private: + void step(); + void step_back(); + bool on_valid_square() const; + + coord_def location, center; + int radius; + bool roguelike_metric, require_los, exclude_center; + const env_show_grid* losgrid; + bool iter_done; +}; + +class adjacent_iterator : public radius_iterator +{ +public: + explicit adjacent_iterator( const coord_def& pos = you.pos(), + bool _exclude_center = true ) : + radius_iterator(pos, 1, true, false, _exclude_center) {} +}; + +#endif + diff --git a/crawl-ref/source/database.cc b/crawl-ref/source/database.cc index 4d20bd5295..34cb1cb73a 100644 --- a/crawl-ref/source/database.cc +++ b/crawl-ref/source/database.cc @@ -21,6 +21,7 @@ #include "database.h" #include "files.h" #include "libutil.h" +#include "random.h" #include "stuff.h" // TextDB handles dependency checking the db vs text files, creating the diff --git a/crawl-ref/source/delay.cc b/crawl-ref/source/delay.cc index 8899683e69..f8e6fbfbad 100644 --- a/crawl-ref/source/delay.cc +++ b/crawl-ref/source/delay.cc @@ -14,6 +14,7 @@ #include "artefact.h" #include "clua.h" #include "command.h" +#include "coordit.h" #include "database.h" #include "delay.h" #include "describe.h" @@ -35,6 +36,7 @@ #include "ouch.h" #include "output.h" #include "player.h" +#include "random.h" #include "religion.h" #include "spells4.h" #include "spl-util.h" diff --git a/crawl-ref/source/describe.cc b/crawl-ref/source/describe.cc index f4db962376..6cb542d5d6 100644 --- a/crawl-ref/source/describe.cc +++ b/crawl-ref/source/describe.cc @@ -45,7 +45,9 @@ #include "newgame.h" #include "jobs.h" #include "player.h" +#include "random.h" #include "religion.h" +#include "rng.h" #include "skills2.h" #include "spells3.h" #include "spl-book.h" diff --git a/crawl-ref/source/dgnevent.cc b/crawl-ref/source/dgnevent.cc index 543d50e6f5..451813ea6f 100644 --- a/crawl-ref/source/dgnevent.cc +++ b/crawl-ref/source/dgnevent.cc @@ -7,7 +7,8 @@ #include "AppHdr.h" #include "dgnevent.h" -#include "stuff.h" + +#include "coord.h" #include dgn_event_dispatcher dungeon_events; diff --git a/crawl-ref/source/directn.cc b/crawl-ref/source/directn.cc index a9e783856e..fc161caa83 100644 --- a/crawl-ref/source/directn.cc +++ b/crawl-ref/source/directn.cc @@ -24,7 +24,9 @@ #include "cio.h" #include "cloud.h" +#include "colour.h" #include "command.h" +#include "coord.h" #include "debug.h" #include "describe.h" #include "dungeon.h" diff --git a/crawl-ref/source/dungeon.cc b/crawl-ref/source/dungeon.cc index 16d55cded1..fcbcf9fb82 100644 --- a/crawl-ref/source/dungeon.cc +++ b/crawl-ref/source/dungeon.cc @@ -42,6 +42,7 @@ #include "notes.h" #include "place.h" #include "player.h" +#include "random.h" #include "religion.h" #include "spells3.h" #include "state.h" diff --git a/crawl-ref/source/effects.cc b/crawl-ref/source/effects.cc index cb04f73c17..9c803996b8 100644 --- a/crawl-ref/source/effects.cc +++ b/crawl-ref/source/effects.cc @@ -21,6 +21,7 @@ #include "artefact.h" #include "beam.h" #include "cloud.h" +#include "colour.h" #include "decks.h" #include "delay.h" #include "directn.h" diff --git a/crawl-ref/source/exclude.cc b/crawl-ref/source/exclude.cc index 03a9be166e..bafc2a738e 100644 --- a/crawl-ref/source/exclude.cc +++ b/crawl-ref/source/exclude.cc @@ -7,6 +7,7 @@ #include "exclude.h" +#include "coord.h" #include "mon-util.h" #include "overmap.h" #include "stuff.h" diff --git a/crawl-ref/source/externs.h b/crawl-ref/source/externs.h index 425f263d21..53a4ce2f9d 100644 --- a/crawl-ref/source/externs.h +++ b/crawl-ref/source/externs.h @@ -231,15 +231,6 @@ struct coord_def typedef bool (*coord_predicate)(const coord_def &c); -struct dice_def -{ - int num; - int size; - - dice_def( int n = 0, int s = 0 ) : num(n), size(s) {} - int roll() const; -}; - struct run_check_dir { dungeon_feature_type grid; diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index c27174835e..cb92d19843 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -68,6 +68,7 @@ #include "overmap.h" #include "place.h" #include "player.h" +#include "random.h" #include "stash.h" #include "state.h" #include "stuff.h" diff --git a/crawl-ref/source/food.cc b/crawl-ref/source/food.cc index b921a1d861..4560d9fc23 100644 --- a/crawl-ref/source/food.cc +++ b/crawl-ref/source/food.cc @@ -40,6 +40,7 @@ #include "mutation.h" #include "output.h" #include "player.h" +#include "random.h" #include "religion.h" #include "skills2.h" #include "spells2.h" diff --git a/crawl-ref/source/format.cc b/crawl-ref/source/format.cc index 14571b6f55..c982501178 100644 --- a/crawl-ref/source/format.cc +++ b/crawl-ref/source/format.cc @@ -5,7 +5,7 @@ #include "AppHdr.h" -#include "initfile.h" +#include "colour.h" #include "format.h" #include "view.h" diff --git a/crawl-ref/source/ghost.cc b/crawl-ref/source/ghost.cc index a27c18254c..72f9d8733e 100644 --- a/crawl-ref/source/ghost.cc +++ b/crawl-ref/source/ghost.cc @@ -11,16 +11,18 @@ #include "ghost.h" #include "artefact.h" +#include "colour.h" #include "externs.h" #include "itemname.h" #include "itemprop.h" #include "ng-input.h" +#include "random.h" #include "skills2.h" -#include "stuff.h" #include "mtransit.h" #include "place.h" #include "player.h" #include "religion.h" + #include #define MAX_GHOST_DAMAGE 50 diff --git a/crawl-ref/source/godabil.cc b/crawl-ref/source/godabil.cc index cb6499325d..23df45d6ae 100644 --- a/crawl-ref/source/godabil.cc +++ b/crawl-ref/source/godabil.cc @@ -6,6 +6,9 @@ #include "AppHdr.h" #include "cloud.h" +#include "colour.h" +#include "coord.h" +#include "coordit.h" #include "database.h" #include "effects.h" #include "files.h" @@ -19,6 +22,7 @@ #include "monstuff.h" #include "mstuff2.h" #include "mutation.h" +#include "random.h" #include "religion.h" #include "shopping.h" #include "spells1.h" diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index a36763ce4c..34a9b0fc3a 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -15,6 +15,7 @@ #include "chardump.h" #include "clua.h" +#include "colour.h" #include "dlua.h" #include "delay.h" #include "directn.h" @@ -81,118 +82,6 @@ god_type str_to_god(std::string god) return (GOD_NO_GOD); } -#ifdef USE_TILE -static std::string tile_cols[24] = -{ - "black", "darkgrey", "grey", "lightgrey", "white", - "blue", "lightblue", "darkblue", - "green", "lightgreen", "darkgreen", - "cyan", "lightcyan", "darkcyan", - "red", "lightred", "darkred", - "magenta", "lightmagenta", "darkmagenta", - "yellow", "lightyellow", "darkyellow", "brown" -}; - -static unsigned int _str_to_tile_colour(std::string colour) -{ - if (colour.empty()) - return (0); - - lowercase(colour); - - if (colour == "darkgray") - colour = "darkgrey"; - else if (colour == "gray") - colour = "grey"; - else if (colour == "lightgray") - colour = "lightgrey"; - - for (unsigned int i = 0; i < 24; i++) - { - if (tile_cols[i] == colour) - return (i); - } - return (0); -} -#endif - -const std::string cols[16] = -{ - "black", "blue", "green", "cyan", "red", "magenta", "brown", - "lightgrey", "darkgrey", "lightblue", "lightgreen", "lightcyan", - "lightred", "lightmagenta", "yellow", "white" -}; - -const std::string colour_to_str(unsigned char colour) -{ - if ( colour >= 16 ) - return "lightgrey"; - else - return cols[colour]; -} - -// Returns -1 if unmatched else returns 0-15. -int str_to_colour( const std::string &str, int default_colour, - bool accept_number ) -{ - int ret; - - static const std::string element_cols[] = - { - "fire", "ice", "earth", "electricity", "air", "poison", - "water", "magic", "mutagenic", "warp", "enchant", "heal", - "holy", "dark", "death", "necro", "unholy", "vehumet", - "beogh", "crystal", "blood", "smoke", "slime", "jewel", - "elven", "dwarven", "orcish", "gila", "floor", "rock", - "stone", "mist", "shimmer_blue", "decay", "silver", "gold", - "iron", "bone", "random" - }; - - ASSERT(ARRAYSZ(element_cols) == (ETC_RANDOM - ETC_FIRE) + 1); - - for (ret = 0; ret < 16; ++ret) - { - if (str == cols[ret]) - break; - } - - // Check for alternate spellings. - if (ret == 16) - { - if (str == "lightgray") - ret = 7; - else if (str == "darkgray") - ret = 8; - } - - if (ret == 16) - { - // Maybe we have an element colour attribute. - for (unsigned i = 0; i < sizeof(element_cols) / sizeof(*element_cols); - ++i) - { - if (str == element_cols[i]) - { - // Ugh. - ret = element_type(ETC_FIRE + i); - break; - } - } - } - - if (ret == 16 && accept_number) - { - // Check if we have a direct colour index. - const char *s = str.c_str(); - char *es = NULL; - const int ci = static_cast(strtol(s, &es, 10)); - if (s != (const char *) es && es && ci >= 0 && ci < 16) - ret = ci; - } - - return ((ret == 16) ? default_colour : ret); -} - // Returns -1 if unmatched else returns 0-15. static int _str_to_channel_colour( const std::string &str ) { @@ -3165,102 +3054,102 @@ void game_options::read_option_line(const std::string &str, bool runscript) else if (key == "tile_player_col") { tile_player_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_monster_col") { tile_monster_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_neutral_col") { tile_neutral_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_friendly_col") { tile_friendly_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_plant_col") { tile_plant_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_item_col") { tile_item_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_unseen_col") { tile_unseen_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_floor_col") { tile_floor_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_wall_col") { tile_wall_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_mapped_wall_col") { tile_mapped_wall_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_door_col") { tile_door_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_downstairs_col") { tile_downstairs_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_upstairs_col") { tile_upstairs_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_feature_col") { tile_feature_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_trap_col") { tile_trap_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_water_col") { tile_water_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_lava_col") { tile_lava_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_excluded_col") { tile_excluded_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_excl_centre_col" || key == "tile_excl_center_col") { tile_excl_centre_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_window_col") { tile_window_col = - _str_to_tile_colour(field); + str_to_tile_colour(field); } else if (key == "tile_font_crt_file") { diff --git a/crawl-ref/source/initfile.h b/crawl-ref/source/initfile.h index 84e29ffe5c..cf10cbb4bf 100644 --- a/crawl-ref/source/initfile.h +++ b/crawl-ref/source/initfile.h @@ -20,10 +20,7 @@ enum drop_mode_type }; god_type str_to_god(std::string god); -int str_to_colour(const std::string &str, int default_colour = -1, - bool accept_number = true); int str_to_fprop (const std::string &str); -const std::string colour_to_str(unsigned char colour); // last updated 12may2000 {dlb} /* *********************************************************************** diff --git a/crawl-ref/source/invent.cc b/crawl-ref/source/invent.cc index 2748df3756..4579ad699f 100644 --- a/crawl-ref/source/invent.cc +++ b/crawl-ref/source/invent.cc @@ -22,6 +22,7 @@ #include "artefact.h" #include "clua.h" +#include "colour.h" #include "decks.h" #include "describe.h" #include "food.h" diff --git a/crawl-ref/source/item_use.cc b/crawl-ref/source/item_use.cc index c494766aeb..000be243f2 100644 --- a/crawl-ref/source/item_use.cc +++ b/crawl-ref/source/item_use.cc @@ -20,6 +20,7 @@ #include "beam.h" #include "cio.h" #include "cloud.h" +#include "colour.h" #include "command.h" #include "debug.h" #include "decks.h" diff --git a/crawl-ref/source/itemname.cc b/crawl-ref/source/itemname.cc index 913cc8ffaf..db5f873da3 100644 --- a/crawl-ref/source/itemname.cc +++ b/crawl-ref/source/itemname.cc @@ -21,6 +21,7 @@ #include "externs.h" #include "artefact.h" +#include "colour.h" #include "decks.h" #include "food.h" #include "goditem.h" diff --git a/crawl-ref/source/itemprop.cc b/crawl-ref/source/itemprop.cc index 07ae4fc5ca..04854a0fd0 100644 --- a/crawl-ref/source/itemprop.cc +++ b/crawl-ref/source/itemprop.cc @@ -31,6 +31,7 @@ #include "notes.h" #include "player.h" #include "quiver.h" +#include "random.h" #include "stuff.h" #include "transfor.h" #include "xom.h" diff --git a/crawl-ref/source/l_crawl.cc b/crawl-ref/source/l_crawl.cc index 257833844c..d7571a63e7 100644 --- a/crawl-ref/source/l_crawl.cc +++ b/crawl-ref/source/l_crawl.cc @@ -20,6 +20,7 @@ #include "message.h" #include "notes.h" #include "player.h" +#include "random.h" #include "stuff.h" #include "view.h" diff --git a/crawl-ref/source/l_dgn.cc b/crawl-ref/source/l_dgn.cc index ebb7cb598c..b8f9198721 100644 --- a/crawl-ref/source/l_dgn.cc +++ b/crawl-ref/source/l_dgn.cc @@ -7,10 +7,13 @@ #include "branch.h" #include "cloud.h" +#include "colour.h" +#include "coord.h" #include "l_defs.h" #include "initfile.h" #include "mapmark.h" #include "maps.h" +#include "random.h" #include "spl-util.h" #include "view.h" diff --git a/crawl-ref/source/l_dgn_bf.cc b/crawl-ref/source/l_dgn_bf.cc index c7c633f0ea..1d9ee8e5b0 100644 --- a/crawl-ref/source/l_dgn_bf.cc +++ b/crawl-ref/source/l_dgn_bf.cc @@ -7,8 +7,8 @@ #include "cluautil.h" #include "l_libs.h" - #include "mapdef.h" +#include "random.h" // Return the integer stored in the table (on the stack) with the key name. // If the key doesn't exist or the value is the wrong type, return defval. diff --git a/crawl-ref/source/l_dgnbld.cc b/crawl-ref/source/l_dgnbld.cc index 7f84b5db1b..6a3d0da004 100644 --- a/crawl-ref/source/l_dgnbld.cc +++ b/crawl-ref/source/l_dgnbld.cc @@ -10,7 +10,9 @@ #include "cluautil.h" #include "l_libs.h" +#include "coord.h" #include "dungeon.h" +#include "random.h" // Return a metatable for a point on the map_lines grid. static int dgn_mapgrd_table(lua_State *ls) diff --git a/crawl-ref/source/l_dgngrd.cc b/crawl-ref/source/l_dgngrd.cc index 1a1af10863..b8f1181fa0 100644 --- a/crawl-ref/source/l_dgngrd.cc +++ b/crawl-ref/source/l_dgngrd.cc @@ -9,8 +9,9 @@ #include "cluautil.h" #include "l_libs.h" +#include "coord.h" #include "directn.h" -#include "stuff.h" +#include "env.h" #include "terrain.h" const char *dngn_feature_names[] = diff --git a/crawl-ref/source/l_dgnit.cc b/crawl-ref/source/l_dgnit.cc index 4ed148940f..6bb166a836 100644 --- a/crawl-ref/source/l_dgnit.cc +++ b/crawl-ref/source/l_dgnit.cc @@ -8,6 +8,7 @@ #include "cluautil.h" #include "l_libs.h" +#include "coord.h" #include "dungeon.h" #include "items.h" #include "mapdef.h" diff --git a/crawl-ref/source/l_item.cc b/crawl-ref/source/l_item.cc index e9c0048562..9de2384eac 100644 --- a/crawl-ref/source/l_item.cc +++ b/crawl-ref/source/l_item.cc @@ -6,6 +6,7 @@ #include "l_libs.h" #include "artefact.h" +#include "colour.h" #include "command.h" #include "initfile.h" #include "invent.h" diff --git a/crawl-ref/source/l_los.cc b/crawl-ref/source/l_los.cc index a3d6141a3b..40a5c6a5f1 100644 --- a/crawl-ref/source/l_los.cc +++ b/crawl-ref/source/l_los.cc @@ -9,6 +9,7 @@ #include "l_libs.h" #include "cluautil.h" +#include "coord.h" #include "los.h" #include "ray.h" #include "stuff.h" diff --git a/crawl-ref/source/l_you.cc b/crawl-ref/source/l_you.cc index 79d4295b63..d3b615c9ab 100644 --- a/crawl-ref/source/l_you.cc +++ b/crawl-ref/source/l_you.cc @@ -5,6 +5,7 @@ #include "abl-show.h" #include "chardump.h" +#include "coord.h" #include "delay.h" #include "food.h" #include "initfile.h" diff --git a/crawl-ref/source/lev-pand.cc b/crawl-ref/source/lev-pand.cc index fe4c4fddd4..827e753cd1 100644 --- a/crawl-ref/source/lev-pand.cc +++ b/crawl-ref/source/lev-pand.cc @@ -14,7 +14,7 @@ #include "dungeon.h" #include "monplace.h" #include "mon-pick.h" -#include "stuff.h" +#include "random.h" void init_pandemonium(void) { diff --git a/crawl-ref/source/los.cc b/crawl-ref/source/los.cc index e4a1d6429e..513e2fb398 100644 --- a/crawl-ref/source/los.cc +++ b/crawl-ref/source/los.cc @@ -48,6 +48,8 @@ #include #include "bitary.h" +#include "coord.h" +#include "coordit.h" #include "debug.h" #include "directn.h" #include "externs.h" diff --git a/crawl-ref/source/makefile.obj b/crawl-ref/source/makefile.obj index 5b7d04cc1c..4b1bada8b0 100644 --- a/crawl-ref/source/makefile.obj +++ b/crawl-ref/source/makefile.obj @@ -13,7 +13,10 @@ cio.o \ cloud.o \ clua.o \ cluautil.o \ +colour.o \ command.o \ +coord.o \ +coordit.o \ ctest.o \ database.o \ debug.o \ @@ -102,8 +105,10 @@ overmap.o \ place.o \ player.o \ quiver.o \ +random.o \ ray.o \ religion.o \ +rng.o \ sha256.o \ shopping.o \ skills.o \ diff --git a/crawl-ref/source/makeitem.cc b/crawl-ref/source/makeitem.cc index 499f3e6522..77dc33ddd5 100644 --- a/crawl-ref/source/makeitem.cc +++ b/crawl-ref/source/makeitem.cc @@ -13,6 +13,8 @@ #include "makeitem.h" #include "artefact.h" +#include "colour.h" +#include "coord.h" #include "decks.h" #include "describe.h" #include "dungeon.h" @@ -22,8 +24,8 @@ #include "misc.h" #include "mon-util.h" #include "player.h" +#include "random.h" #include "spl-book.h" -#include "stuff.h" #include "travel.h" #include "view.h" diff --git a/crawl-ref/source/mapdef.cc b/crawl-ref/source/mapdef.cc index e05d021a99..713c3bce9f 100644 --- a/crawl-ref/source/mapdef.cc +++ b/crawl-ref/source/mapdef.cc @@ -15,6 +15,8 @@ #include "artefact.h" #include "branch.h" +#include "colour.h" +#include "coord.h" #include "describe.h" #include "directn.h" #include "dungeon.h" @@ -31,6 +33,7 @@ #include "monplace.h" #include "mon-util.h" #include "place.h" +#include "random.h" #include "religion.h" #include "stuff.h" #include "tags.h" diff --git a/crawl-ref/source/mapdef.h b/crawl-ref/source/mapdef.h index 8994a3faba..1ae98bf10e 100644 --- a/crawl-ref/source/mapdef.h +++ b/crawl-ref/source/mapdef.h @@ -15,6 +15,7 @@ #include #include +#include "coordit.h" #include "dlua.h" #include "enum.h" #include "externs.h" diff --git a/crawl-ref/source/maps.cc b/crawl-ref/source/maps.cc index 02b2456c51..6540c05401 100644 --- a/crawl-ref/source/maps.cc +++ b/crawl-ref/source/maps.cc @@ -23,8 +23,8 @@ #include "message.h" #include "monplace.h" #include "mapdef.h" +#include "random.h" #include "state.h" -#include "stuff.h" #include "terrain.h" static bool _safe_vault_place(const map_def &md, diff --git a/crawl-ref/source/menu.cc b/crawl-ref/source/menu.cc index 6ca1498c2f..b5dd4e3146 100644 --- a/crawl-ref/source/menu.cc +++ b/crawl-ref/source/menu.cc @@ -12,20 +12,19 @@ #include "menu.h" #include "macro.h" #include "message.h" +#include "player.h" +#include "tutorial.h" +#include "view.h" + #ifdef USE_TILE + #include "coord.h" #include "monstuff.h" #include "mon-util.h" #include "newgame.h" -#endif -#include "player.h" -#ifdef USE_TILE - #include "stuff.h" #include "terrain.h" #include "tiles.h" #include "travel.h" #endif -#include "tutorial.h" -#include "view.h" MenuDisplay::MenuDisplay(Menu *menu) : m_menu(menu) { diff --git a/crawl-ref/source/message.cc b/crawl-ref/source/message.cc index 72bb7b560d..c336b5e24e 100644 --- a/crawl-ref/source/message.cc +++ b/crawl-ref/source/message.cc @@ -20,6 +20,7 @@ #include "externs.h" #include "cio.h" +#include "colour.h" #include "delay.h" #include "initfile.h" #include "libutil.h" @@ -27,6 +28,7 @@ #include "message.h" #include "monstuff.h" #include "notes.h" +#include "random.h" #include "religion.h" #include "stash.h" #include "state.h" diff --git a/crawl-ref/source/mgrow.cc b/crawl-ref/source/mgrow.cc index 777aba38b9..6334387a82 100644 --- a/crawl-ref/source/mgrow.cc +++ b/crawl-ref/source/mgrow.cc @@ -10,7 +10,7 @@ #include "mgrow.h" #include "mon-util.h" #include "monstuff.h" -#include "stuff.h" +#include "random.h" // Base experience required by a monster to reach HD 1. const int monster_xp_base = 15; diff --git a/crawl-ref/source/misc.cc b/crawl-ref/source/misc.cc index acb1c47807..f357884804 100644 --- a/crawl-ref/source/misc.cc +++ b/crawl-ref/source/misc.cc @@ -62,6 +62,7 @@ #include "overmap.h" #include "place.h" #include "player.h" +#include "random.h" #include "religion.h" #include "shopping.h" #include "skills.h" diff --git a/crawl-ref/source/mon-util.cc b/crawl-ref/source/mon-util.cc index 905150c757..7e8785814b 100644 --- a/crawl-ref/source/mon-util.cc +++ b/crawl-ref/source/mon-util.cc @@ -26,6 +26,7 @@ #include "artefact.h" #include "beam.h" #include "cloud.h" +#include "colour.h" #include "database.h" #include "debug.h" #include "delay.h" @@ -45,6 +46,7 @@ #include "mstuff2.h" #include "mtransit.h" #include "player.h" +#include "random.h" #include "religion.h" #include "shopping.h" // for item values #include "spells3.h" diff --git a/crawl-ref/source/monplace.cc b/crawl-ref/source/monplace.cc index e64e04c0b2..be27711eef 100644 --- a/crawl-ref/source/monplace.cc +++ b/crawl-ref/source/monplace.cc @@ -23,6 +23,7 @@ #include "mon-pick.h" #include "mon-util.h" #include "player.h" +#include "random.h" #include "religion.h" #include "state.h" #include "stuff.h" diff --git a/crawl-ref/source/monplace.h b/crawl-ref/source/monplace.h index 61f9c8b630..eb30a97db2 100644 --- a/crawl-ref/source/monplace.h +++ b/crawl-ref/source/monplace.h @@ -8,6 +8,7 @@ #ifndef MONPLACE_H #define MONPLACE_H +#include "coord.h" #include "enum.h" #include "dungeon.h" #include "fixvec.h" diff --git a/crawl-ref/source/monstuff.cc b/crawl-ref/source/monstuff.cc index 5977a64d7c..c6dcc55c7b 100644 --- a/crawl-ref/source/monstuff.cc +++ b/crawl-ref/source/monstuff.cc @@ -22,6 +22,7 @@ #include "artefact.h" #include "beam.h" #include "cloud.h" +#include "colour.h" #include "database.h" #include "debug.h" #include "delay.h" diff --git a/crawl-ref/source/mstuff2.cc b/crawl-ref/source/mstuff2.cc index 7d16255e7f..2c6bc1fe7d 100644 --- a/crawl-ref/source/mstuff2.cc +++ b/crawl-ref/source/mstuff2.cc @@ -19,6 +19,7 @@ #include "artefact.h" #include "beam.h" #include "cloud.h" +#include "colour.h" #include "database.h" #include "debug.h" #include "delay.h" diff --git a/crawl-ref/source/mtransit.cc b/crawl-ref/source/mtransit.cc index 81a1bc1326..a743278398 100644 --- a/crawl-ref/source/mtransit.cc +++ b/crawl-ref/source/mtransit.cc @@ -15,7 +15,7 @@ #include "items.h" #include "monplace.h" #include "mon-util.h" -#include "stuff.h" +#include "random.h" #define MAX_LOST 100 diff --git a/crawl-ref/source/mutation.cc b/crawl-ref/source/mutation.cc index 466d892e2a..68dcf7f71a 100644 --- a/crawl-ref/source/mutation.cc +++ b/crawl-ref/source/mutation.cc @@ -38,8 +38,8 @@ #include "ouch.h" #include "player.h" #include "religion.h" +#include "random.h" #include "skills2.h" -#include "stuff.h" #include "transfor.h" #include "tutorial.h" #include "view.h" diff --git a/crawl-ref/source/newgame.cc b/crawl-ref/source/newgame.cc index df214075d7..bf38109359 100644 --- a/crawl-ref/source/newgame.cc +++ b/crawl-ref/source/newgame.cc @@ -52,6 +52,7 @@ #include "mutation.h" #include "misc.h" #include "player.h" +#include "random.h" #include "religion.h" #include "skills.h" #include "skills2.h" diff --git a/crawl-ref/source/ng-init.cc b/crawl-ref/source/ng-init.cc index f8b7b9c9ef..446c95af1a 100644 --- a/crawl-ref/source/ng-init.cc +++ b/crawl-ref/source/ng-init.cc @@ -12,7 +12,7 @@ #include "describe.h" #include "itemname.h" #include "player.h" -#include "stuff.h" +#include "random.h" static unsigned char _random_potion_description() { diff --git a/crawl-ref/source/ouch.cc b/crawl-ref/source/ouch.cc index 21bba6736e..5fd554d40f 100644 --- a/crawl-ref/source/ouch.cc +++ b/crawl-ref/source/ouch.cc @@ -55,6 +55,7 @@ #include "notes.h" #include "output.h" #include "player.h" +#include "random.h" #include "religion.h" #include "shopping.h" #include "skills2.h" diff --git a/crawl-ref/source/output.cc b/crawl-ref/source/output.cc index 3632b6bd79..6914326466 100644 --- a/crawl-ref/source/output.cc +++ b/crawl-ref/source/output.cc @@ -21,6 +21,8 @@ #include "abl-show.h" #include "branch.h" #include "cio.h" +#include "colour.h" +#include "coord.h" #include "describe.h" #include "directn.h" #include "format.h" diff --git a/crawl-ref/source/overmap.cc b/crawl-ref/source/overmap.cc index 2a2ae64f63..523989d022 100644 --- a/crawl-ref/source/overmap.cc +++ b/crawl-ref/source/overmap.cc @@ -20,6 +20,8 @@ #include "branch.h" #include "cio.h" +#include "colour.h" +#include "coord.h" #include "dgnevent.h" #include "directn.h" #include "dungeon.h" diff --git a/crawl-ref/source/player.cc b/crawl-ref/source/player.cc index 6c7d146ab5..272eceb391 100644 --- a/crawl-ref/source/player.cc +++ b/crawl-ref/source/player.cc @@ -24,6 +24,8 @@ #include "branch.h" #include "cloud.h" #include "clua.h" +#include "coord.h" +#include "coordit.h" #include "delay.h" #include "dgnevent.h" #include "effects.h" @@ -47,6 +49,7 @@ #include "ouch.h" #include "output.h" #include "quiver.h" +#include "random.h" #include "religion.h" #include "shopping.h" #include "skills.h" diff --git a/crawl-ref/source/random.cc b/crawl-ref/source/random.cc new file mode 100644 index 0000000000..0ea2d12a59 --- /dev/null +++ b/crawl-ref/source/random.cc @@ -0,0 +1,263 @@ +#include "AppHdr.h" + +#include "random.h" + +int random_range(int low, int high) +{ + ASSERT(low <= high); + return (low + random2(high - low + 1)); +} + +int random_range(int low, int high, int nrolls) +{ + ASSERT(nrolls > 0); + int sum = 0; + for (int i = 0; i < nrolls; ++i) + sum += random_range(low, high); + return (sum / nrolls); +} + +// Chooses one of the numbers passed in at random. The list of numbers +// must be terminated with -1. +int random_choose(int first, ...) +{ + va_list args; + va_start(args, first); + + int chosen = first, count = 1, nargs = 100; + + while (nargs-- > 0) + { + const int pick = va_arg(args, int); + if (pick == -1) + break; + if (one_chance_in(++count)) + chosen = pick; + } + + ASSERT(nargs > 0); + + va_end(args); + return (chosen); +} + +// Chooses one of the strings passed in at random. The list of strings +// must be terminated with NULL. +const char* random_choose_string(const char* first, ...) +{ + va_list args; + va_start(args, first); + + const char* chosen = first; + int count = 1, nargs = 100; + + while (nargs-- > 0) + { + char* pick = va_arg(args, char*); + if (pick == NULL) + break; + if (one_chance_in(++count)) + chosen = pick; + } + + ASSERT(nargs > 0); + + va_end(args); + return (chosen); +} + +int random_choose_weighted(int weight, int first, ...) +{ + va_list args; + va_start(args, first); + + int chosen = first, cweight = weight, nargs = 100; + + while (nargs-- > 0) + { + const int nweight = va_arg(args, int); + if (!nweight) + break; + + const int choice = va_arg(args, int); + if (random2(cweight += nweight) < nweight) + chosen = choice; + } + + ASSERT(nargs > 0); + + va_end(args); + return (chosen); +} + +int random2(int max) +{ + if (max <= 1) + return (0); + + return (static_cast(random_int() / (0xFFFFFFFFUL / max + 1))); +} + +bool coinflip(void) +{ + return (static_cast(random2(2))); +} + +// Returns random2(x) if random_factor is true, otherwise the mean. +int maybe_random2(int x, bool random_factor) +{ + if (random_factor) + return (random2(x)); + else + return (x / 2); +} + +int roll_dice(int num, int size) +{ + int ret = 0; + int i; + + // If num <= 0 or size <= 0, then we'll just return the default + // value of zero. This is good behaviour in that it will be + // appropriate for calculated values that might be passed in. + if (num > 0 && size > 0) + { + ret += num; // since random2() is zero based + + for (i = 0; i < num; i++) + ret += random2(size); + } + + return (ret); +} + +int dice_def::roll() const +{ + return roll_dice(this->num, this->size); +} + +dice_def calc_dice(int num_dice, int max_damage) +{ + dice_def ret(num_dice, 0); + + if (num_dice <= 1) + { + ret.num = 1; + ret.size = max_damage; + } + else if (max_damage <= num_dice) + { + ret.num = max_damage; + ret.size = 1; + } + else + { + // Divide the damage among the dice, and add one + // occasionally to make up for the fractions. -- bwr + ret.size = max_damage / num_dice; + ret.size += x_chance_in_y(max_damage % num_dice, num_dice); + } + + return (ret); +} + +// Attempts to make missile weapons nicer to the player by reducing the +// extreme variance in damage done. +void scale_dice(dice_def &dice, int threshold) +{ + while (dice.size > threshold) + { + dice.num *= 2; + // If it's an odd number, lose one; this is more than + // compensated by the increase in number of dice. + dice.size /= 2; + } +} + +// Calculates num/den and randomly adds one based on the remainder. +int div_rand_round(int num, int den) +{ + return (num / den + (random2(den) < num % den)); +} + +int bestroll(int max, int rolls) +{ + int best = 0; + + for (int i = 0; i < rolls; i++) + { + int curr = random2(max); + if (curr > best) + best = curr; + } + + return (best); +} + +// random2avg() returns same mean value as random2() but with a lower variance +// never use with rolls < 2 as that would be silly - use random2() instead {dlb} +int random2avg(int max, int rolls) +{ + int sum = random2(max); + + for (int i = 0; i < (rolls - 1); i++) + sum += random2(max + 1); + + return (sum / rolls); +} + +// originally designed to randomise evasion - +// values are slightly lowered near (max) and +// approach an upper limit somewhere near (limit/2) +int random2limit(int max, int limit) +{ + int i; + int sum = 0; + + if (max < 1) + return (0); + + for (i = 0; i < max; i++) + if (random2(limit) >= i) + sum++; + + return (sum); +} + +// Generate samples from a binomial distribution with n_trials and trial_prob +// probability of success per trial. trial_prob is a integer less than 100 +// representing the % chancee of success. +// This just evaluates all n trials, there is probably an efficient way of +// doing this but I'm not much of a statistician. -CAO +int binomial_generator(unsigned n_trials, unsigned trial_prob) +{ + int count = 0; + for (unsigned i = 0; i < n_trials; ++i) + if (::x_chance_in_y(trial_prob, 100)) + count++; + + return count; +} + +bool one_chance_in(int a_million) +{ + return (random2(a_million) == 0); +} + +bool x_chance_in_y(int x, int y) +{ + if (x <= 0) + return (false); + + if (x >= y) + return (true); + + return (random2(y) < x); +} + +int fuzz_value(int val, int lowfuzz, int highfuzz, int naverage) +{ + const int lfuzz = lowfuzz * val / 100, + hfuzz = highfuzz * val / 100; + return val + random2avg(lfuzz + hfuzz + 1, naverage) - lfuzz; +} diff --git a/crawl-ref/source/random.h b/crawl-ref/source/random.h new file mode 100644 index 0000000000..b1bdbe5021 --- /dev/null +++ b/crawl-ref/source/random.h @@ -0,0 +1,76 @@ +#ifndef RANDOM_H +#define RANDOM_H + +#include "rng.h" + +bool coinflip(); +int div_rand_round( int num, int den ); +int div_round_up( int num, int den ); +bool one_chance_in(int a_million); +bool x_chance_in_y(int x, int y); +int random2(int max); +int maybe_random2(int x, bool random_factor); +int random_range(int low, int high); +int random_range(int low, int high, int nrolls); +const char* random_choose_string(const char* first, ...); +int random_choose(int first, ...); +int random_choose_weighted(int weight, int first, ...); +int random2avg(int max, int rolls); +int bestroll(int max, int rolls); +int random2limit(int max, int limit); +int binomial_generator(unsigned n_trials, unsigned trial_prob); +int fuzz_value(int val, int lowfuzz, int highfuzz, int naverage = 2); +int roll_dice(int num, int size); + +struct dice_def +{ + int num; + int size; + + dice_def(int n = 0, int s = 0) : num(n), size(s) {} + int roll() const; +}; + +dice_def calc_dice(int num_dice, int max_damage); +void scale_dice(dice_def &dice, int threshold = 24); + +class rng_save_excursion +{ +public: + rng_save_excursion(long seed) { push_rng_state(); seed_rng(seed); } + rng_save_excursion() { push_rng_state(); } + ~rng_save_excursion() { pop_rng_state(); } +}; + +template +int choose_random_weighted(Iterator beg, const Iterator end) +{ + ASSERT(beg < end); + +#ifdef DEBUG + int times_set = 0; +#endif + + int totalweight = 0; + int count = 0, result = 0; + while (beg != end) + { + totalweight += *beg; + if (random2(totalweight) < *beg) + { + result = count; +#ifdef DEBUG + times_set++; +#endif + } + ++count; + ++beg; + } +#ifdef DEBUG + ASSERT(times_set > 0); +#endif + return result; +} + +#endif + diff --git a/crawl-ref/source/rng.cc b/crawl-ref/source/rng.cc new file mode 100644 index 0000000000..409f0975c9 --- /dev/null +++ b/crawl-ref/source/rng.cc @@ -0,0 +1,91 @@ +/* + * File: rng.cc + * Summary: Random number generator wrapping. + */ + +#include "AppHdr.h" + +#include "rng.h" + +#include "mt19937ar.h" + +#ifdef USE_MORE_SECURE_SEED + +// for times() +#include + +// for getpid() +#include +#include + +#endif + +#ifdef MORE_HARDENED_PRNG +#include "sha256.h" +#endif + +void seed_rng(unsigned long* seed_key, size_t num_keys) +{ + // MT19937 -- see mt19937ar.cc for details/licence + init_by_array(seed_key, num_keys); +} + +void seed_rng(long seed) +{ + // MT19937 -- see mt19937ar.cc for details/licence + init_genrand(seed); +} + +void seed_rng() +{ + unsigned long seed = time( NULL ); +#ifdef USE_MORE_SECURE_SEED + + /* (at least) 256-bit wide seed */ + unsigned long seed_key[8]; + + struct tms buf; + seed += times( &buf ) + getpid(); + seed_key[0] = seed; + + /* Try opening from various system provided (hopefully) CSPRNGs */ + FILE* seed_f = fopen("/dev/urandom", "rb"); + if (!seed_f) + seed_f = fopen("/dev/random", "rb"); + if (!seed_f) + seed_f = fopen("/dev/srandom", "rb"); + if (!seed_f) + seed_f = fopen("/dev/arandom", "rb"); + if (seed_f) + { + fread(&seed_key[1], sizeof(unsigned long), 7, seed_f); + fclose(seed_f); + } + + seed_rng(seed_key, 8); + +#else + seed_rng(seed); +#endif +} + +// MT19937 -- see mt19937ar.cc for details +unsigned long random_int( void ) +{ +#ifndef MORE_HARDENED_PRNG + return (genrand_int32()); +#else + return (sha256_genrand()); +#endif +} + +void push_rng_state() +{ + push_mt_state(); +} + +void pop_rng_state() +{ + pop_mt_state(); +} + diff --git a/crawl-ref/source/rng.h b/crawl-ref/source/rng.h new file mode 100644 index 0000000000..0f8bf4b7f6 --- /dev/null +++ b/crawl-ref/source/rng.h @@ -0,0 +1,14 @@ +#ifndef RNG_H +#define RNG_H + +void seed_rng(); +void seed_rng(long seed); +void push_rng_state(); +void pop_rng_state(); + +void cf_setseed(); + +unsigned long random_int(); + +#endif + diff --git a/crawl-ref/source/skills.cc b/crawl-ref/source/skills.cc index aef49cb535..8ea09fca34 100644 --- a/crawl-ref/source/skills.cc +++ b/crawl-ref/source/skills.cc @@ -18,9 +18,9 @@ #include "notes.h" #include "output.h" #include "player.h" +#include "random.h" #include "skills2.h" #include "spl-cast.h" -#include "stuff.h" #include "tutorial.h" diff --git a/crawl-ref/source/species.cc b/crawl-ref/source/species.cc index f68dfa83f0..4481ae4669 100644 --- a/crawl-ref/source/species.cc +++ b/crawl-ref/source/species.cc @@ -2,7 +2,7 @@ #include "species.h" -#include "stuff.h" +#include "random.h" // March 2008: change order of species and jobs on character selection // screen as suggested by Markus Maier. Summarizing comments below are diff --git a/crawl-ref/source/spells1.cc b/crawl-ref/source/spells1.cc index 96a64f7f24..668ff38dcc 100644 --- a/crawl-ref/source/spells1.cc +++ b/crawl-ref/source/spells1.cc @@ -20,6 +20,8 @@ #include "artefact.h" #include "beam.h" #include "cloud.h" +#include "coord.h" +#include "coordit.h" #include "describe.h" #include "directn.h" #include "effects.h" diff --git a/crawl-ref/source/spl-cast.cc b/crawl-ref/source/spl-cast.cc index f536bea3f1..d1a75373fb 100644 --- a/crawl-ref/source/spl-cast.cc +++ b/crawl-ref/source/spl-cast.cc @@ -14,6 +14,8 @@ #include "externs.h" #include "beam.h" +#include "coord.h" +#include "coordit.h" #include "describe.h" #include "effects.h" #include "food.h" diff --git a/crawl-ref/source/spl-mis.cc b/crawl-ref/source/spl-mis.cc index 8fc6a30005..4b349054c6 100644 --- a/crawl-ref/source/spl-mis.cc +++ b/crawl-ref/source/spl-mis.cc @@ -12,6 +12,7 @@ #include +#include "colour.h" #include "cloud.h" #include "effects.h" #include "it_use2.h" diff --git a/crawl-ref/source/spl-util.cc b/crawl-ref/source/spl-util.cc index a8721ddf87..d588fb62e7 100644 --- a/crawl-ref/source/spl-util.cc +++ b/crawl-ref/source/spl-util.cc @@ -19,6 +19,7 @@ #include "externs.h" #include "beam.h" +#include "coordit.h" #include "directn.h" #include "debug.h" #include "godabil.h" diff --git a/crawl-ref/source/stash.cc b/crawl-ref/source/stash.cc index e62ddad8b6..52398ad871 100644 --- a/crawl-ref/source/stash.cc +++ b/crawl-ref/source/stash.cc @@ -11,6 +11,7 @@ #include "cio.h" #include "clua.h" #include "command.h" +#include "coord.h" #include "describe.h" #include "directn.h" #include "food.h" diff --git a/crawl-ref/source/stuff.cc b/crawl-ref/source/stuff.cc index 1654202916..ed29feb547 100644 --- a/crawl-ref/source/stuff.cc +++ b/crawl-ref/source/stuff.cc @@ -8,6 +8,7 @@ #include "beam.h" #include "cio.h" +#include "coordit.h" #include "database.h" #include "directn.h" #include "los.h" @@ -32,21 +33,6 @@ #include -#ifdef USE_MORE_SECURE_SEED - -// for times() -#include - -// for getpid() -#include -#include - -#endif - -#ifdef MORE_HARDENED_PRNG -#include "sha256.h" -#endif - #ifdef TARGET_OS_DOS #include #endif @@ -66,7 +52,6 @@ #include "misc.h" #include "monstuff.h" #include "mon-util.h" -#include "mt19937ar.h" #include "notes.h" #include "output.h" #include "player.h" @@ -129,183 +114,6 @@ stack_iterator stack_iterator::operator++(int dummy) return copy; } -rectangle_iterator::rectangle_iterator( const coord_def& corner1, - const coord_def& corner2 ) -{ - topleft.x = std::min(corner1.x, corner2.x); - topleft.y = std::min(corner1.y, corner2.y); // not really necessary - bottomright.x = std::max(corner1.x, corner2.x); - bottomright.y = std::max(corner1.y, corner2.y); - current = topleft; -} - -rectangle_iterator::rectangle_iterator( int x_border_dist, int y_border_dist ) -{ - if (y_border_dist < 0) - y_border_dist = x_border_dist; - - topleft.set(x_border_dist, y_border_dist); - bottomright.set(GXM - x_border_dist - 1, GYM - y_border_dist - 1); - current = topleft; -} - -rectangle_iterator::operator bool() const -{ - return (current.y <= bottomright.y); -} - -coord_def rectangle_iterator::operator *() const -{ - return current; -} - -const coord_def* rectangle_iterator::operator->() const -{ - return ¤t; -} - -rectangle_iterator& rectangle_iterator::operator ++() -{ - if (current.x == bottomright.x) - { - current.x = topleft.x; - current.y++; - } - else - { - current.x++; - } - return *this; -} - -rectangle_iterator rectangle_iterator::operator++( int dummy ) -{ - const rectangle_iterator copy = *this; - ++(*this); - return (copy); -} - -radius_iterator::radius_iterator(const coord_def& _center, int _radius, - bool _roguelike_metric, bool _require_los, - bool _exclude_center, - const env_show_grid* _losgrid) - : center(_center), radius(_radius), roguelike_metric(_roguelike_metric), - require_los(_require_los), exclude_center(_exclude_center), - losgrid(_losgrid), iter_done(false) -{ - reset(); -} - -void radius_iterator::reset() -{ - iter_done = false; - - location.x = center.x - radius; - location.y = center.y - radius; - - if ( !this->on_valid_square() ) - ++(*this); -} - -bool radius_iterator::done() const -{ - return iter_done; -} - -coord_def radius_iterator::operator *() const -{ - return location; -} - -const coord_def* radius_iterator::operator->() const -{ - return &location; -} - -void radius_iterator::step() -{ - const int minx = std::max(X_BOUND_1+1, center.x - radius); - const int maxx = std::min(X_BOUND_2-1, center.x + radius); - const int maxy = std::min(Y_BOUND_2-1, center.y + radius); - - // Sweep L-R, U-D - location.x++; - if (location.x > maxx) - { - location.x = minx; - location.y++; - if (location.y > maxy) - iter_done = true; - } -} - -void radius_iterator::step_back() -{ - const int minx = std::max(X_BOUND_1+1, center.x - radius); - const int maxx = std::min(X_BOUND_2-1, center.x + radius); - const int miny = std::max(Y_BOUND_1+1, center.y - radius); - - location.x--; - if (location.x < minx) - { - location.x = maxx; - location.y--; - if (location.y < miny) - iter_done = true; - } -} - -bool radius_iterator::on_valid_square() const -{ - if (!in_bounds(location)) - return (false); - if (!roguelike_metric && (location - center).abs() > radius*radius) - return (false); - if (require_los) - { - if (!losgrid && !see_cell(location)) - return (false); - if (losgrid && !see_cell(*losgrid, center, location)) - return (false); - } - if (exclude_center && location == center) - return (false); - - return (true); -} - -const radius_iterator& radius_iterator::operator++() -{ - do - this->step(); - while (!this->done() && !this->on_valid_square()); - - return (*this); -} - -const radius_iterator& radius_iterator::operator--() -{ - do - this->step_back(); - while (!this->done() && !this->on_valid_square()); - - return (*this); -} - -radius_iterator radius_iterator::operator++(int dummy) -{ - const radius_iterator copy = *this; - ++(*this); - return (copy); -} - -radius_iterator radius_iterator::operator--(int dummy) -{ - const radius_iterator copy = *this; - --(*this); - return (copy); -} - // Crude, but functional. std::string make_time_string(time_t abs_time, bool terse) { @@ -471,276 +279,6 @@ unsigned char get_ch() return gotched; } -void seed_rng(unsigned long* seed_key, size_t num_keys) -{ - // MT19937 -- see mt19937ar.cc for details/licence - init_by_array(seed_key, num_keys); -} - -void seed_rng(long seed) -{ - // MT19937 -- see mt19937ar.cc for details/licence - init_genrand(seed); -} - -void seed_rng() -{ - unsigned long seed = time( NULL ); -#ifdef USE_MORE_SECURE_SEED - - /* (at least) 256-bit wide seed */ - unsigned long seed_key[8]; - - struct tms buf; - seed += times( &buf ) + getpid(); - seed_key[0] = seed; - - /* Try opening from various system provided (hopefully) CSPRNGs */ - FILE* seed_f = fopen("/dev/urandom", "rb"); - if (!seed_f) - seed_f = fopen("/dev/random", "rb"); - if (!seed_f) - seed_f = fopen("/dev/srandom", "rb"); - if (!seed_f) - seed_f = fopen("/dev/arandom", "rb"); - if (seed_f) - { - fread(&seed_key[1], sizeof(unsigned long), 7, seed_f); - fclose(seed_f); - } - - seed_rng(seed_key, 8); - -#else - seed_rng(seed); -#endif -} - -// MT19937 -- see mt19937ar.cc for details -unsigned long random_int( void ) -{ -#ifndef MORE_HARDENED_PRNG - return (genrand_int32()); -#else - return (sha256_genrand()); -#endif -} - -int random_range(int low, int high) -{ - ASSERT(low <= high); - return (low + random2(high - low + 1)); -} - -int random_range(int low, int high, int nrolls) -{ - ASSERT(nrolls > 0); - int sum = 0; - for (int i = 0; i < nrolls; ++i) - sum += random_range(low, high); - return (sum / nrolls); -} - -// Chooses one of the numbers passed in at random. The list of numbers -// must be terminated with -1. -int random_choose(int first, ...) -{ - va_list args; - va_start(args, first); - - int chosen = first, count = 1, nargs = 100; - - while (nargs-- > 0) - { - const int pick = va_arg(args, int); - if (pick == -1) - break; - if (one_chance_in(++count)) - chosen = pick; - } - - ASSERT(nargs > 0); - - va_end(args); - return (chosen); -} - -// Chooses one of the strings passed in at random. The list of strings -// must be terminated with NULL. -const char* random_choose_string(const char* first, ...) -{ - va_list args; - va_start(args, first); - - const char* chosen = first; - int count = 1, nargs = 100; - - while (nargs-- > 0) - { - char* pick = va_arg(args, char*); - if (pick == NULL) - break; - if (one_chance_in(++count)) - chosen = pick; - } - - ASSERT(nargs > 0); - - va_end(args); - return (chosen); -} - -int random_choose_weighted(int weight, int first, ...) -{ - va_list args; - va_start(args, first); - - int chosen = first, cweight = weight, nargs = 100; - - while (nargs-- > 0) - { - const int nweight = va_arg(args, int); - if (!nweight) - break; - - const int choice = va_arg(args, int); - if (random2(cweight += nweight) < nweight) - chosen = choice; - } - - ASSERT(nargs > 0); - - va_end(args); - return (chosen); -} - -int random2(int max) -{ - if (max <= 1) - return (0); - - #ifdef MORE_HARDENED_PRNG - return (static_cast(sha256_genrand() / (0xFFFFFFFFUL / max + 1))); - #else - return (static_cast(genrand_int32() / (0xFFFFFFFFUL / max + 1))); - #endif -} - -bool coinflip(void) -{ - return (static_cast(random2(2))); -} - -// Returns random2(x) if random_factor is true, otherwise the mean. -int maybe_random2(int x, bool random_factor) -{ - if (random_factor) - return (random2(x)); - else - return (x / 2); -} - -void push_rng_state() -{ - push_mt_state(); -} - -void pop_rng_state() -{ - pop_mt_state(); -} - -// Attempts to make missile weapons nicer to the player by reducing the -// extreme variance in damage done. -void scale_dice(dice_def &dice, int threshold) -{ - while (dice.size > threshold) - { - dice.num *= 2; - // If it's an odd number, lose one; this is more than - // compensated by the increase in number of dice. - dice.size /= 2; - } -} - -int bestroll(int max, int rolls) -{ - int best = 0; - - for (int i = 0; i < rolls; i++) - { - int curr = random2(max); - if (curr > best) - best = curr; - } - - return (best); -} - -// random2avg() returns same mean value as random2() but with a lower variance -// never use with rolls < 2 as that would be silly - use random2() instead {dlb} -int random2avg(int max, int rolls) -{ - int sum = random2(max); - - for (int i = 0; i < (rolls - 1); i++) - sum += random2(max + 1); - - return (sum / rolls); -} - -int roll_dice(int num, int size) -{ - int ret = 0; - int i; - - // If num <= 0 or size <= 0, then we'll just return the default - // value of zero. This is good behaviour in that it will be - // appropriate for calculated values that might be passed in. - if (num > 0 && size > 0) - { - ret += num; // since random2() is zero based - - for (i = 0; i < num; i++) - ret += random2(size); - } - - return (ret); -} - -// originally designed to randomise evasion - -// values are slightly lowered near (max) and -// approach an upper limit somewhere near (limit/2) -int random2limit(int max, int limit) -{ - int i; - int sum = 0; - - if (max < 1) - return (0); - - for (i = 0; i < max; i++) - if (random2(limit) >= i) - sum++; - - return (sum); -} - -// Generate samples from a binomial distribution with n_trials and trial_prob -// probability of success per trial. trial_prob is a integer less than 100 -// representing the % chancee of success. -// This just evaluates all n trials, there is probably an efficient way of -// doing this but I'm not much of a statistician. -CAO -int binomial_generator(unsigned n_trials, unsigned trial_prob) -{ - int count = 0; - for (unsigned i = 0; i < n_trials; ++i) - if (::x_chance_in_y(trial_prob, 100)) - count++; - - return count; -} - void cio_init() { crawl_state.io_inited = true; @@ -968,33 +506,11 @@ int stat_div( int stat_level, int value, int mult, int shift ) return ((mult * value) / div); } -// Calculates num/den and randomly adds one based on the remainder. -int div_rand_round(int num, int den) -{ - return (num / den + (random2(den) < num % den)); -} - int div_round_up(int num, int den) { return (num / den + (num % den != 0)); } -bool one_chance_in(int a_million) -{ - return (random2(a_million) == 0); -} - -bool x_chance_in_y(int x, int y) -{ - if (x <= 0) - return (false); - - if (x >= y) - return (true); - - return (random2(y) < x); -} - // Simple little function to quickly modify all three stats // at once - does check for '0' modifiers to prevent needless // adding .. could use checking for sums less than zero, I guess. @@ -1269,41 +785,6 @@ int yesnoquit( const char* str, bool safe, int safeanswer, bool allow_all, } } -int grid_distance( const coord_def& p1, const coord_def& p2 ) -{ - return grid_distance(p1.x, p1.y, p2.x, p2.y); -} - -// More accurate than distance() given the actual movement geometry -- bwr -int grid_distance( int x, int y, int x2, int y2 ) -{ - const int dx = abs( x - x2 ); - const int dy = abs( y - y2 ); - - // returns distance in terms of moves: - return ((dx > dy) ? dx : dy); -} - -int distance( const coord_def& p1, const coord_def& p2 ) -{ - return distance(p1.x, p1.y, p2.x, p2.y); -} - -int distance( int x, int y, int x2, int y2 ) -{ - //jmf: now accurate, but remember to only compare vs. pre-squared distances - // thus, next to == (distance(m1.x,m1.y, m2.x,m2.y) <= 2) - const int dx = x - x2; - const int dy = y - y2; - - return ((dx * dx) + (dy * dy)); -} - -bool adjacent( const coord_def& p1, const coord_def& p2 ) -{ - return grid_distance(p1, p2) <= 1; -} - bool silenced(const coord_def& p) { // FIXME: implement for monsters @@ -1315,315 +796,6 @@ bool player_can_hear(const coord_def& p) return (!silenced(p) && !silenced(you.pos())); } -bool in_bounds_x(int x) -{ - return (x > X_BOUND_1 && x < X_BOUND_2); -} - -bool in_bounds_y(int y) -{ - return (y > Y_BOUND_1 && y < Y_BOUND_2); -} - -// Returns true if inside the area the player can move and dig (ie exclusive). -bool in_bounds(int x, int y) -{ - return (in_bounds_x(x) && in_bounds_y(y)); -} - -bool map_bounds_x(int x) -{ - return (x >= X_BOUND_1 && x <= X_BOUND_2); -} - -bool map_bounds_y(int y) -{ - return (y >= Y_BOUND_1 && y <= Y_BOUND_2); -} - -// Returns true if inside the area the player can map (ie inclusive). -// Note that terrain features should be in_bounds() leaving an outer -// ring of rock to frame the level. -bool map_bounds(int x, int y) -{ - return (map_bounds_x(x) && map_bounds_y(y)); -} - -coord_def random_in_bounds() -{ - if (crawl_state.arena) - { - const coord_def &ul = crawl_view.glos1; // Upper left - const coord_def &lr = crawl_view.glos2; // Lower right - - return coord_def(random_range(ul.x, lr.x - 1), - random_range(ul.y, lr.y - 1)); - } - else - return coord_def(random_range(MAPGEN_BORDER, GXM - MAPGEN_BORDER - 1), - random_range(MAPGEN_BORDER, GYM - MAPGEN_BORDER - 1)); -} - -unsigned char random_colour(void) -{ - return (1 + random2(15)); -} - -unsigned char random_uncommon_colour() -{ - unsigned char result; - - do - result = random_colour(); - while (result == LIGHTCYAN || result == CYAN || result == BROWN); - - return (result); -} - -unsigned char make_low_colour(unsigned char colour) -{ - if (colour >= 8 && colour <= 15) - return (colour - 8); - - return (colour); -} - -unsigned char make_high_colour(unsigned char colour) -{ - if (colour <= 7) - return (colour + 8); - - return (colour); -} - -// returns if a colour is one of the special element colours (ie not regular) -bool is_element_colour( int col ) -{ - // stripping any COLFLAGS (just in case) - return ((col & 0x007f) >= ETC_FIRE); -} - -int element_colour( int element, bool no_random ) -{ - // Doing this so that we don't have to do recursion here at all - // (these were the only cases which had possible double evaluation): - if (element == ETC_FLOOR) - element = env.floor_colour; - else if (element == ETC_ROCK) - element = env.rock_colour; - - // pass regular colours through for safety. - if (!is_element_colour( element )) - return (element); - - int ret = BLACK; - - // Setting no_random to true will get the first colour in the cases - // below. This is potentially useful for calls to this function - // which might want a consistent result. - int tmp_rand = (no_random ? 0 : random2(120)); - - switch (element & 0x007f) // strip COLFLAGs just in case - { - case ETC_FIRE: - ret = (tmp_rand < 40) ? RED : - (tmp_rand < 80) ? YELLOW - : LIGHTRED; - break; - - case ETC_ICE: - ret = (tmp_rand < 40) ? LIGHTBLUE : - (tmp_rand < 80) ? BLUE - : WHITE; - break; - - case ETC_EARTH: - ret = (tmp_rand < 60) ? BROWN : LIGHTRED; - break; - - case ETC_AIR: - ret = (tmp_rand < 60) ? LIGHTGREY : WHITE; - break; - - case ETC_ELECTRICITY: - ret = (tmp_rand < 40) ? LIGHTCYAN : - (tmp_rand < 80) ? LIGHTBLUE - : CYAN; - break; - - case ETC_POISON: - ret = (tmp_rand < 60) ? LIGHTGREEN : GREEN; - break; - - case ETC_WATER: - ret = (tmp_rand < 60) ? BLUE : CYAN; - break; - - case ETC_MAGIC: - ret = (tmp_rand < 30) ? LIGHTMAGENTA : - (tmp_rand < 60) ? LIGHTBLUE : - (tmp_rand < 90) ? MAGENTA - : BLUE; - break; - - case ETC_MUTAGENIC: - case ETC_WARP: - ret = (tmp_rand < 60) ? LIGHTMAGENTA : MAGENTA; - break; - - case ETC_ENCHANT: - ret = (tmp_rand < 60) ? LIGHTBLUE : BLUE; - break; - - case ETC_HEAL: - ret = (tmp_rand < 60) ? LIGHTBLUE : YELLOW; - break; - - case ETC_BLOOD: - ret = (tmp_rand < 60) ? RED : DARKGREY; - break; - - case ETC_DEATH: // assassin - case ETC_NECRO: // necromancer - ret = (tmp_rand < 80) ? DARKGREY : MAGENTA; - break; - - case ETC_UNHOLY: // ie demonology - ret = (tmp_rand < 80) ? DARKGREY : RED; - break; - - case ETC_DARK: - ret = (tmp_rand < 80) ? DARKGREY : LIGHTGREY; - break; - - case ETC_HOLY: - ret = (tmp_rand < 60) ? YELLOW : WHITE; - break; - - case ETC_VEHUMET: - ret = (tmp_rand < 40) ? LIGHTRED : - (tmp_rand < 80) ? LIGHTMAGENTA - : LIGHTBLUE; - break; - - case ETC_BEOGH: - ret = (tmp_rand < 60) ? LIGHTRED // plain Orc colour - : BROWN; // Orcish mines wall/idol colour - break; - - case ETC_CRYSTAL: - ret = (tmp_rand < 40) ? LIGHTGREY : - (tmp_rand < 80) ? GREEN - : LIGHTRED; - break; - - case ETC_SLIME: - ret = (tmp_rand < 40) ? GREEN : - (tmp_rand < 80) ? BROWN - : LIGHTGREEN; - break; - - case ETC_SMOKE: - ret = (tmp_rand < 30) ? LIGHTGREY : - (tmp_rand < 60) ? DARKGREY : - (tmp_rand < 90) ? LIGHTBLUE - : MAGENTA; - break; - - case ETC_JEWEL: - ret = (tmp_rand < 12) ? WHITE : - (tmp_rand < 24) ? YELLOW : - (tmp_rand < 36) ? LIGHTMAGENTA : - (tmp_rand < 48) ? LIGHTRED : - (tmp_rand < 60) ? LIGHTGREEN : - (tmp_rand < 72) ? LIGHTBLUE : - (tmp_rand < 84) ? MAGENTA : - (tmp_rand < 96) ? RED : - (tmp_rand < 108) ? GREEN - : BLUE; - break; - - case ETC_ELVEN: - ret = (tmp_rand < 40) ? LIGHTGREEN : - (tmp_rand < 80) ? GREEN : - (tmp_rand < 100) ? LIGHTBLUE - : BLUE; - break; - - case ETC_DWARVEN: - ret = (tmp_rand < 40) ? BROWN : - (tmp_rand < 80) ? LIGHTRED : - (tmp_rand < 100) ? LIGHTGREY - : CYAN; - break; - - case ETC_ORCISH: - ret = (tmp_rand < 40) ? DARKGREY : - (tmp_rand < 80) ? RED : - (tmp_rand < 100) ? BROWN - : MAGENTA; - break; - - case ETC_GILA: - ret = (tmp_rand < 30) ? LIGHTMAGENTA : - (tmp_rand < 60) ? MAGENTA : - (tmp_rand < 90) ? YELLOW : - (tmp_rand < 105) ? LIGHTRED - : RED; - break; - - case ETC_STONE: - if (player_in_branch( BRANCH_HALL_OF_ZOT )) - ret = env.rock_colour; - else - ret = LIGHTGREY; - break; - - case ETC_MIST: - ret = tmp_rand < 100? CYAN : BLUE; - break; - - case ETC_SHIMMER_BLUE: - ret = random_choose_weighted(80, BLUE, 20, LIGHTBLUE, 5, CYAN, 0); - break; - - case ETC_DECAY: - ret = (tmp_rand < 60) ? BROWN : GREEN; - break; - - case ETC_SILVER: - ret = (tmp_rand < 90) ? LIGHTGREY : WHITE; - break; - - case ETC_GOLD: - ret = (tmp_rand < 60) ? YELLOW : BROWN; - break; - - case ETC_IRON: - ret = (tmp_rand < 40) ? CYAN : - (tmp_rand < 80) ? LIGHTGREY : - DARKGREY; - break; - - case ETC_BONE: - ret = (tmp_rand < 90) ? WHITE : LIGHTGREY; - break; - - case ETC_RANDOM: - ret = 1 + random2(15); // always random - break; - - case ETC_FLOOR: // should already be handled - case ETC_ROCK: // should already be handled - default: - break; - } - - ASSERT( !is_element_colour( ret ) ); - - return ((ret == BLACK) ? GREEN : ret); -} - char index_to_letter(int the_index) { return (the_index + ((the_index < 26) ? 'a' : ('A' - 26))); @@ -1641,13 +813,6 @@ int letter_to_index(int the_letter) return (the_letter); } -int fuzz_value(int val, int lowfuzz, int highfuzz, int naverage) -{ - const int lfuzz = lowfuzz * val / 100, - hfuzz = highfuzz * val / 100; - return val + random2avg(lfuzz + hfuzz + 1, naverage) - lfuzz; -} - // Returns 0 if the point is not near stairs. // Returns 1 if the point is near unoccupied stairs. // Returns 2 if the point is near player-occupied stairs. @@ -1740,13 +905,6 @@ void zap_los_monsters() } } -////////////////////////////////////////////////////////////////////////// -// coord_def -int coord_def::distance_from(const coord_def &other) const -{ - return (grid_distance(*this, other)); -} - int integer_sqrt(int value) { if (value <= 0) @@ -1772,8 +930,3 @@ int random_rod_subtype() { return STAFF_FIRST_ROD + random2(NUM_STAVES - STAFF_FIRST_ROD); } - -int dice_def::roll() const -{ - return roll_dice(this->num, this->size); -} diff --git a/crawl-ref/source/stuff.h b/crawl-ref/source/stuff.h index 67b9e1178e..3bf452fb49 100644 --- a/crawl-ref/source/stuff.h +++ b/crawl-ref/source/stuff.h @@ -17,35 +17,6 @@ void set_redraw_status(unsigned long flags); void tag_followers(); void untag_followers(); -void seed_rng(); -void seed_rng(long seed); -void push_rng_state(); -void pop_rng_state(); - -void cf_setseed(); -bool coinflip(); -int div_rand_round( int num, int den ); -int div_round_up( int num, int den ); -bool one_chance_in(int a_million); -bool x_chance_in_y(int x, int y); -int random2(int max); -int maybe_random2(int x, bool random_factor); -int random_range(int low, int high); -int random_range(int low, int high, int nrolls); -const char* random_choose_string(const char* first, ...); -int random_choose(int first, ...); -int random_choose_weighted(int weight, int first, ...); -unsigned long random_int(); -int random2avg(int max, int rolls); -int bestroll(int max, int rolls); - -int roll_dice(int num, int size); -void scale_dice(dice_def &dice, int threshold = 24); - -int binomial_generator(unsigned n_trials, unsigned trial_prob); - -// Various ways to iterate over things. - // stack_iterator guarantees validity so long as you don't manually // mess with item_def.link: i.e., you can kill the item you're // examining but you can't kill the item linked to it. @@ -68,62 +39,6 @@ private: int next_link; }; -class rectangle_iterator : - public std::iterator -{ -public: - rectangle_iterator( const coord_def& corner1, const coord_def& corner2 ); - explicit rectangle_iterator( int x_border_dist, int y_border_dist = -1 ); - operator bool() const; - coord_def operator *() const; - const coord_def* operator->() const; - - rectangle_iterator& operator ++ (); - rectangle_iterator operator ++ (int); -private: - coord_def current, topleft, bottomright; -}; - -class radius_iterator : public std::iterator -{ -public: - radius_iterator( const coord_def& center, int radius, - bool roguelike_metric = true, - bool require_los = true, - bool exclude_center = false, - const env_show_grid* losgrid = NULL ); - bool done() const; - void reset(); - operator bool() const { return !done(); } - coord_def operator *() const; - const coord_def* operator->() const; - - const radius_iterator& operator ++ (); - const radius_iterator& operator -- (); - radius_iterator operator ++ (int); - radius_iterator operator -- (int); -private: - void step(); - void step_back(); - bool on_valid_square() const; - - coord_def location, center; - int radius; - bool roguelike_metric, require_los, exclude_center; - const env_show_grid* losgrid; - bool iter_done; -}; - -class adjacent_iterator : public radius_iterator -{ -public: - explicit adjacent_iterator( const coord_def& pos = you.pos(), - bool _exclude_center = true ) : - radius_iterator(pos, 1, true, false, _exclude_center) {} -}; - -int random2limit(int max, int limit); int stepdown_value(int base_value, int stepping, int first_step, int last_step, int ceiling_value); int stat_mult( int stat_level, int value, int div = 20, int shift = 3 ); @@ -131,8 +46,6 @@ int stat_div( int stat_level, int value, int div = 20, int shift = 3 ); int skill_bump( int skill ); unsigned char get_ch(); -int fuzz_value(int val, int lowfuzz, int highfuzz, int naverage = 2); - void cio_init(); void cio_cleanup(); void clear_globals_on_exit(); @@ -156,48 +69,10 @@ int yesnoquit( const char* str, bool safe = true, int safeanswer = 0, bool allow_all = false, bool clear_after = true, char alt_yes = 'Y', char alt_yes2 = 'Y' ); -bool in_bounds_x(int x); -bool in_bounds_y(int y); -bool in_bounds(int x, int y); -bool map_bounds_x(int x); -bool map_bounds_y(int y); -bool map_bounds(int x, int y); -coord_def random_in_bounds(); - -inline bool in_bounds(const coord_def &p) -{ - return in_bounds(p.x, p.y); -} - -inline bool map_bounds(const coord_def &p) -{ - return map_bounds(p.x, p.y); -} - -// Determines if the coordinate is within bounds of an LOS array. -inline bool show_bounds(const coord_def &p) -{ - return (p.x >= 0 && p.x < ENV_SHOW_DIAMETER - && p.y >= 0 && p.y < ENV_SHOW_DIAMETER); -} - -int grid_distance( const coord_def& p1, const coord_def& p2 ); -int grid_distance( int x, int y, int x2, int y2 ); -int distance( const coord_def& p1, const coord_def& p2 ); -int distance( int x, int y, int x2, int y2); -bool adjacent( const coord_def& p1, const coord_def& p2 ); - bool silenced(const coord_def& p); bool player_can_hear(const coord_def& p); -unsigned char random_colour(); -unsigned char random_uncommon_colour(); -unsigned char make_low_colour(unsigned char colour); -unsigned char make_high_colour(unsigned char colour); -bool is_element_colour( int col ); -int element_colour( int element, bool no_random = false ); - char index_to_letter (int the_index); int letter_to_index(int the_letter); @@ -218,44 +93,6 @@ template inline Z sgn(Z x) bool is_trap_square(dungeon_feature_type grid); void zap_los_monsters(); -class rng_save_excursion -{ -public: - rng_save_excursion(long seed) { push_rng_state(); seed_rng(seed); } - rng_save_excursion() { push_rng_state(); } - ~rng_save_excursion() { pop_rng_state(); } -}; - -template -int choose_random_weighted(Iterator beg, const Iterator end) -{ - ASSERT(beg < end); - -#ifdef DEBUG - int times_set = 0; -#endif - - int totalweight = 0; - int count = 0, result = 0; - while (beg != end) - { - totalweight += *beg; - if (random2(totalweight) < *beg) - { - result = count; -#ifdef DEBUG - times_set++; -#endif - } - ++count; - ++beg; - } -#ifdef DEBUG - ASSERT(times_set > 0); -#endif - return result; -} - int integer_sqrt(int value); int random_rod_subtype(); diff --git a/crawl-ref/source/terrain.cc b/crawl-ref/source/terrain.cc index 8bb6cc211b..b3ea553313 100644 --- a/crawl-ref/source/terrain.cc +++ b/crawl-ref/source/terrain.cc @@ -27,6 +27,7 @@ #include "ouch.h" #include "overmap.h" #include "player.h" +#include "random.h" #include "spells3.h" #include "stuff.h" #include "transfor.h" diff --git a/crawl-ref/source/tilepick.cc b/crawl-ref/source/tilepick.cc index f8e5638177..c56e79f1e6 100644 --- a/crawl-ref/source/tilepick.cc +++ b/crawl-ref/source/tilepick.cc @@ -9,6 +9,9 @@ #include #include "artefact.h" #include "decks.h" +#include "colour.h" +#include "coord.h" +#include "coordit.h" #include "directn.h" #include "externs.h" #include "food.h" diff --git a/crawl-ref/source/tilereg.cc b/crawl-ref/source/tilereg.cc index 268bac30ad..18426ca0d7 100644 --- a/crawl-ref/source/tilereg.cc +++ b/crawl-ref/source/tilereg.cc @@ -10,6 +10,7 @@ #ifdef USE_TILE #include "cio.h" +#include "coord.h" #include "debug.h" #include "describe.h" #include "files.h" diff --git a/crawl-ref/source/tilesdl.cc b/crawl-ref/source/tilesdl.cc index 1e3628e75f..fb56de2c8e 100644 --- a/crawl-ref/source/tilesdl.cc +++ b/crawl-ref/source/tilesdl.cc @@ -4,6 +4,7 @@ #include "artefact.h" #include "cio.h" +#include "coord.h" #include "itemname.h" #include "items.h" #include "itemprop.h" diff --git a/crawl-ref/source/transfor.cc b/crawl-ref/source/transfor.cc index d43e1ee88e..b2b2bec286 100644 --- a/crawl-ref/source/transfor.cc +++ b/crawl-ref/source/transfor.cc @@ -22,6 +22,7 @@ #include "items.h" #include "output.h" #include "player.h" +#include "random.h" #include "skills2.h" #include "state.h" #include "stuff.h" diff --git a/crawl-ref/source/traps.cc b/crawl-ref/source/traps.cc index 664d3edb18..362859a06b 100644 --- a/crawl-ref/source/traps.cc +++ b/crawl-ref/source/traps.cc @@ -14,6 +14,7 @@ #include "artefact.h" #include "beam.h" #include "branch.h" +#include "coord.h" #include "delay.h" #include "describe.h" #include "directn.h" diff --git a/crawl-ref/source/travel.cc b/crawl-ref/source/travel.cc index f7d1a5ab5b..1dcfdaa0b1 100644 --- a/crawl-ref/source/travel.cc +++ b/crawl-ref/source/travel.cc @@ -9,6 +9,8 @@ */ #include "AppHdr.h" +#include "coord.h" +#include "coordit.h" #include "files.h" #include "fixary.h" #include "branch.h" diff --git a/crawl-ref/source/tutorial.cc b/crawl-ref/source/tutorial.cc index 1948cf0c83..3d9a177e72 100644 --- a/crawl-ref/source/tutorial.cc +++ b/crawl-ref/source/tutorial.cc @@ -18,6 +18,8 @@ #include "abl-show.h" #include "artefact.h" #include "cloud.h" +#include "colour.h" +#include "coordit.h" #include "command.h" #include "decks.h" #include "describe.h" @@ -40,6 +42,7 @@ #include "newgame.h" #include "jobs.h" #include "player.h" +#include "random.h" #include "religion.h" #include "shopping.h" #include "skills2.h" diff --git a/crawl-ref/source/view.cc b/crawl-ref/source/view.cc index 1dd2a13a42..67f208b715 100644 --- a/crawl-ref/source/view.cc +++ b/crawl-ref/source/view.cc @@ -25,6 +25,7 @@ #include "cio.h" #include "cloud.h" #include "clua.h" +#include "colour.h" #include "database.h" #include "debug.h" #include "delay.h" @@ -51,6 +52,7 @@ #include "overmap.h" #include "place.h" #include "player.h" +#include "random.h" #include "religion.h" #include "skills.h" #include "stuff.h" diff --git a/crawl-ref/source/view.h b/crawl-ref/source/view.h index 30c179d4d1..eb065ccf8d 100644 --- a/crawl-ref/source/view.h +++ b/crawl-ref/source/view.h @@ -10,52 +10,6 @@ #include "externs.h" -// various elemental colour schemes... used for abstracting random -// short lists. When adding colours, please also add their names in -// initfile.cc (str_to_colour)! -enum element_type -{ - ETC_FIRE = 32, // fiery colours (must be first and > highest colour) - ETC_ICE, // icy colours - ETC_EARTH, // earthy colours - ETC_ELECTRICITY, // electrical side of air - ETC_AIR, // non-electric and general air magic - ETC_POISON, // used only for venom mage and stalker stuff - ETC_WATER, // used only for the elemental - ETC_MAGIC, // general magical effect - ETC_MUTAGENIC, // transmute, poly, radiation effects - ETC_WARP, // teleportation and anything similar - ETC_ENCHANT, // magical enhancements - ETC_HEAL, // holy healing (not necromantic stuff) - ETC_HOLY, // general "good" god effects - ETC_DARK, // darkness - ETC_DEATH, // currently only assassin (and equal to ETC_NECRO) - ETC_NECRO, // necromancy stuff - ETC_UNHOLY, // demonology stuff - ETC_VEHUMET, // vehumet's oddball colours - ETC_BEOGH, // Beogh altar colours - ETC_CRYSTAL, // colours of crystal - ETC_BLOOD, // colours of blood - ETC_SMOKE, // colours of smoke - ETC_SLIME, // colours of slime - ETC_JEWEL, // colourful - ETC_ELVEN, // used for colouring elf fabric items - ETC_DWARVEN, // used for colouring dwarf fabric items - ETC_ORCISH, // used for colouring orc fabric items - ETC_GILA, // gila monster colours - ETC_FLOOR, // colour of the area's floor - ETC_ROCK, // colour of the area's rock - ETC_STONE, // colour of the area's stone - ETC_MIST, // colour of mist - ETC_SHIMMER_BLUE, // shimmering colours of blue. - ETC_DECAY, // colour of decay/swamp - ETC_SILVER, // colour of silver - ETC_GOLD, // colour of gold - ETC_IRON, // colour of iron - ETC_BONE, // colour of bone - ETC_RANDOM // any colour (except BLACK) -}; - void init_char_table(char_set_type set); void init_feature_table(); void init_monsters_seens(); -- cgit v1.2.3-54-g00ecf