--smallg local chat_angle = {} local chat_range = {} local max_chat_sounds_num = {} local sound_num = {} local current_sound_num = {} local subtitle = {} local freeze = {} local frozen = {} local played = {} local reset_on_leave = {} local state = {} local volume = {} local conv_pressed = 0 local current_convo = nil --load all the global sounds here first LoadGlobalSound("audiobank\\globalsounds\\sound1.wav",1) subtitle[1] = "Hello there adventurer" volume[1] = "3d" --set the volume here ("3d" = gets quieter/louder based on distance - not currently working?) LoadGlobalSound("audiobank\\globalsounds\\sound2.wav",2) subtitle[2] = "What? Ogres can't speak!" volume[2] = 100 LoadGlobalSound("audiobank\\globalsounds\\sound3.wav",3) subtitle[3] = {} --create new sub array for this sound so we can store multiple lines of subtitles subtitle[3][1] = "What do you mean Ogres can't speak?" subtitle[3][2] = "I'm talking right now aren't I?" volume[3] = "3d" LoadGlobalSound("audiobank\\globalsounds\\sound4.wav",4) subtitle[4] = "Well I am a mighty Hero!" volume[4] = "3d" function ai_conversation_init_name(e,name) chat_range[e] = 150 --range when to start the conversation chat_angle[e] = 35 --angle player needs to be facing to start conversation freeze[e] = 0 --toggle freezing the player in place for the conversation or not reset_on_leave[e] = 1 --toggle wether the conversation can be replayed if player leaves and comes back --set up each character to specify which sounds here, remember to name them in the editor "conversation1" etc if name == "conversation1" then sound_num[e] = 1 --starting global sound slot max_chat_sounds_num[e] = 2 --maximum number of sounds to play for the entity named "conversation1" elseif name == "conversation2" then sound_num[e] = 3 max_chat_sounds_num[e] = 4 elseif name == "conversation3" then sound_num[e] = 1 max_chat_sounds_num[e] = 1 elseif name == "conversation4" then sound_num[e] = 2 max_chat_sounds_num[e] = 4 end -- don't change below unless you know what you're doing :) frozen[e] = 0 played[sound_num[e]] = 0 state[e] = "wait" end function ai_conversation_main(e) if GetPlayerDistance(e) <= chat_range[e]*1.5 then RotateToPlayer(e) LookAtPlayer(e) end if PlayerLooking(e,chat_range[e],chat_angle[e]) == 1 then if state[e] == "wait" then Prompt("Talk to the NPC?") if g_KeyPressE == 1 and conv_pressed == 0 then conv_pressed = 1 current_sound_num[e] = nil current_convo = nil state[e] = "talking" end end end if state[e] == "talking" then if current_convo == nil then current_convo = e end if current_sound_num[e] == nil then current_sound_num[e] = sound_num[e] played[current_sound_num[e]] = 0 end if current_sound_num[e] <= max_chat_sounds_num[e] then if freeze[e] == 1 and frozen[e] == 0 then FreezePlayer() frozen[e] = 1 end if GetGlobalSoundPlaying(current_sound_num[e]) == 0 and played[current_sound_num[e]] == 0 then PlayGlobalSound(current_sound_num[e]) if volume[current_sound_num[e]] ~= nil then if volume[current_sound_num[e]] == "3d" then SetGlobalSoundVolume(current_sound_num[e],101-math.ceil((GetPlayerDistance(e)/75))) else SetGlobalSoundVolume(volume[current_sound_num[e]]) end end played[current_sound_num[e]] = 1 elseif GetGlobalSoundPlaying(current_sound_num[e]) == 0 then current_sound_num[e] = current_sound_num[e] + 1 played[current_sound_num[e]] = 0 end if subtitle[current_sound_num[e]] ~= nil then if subtitle[current_sound_num[e]][1] ~= nil then ypos = 85 line = 1 repeat TextCenterOnX(50,ypos,3,subtitle[current_sound_num[e]][line]) ypos = ypos + 3 line = line + 1 until subtitle[current_sound_num[e]][line] == nil else TextCenterOnX(50,90,3,subtitle[current_sound_num[e]]) end end if g_KeyPressE == 1 and conv_pressed == 0 then conv_pressed = 1 StopGlobalSound(current_sound_num[e]) current_sound_num[e] = current_sound_num[e] + 1 played[current_sound_num[e]] = 0 end elseif PlayerLooking(e,chat_range[e]*1.5,chat_angle[e]*1.5) == 1 and freeze[e] == 1 and frozen[e] == 1 then UnFreezePlayer() frozen[e] = 0 state[e] = "wait" elseif reset_on_leave[e] == 1 then if current_convo ~= nil then if current_convo == e then current_sound_num[e] = sound_num[e] played[current_sound_num[e]] = 0 current_convo = nil state[e] = "wait" end end end end if g_KeyPressE == 0 then conv_pressed = 0 end end function ai_conversation_exit(e) end function PlayerLooking(e,dis,v) if g_Entity[e] ~= nil then if dis == nil then dis = 3000 end if v == nil then v = 0.5 end if GetPlayerDistance(e) <= dis then local destx = g_Entity[e]['x'] - g_PlayerPosX local destz = g_Entity[e]['z'] - g_PlayerPosZ local angle = math.atan2(destx,destz) angle = angle * (180.0 / math.pi) if angle <= 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end while g_PlayerAngY < 0 or g_PlayerAngY > 360 do if g_PlayerAngY <= 0 then g_PlayerAngY = 360 + g_PlayerAngY elseif g_PlayerAngY > 360 then g_PlayerAngY = g_PlayerAngY - 360 end end local L = angle - v local R = angle + v if L <= 0 then L = 360 + L elseif L > 360 then L = L - 360 end if R <= 0 then R = 360 + R elseif R > 360 then R = R - 360 end if (L < R and math.abs(g_PlayerAngY) > L and math.abs(g_PlayerAngY) < R) then return 1 elseif (L > R and (math.abs(g_PlayerAngY) > L or math.abs(g_PlayerAngY) < R)) then return 1 else return 0 end else return 0 end end end