IF.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. local InfoWindow = require("netpacks.InfoWindow")
  2. local ReceiverBase = require("core:erm.ReceiverBase")
  3. local IF = ReceiverBase:new()
  4. function IF:new(ERM)
  5. return ReceiverBase.new(self,{ERM = ERM})
  6. end
  7. --[[
  8. according to the ERM help:
  9. "%%" -> "%"
  10. "%F#" -> current value of # flag.
  11. "%Vf"..."%Vt" -> current value of corresponding variable.
  12. "%W1"..."%W100" -> current value of corresponding hero variable.
  13. "%X1"..."%X16" -> current value of corresponding function parameter.
  14. "%Y1"..."%Y100" -> current value of corresponding local variable.
  15. "%Z1"..."%Z500" -> current value of corresponding string variable.
  16. "%$macro$" -> macro name of corresponding variable
  17. "%Dd" -> current day of week
  18. "%Dw" -> current week
  19. "%Dm" -> current month
  20. "%Da" -> current day from beginning of the game
  21. "%Gc" -> the color of current gamer in text
  22. ]]
  23. function IF:M(x, message)
  24. local pack = InfoWindow.new()
  25. pack:setPlayer(GAME:getCurrentPlayer())
  26. local _self = self
  27. local onMatch1 = function (key1, key2)
  28. if key1 == 'X' then
  29. return x[key2]
  30. end
  31. if key1 == 'V' then
  32. return _self.ERM.v[key2]
  33. end
  34. if key1 == 'Z' then
  35. return _self.ERM.z[key2]
  36. end
  37. if key1 == 'F' then
  38. return _self.ERM.F[key2]
  39. end
  40. return nil
  41. end
  42. local onMatch2 = function (key1, key2)
  43. if key1 == 'V' then
  44. return _self.ERM.Q[key2]
  45. end
  46. return nil
  47. end
  48. message = string.gsub(message, "%%([FVWXYZ])([%d]+)", onMatch1)
  49. message = string.gsub(message, "%%(V)(%w)", onMatch2)
  50. message = string.gsub(message, "(%%)(%%)", "%1")
  51. pack:addText(message)
  52. SERVER:commitPackage(pack)
  53. end
  54. function IF:W(x, heroIndex)
  55. if heroIndex == nil then
  56. return tonumber(DATA.ERM.wKey or -2)
  57. else
  58. if heroIndex == -1 then
  59. error("IF:W-1 not implemented")
  60. end
  61. DATA.ERM.wKey = tostring(heroIndex)
  62. end
  63. end
  64. return IF