use phosg random functions

This commit is contained in:
Martin Michelsen
2018-11-10 19:55:51 -08:00
parent 05d74ce496
commit 86243bd2e2
5 changed files with 51 additions and 53 deletions
+34 -32
View File
@@ -1,5 +1,7 @@
#include "Items.hh" #include "Items.hh"
#include <phosg/Random.hh>
using namespace std; using namespace std;
@@ -263,7 +265,7 @@ CommonItemCreator::CommonItemCreator(
} }
int32_t CommonItemCreator::decide_item_type(bool is_box) const { int32_t CommonItemCreator::decide_item_type(bool is_box) const {
uint32_t determinant = (rand() << 30) ^ (rand() << 15) ^ rand(); uint32_t determinant = random_object<uint32_t>();
const auto* v = is_box ? &this->box_item_categories : &this->enemy_item_categories; const auto* v = is_box ? &this->box_item_categories : &this->enemy_item_categories;
for (size_t x = 0; x < v->size(); x++) { for (size_t x = 0; x < v->size(); x++) {
@@ -320,25 +322,25 @@ ItemData CommonItemCreator::create_item(bool is_box, uint8_t episode,
case 0x00: // material case 0x00: // material
item.item_data1[0] = 0x03; item.item_data1[0] = 0x03;
item.item_data1[1] = 0x0B; item.item_data1[1] = 0x0B;
item.item_data1[2] = rand() % 7; item.item_data1[2] = random_int(0, 6);
break; break;
case 0x01: // equipment case 0x01: // equipment
switch (rand() % 4) { switch (random_int(0, 3)) {
case 0x00: // weapon case 0x00: // weapon
item.item_data1[1] = 1 + (rand() % 12); // random normal class item.item_data1[1] = random_int(1, 12); // random normal class
item.item_data1[2] = difficulty + (rand() % 3); // special type item.item_data1[2] = difficulty + random_int(0, 2); // special type
if ((item.item_data1[1] > 0x09) && (item.item_data1[2] > 0x04)) { if ((item.item_data1[1] > 0x09) && (item.item_data1[2] > 0x04)) {
item.item_data1[2] = 0x04; // no special classes above 4 item.item_data1[2] = 0x04; // no special classes above 4
} }
item.item_data1[4] = 0x80; // untekked item.item_data1[4] = 0x80; // untekked
if (item.item_data1[2] < 0x04) { if (item.item_data1[2] < 0x04) {
item.item_data1[4] |= (rand() % 41); // give a special item.item_data1[4] |= random_int(0, 40); // give a special
} }
for (size_t x = 0, y = 0; (x < 5) && (y < 3); x++) { // percentages for (size_t x = 0, y = 0; (x < 5) && (y < 3); x++) { // percentages
if ((rand() % 11) < 2) { // 1/11 chance of getting each type of percentage if (random_int(0, 10) == 1) { // 1/11 chance of getting each type of percentage
item.item_data1[6 + (y * 2)] = x + 1; item.item_data1[6 + (y * 2)] = x + 1;
item.item_data1[7 + (y * 2)] = (rand() % 11) * 5; item.item_data1[7 + (y * 2)] = random_int(0, 10) * 5;
y++; y++;
} }
} }
@@ -347,33 +349,33 @@ ItemData CommonItemCreator::create_item(bool is_box, uint8_t episode,
case 0x01: // armor case 0x01: // armor
item.item_data1[0] = 0x01; item.item_data1[0] = 0x01;
item.item_data1[1] = 0x01; item.item_data1[1] = 0x01;
item.item_data1[2] = (6 * difficulty) + (rand() % ((area / 2) + 2)); // standard type based on difficulty and area item.item_data1[2] = (6 * difficulty) + random_int(0, ((area / 2) + 2) - 1); // standard type based on difficulty and area
if (item.item_data1[2] > 0x17) { if (item.item_data1[2] > 0x17) {
item.item_data1[2] = 0x17; // no standard types above 0x17 item.item_data1[2] = 0x17; // no standard types above 0x17
} }
if ((rand() % 10) < 2) { // +/- if (random_int(0, 10) == 0) { // +/-
item.item_data1[4] = rand() % 6; item.item_data1[4] = random_int(0, 5);
item.item_data1[6] = rand() % 3; item.item_data1[6] = random_int(0, 2);
} }
item.item_data1[5] = rand() % 5; // slots item.item_data1[5] = random_int(0, 4); // slots
break; break;
case 0x02: // shield case 0x02: // shield
item.item_data1[0] = 0x01; item.item_data1[0] = 0x01;
item.item_data1[1] = 0x02; item.item_data1[1] = 0x02;
item.item_data1[2] = (5 * difficulty) + (rand() % ((area / 2) + 2)); // standard type based on difficulty and area item.item_data1[2] = (5 * difficulty) + random_int(0, ((area / 2) + 2) - 1); // standard type based on difficulty and area
if (item.item_data1[2] > 0x14) { if (item.item_data1[2] > 0x14) {
item.item_data1[2] = 0x14; // no standard types above 0x14 item.item_data1[2] = 0x14; // no standard types above 0x14
} }
if ((rand() % 11) < 2) { // +/- if (random_int(0, 10) == 0) { // +/-
item.item_data1[4] = rand() % 6; item.item_data1[4] = random_int(0, 5);
item.item_data1[6] = rand() % 6; item.item_data1[6] = random_int(0, 5);
} }
break; break;
case 0x03: { // unit case 0x03: { // unit
const auto& type_table = this->unit_types.at(difficulty); const auto& type_table = this->unit_types.at(difficulty);
uint8_t type = type_table[rand() % type_table.size()]; uint8_t type = type_table[random_int(0, type_table.size() - 1)];
if (type == 0xFF) { if (type == 0xFF) {
throw out_of_range("no item dropped"); // 0xFF -> no item drops throw out_of_range("no item dropped"); // 0xFF -> no item drops
} }
@@ -388,19 +390,19 @@ ItemData CommonItemCreator::create_item(bool is_box, uint8_t episode,
case 0x02: // technique case 0x02: // technique
item.item_data1[0] = 0x03; item.item_data1[0] = 0x03;
item.item_data1[1] = 0x02; item.item_data1[1] = 0x02;
item.item_data1[4] = rand() % 19; // tech type item.item_data1[4] = random_int(0, 18); // tech type
if ((item.item_data1[4] != 14) && (item.item_data1[4] != 17)) { // if not ryuker or reverser, give it a level if ((item.item_data1[4] != 14) && (item.item_data1[4] != 17)) { // if not ryuker or reverser, give it a level
if (item.item_data1[4] == 16) { // if not anti, give it a level between 1 and 30 if (item.item_data1[4] == 16) { // if not anti, give it a level between 1 and 30
if (area > 3) { if (area > 3) {
item.item_data1[2] = difficulty + (rand() % ((area - 1) / 2)); item.item_data1[2] = difficulty + random_int(0, ((area - 1) / 2) - 1);
} else { } else {
item.item_data1[2] = difficulty + (rand() % 1); item.item_data1[2] = difficulty;
} }
if (item.item_data1[2] > 6) { if (item.item_data1[2] > 6) {
item.item_data1[2] = 6; item.item_data1[2] = 6;
} }
} else { } else {
item.item_data1[2] = (5 * difficulty) + (rand() % ((area * 3) / 2)); // else between 1 and 7 item.item_data1[2] = (5 * difficulty) + random_int(0, ((area * 3) / 2) - 1); // else between 1 and 7
} }
} }
break; break;
@@ -414,24 +416,24 @@ ItemData CommonItemCreator::create_item(bool is_box, uint8_t episode,
case 0x04: // grinder case 0x04: // grinder
item.item_data1[0] = 0x03; item.item_data1[0] = 0x03;
item.item_data1[1] = 0x0A; item.item_data1[1] = 0x0A;
item.item_data1[2] = rand() % 3; // mono, di, tri item.item_data1[2] = random_int(0, 2); // mono, di, tri
break; break;
case 0x05: // consumable case 0x05: // consumable
item.item_data1[0] = 0x03; item.item_data1[0] = 0x03;
item.item_data1[5] = 0x01; item.item_data1[5] = 0x01;
switch (rand() % 3) { switch (random_int(0, 2)) {
case 0: // antidote / antiparalysis case 0: // antidote / antiparalysis
item.item_data1[1] = 6; item.item_data1[1] = 6;
item.item_data1[2] = rand() & 1; item.item_data1[2] = random_int(0, 1);
break; break;
case 1: // telepipe / trap vision case 1: // telepipe / trap vision
item.item_data1[1] = 7 + rand() & 1; item.item_data1[1] = 7 + random_int(0, 1);
break; break;
case 2: // sol / moon / star atomizer case 2: // sol / moon / star atomizer
item.item_data1[1] = 3 + rand() % 3; item.item_data1[1] = 3 + random_int(0, 2);
break; break;
} }
break; break;
@@ -439,19 +441,19 @@ ItemData CommonItemCreator::create_item(bool is_box, uint8_t episode,
case 0x06: // consumable case 0x06: // consumable
item.item_data1[0] = 0x03; item.item_data1[0] = 0x03;
item.item_data1[5] = 0x01; item.item_data1[5] = 0x01;
item.item_data1[1] = rand() & 1; // mate or fluid item.item_data1[1] = random_int(0, 1); // mate or fluid
if (difficulty == 0) { if (difficulty == 0) {
item.item_data1[2] = rand() & 1; // only mono and di on normal item.item_data1[2] = random_int(0, 1); // only mono and di on normal
} else if (difficulty == 3) { } else if (difficulty == 3) {
item.item_data1[2] = 1 + rand() & 1; // only di and tri on ultimate item.item_data1[2] = random_int(1, 2); // only di and tri on ultimate
} else { } else {
item.item_data1[2] = rand() % 3; // else, any of the three item.item_data1[2] = random_int(0, 2); // else, any of the three
} }
break; break;
case 0x07: // meseta case 0x07: // meseta
item.item_data1[0] = 0x04; item.item_data1[0] = 0x04;
item.item_data2d = (90 * difficulty) + ((rand() % 20) * (area * 2)); // meseta amount item.item_data2d = (90 * difficulty) + (random_int(0, 20) * (area * 2)); // meseta amount
break; break;
default: default:
+1
View File
@@ -3,6 +3,7 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <phosg/Filesystem.hh> #include <phosg/Filesystem.hh>
#include <phosg/Random.hh>
#include <phosg/Strings.hh> #include <phosg/Strings.hh>
#include "Compression.hh" #include "Compression.hh"
+2 -3
View File
@@ -1,6 +1,7 @@
#include "RareItemSet.hh" #include "RareItemSet.hh"
#include <phosg/Filesystem.hh> #include <phosg/Filesystem.hh>
#include <phosg/Random.hh>
using namespace std; using namespace std;
@@ -19,7 +20,5 @@ bool sample_rare_item(uint8_t pc) {
shift = 0; shift = 0;
} }
uint32_t rate = ((2 << shift) * ((pc & 7) + 7)); uint32_t rate = ((2 << shift) * ((pc & 7) + 7));
return (random_object<uint32_t>() < rate);
uint32_t x = ((rand() << 30) | (rand() << 15) | rand());
return (x < rate);
} }
+3 -2
View File
@@ -4,6 +4,7 @@
#include <memory> #include <memory>
#include <phosg/Filesystem.hh> #include <phosg/Filesystem.hh>
#include <phosg/Random.hh>
#include <phosg/Strings.hh> #include <phosg/Strings.hh>
#include <phosg/Time.hh> #include <phosg/Time.hh>
@@ -1369,7 +1370,7 @@ shared_ptr<Lobby> create_game_generic(shared_ptr<ServerState> s,
if (game->mode == 3) { if (game->mode == 3) {
for (size_t x = 0; x < 0x20; x++) { for (size_t x = 0; x < 0x20; x++) {
game->variations[x] = rand() % variation_maxes_solo[(episode - 1)][x]; game->variations[x] = random_int(0, variation_maxes_solo[(episode - 1)][x] - 1);
} }
for (size_t x = 0; x < 0x10; x++) { for (size_t x = 0; x < 0x10; x++) {
for (const char* type_char = "sm"; *type_char; type_char++) { for (const char* type_char = "sm"; *type_char; type_char++) {
@@ -1389,7 +1390,7 @@ shared_ptr<Lobby> create_game_generic(shared_ptr<ServerState> s,
} }
} else { } else {
for (size_t x = 0; x < 0x20; x++) { for (size_t x = 0; x < 0x20; x++) {
game->variations[x] = rand() % variation_maxes_online[(episode - 1)][x]; game->variations[x] = random_int(0, variation_maxes_online[(episode - 1)][x] - 1);
} }
for (size_t x = 0; x < 0x10; x++) { for (size_t x = 0; x < 0x10; x++) {
auto filename = string_printf( auto filename = string_printf(
+11 -16
View File
@@ -1,6 +1,7 @@
#include "SendCommands.hh" #include "SendCommands.hh"
#include <memory> #include <memory>
#include <phosg/Random.hh>
#include <phosg/Strings.hh> #include <phosg/Strings.hh>
#include <phosg/Time.hh> #include <phosg/Time.hh>
@@ -109,8 +110,8 @@ static void send_server_init_dc_pc_gc(shared_ptr<Client> c, const char* copyrigh
char after_message[200]; char after_message[200];
} cmd; } cmd;
uint32_t server_key = (rand() << 30) | (rand() << 15) | rand(); uint32_t server_key = random_object<uint32_t>();
uint32_t client_key = (rand() << 30) | (rand() << 15) | rand(); uint32_t client_key = random_object<uint32_t>();
memset(&cmd, 0, sizeof(cmd)); memset(&cmd, 0, sizeof(cmd));
strcpy(cmd.copyright, copyright_text); strcpy(cmd.copyright, copyright_text);
@@ -146,20 +147,14 @@ static void send_server_init_bb(shared_ptr<Client> c, bool initial_connection) {
memset(&cmd, 0, sizeof(cmd)); memset(&cmd, 0, sizeof(cmd));
strcpy(cmd.copyright, bb_game_server_copyright); strcpy(cmd.copyright, bb_game_server_copyright);
uint8_t server_key[0x30]; random_data(cmd.server_key, 0x30);
uint8_t client_key[0x30]; random_data(cmd.client_key, 0x30);
for (size_t x = 0; x < 0x30; x++) {
server_key[x] = rand();
client_key[x] = rand();
}
memcpy(cmd.server_key, server_key, 0x30);
memcpy(cmd.client_key, client_key, 0x30);
strcpy(cmd.after_message, anti_copyright); strcpy(cmd.after_message, anti_copyright);
send_command(c, 0x03, 0x00, cmd); send_command(c, 0x03, 0x00, cmd);
rw_guard(c->lock, true); rw_guard(c->lock, true);
c->crypt_out.reset(new PSOBBEncryption(server_key)); c->crypt_out.reset(new PSOBBEncryption(cmd.server_key));
c->crypt_in.reset(new PSOBBEncryption(client_key)); c->crypt_in.reset(new PSOBBEncryption(cmd.client_key));
} }
static void send_server_init_patch(shared_ptr<Client> c, bool initial_connection) { static void send_server_init_patch(shared_ptr<Client> c, bool initial_connection) {
@@ -171,8 +166,8 @@ static void send_server_init_patch(shared_ptr<Client> c, bool initial_connection
// anti-copyright message... lawyers plz be kind kthx // anti-copyright message... lawyers plz be kind kthx
} cmd; } cmd;
uint32_t server_key = (rand() << 30) | (rand() << 15) | rand(); uint32_t server_key = random_object<uint32_t>();
uint32_t client_key = (rand() << 30) | (rand() << 15) | rand(); uint32_t client_key = random_object<uint32_t>();
memset(&cmd, 0, sizeof(cmd)); memset(&cmd, 0, sizeof(cmd));
strcpy(cmd.copyright, patch_server_copyright); strcpy(cmd.copyright, patch_server_copyright);
@@ -271,7 +266,7 @@ void send_client_init_bb(shared_ptr<Client> c, uint32_t error) {
error, error,
0x00000100, 0x00000100,
c->license->serial_number, c->license->serial_number,
static_cast<uint32_t>((rand() << 30) | (rand() << 15) | rand()), static_cast<uint32_t>(random_object<uint32_t>()),
c->config, c->config,
0x00000102, 0x00000102,
}; };
@@ -1891,7 +1886,7 @@ void send_bank(shared_ptr<Client> c) {
vector<PlayerBankItem> items(c->player.bank.items, vector<PlayerBankItem> items(c->player.bank.items,
&c->player.bank.items[c->player.bank.num_items]); &c->player.bank.items[c->player.bank.num_items]);
uint32_t checksum = (rand() << 30) | (rand() << 15) | rand(); uint32_t checksum = random_object<uint32_t>();
struct { struct {
uint8_t subcommand; uint8_t subcommand;
uint8_t unused1; uint8_t unused1;