Guest User

mixbotv2

a guest
Jan 9th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import <zlib.ash>;
  2.  
  3. boolean MIXBOT_REFRESHED = false;
  4. string MIX_MESSAGE = "Thank you for choosing FREE BEER as your bartender today!";
  5. string NOMIX_MESSAGE = "";
  6.  
  7. string [string, item, item] RECIPES;
  8.  
  9. string as_list(int[item] stuff) {
  10. string s = "";
  11. foreach thing in stuff {
  12. if (s != "") {
  13. s += ", ";
  14. }
  15. s += stuff[thing] + " " + thing;
  16. }
  17. return s;
  18. }
  19.  
  20. int[item] diff(int[item] before, int[item] after) {
  21. int[item] delta;
  22. foreach it in before {
  23. if (before[it] != after[it]) {
  24. delta[it] = after[it] - before[it];
  25. }
  26. }
  27. foreach it in after {
  28. if (before[it] != after[it]) {
  29. delta[it] = after[it] - before[it];
  30. }
  31. }
  32. return delta;
  33. }
  34.  
  35. int[item] sum(int[item] first, int[item] second) {
  36. int[item] delta;
  37. foreach it in first {
  38. if (first[it] != 0) {
  39. delta[it] += first[it];
  40. }
  41. }
  42. foreach it in second {
  43. if (second[it] != 0) {
  44. delta[it] += second[it];
  45. }
  46. }
  47. return delta;
  48. }
  49.  
  50. void parse_recipes() {
  51. item [string, string, item] r;
  52. file_to_map("concoctions.txt", r);
  53. foreach target, str, src1, src2 in r {
  54. if (str == "MIX" || str == "ACOCK" || str == "SCOCK") {
  55. RECIPES["cocktail", src1, src2] = target;
  56. }
  57. }
  58. }
  59.  
  60.  
  61. int[item] mix(int[item] stuff) {
  62. int [item] before = get_inventory();
  63. foreach method in RECIPES {
  64. foreach it1 in stuff {
  65. if (stuff[it1] == 0) {
  66. continue;
  67. }
  68. foreach it2 in stuff {
  69. if (stuff[it2] == 0) {
  70. continue;
  71. }
  72. if (RECIPES[method, it1] contains it2) {
  73. print(method + " " + it1 + " + " + it2 + " = " + RECIPES[method, it1, it2]);
  74. int qty = min(stuff[it1], stuff[it2]);
  75. craft(method, qty, it1, it2);
  76. stuff[it1] -= qty;
  77. stuff[it2] -= qty;
  78. before[it1] -= qty;
  79. before[it2] -= qty;
  80. }
  81. }
  82. }
  83. }
  84. return sum(diff(before, get_inventory()), stuff);
  85. }
  86.  
  87. int [item] fullmix(int[item] stuff) {
  88. int[item] yield = mix(stuff);
  89. int[item] oldYield;
  90. while (count(diff(oldYield, yield)) > 0) {
  91. oldYield = yield;
  92. yield = mix(yield);
  93. }
  94. return yield;
  95. }
  96.  
  97.  
  98. boolean mixbot_process(kmessage msg) {
  99. print("Got " + as_list(msg.items) + " from " + msg.fromname + ".");
  100. int[item] yield = (msg.message.contains_text("fullmix")) ? fullmix(msg.items) : mix(msg.items);
  101. print("Mixed " + as_list(yield) + ".");
  102. if (count(yield) > 0) {
  103. kmail(msg.fromname, MIX_MESSAGE, 0, yield);
  104. return true;
  105. }
  106. return false;
  107. }
  108.  
  109. void mixbot() {
  110. MIXBOT_REFRESHED = false;
  111. load_kmail("mixbot");
  112. cli_execute("refresh inv");
  113. process_kmail("mixbot_process");
  114. }
  115.  
  116. parse_recipes();
  117.  
  118.  
  119. void main(string sender, string message, string channel){
  120. if (channel == "Events") {
  121. mixbot();
  122. }
  123. // less than 3 minutes remaining
  124. if (86400000 - gametime_to_int() < 180000) {
  125. waitq(900); // sleep 15 minutes
  126. cli_execute("login free beer");
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment