Advertisement
Daemonion

xr_hear

Oct 21st, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. local function get_sound_type(sound_type)
  2. local s_type = "NIL"
  3. if bit_and(sound_type, snd_type.weapon) == snd_type.weapon then
  4. s_type = "WPN"
  5. if bit_and(sound_type, snd_type.weapon_shoot) == snd_type.weapon_shoot then
  6. s_type = "WPN_shoot"
  7. elseif bit_and(sound_type, snd_type.weapon_empty) == snd_type.weapon_empty then
  8. s_type = "WPN_empty"
  9. elseif bit_and(sound_type, snd_type.weapon_bullet_hit) == snd_type.weapon_bullet_hit then
  10. s_type = "WPN_hit"
  11. elseif bit_and(sound_type, snd_type.weapon_reload) == snd_type.weapon_reload then
  12. s_type = "WPN_reload"
  13. end
  14. elseif bit_and(sound_type, snd_type.item) == snd_type.item then
  15. s_type = "ITM"
  16. if bit_and(sound_type, snd_type.item_pick_up) == snd_type.item_pick_up then
  17. s_type = "ITM_pckup"
  18. elseif bit_and(sound_type, snd_type.item_drop) == snd_type.item_drop then
  19. s_type = "ITM_drop"
  20. elseif bit_and(sound_type, snd_type.item_hide) == snd_type.item_hide then
  21. s_type = "ITM_hide"
  22. elseif bit_and(sound_type, snd_type.item_take) == snd_type.item_take then
  23. s_type = "ITM_take"
  24. elseif bit_and(sound_type, snd_type.item_use) == snd_type.item_use then
  25. s_type = "ITM_use"
  26. end
  27. elseif bit_and(sound_type, snd_type.monster) == snd_type.monster then
  28. s_type = "MST"
  29. if bit_and(sound_type, snd_type.monster_die) == snd_type.monster_die then
  30. s_type = "MST_die"
  31. elseif bit_and(sound_type, snd_type.monster_injure) == snd_type.monster_injure then
  32. s_type = "MST_damage"
  33. elseif bit_and(sound_type, snd_type.monster_step) == snd_type.monster_step then
  34. s_type = "MST_step"
  35. elseif bit_and(sound_type, snd_type.monster_talk) == snd_type.monster_talk then
  36. s_type = "MST_talk"
  37. elseif bit_and(sound_type, snd_type.monster_attack) == snd_type.monster_attack then
  38. s_type = "MST_attack"
  39. elseif bit_and(sound_type, snd_type.monster_eat) == snd_type.monster_eat then
  40. s_type = "MST_eat"
  41. end
  42. end
  43. return s_type
  44. end
  45. function reset_hear_callback(st, section)
  46. local function is_on_sound_line(candidate)
  47. return string.find( candidate, "^on_sound%d*$" ) ~= nil
  48. end
  49. local function add_parsed_data_to_storage(value, st)
  50. local obj = st.object
  51. local parsed_params = utils.parse_params(value)
  52. st.hear_sounds[parsed_params[1]] = st.hear_sounds[parsed_params[1]] or {}
  53. st.hear_sounds[parsed_params[1]][parsed_params[2]] = { dist = tonumber(parsed_params[3]),
  54. power = tonumber(parsed_params[4]),
  55. condlist = xr_logic.parse_condlist(obj, "hear_callback", "hear_callback", parsed_params[5])}
  56. end
  57. local ini = st.ini
  58. if not ini:section_exist(section) then
  59. return
  60. end
  61. local n = ini:line_count(section)
  62. local id, value = "",""
  63. local category = ""
  64. st.hear_sounds = {}
  65. for i=0,n-1 do
  66. result, id, value = ini:r_line(section,i,"","")
  67. if is_on_sound_line(id) then
  68. add_parsed_data_to_storage(value, st)
  69. end
  70. end
  71. end
  72. function hear_callback(obj, who_id, sound_type, sound_position, sound_power)
  73. local st = db.storage[obj:id()]
  74. local s_type = get_sound_type(sound_type)
  75. if (s_type == "WPN_hit" or s_type == "MST_attack" or s_type == "MST_damage" and sound_position:distance_to_sqr(obj:position()) < 225) then
  76. if not xr_wounded.is_wounded(obj) then
  77. xr_danger.set_script_danger(obj,3000)
  78. end
  79. end
  80.  
  81. local who = who_id and level.object_by_id(who)
  82. if (who) then
  83. obj:enable_memory_object(who,true)
  84. end
  85.  
  86. local story_id = get_object_story_id(who_id) or "any"
  87. if st.hear_sounds == nil then
  88. return
  89. end
  90. if st.hear_sounds[story_id] and st.hear_sounds[story_id][s_type] then
  91. local hear_sound_params = st.hear_sounds[story_id][s_type]
  92. if hear_sound_params.dist >= sound_position:distance_to(obj:position()) and sound_power >= hear_sound_params.power then
  93. local new_section = xr_logic.pick_section_from_condlist(db.actor, obj, hear_sound_params.condlist)
  94. if new_section ~= nil and new_section ~= "" then
  95. xr_logic.switch_to_section(obj, st.ini, new_section)
  96. elseif new_section == "" then
  97. st.hear_sounds[story_id][s_type] = nil
  98. end
  99. end
  100. end
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement