-- LUA Script - precede every function and global member with lowercase name of script -- globals local aniplaying = 0 ai_newdest_time = {} ai_cover_on = {} local nearest = nil local greet_range = 200 local leave_range = 220 local civ_image = {} local civ_spr = {} local sound_played = {} local destx = {} local destz = {} local vol = 100 -- init when level first runs function civilian_freeroam_init_name(e,name) civ_image[e] = LoadImage("scriptbank\\images\\civilian\\"..name..".png") --nearest = e sound_played[e] = 0 ModulateSpeed(e,1.0) destx[e] = g_Entity[e]['x'] destz[e] = g_Entity[e]['z'] CharacterControlManual(e) end function civilian_freeroam_main(e) EntObjNo = g_Entity[e]['obj'] if nearest == nil then nearest = e end if GetPlayerDistance(e) < greet_range then if GetPlayerDistance(e) < GetPlayerDistance(nearest) then if civ_spr[nearest] ~= nil then SetSpritePosition(civ_spr[nearest],200,200) DeleteSprite(civ_spr[nearest]) civ_spr[nearest] = nil sound_played[nearest] = 0 end nearest = e end if g_Entity[e]['animating'] ~= -1 then StopAnimation(e) end if g_Entity[e]['animating'] == 0 then SetAnimation(0) LoopAnimation(e) g_Entity[e]['animating'] = -1 end AIEntityStop(EntObjNo) SetRotation(e,0,AngleToPoint(e,g_PlayerPosX,g_PlayerPosZ),0) AISetEntityPosition(EntObjNo,GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) else if GetDistanceToPoint(e,destx[e],destz[e]) > 100 then if g_Entity[e]['animating'] ~= 1 then StopAnimation(e) end if g_Entity[e]['animating'] == 0 then SetAnimation(1) LoopAnimation(e) g_Entity[e]['animating'] = 1 end AIEntityGoToPosition(EntObjNo,destx[e],g_Entity[e]['y'],destz[e]) MoveForward(e,AIGetEntitySpeed(EntObjNo)) SetRotation(e,0,AIGetEntityAngleY(EntObjNo),0) AISetEntityPosition(EntObjNo,GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) else destx[e] = g_Entity[e]['x'] + math.random(-1000,1000) destz[e] = g_Entity[e]['z'] + math.random(-1000,1000) end end if nearest == e then if GetPlayerDistance(e) > leave_range then if civ_spr[e] ~= nil then SetSpritePosition(civ_spr[e],200,200) DeleteSprite(civ_spr[e]) civ_spr[e] = nil sound_played[e] = 0 end else if civ_spr[e] == nil then civ_spr[e] = CreateSprite(civ_image[e]) SetSpriteSize(civ_spr[e],100,100) SetSpritePosition(civ_spr[e],0,0) end if sound_played[e] == 0 then PlaySound(e,1) sound_played[e] = 1 end vol = 101 - (GetPlayerDistance(e)/25) if vol < 1 then vol = 1 end if vol > 99 then vol = 100 end SetSoundVolume(vol) end end end function AngleToPoint(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 function GetDistanceToPoint(e,x,z) if g_Entity[e] ~= nil and g_Entity[e] ~= 0 then local disx = g_Entity[e]['x'] - x local disz = g_Entity[e]['z'] - z return math.sqrt(disx^2 + disz^2) end end