parrot_brain.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. local modpath = minetest.get_modpath("markov_macaws")
  2. local settings = Settings(modpath .. "/markov_macaws.conf")
  3. local S = minetest.get_translator("markov_macaws")
  4. local function parse_num(str)
  5. return 0 + str
  6. end
  7. local HEAR_DISTANCE = parse_num(settings:get("hear_distance"))
  8. local PARROT_TALK_CHANCE = parse_num(settings:get("talk_chance"))
  9. local PARROT_TALK_CHANCE_FRIEND = parse_num(settings:get("talk_chance_friend"))
  10. local function get_players_inside_radius(pos, radius)
  11. local inrad = {}
  12. local connected = minetest.get_connected_players()
  13. for i, player in ipairs(connected)
  14. do
  15. local ppos = player:get_pos()
  16. if vector.distance(pos, ppos) < radius
  17. then
  18. table.insert(inrad, player)
  19. end
  20. end
  21. return inrad
  22. end
  23. local prefix = "<" .. S("Parrot") .. "> "
  24. local function set_nametag(self, str)
  25. local objprops = self.object:get_properties()
  26. objprops.nametag = str
  27. self.object:set_properties(objprops)
  28. end
  29. local function talk(self)
  30. end
  31. local function sign(x)
  32. return x < 0 and -1 or (x == 0 and 0 or 1)
  33. end
  34. local function lq_emote(self)
  35. local start_time = 0
  36. local func = function(self)
  37. start_time = start_time + self.dtime
  38. mobkit.animate(self, "emote")
  39. if start_time > 0.5
  40. then
  41. return true
  42. end
  43. end
  44. mobkit.queue_low(self, func)
  45. end
  46. local function hq_interact_with_friend(self, prty, friend)
  47. local friend_yvel_history = {}
  48. local hpointer = 0
  49. local track_count = 4
  50. local iterations = 0
  51. local hopping = false
  52. --if close, watch friend
  53. local func = function(self)
  54. --track player
  55. if mobkit.timer(self, 0.1)
  56. then
  57. iterations = iterations + 1
  58. hpointer = (hpointer + 1) % track_count
  59. friend_yvel_history[hpointer + 1] = sign(friend:get_player_velocity().y)
  60. hopping = false
  61. for i = 1, track_count
  62. do
  63. local index1 = (hpointer + i) % track_count + 1
  64. local index2 = (hpointer + i - 1) % track_count + 1
  65. local vel1 = friend_yvel_history[index1]
  66. local vel2 = friend_yvel_history[index2]
  67. hopping = hopping or
  68. (vel1 ~= vel2 and
  69. vel1 ~= 0 and
  70. vel2 ~= 0)
  71. end
  72. end
  73. --animate if player is hopping
  74. if mobkit.is_queue_empty_low(self) and self.isonground
  75. then
  76. if hopping
  77. then
  78. lq_emote(self)
  79. else
  80. mobkit.lq_idle(self, 0.5)
  81. if math.random() < 0.1 and iterations > track_count
  82. then
  83. return true
  84. end
  85. end
  86. end
  87. end
  88. mobkit.queue_high(self, func, prty)
  89. end
  90. --TODO: use low level queue properly
  91. local function choose_action(self)
  92. local pos = self.object:get_pos()
  93. local in_rad = get_players_inside_radius(pos, HEAR_DISTANCE)
  94. local fav = nil
  95. local fav_rel = -math.huge
  96. for i, player in ipairs(in_rad)
  97. do
  98. local name = player:get_player_name()
  99. if self.player_relations[name] > fav_rel
  100. then
  101. fav_rel = self.player_relations[name]
  102. fav = player
  103. end
  104. end
  105. if fav
  106. then
  107. local favpos = fav:get_pos()
  108. --run if best player is bad and close
  109. local dist = vector.distance(favpos, pos)
  110. if fav_rel / dist < -0.5
  111. then
  112. mobkit.hq_runfrom(self, 20, fav)
  113. return true
  114. end
  115. if fav_rel > 7
  116. then
  117. if dist > 5
  118. then
  119. mobkit.hq_goto(self, 10, favpos)
  120. else
  121. if math.random(2) < 1
  122. then
  123. mobkit.hq_roam(self,0)
  124. else
  125. hq_interact_with_friend(self, 10, fav)
  126. end
  127. end
  128. return true, true
  129. end
  130. end
  131. end
  132. local function lq_make_sound(self, sound)
  133. local timer = 0
  134. mobkit.make_sound(self, sound)
  135. local func = function()
  136. timer = timer + self.dtime
  137. if timer > 0.5
  138. then
  139. return true
  140. end
  141. end
  142. mobkit.queue_low(self, func)
  143. end
  144. local function lq_talk(self)
  145. local sentence = self.talker:get_sentence()
  146. --if the parrot knows no words it doesn't talk
  147. if sentence == ""
  148. then
  149. return
  150. end
  151. set_nametag(self, sentence)
  152. --add '<Parrot>' in front of sentence
  153. sentence = minetest.colorize(self.color, prefix) .. sentence
  154. local pos = self.object:get_pos()
  155. mobkit.make_sound(self, "talk")
  156. --send chat message to nearby players
  157. for i, p in ipairs(get_players_inside_radius(pos, HEAR_DISTANCE))
  158. do
  159. local name = p:get_player_name()
  160. minetest.chat_send_player(name, sentence)
  161. end
  162. --make nametag disappear again
  163. local timer = 0
  164. local func = function()
  165. timer = timer + self.dtime
  166. if timer > 1
  167. then
  168. set_nametag(self, "")
  169. return true
  170. end
  171. end
  172. mobkit.queue_low(self, func)
  173. end
  174. local function parrot_brain(self)
  175. if self.hp <= 0
  176. then
  177. mobkit.clear_queue_high(self)
  178. mobkit.hq_die(self)
  179. return
  180. end
  181. if mobkit.timer(self,1)
  182. then
  183. local prty = mobkit.get_queue_priority(self)
  184. local talk_chance = PARROT_TALK_CHANCE
  185. --interact with player
  186. local chose_action, friend
  187. if prty < 10
  188. then
  189. chose_action, friend = choose_action(self)
  190. end
  191. if friend
  192. then
  193. --talk chance is increased if a friend is nearby
  194. talk_chance = PARROT_TALK_CHANCE_FRIEND
  195. end
  196. if math.random(100) < talk_chance
  197. then
  198. lq_talk(self)
  199. elseif math.random(100) < talk_chance
  200. then
  201. lq_make_sound(self, "playful")
  202. end
  203. if chose_action
  204. then
  205. return
  206. end
  207. if prty < 20 and self.isinliquid then
  208. mobkit.hq_liquid_recovery(self, 20)
  209. return
  210. end
  211. local pos = self.object:get_pos()
  212. if mobkit.is_queue_empty_high(self) then
  213. mobkit.hq_roam(self, 0)
  214. end
  215. end
  216. end
  217. return parrot_brain