-- 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 vol = 100 -- init when level first runs function civilian_waypoint_init_name(e,name) civ_image[e] = LoadImage("scriptbank\\images\\civilian\\"..name..".png") --nearest = e sound_played[e] = 0 ai_soldier_state[e] = "patrol"; ai_soldier_pathindex[e] = -1; ai_start_x[e] = nil ai_starting_heath[e] = nil ai_ran_to_cover[e] = 0 ModulateSpeed(e,1.0) ai_old_health[e] = 0 --CharacterControlStand(e) ai_returning_home[e] = 0 ai_newdest_time[e] = -1 ai_cover_on[e] = 0 ai_alerted_spoken[e] = 0 end function civilian_waypoint_main(e) EntObjNo = g_Entity[e]['obj'] if nearest == nil then nearest = e end if ai_soldier_state[e] == "patrol" then if ai_soldier_pathindex[e] == -1 then ai_soldier_pathindex[e] = -2 CharacterControlUnarmed(e) PathIndex = -1 PathPointIndex = -1 pClosest = 99999 for pa = 1, AIGetTotalPaths(), 1 do for po = 1 , AIGetPathCountPoints(pa), 1 do pDX = g_Entity[e]['x'] - AIPathGetPointX(pa,po) pDZ = g_Entity[e]['z'] - AIPathGetPointZ(pa,po) pDist = math.sqrt(math.abs(pDX*pDX)+math.abs(pDZ*pDZ)) if pDist < pClosest and pDist < 200 then pClosest = pDist PathIndex = pa PathPointIndex = po end end -- po end -- pa if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex ai_path_point_index[e] = PathPointIndex ModulateSpeed(e,1.0) SetCharacterToWalk(e) ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) end elseif ai_soldier_pathindex[e] > -1 then ai_patrol_x[e] = AIPathGetPointX(ai_soldier_pathindex[e],ai_path_point_index[e]) ai_patrol_z[e] = AIPathGetPointZ(ai_soldier_pathindex[e],ai_path_point_index[e]) AIEntityGoToPosition(EntObjNo,ai_patrol_x[e],ai_patrol_z[e]) tDistX = g_Entity[e]['x'] - ai_patrol_x[e] tDistZ = g_Entity[e]['z'] - ai_patrol_z[e] DistFromPath = math.sqrt(math.abs(tDistX*tDistX)+math.abs(tDistZ*tDistZ)) if DistFromPath < 50 then if ai_path_point_direction[e] == 1 then ai_path_point_index[e] = ai_path_point_index[e] + 1 if ( ai_path_point_index[e] > ai_path_point_max[e] ) then ai_path_point_index[e] = ai_path_point_max[e] - 1 ai_path_point_direction[e] = 0 end else ai_path_point_index[e] = ai_path_point_index[e] - 1 if ( ai_path_point_index[e] < 1 ) then ai_path_point_index[e] = 2 ai_path_point_direction[e] = 1 end end end else LookAtPlayer(e) --CharacterControlFidget(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 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 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