-- LUA Script - precede every function and global member with lowercase name of script -- globals local aniplaying = 0 ai_newdest_time = {} ai_cover_on = {} -- init when level first runs function civilian_init(e) 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_main(e) EntObjNo = g_Entity[e]['obj']; MoveUp(e,1) -- Detect player distance (note: possible optimization here) PlayerDX = g_Entity[e]['x'] - g_PlayerPosX; PlayerDY = g_Entity[e]['y'] - g_PlayerPosY; PlayerDZ = g_Entity[e]['z'] - g_PlayerPosZ; PlayerDist = math.sqrt(math.abs(PlayerDX*PlayerDX)+math.abs(PlayerDY*PlayerDY)+math.abs(PlayerDZ*PlayerDZ)); --if ai_ran_to_cover[e] ~= 1 then -- Always looking at player --if PlayerDist < 200 then --LookAtPlayer(e) --AIEntityGoToPosition(EntObjNo,g_Entity[e]['x'],g_Entity[e]['z']) --else -- Patrol Mode if ai_soldier_state[e] == "patrol" then -- Try and find a close path to patrol, just check once for it if ai_soldier_pathindex[e] == -1 then ai_soldier_pathindex[e] = -2 CharacterControlArmed(e) -- find initial waypoint path to follow PathIndex = -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; end end -- po end -- pa -- follow found path if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex; ai_path_point_index[e] = 2 ModulateSpeed(e,1.0) SetCharacterToWalk(e) ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) end end -- If we have a path then lets patrol it if 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 CharacterControlFidget(e) end end -- end --if g_Entity[e]['health'] < 100 then -- SetCharacterToRun(e) -- ModulateSpeed(e,1.3) -- ai_ran_to_cover[e] = 1 -- AISetEntityControl(EntObjNo,AI_MANUAL); -- AIEntityMoveToCover(EntObjNo,g_PlayerPosX,g_PlayerPosZ); -- end end