hugmeir

Untitled

Sep 13th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. // NOTE! This requires Ezandora's bastille override to be installed.
  2. // It also steals code wholesale from her. All bugs are mine.
  3. import "scripts/bastille.ash";
  4.  
  5. boolean can_play() {
  6. buffer html = visit_url("inv_use.php?which=3&whichitem=9928");
  7. matcher m = create_matcher('bbatt/start\\.png', html);
  8. if ( m.find() ) {
  9. BastilleStateParse(html, true);
  10. return true;
  11. }
  12. return false;
  13. }
  14.  
  15. void walk_away() {
  16. visit_url("choice.php?whichchoice=1313&option=8");
  17. }
  18.  
  19. // Stolen wholesale from Ezandora:
  20. Record CheeseDataEntry
  21. {
  22. string name;
  23. int turn;
  24. int relevant_stat_value;
  25. int cheese_gained;
  26. };
  27. int [string] relevantStatsForButton(string button_text)
  28. {
  29. int [string] relevant_stats;
  30. if (button_text == "Put on the bad art show")
  31. relevant_stats["Psychological defense"] = -1;
  32. else if (button_text == "Enter the childrens' art contest")
  33. relevant_stats["Psychological attack strength"] = -1;
  34. else if (button_text == "Try the wall thing")
  35. relevant_stats["Castle defense"] = 1;
  36. else if (button_text == "Raid the cave")
  37. relevant_stats["Military attack strength"] = 1;
  38. else if (button_text == "Convert the barracks")
  39. relevant_stats["Military defense"] = 1;
  40. else if (button_text == "Raid the cart" || button_text == "Grab the boulder" || button_text == "Scrape out the mine")
  41. relevant_stats["none"] = 1;
  42. else if (button_text == "Enter the Weakest Army competition")
  43. relevant_stats["Military attack strength"] = -1;
  44. else if (button_text == "Let the cheese horse in")
  45. relevant_stats["Military defense"] = -1;
  46. else if (button_text == "Shoot the glacier")
  47. relevant_stats["Castle attack strength"] = 1;
  48. else if (button_text == "Stand in the waterfall")
  49. relevant_stats["Castle defense"] = -1;
  50. else if (button_text == "Have the cheese contest")
  51. relevant_stats["Psychological defense"] = 1;
  52. else if (button_text == "Rob the suburb")
  53. relevant_stats["Psychological attack strength"] = 1;
  54. else if (button_text == "Submit embarrassing catapult photos")
  55. relevant_stats["Castle attack strength"] = -1;
  56. return relevant_stats;
  57. }
  58.  
  59. float calculateAverageCheeseGained(CheeseDataEntry [int] cheese_data, string button_text) {
  60. matcher m = create_matcher('wishing well', button_text);
  61. if ( m.find() )
  62. return 300.0;
  63.  
  64. int [string] relevant_stats = relevantStatsForButton(button_text);
  65. int relevant_stat_index = -1;
  66. foreach stat_name in relevant_stats
  67. {
  68. if (__stat_name_to_index contains stat_name)
  69. relevant_stat_index = __stat_name_to_index[stat_name];
  70. }
  71. float average_cheese_gained = 0;
  72. int average_cheese_count = 0;
  73. foreach key, entry in cheese_data
  74. {
  75. if (entry.name != button_text) continue;
  76. if (relevant_stat_index != -1 && __bastille_state.stats[relevant_stat_index] != entry.relevant_stat_value) continue;
  77. average_cheese_gained += entry.cheese_gained;
  78. average_cheese_count += 1;
  79. }
  80. if (average_cheese_count != 0)
  81. average_cheese_gained /= to_float(average_cheese_count);
  82.  
  83.  
  84. if (average_cheese_count == 0 && relevant_stat_index != -1)
  85. {
  86. average_cheese_gained = 0.0;
  87. //Simple linear regression:
  88. float x_sum = 0.0;
  89. float y_sum = 0.0;
  90. float xy_sum = 0.0;
  91. float x_squared_sum = 0.0;
  92. float y_squared_sum = 0.0;
  93. float n = 0.0;
  94. foreach key, entry in cheese_data
  95. {
  96. if (entry.name != button_text) continue;
  97. if (entry.relevant_stat_value <= 0.0) continue;
  98.  
  99. float x = entry.relevant_stat_value;
  100. float y = entry.cheese_gained;
  101.  
  102. x_sum += x;
  103. y_sum += y;
  104. xy_sum += x * y;
  105. x_squared_sum += x * x;
  106. y_squared_sum += y * y;
  107. n += 1;
  108. }
  109. float a = y_sum * x_squared_sum - x_sum * xy_sum;
  110. float denom = n * x_squared_sum - x_sum * x_sum;
  111. if (denom != 0.0)
  112. a /= denom;
  113. else
  114. return 0.0;
  115. float b = n * xy_sum - x_sum * y_sum;
  116. denom = n * x_squared_sum - x_sum * x_sum;
  117. if (denom != 0.0)
  118. b /= denom;
  119. else
  120. return 0.0;
  121.  
  122. average_cheese_gained = b * __bastille_state.stats[relevant_stat_index] + a;
  123. }
  124. return average_cheese_gained;
  125. }
  126.  
  127. int find_best_cheese_option(CheeseDataEntry [int] cheese_data, buffer page_text) {
  128. string [int][int] buttons = page_text.group_string("<input class=button type=submit value=\"(.*?)\">");
  129. int chosen_button = 1;
  130. int idx = 0;
  131. float max_cheese_gained = 0;
  132. foreach key in buttons {
  133. idx = idx + 1;
  134. string button_text = buttons[key][1];
  135.  
  136. float cheese_gained = calculateAverageCheeseGained(cheese_data, button_text);
  137. if ( cheese_gained > max_cheese_gained ) {
  138. chosen_button = idx;
  139. max_cheese_gained = cheese_gained;
  140. }
  141. }
  142.  
  143. return chosen_button;
  144. }
  145.  
  146. void main() {
  147. CheeseDataEntry [int] cheese_data;
  148. file_to_map("Bastille Cheese Data.txt", cheese_data);
  149.  
  150. if ( !can_play() ) {
  151. walk_away();
  152. return;
  153. }
  154.  
  155. visit_url("choice.php?whichchoice=1313&option=5");
  156.  
  157. boolean razed = false;
  158. while ( !razed ) {
  159. for i from 1 to 2 {
  160. buffer page_text = visit_url("choice.php?whichchoice=1314&option=3");
  161. int button = find_best_cheese_option(cheese_data, page_text);
  162. visit_url("choice.php?whichchoice=1319&option=" + button);
  163. }
  164. buffer battle_result = visit_url("choice.php?whichchoice=1315&option=2");
  165. matcher razed_matcher = create_matcher('You survived for [0-9]+ turns and collected ([0-9]+) cheese', battle_result);
  166. if ( razed_matcher.find() ) {
  167. razed = true;
  168. int collected_cheese = group(razed_matcher, 1).to_int();
  169. print("COLLECTED CHEESE: " + collected_cheese);
  170. if ( collected_cheese > 1400 ) {
  171. visit_url("choice.php?whichchoice=1316&option=1");
  172. }
  173. else {
  174. visit_url("choice.php?whichchoice=1316&option=3");
  175. }
  176. }
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment