Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NOTE! This requires Ezandora's bastille override to be installed.
- // It also steals code wholesale from her. All bugs are mine.
- import "scripts/bastille.ash";
- boolean can_play() {
- buffer html = visit_url("inv_use.php?which=3&whichitem=9928");
- matcher m = create_matcher('bbatt/start\\.png', html);
- if ( m.find() ) {
- BastilleStateParse(html, true);
- return true;
- }
- return false;
- }
- void walk_away() {
- visit_url("choice.php?whichchoice=1313&option=8");
- }
- // Stolen wholesale from Ezandora:
- Record CheeseDataEntry
- {
- string name;
- int turn;
- int relevant_stat_value;
- int cheese_gained;
- };
- int [string] relevantStatsForButton(string button_text)
- {
- int [string] relevant_stats;
- if (button_text == "Put on the bad art show")
- relevant_stats["Psychological defense"] = -1;
- else if (button_text == "Enter the childrens' art contest")
- relevant_stats["Psychological attack strength"] = -1;
- else if (button_text == "Try the wall thing")
- relevant_stats["Castle defense"] = 1;
- else if (button_text == "Raid the cave")
- relevant_stats["Military attack strength"] = 1;
- else if (button_text == "Convert the barracks")
- relevant_stats["Military defense"] = 1;
- else if (button_text == "Raid the cart" || button_text == "Grab the boulder" || button_text == "Scrape out the mine")
- relevant_stats["none"] = 1;
- else if (button_text == "Enter the Weakest Army competition")
- relevant_stats["Military attack strength"] = -1;
- else if (button_text == "Let the cheese horse in")
- relevant_stats["Military defense"] = -1;
- else if (button_text == "Shoot the glacier")
- relevant_stats["Castle attack strength"] = 1;
- else if (button_text == "Stand in the waterfall")
- relevant_stats["Castle defense"] = -1;
- else if (button_text == "Have the cheese contest")
- relevant_stats["Psychological defense"] = 1;
- else if (button_text == "Rob the suburb")
- relevant_stats["Psychological attack strength"] = 1;
- else if (button_text == "Submit embarrassing catapult photos")
- relevant_stats["Castle attack strength"] = -1;
- return relevant_stats;
- }
- float calculateAverageCheeseGained(CheeseDataEntry [int] cheese_data, string button_text) {
- matcher m = create_matcher('wishing well', button_text);
- if ( m.find() )
- return 300.0;
- int [string] relevant_stats = relevantStatsForButton(button_text);
- int relevant_stat_index = -1;
- foreach stat_name in relevant_stats
- {
- if (__stat_name_to_index contains stat_name)
- relevant_stat_index = __stat_name_to_index[stat_name];
- }
- float average_cheese_gained = 0;
- int average_cheese_count = 0;
- foreach key, entry in cheese_data
- {
- if (entry.name != button_text) continue;
- if (relevant_stat_index != -1 && __bastille_state.stats[relevant_stat_index] != entry.relevant_stat_value) continue;
- average_cheese_gained += entry.cheese_gained;
- average_cheese_count += 1;
- }
- if (average_cheese_count != 0)
- average_cheese_gained /= to_float(average_cheese_count);
- if (average_cheese_count == 0 && relevant_stat_index != -1)
- {
- average_cheese_gained = 0.0;
- //Simple linear regression:
- float x_sum = 0.0;
- float y_sum = 0.0;
- float xy_sum = 0.0;
- float x_squared_sum = 0.0;
- float y_squared_sum = 0.0;
- float n = 0.0;
- foreach key, entry in cheese_data
- {
- if (entry.name != button_text) continue;
- if (entry.relevant_stat_value <= 0.0) continue;
- float x = entry.relevant_stat_value;
- float y = entry.cheese_gained;
- x_sum += x;
- y_sum += y;
- xy_sum += x * y;
- x_squared_sum += x * x;
- y_squared_sum += y * y;
- n += 1;
- }
- float a = y_sum * x_squared_sum - x_sum * xy_sum;
- float denom = n * x_squared_sum - x_sum * x_sum;
- if (denom != 0.0)
- a /= denom;
- else
- return 0.0;
- float b = n * xy_sum - x_sum * y_sum;
- denom = n * x_squared_sum - x_sum * x_sum;
- if (denom != 0.0)
- b /= denom;
- else
- return 0.0;
- average_cheese_gained = b * __bastille_state.stats[relevant_stat_index] + a;
- }
- return average_cheese_gained;
- }
- int find_best_cheese_option(CheeseDataEntry [int] cheese_data, buffer page_text) {
- string [int][int] buttons = page_text.group_string("<input class=button type=submit value=\"(.*?)\">");
- int chosen_button = 1;
- int idx = 0;
- float max_cheese_gained = 0;
- foreach key in buttons {
- idx = idx + 1;
- string button_text = buttons[key][1];
- float cheese_gained = calculateAverageCheeseGained(cheese_data, button_text);
- if ( cheese_gained > max_cheese_gained ) {
- chosen_button = idx;
- max_cheese_gained = cheese_gained;
- }
- }
- return chosen_button;
- }
- void main() {
- CheeseDataEntry [int] cheese_data;
- file_to_map("Bastille Cheese Data.txt", cheese_data);
- if ( !can_play() ) {
- walk_away();
- return;
- }
- visit_url("choice.php?whichchoice=1313&option=5");
- boolean razed = false;
- while ( !razed ) {
- for i from 1 to 2 {
- buffer page_text = visit_url("choice.php?whichchoice=1314&option=3");
- int button = find_best_cheese_option(cheese_data, page_text);
- visit_url("choice.php?whichchoice=1319&option=" + button);
- }
- buffer battle_result = visit_url("choice.php?whichchoice=1315&option=2");
- matcher razed_matcher = create_matcher('You survived for [0-9]+ turns and collected ([0-9]+) cheese', battle_result);
- if ( razed_matcher.find() ) {
- razed = true;
- int collected_cheese = group(razed_matcher, 1).to_int();
- print("COLLECTED CHEESE: " + collected_cheese);
- if ( collected_cheese > 1400 ) {
- visit_url("choice.php?whichchoice=1316&option=1");
- }
- else {
- visit_url("choice.php?whichchoice=1316&option=3");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment