function speedup_random_row($id, $table, $where) { $cia = fetch_first("select count({$id}) as c, min({$id}) as i, max({$id}) as a from {$table} where {$where}", __FILE__, __LINE__, __FUNCTION__); $count = $cia['c']; $min = $cia['i']; $max = $cia['a']; if ($count > 0) { $range = $max - $min; $m = rand($min, $max); $root = ceil(sqrt($range)); $a = $m - $root; $b = $m + $root; $whereid = " {$a}<={$id} and {$id}<={$b} "; $gotsome = fetch_value("select {$id} from {$table} where {$where} and {$whereid}", __FILE__, __LINE__, __FUNCTION__); if ($gotsome) { return $whereid; } else { //play it safe return " 1=1 "; } } else { return " 1=1 "; } }
function fetch_random_item_for_location($location) { $query = "select item_id as id, item_type as type, number from phaos_ground where location= '{$location}' order by RAND() LIMIT 1"; //defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][] = __FUNCTION__.":". $query; return fetch_first($query, __FILE__, __LINE__, __FUNCTION__); }
/** * constructor * @param character id */ function character($id) { $result = mysql_query("SELECT * FROM phaos_characters WHERE id = '{$id}'"); if ($row = mysql_fetch_array($result)) { //define main vars $this->id = $row["id"]; $this->name = $row["name"]; $this->user = $row["username"]; $this->cclass = $row["class"]; $this->race = $row["race"]; $this->sex = $row["sex"]; $this->image = $row["image_path"]; $this->age = $row["age"]; $this->location = $row["location"]; //define attribute vars $this->strength = $row["strength"]; $this->dexterity = $row["dexterity"]; $this->wisdom = $row["wisdom"]; $this->constitution = $row["constitution"]; //define changeable vars $this->hit_points = $row["hit_points"]; $this->stamina_points = $row["stamina"]; if ($row['level'] == 0 or $row['level'] == "") { $this->level = 1; } else { $this->level = (int) $row['level']; } $this->xp = (int) $row["xp"]; $this->gold = $row["gold"]; $this->stat_points = $row["stat_points"]; //define equipment vars $this->weapon = $row["weapon"]; $this->armor = $row["armor"]; $this->boots = $row["boots"]; $this->shield = $row["shield"]; $this->gloves = $row["gloves"]; $this->helm = $row["helm"]; //FIX //at some point during development some characters had negative strength //this should not happen usually if ($this->constitution < 1) { $this->constitution = 1; } if ($this->strength < 1) { $this->strength = 1; } //calculated stuff $this->available_points = $this->strength + $this->dexterity + $this->wisdom + $this->constitution; $this->max_hp = $this->constitution * 6; $this->max_stamina = ($this->constitution + $this->strength) * 10; $this->max_rep = 7; //other stuff $this->regen_time = $row["regen_time"]; $this->stamina_time = $row["stamina_time"]; $this->rep_time = $row["rep_time"]; $this->no_regen_hp = $row["hit_points"]; //regeneration $actTime = time(); $this->time_since_regen = $actTime - $this->regen_time; $this->stamina_time_since_regen = $actTime - $this->stamina_time; $this->rep_time_since_regen = $actTime - $this->rep_time; //skills $this->fight = $row["fight"]; $this->defence = $row["defence"]; $this->weaponless = $row["weaponless"]; $this->lockpick = $row["lockpick"]; $this->traps = $row["traps"]; //reputation $this->rep_points = $row["rep_points"]; $this->rep_helpfull = $row["rep_helpfull"]; $this->rep_generious = $row["rep_generious"]; $this->rep_combat = $row["rep_combat"]; //weapon & fight Calculation //fill weapon: $result = mysql_query("SELECT * FROM phaos_weapons WHERE id = '" . $this->weapon . "'"); if ($row = mysql_fetch_array($result)) { $this->weapon_min = $row["min_damage"]; $this->weapon_max = $row["max_damage"]; $this->weapon_name = $row["name"]; } else { $this->weapon = 0; $this->weapon_min = 0; $this->weapon_max = 1; $this->weapon_name = 'Bare Hands'; } //fill armor $result = mysql_query("SELECT * FROM phaos_armor WHERE id = '" . $this->armor . "'"); if ($row = mysql_fetch_array($result)) { $this->armor_ac = $row["armor_class"]; } else { $this->armor_ac = 0; } $result = mysql_query("SELECT * FROM phaos_boots WHERE id = '" . $this->boots . "'"); if ($row = mysql_fetch_array($result)) { $this->boots_ac = $row["armor_class"]; } else { $this->boots_ac = 0; } $result = mysql_query("SELECT * FROM phaos_gloves WHERE id = '" . $this->gloves . "'"); if ($row = mysql_fetch_array($result)) { $this->gloves_ac = $row["armor_class"]; } else { $this->gloves_ac = 0; } $result = mysql_query("SELECT * FROM phaos_shields WHERE id = '" . $this->shield . "'"); if ($row = mysql_fetch_array($result)) { $this->shield_ac = $row["armor_class"]; } else { $this->shield_ac = 0; } $result = mysql_query("SELECT * FROM phaos_helmets WHERE id = '" . $this->helm . "'"); if ($row = mysql_fetch_array($result)) { $this->helm_ac = $row["armor_class"]; } else { $this->helm_ac = 0; } $this->max_inventory = $this->strength * 5; } else { global $lang_na; $this->name = $lang_na; $this->strength = $lang_na; $this->dexterity = $lang_na; $this->wisdom = $lang_na; $this->constitution = $lang_na; $this->hit_points = $lang_na; $this->max_hp = $lang_na; $this->weapon = $lang_na; $this->armor = $lang_na; $this->boots = $lang_na; $this->shield = $lang_na; $this->gloves = $lang_na; $this->helm = $lang_na; $this->level = $lang_na; $this->next_lev_xp = $lang_na; $this->xp = $lang_na; $this->gold = $lang_na; $this->available_points = $lang_na; } if (!$this->image) { if ($this->user == 'phaos_npc') { $this->image = "images/monster/forest_troll.gif"; } else { $this->image = "images/icons/characters/character_1.gif"; } } //get location to be able to have location modifiers $this->location_data = fetch_first("select * from phaos_locations where id='{$this->location}'"); //FIXME: since characters now have location data, many places in the code don't need to fetch it. }
$item = fetch_random_item_for_location($shop_basics['item_location_id']); if ($item && $item['id'] && $item['type'] != 'gold') { $item['number'] = 1; $item['number'] = item_pickup($shop_basics['item_location_id'], $item); $restock += $item['number']; } //take inventory $items = fetch_items_for_location($shop_basics['item_location_id']); foreach ($items as $item) { @($inventory[$item['type']] += $item['number']); } //add items $tries = 40; //FIXME: replace the completely random selection of refill candidates by choosing only those with a deficit while ($restock > 0 && $tries-- > 0) { $shop_refill = fetch_first("SELECT * FROM phaos_shop_refill WHERE shop_id='{$shop_id}' order by rand()*item_count_min DESC LIMIT 1", __FILE__, __LINE__); if (!$shop_refill) { break; //stop loop } if (@$inventory[$shop_refill['item_type']] >= $shop_refill['item_count_min']) { continue; //next try } defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][] = "passed({$shop_refill['item_type']}):(" . @$inventory[$shop_refill['item_type']] . ">=" . $shop_refill['item_count_min'] . ")"; $minvalue = $shop_refill['item_value_min']; $maxvalue = (int) ($minvalue * $shop_refill['item_value_growth'] * powrand($shop_refill['item_value_growth'], $shop_refill['item_value_growth_probability'], 23)); $item = random_item($minvalue, $maxvalue, $shop_refill['item_type'], $shop_refill['item_name_like']); defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][] = "{$item['type']}({$item['id']}) from between values ({$minvalue},{$maxvalue})"; if ($item) { item_drop($shop_basics['item_location_id'], $item);