2
0

randomizer.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. --- @module randomizer
  2. -- A utility module that outputs a random result from a set of options
  3. local randomizer = {
  4. name = "Randomizer",
  5. slug = "randomizer",
  6. desc = "A utility module other modules use for random decisions",
  7. hide = true
  8. }
  9. randomizer.data = {
  10. }
  11. -- options_table should be a table of strings
  12. randomizer.config = {
  13. options_table = {},
  14. num_results = 1,
  15. result_table = {}
  16. }
  17. function randomizer:initiate(result)
  18. self.data.result = result
  19. self.data.options_table = modpol.util.copy_table(self.config.options_table)
  20. -- if options table is empty, randomizer returns that
  21. if #self.data.options_table == 0 or self.config.num_results == 0 then
  22. if self.data.result then
  23. self.data.result({}) end
  24. self.org:delete_process(self.id)
  25. else
  26. -- otherwise, choose a random result
  27. self.random_loop()
  28. end
  29. end
  30. -- returns result_table
  31. function randomizer:random_loop()
  32. self.data.results = 0
  33. if results == self.config.num_results then
  34. self.data.result(self.data.result_table)
  35. else
  36. math.randomseed(os.time())
  37. local index = math.random(self.data.options_table)
  38. table.insert(self.data.result_table, self.data.options_table[index])
  39. table.remove(self.data.options_table, index)
  40. self.data.results = self.data.results + 1
  41. end
  42. end
  43. modpol.modules.randomizer = randomizer