Soron

Hackmud inventory sorter in lisp

Oct 26th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. ;;; can be run in-game with: soron.lisp{run:"/bin/sort-inv", upg:#s.sys.upgrades}
  2. ;;; can be viewed in-game with: soron.lisp{ev:"(prn ($js-str (slurp '/bin/sort-inv)))"}
  3.  
  4. (do
  5. (def! get-inv-data (fn* ()
  6. (let*
  7. (
  8. num-list (map (fn* (a b) b) ($scr 'upg))
  9. )
  10. (map (fn* (a i) (do ($js-set a '_index i) a)) ($scr 'upg (list->hash (list 'info num-list))))
  11. )
  12. ))
  13.  
  14. (def! inv-loaded-str (fn* (data)
  15. ($js-call (map (fn* (a) (if ($js a 'loaded) '+ '-)) data) 'join "")
  16. ))
  17.  
  18. (def! last-char (fn* (str char anchor)
  19. ($js-call str 'lastIndexOf char anchor)
  20. ))
  21. (def! first-char (fn* (str char anchor)
  22. ($js-call str 'indexOf char anchor)
  23. ))
  24.  
  25.  
  26.  
  27. (let*
  28. (
  29. data (get-inv-data),
  30. loaded-str (inv-loaded-str data),
  31. rev-swaps (list)
  32. )
  33. (do
  34. (def! move-uneq-first (fn* (ind)
  35. (let*
  36. (uneq-last (last-char loaded-str '- ind))
  37. (if (> uneq-last (first-char loaded-str '+))
  38. (do
  39. ($js-call rev-swaps 'push (list->hash (list 'from uneq-last, 'to 0)))
  40. (move-uneq-first (- uneq-last 1))
  41. )
  42. )
  43. )
  44. ))
  45.  
  46. (move-uneq-first (last-char loaded-str '+))
  47. (let* (swaps ($js-call rev-swaps 'reverse)) (do
  48. (prn (->json swaps))
  49. ($scr 'upg (list->hash (list 'reorder swaps)))
  50. ))
  51. )
  52. )
  53.  
  54.  
  55. (let*
  56. (
  57. full_data (get-inv-data),
  58. loaded-str (inv-loaded-str full_data),
  59. data ($js-call full_data 'slice 0 (+ 1 (last-char loaded-str '+))),
  60. )
  61. (do
  62. ($js-call data 'sort
  63. (fn* (a b)
  64. (if
  65. (and
  66. ($js a 'loaded)
  67. (and
  68. ($js b 'loaded)
  69. (and
  70. ($eq ($js a 'type) 'lock)
  71. ($eq ($js b 'type) 'lock)
  72. )
  73. )
  74. )
  75. ; DO NOT CHANGE ORDER OF EQUIPPED LOCKS
  76. (- ($js a '_index) ($js b '_index))
  77. ; but sort everything else
  78. ($js-call
  79. (list
  80. (if ($js a 'loaded)
  81. (+
  82. 10000
  83. (if ($eq ($js a 'type) 'lock)
  84. -100000
  85. 0
  86. )
  87. )
  88. 0
  89. )
  90. (if ($js b 'loaded)
  91. (+
  92. -10000
  93. (if ($eq ($js b 'type) 'lock)
  94. 100000
  95. 0
  96. )
  97. )
  98. 0
  99. )
  100.  
  101.  
  102. ; ASCIIbetical sort on name, first
  103. (if (< ($js b 'name) ($js a 'name))
  104. 10
  105. (if (> ($js b 'name) ($js a 'name))
  106. -10
  107. 0
  108. )
  109. ),
  110.  
  111. ; rare things LAST (because of tier-based ASCII sort)
  112. (- ($js a 'rarity) ($js b 'rarity)),
  113. )
  114. 'reduce
  115. +
  116. )
  117. )
  118. )
  119. )
  120.  
  121. (let*
  122. (
  123. nums (map (fn* (v i) i) data)
  124. swaps ($js-call
  125. (map
  126. (fn* (v i)
  127. (let*
  128. (
  129. orig ($js v '_index)
  130. from ($js-call nums 'indexOf orig),
  131. oper (list->hash (list
  132. 'name ($js v 'name),
  133. 'from from,
  134. 'to i,
  135. )),
  136. )
  137. (do
  138. ($js-call nums 'splice i 0 ($js ($js-call nums 'splice from 1) 0))
  139. oper
  140. )
  141. )
  142. )
  143. data
  144. )
  145. 'filter
  146. (fn* (a) (not ($eq ($js a 'from) ($js a 'to))))
  147. )
  148. )
  149. (do
  150. (prn (->json swaps))
  151. ($scr 'upg (list->hash (list 'reorder swaps)))
  152. )
  153. )
  154. )
  155. )
  156. )
Add Comment
Please, Sign In to add comment