[Gcblue-commits] gcb_wx/scripts AI.py,1.16,1.17
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2005-02-18 17:42:17
|
Update of /cvsroot/gcblue/gcb_wx/scripts In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1968/scripts Modified Files: AI.py Log Message: Parallel ai update Index: AI.py =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/scripts/AI.py,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** AI.py 16 Feb 2005 23:13:23 -0000 1.16 --- AI.py 18 Feb 2005 17:42:08 -0000 1.17 *************** *** 71,86 **** TI.SetMemoryValue(1, iteration) ! def AvoidGround(UI): ! if (UI.IsAir()): ! terrainElevation = UI.GetTerrainElevation() ! if (terrainElevation < 0): ! terrainElevation = 0 ! alt = UI.GetAlt() ! if ((alt - terrainElevation) < 80): ! UI.SetAlt(terrainElevation + 100) ! def Patrol(UI): targetid = UI.GetTarget() if targetid != -1: --- 71,279 ---- TI.SetMemoryValue(1, iteration) + def Patrol(TI): + ZigZagPatrol(TI) ! # zig-zag patrol task ! def ZigZagPatrol(TI): ! UI = TI.GetPlatformInterface() ! BB = TI.GetBlackboardInterface() + iteration = TI.GetMemoryValue(1) # will return 0 first time + if (iteration == 0): # do initialization + TI.SetMemoryValue(2, UI.GetHeading()) + TI.SetMemoryValue(3, 0) # 0 is turn left next zig-zag, 1 turn right + TI.SetMemoryValue(4, 90.0 + 90.0 * UI.Rand()) # random turn interval in seconds + TI.SetMemoryText('Description', 'Perform surveillance along a zig-zag course') ! iteration = iteration + 1 ! TI.SetMemoryValue(1, iteration) ! ! # activate all sensors ! can_radiate = GetSensorControl(BB) ! if (can_radiate): ! ActivateAllSensors(UI) ! else: ! ActivatePassiveSensors(UI) ! ! # return if conn is not available ! if (not GetConnControl(BB)): ! return ! ! if (TI.GetMemoryValue(3) == 0): ! new_heading = TI.GetMemoryValue(2) - 45 ! TI.SetMemoryValue(3, 1) ! else: ! new_heading = TI.GetMemoryValue(2) + 45 ! TI.SetMemoryValue(3, 0) ! ! UI.SetHeading(new_heading) ! TI.SetUpdateInterval(TI.GetMemoryValue(4)) ! UI.SetActionText('Patrol') ! ! ! # shoot hostiles and unknowns within range, but do not intercept. ! # Turn if necessary to position launchers ! def EngageAll(TI): ! UI = TI.GetPlatformInterface() ! BB = TI.GetBlackboardInterface() ! ! iteration = TI.GetMemoryValue(1) # will return 0 first time ! if (iteration == 0): # do initialization ! TI.SetMemoryText('Description', 'Engage hostiles and unknowns within range, but do not intercept') ! ! iteration = iteration + 1 ! TI.SetMemoryValue(1, iteration) ! ! # engage_mode values: ! # 0 - searching, 1 - turning to place target within launch sector, 2 - keeping target within semiactive sector ! engage_mode = TI.GetMemoryValue(2) ! ! # memory values ! # 10: target id ! # 11: launcher index ! # 12: launch/follow sector center ! # 13: launch/follow sector width ! ! if (engage_mode == 0): # searching ! TI.SetUpdateInterval(30) ! best_target, best_launcher = GetImmediateTarget(UI) ! if (best_target != -1): ! UI.DisplayMessage('%s: targ: %d, lau: %d' % (UI.GetName(), best_target, best_launcher)) ! launcher_info = UI.GetLauncherInfo(best_launcher) ! if (launcher_info.IsValid()): ! TI.SetMemoryValue(10, best_target) ! TI.SetMemoryValue(11, best_launcher) ! TI.SetMemoryValue(12, launcher_info.SectorCenter) ! TI.SetMemoryValue(13, launcher_info.SectorWidth) ! TI.SetMemoryValue(2, 1) ! engage_mode = 1 ! TI.SetUpdateInterval(10.0) ! else: ! TI.SetMemoryValue(2, 0) ! ! if (engage_mode == 1): # turn to target ! target_id = long(TI.GetMemoryValue(10)) ! if (target_id == -1): ! TI.SetMemoryValue(2, 0) ! return ! UI.SetTarget(target_id) ! launcher = long(TI.GetMemoryValue(11)) ! launcher_angle = TI.GetMemoryValue(12) ! sector_width = TI.GetMemoryValue(13) ! target_bearing = GetTargetBearing(UI) ! if (GetConnControl(BB)): ! UI.SetHeading(target_bearing-launcher_angle) ! ! engagement_angle = target_bearing - launcher_angle ! if (engagement_angle > 180.0): ! engagement_angle = engagement_angle - 360.0 ! elif (engagement_angle < -180.0): ! engagement_angle = engagement_angle + 360.0 ! if (engagement_angle <= 0.5 * sector_width): ! EngageTargetWithLauncher(UI, launcher) ! TI.SetMemoryValue(2, 2) ! TI.SetUpdateInterval(10.0) # wait 10 seconds ! ! if (engage_mode >= 2): ! TI.SetMemoryValue(2, 0) # return to search mode ! TI.SetUpdateInterval(20.0) ! ReleaseConnControl(BB) ! ! ! ! # returns id of closest target that is in-range, engageable, and not already ! # overwhelmingly engaged ! # returns -1 if none ! def GetImmediateTarget(UI): ! ! # anAffiliation: UNKNOWN = 0, FRIENDLY = 1, NEUTRAL = 2, HOSTILE = 3 ! # ! # anClassMask: ! # PTYPE_SURFACE 0x0010 ! # PTYPE_AIR 0x0020 ! # PTYPE_MISSILE 0x0040 ! # PTYPE_SUBSURFACE 0x0080 ! # PTYPE_FIXED 0x0100 ! # int anClassMask, float afMaxRange_km, UINT8 anAffiliation ! track_list = UI.GetTrackList(0x0FFF, 150, 3) ! ! nTracks = track_list.Size() ! best_range = 1e6 ! best_target = -1 ! best_launcher = -1 ! ! for n in range(0, nTracks): ! track_info = track_list.GetTrack(n) ! track_id = track_info.ID ! ! engaged_count = track_info.GetEngagedCount() ! if (track_info.IsAir() or track_info.IsMissile()): ! max_engaged_count = 2 ! else: ! max_engaged_count = 6 ! ! if (engaged_count < max_engaged_count): ! UI.SetTarget(track_id) ! launcher_info = UI.GetBestLauncher() ! launcher_idx = launcher_info.Launcher ! if (launcher_idx != -1): ! target_range = UI.GetRangeToTarget() ! launch_range = launcher_info.Range_km # reference max range, use for launch decision ! if ((target_range <= launch_range) and (target_range < best_range)): ! best_range = target_range ! best_target = track_id ! best_launcher = launcher_idx ! UI.SetTarget(best_target) ! ! return (best_target, best_launcher) ! ! ! ! ! ! ! ! def ActivateAllSensors(UI): ! UI.SetAllSensorState(1) ! ! def ActivatePassiveSensors(UI): ! nSensors = UI.GetSensorCount() ! for n in range(0, nSensors): ! sensor_info = UI.GetSensorInfo(n) ! if (sensor_info.IsPassive()): ! UI.SetSensorState(n, 1) ! ! def Emcon(TI): ! UI = TI.GetPlatformInterface() ! BB = TI.GetBlackboardInterface() ! ! # take control of (active) sensors ! if (not GetSensorControl(BB)): ! return ! ! nSensors = UI.GetSensorCount() ! for n in range(0, nSensors): ! sensor_info = UI.GetSensorInfo(n) ! if (not sensor_info.IsPassive()): # deactivate active sensors ! UI.SetSensorState(n, 0) ! ! ! def AirPatrol(TI): ! UI = TI.GetPlatformInterface() ! ! def GetConnControl(BB): ! return BB.Write('ConnLock', '') ! ! def ReleaseConnControl(BB): ! BB.Erase('ConnLock') ! ! def GetSensorControl(BB): ! return BB.Write('SensorLock', '') ! ! def ReleaseSensorControl(BB): ! BB.Erase('SensorLock') ! ! def SAM_Defense(UI): ! UI = TI.GetPlatformInterface() targetid = UI.GetTarget() if targetid != -1: *************** *** 95,99 **** heading = heading - 360.0 UI.SetHeading(heading) ! UI.SetUpdate(30.0) # engage hostiles in range --- 288,292 ---- heading = heading - 360.0 UI.SetHeading(heading) ! TI.SetUpdateInterval(30.0) # engage hostiles in range *************** *** 114,122 **** UI.SetTarget(best_id) UI.SetHeadingToInterceptTarget() ! UI.SetUpdate(4.0) # Patrols in given direction, zig-zagging along the way # then reverses path ! def ZigZagPatrol(UI): targetid = UI.GetTarget() if targetid != -1: --- 307,329 ---- UI.SetTarget(best_id) UI.SetHeadingToInterceptTarget() ! TI.SetUpdateInterval(4.0) ! ! ! ! def AvoidGround(UI): ! if (UI.IsAir()): ! terrainElevation = UI.GetTerrainElevation() ! if (terrainElevation < 0): ! terrainElevation = 0 ! alt = UI.GetAlt() ! if ((alt - terrainElevation) < 80): ! UI.SetAlt(terrainElevation + 100) ! ! ! # Patrols in given direction, zig-zagging along the way # then reverses path ! def ZigZagPatrolOld(UI): targetid = UI.GetTarget() if targetid != -1: *************** *** 361,374 **** return -1 ! ! # set heading relative to target bearing ! def SetHeadingOffTarget(UI, heading_offset): track_info = UI.GetTargetTrackInfo() lat = track_info.Lat lon = track_info.Lon ! heading = UI.GetHeadingToDatum(lon,lat) heading = heading + heading_offset if (heading > 360.0): heading = heading - 360.0 UI.SetHeading(heading) return --- 568,590 ---- return -1 ! # get target bearing in deg ! def GetTargetBearing(UI): track_info = UI.GetTargetTrackInfo() lat = track_info.Lat lon = track_info.Lon ! bearing = UI.GetHeadingToDatum(lon,lat) ! return bearing ! ! ! # set heading relative to target bearing ! def SetHeadingOffTarget(UI, heading_offset): ! heading = GetTargetBearing(UI) ! heading = heading + heading_offset if (heading > 360.0): heading = heading - 360.0 + elif (heading < -180): + heading = heading + 360.0 + UI.SetHeading(heading) return *************** *** 515,518 **** --- 731,764 ---- + def EngageTargetWithLauncher(UI, launcher): + launcher_info = UI.GetLauncherInfo(launcher) + if (not launcher_info.IsValid()): + return + launch_mode = launcher_info.LaunchMode # 0 - datum, 1 - seeker, 2 - other + target_info = UI.GetTargetTrackInfo() + + if target_info.IsAir(): + launch_qty = 1 + elif target_info.IsMissile(): + launch_qty = 1 + elif target_info.IsSurface(): + launch_qty = 4 + else: + launch_qty = 1 + if launch_mode == 0: # datum launch + lat = target_info.Lat + lon = target_info.Lon + alt = target_info.Alt + UI.HandoffTargetToLauncher(launcher) # to store intended target + UI.SendDatumToLauncher(lon,lat,alt,launcher) + UI.Launch(launcher, launch_qty) + UI.SetActionText('Datum launch') + elif launch_mode == 1: # handoff to active seeker + target_accepted = UI.HandoffTargetToLauncher(launcher) + if (target_accepted): + UI.Launch(launcher, launch_qty) + UI.SetActionText('Handoff launch') + + |