local state = {} local nearest_dist = {} local target = {} local old_playerhealth = {} local temp = {} local stood = {} local pressed = 0 local new_y = {} local pos_x = {} local pos_z = {} isfriendly = {} local attack_range = {} local damage = {} local attack_delay = {} local run_range = {} local walk_range = {} local max_aggro_range = {} local check_aggro_delay = {} local sound_played = {} local hired = {} local hire_cost = {} RPG_Gold = 130 function ranger_ally_init_name(e,name) weapon_name[e] = name if weapon_name[e] == "melee" then attack_range[e] = 100 damage[e] = 25 attack_delay[e] = 1200 run_range[e] = 650 walk_range[e] = 150 max_aggro_range[e] = 700 check_aggro_delay[e] = 1000 hire_cost[e] = 100 else attack_range[e] = 600 damage[e] = 30 attack_delay[e] = 3750 run_range[e] = 750 walk_range[e] = 250 max_aggro_range[e] = 700 check_aggro_delay[e] = 1000 hire_cost[e] = 100 end state[e] = "wait" target[e] = nil nearest_dist[e] = 10 old_playerhealth[e] = nil temp[e] = 0 stood[e] = 1 isfriendly[e] = 1 sound_played[e] = 0 hired[e] = 0 end function ranger_ally_main(e) if state[e] == "wait" then if stood[e] == 0 then CharacterControlStand(e) stood[e] = 1 end CharacterControlFidget(e) CharacterControlStand(e) RotateToPoint(e,g_PlayerPosX,g_PlayerPosZ) if PlayerLooking(e,walk_range[e],30) == 1 and AICouldSee(g_Entity[e]['obj'],g_PlayerPosX,g_Entity[e]['y'],g_PlayerPosZ) == 1 then if g_KeyPressE == 0 then if hired[e] == 0 then Prompt("You can hire my services for a mere "..hire_cost[e].." gold") else Prompt("Want me to come with you kind sir?") end end if g_KeyPressE == 1 and pressed == 0 then pressed = 1 if hired[e] == 0 then if RPG_Gold ~= nil then if RPG_Gold > hire_cost[e] then RPG_Gold = RPG_Gold - hire_cost[e] PromptDuration("Lead the way good sir!",2000) hired[e] = 1 state[e] = "follow" else PromptDuration("Not enough gold to make it worth my time sir",2000) end end else state[e] = "follow" end end end elseif state[e] == "follow" then if stood[e] == 0 then CharacterControlStand(e) stood[e] = 1 end CharacterControlArmed(e) if old_playerhealth[e] == nil then old_playerhealth[e] = g_PlayerHealth return end if GetPlayerDistance(e) > run_range[e] then SetCharacterToRun(e) AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ) elseif GetPlayerDistance(e) > walk_range[e] then SetCharacterToWalk(e) AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ) elseif GetPlayerDistance(e) < walk_range[e]*0.8 then new_y[e] = math.rad(g_PlayerAngY+90) pos_x[e] = g_PlayerPosX + (math.sin(new_y[e]) * 250) pos_z[e] = g_PlayerPosZ + (math.cos(new_y[e]) * 250) AIEntityGoToPosition(g_Entity[e]['obj'],pos_x[e],pos_z[e]) else AIEntityStop(g_Entity[e]['obj']) RotateToPoint(e,g_PlayerPosX,g_PlayerPosZ) end if GetTimer(e) > check_aggro_delay[e] then StartTimer(e) nearest_dist[e] = 10 target[e] = nil temp[e] = 0 repeat for a = 1,9999 do if g_Entity[a] ~= nil and isfriendly[a] == nil then if GetPlayerDistance(a) < nearest_dist[e] then if AICouldSee(g_Entity[a]['obj'],g_PlayerPosX,g_Entity[e]['y'],g_PlayerPosZ) == 1 then target[e] = a break end end end end nearest_dist[e] = nearest_dist[e] + 10 temp[e] = temp[e] + 1 until target[e] ~= nil or temp[e] > 99 or nearest_dist[e] > max_aggro_range[e] if target[e] ~= nil then --SetCharacterToWalk(e) state[e] = "move to target" return end end if g_PlayerHealth < old_playerhealth[e] then old_playerhealth[e] = g_PlayerHealth nearest_dist[e] = 10 target[e] = nil temp[e] = 0 repeat for a = 1,9999 do if g_Entity[a] ~= nil and isfriendly[a] == nil then if GetPlayerDistance(a) < nearest_dist[e] then if AICouldSee(g_Entity[a]['obj'],g_PlayerPosX,g_Entity[e]['y'],g_PlayerPosZ) == 1 then target[e] = a break end end end end nearest_dist[e] = nearest_dist[e] + 10 temp[e] = temp[e] + 1 until target[e] ~= nil or temp[e] > 99 if target[e] ~= nil then --SetCharacterToWalk(e) state[e] = "move to target" return end end if PlayerLooking(e,walk_range[e],20) == 1 then if g_KeyPressE == 0 then Prompt("Should i wait here for you my master?") end if g_KeyPressE == 1 and pressed == 0 then pressed = 1 state[e] = "wait" end end elseif state[e] == "move to target" then if g_Entity[target[e]]['health'] > 0 then if GetDistance(e,target[e]) > attack_range[e] then SetCharacterToRun(e) local x_pos_mod = attack_range[e]*0.9 local z_pos_mod = attack_range[e]*0.9 new_y[e] = math.rad(g_Entity[target[e]]['angley']) pos_x[e] = g_Entity[target[e]]['x'] + (math.sin(new_y[e]) * x_pos_mod) pos_z[e] = g_Entity[target[e]]['z'] + (math.cos(new_y[e]) * z_pos_mod) AIEntityGoToPosition(g_Entity[e]['obj'],pos_x[e],pos_z[e]) else AIEntityGoToPosition(g_Entity[e]['obj'],g_Entity[e]['x'],g_Entity[e]['z']) AIEntityStop(g_Entity[e]['obj']) StartTimer(e) state[e] = "attack" end else target[e] = nil old_playerhealth[e] = g_PlayerHealth state[e] = "follow" end elseif state[e] == "attack" then AIEntityStop(g_Entity[e]['obj']) if g_Entity[target[e]]['health'] > 0 then if GetDistance(e,target[e]) <= attack_range[e]*1.15 then if stood[e] == 1 then CharacterControlDucked(e) if sound_played[e] == 0 and weapon_name[e] == "melee" then PlaySound(e,1) sound_played[e] = 1 end stood[e] = 0 end RotateToPoint(e,g_Entity[target[e]]['x'],g_Entity[target[e]]['z']) if GetTimer(e) > attack_delay[e] then sound_played[e] = 0 StartTimer(e) SetEntityHealth(target[e],g_Entity[target[e]]['health']-damage[e]) CharacterControlStand(e) stood[e] = 1 elseif GetTimer(e) > attack_delay[e]*0.75 and GetTimer(e) < attack_delay[e] then if sound_played[e] == 0 and weapon_name[e] ~= "melee" then PlaySound(e,1) sound_played[e] = 1 end CharacterControlStand(e) stood[e] = 1 end else CharacterControlStand(e) stood[e] = 1 state[e] = "move to target" end else CharacterControlStand(e) stood[e] = 1 target[e] = nil old_playerhealth[e] = g_PlayerHealth state[e] = "follow" end end --state --Prompt(state[e]) if g_KeyPressE == 0 then pressed = 0 end end --main(e) function ranger_ally_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 function RotateToPoint(e,x,z) if g_Entity[e] ~= nil and x > 0 and z > 0 then local destx = x - g_Entity[e]['x'] local destz = z - g_Entity[e]['z'] 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 SetRotation(e,0,angle,0) return angle end end