internal Vector2 TranslateToRaderSpace(Vector2 pInWorld) { Vector2 result; Vector2 V = pInWorld - pos; float aziP; if (V.Y == 0) { if (V.X == 0) { return(Vector2.Zero); } else if (V.X > 0) { aziP = MathHelper.PiOver2; } else { aziP = -MathHelper.PiOver2; } } else if (V.Y < 0) { aziP = -(float)Math.Atan(V.X / V.Y); } else { aziP = MathHelper.Pi - (float)Math.Atan(V.X / V.Y); } result.X = MathTools.AngTransInPI(aziP - azi) / ang; result.Y = V.Length() / r; return(result); }
public void Authorize() { if (enemyInfo == null) { return; } Vector2 selfToEnemyVec = orderServer.Pos - enemyInfo.Pos; float selfToEnemyAzi = MathTools.AziFromRefPos(selfToEnemyVec); float deltaAzi = MathTools.AngTransInPI(selfToEnemyAzi - enemyInfo.TurretAimAzi); lastDeltaAngWise = deltaAzi > 0; Vector2 aimDir = Vector2.Transform(selfToEnemyVec, Matrix.CreateRotationZ((deltaAzi > 0 ? MathHelper.PiOver2 : -MathHelper.PiOver2) + deltaAzi)); Area curArea = FindArea(); if (curArea != Area.Middle) { Vector2 borderDir = FindBorderDir(curArea); if (Vector2.Dot(aimDir, borderDir) < 0) { aimDir = -aimDir; } } float aimAzi = MathTools.AziFromRefPos(aimDir); Vector2 selfDir = orderServer.Direction; if (Vector2.Dot(selfDir, aimDir) > 0) { action.AddOrder(new OrderRotaToAzi(aimAzi)); action.AddOrder(new OrderMove(100)); } else { action.AddOrder(new OrderRotaToAzi(aimAzi + MathHelper.Pi)); action.AddOrder(new OrderMove(-100)); } }
public void Observe() { List <IEyeableInfo> eyeableInfo = orderServer.GetEyeableInfo(); if (eyeableInfo.Count != 0) { IEyeableInfo first = eyeableInfo[0]; if (first is TankSinTur.TankCommonEyeableInfo) { enemyInfo = (TankSinTur.TankCommonEyeableInfo)first; float selfToEnemyAzi = MathTools.AziFromRefPos(orderServer.Pos - enemyInfo.Pos); float enemyTurretAzi = enemyInfo.TurretAimAzi; if (Math.Abs(MathTools.AngTransInPI(enemyTurretAzi - selfToEnemyAzi)) < guardAng) { curPriority = ConsiderPriority.High; return; } } } curPriority = ConsiderPriority.Vacancy; }
public void Authorize() { float curTurretAzi = orderServer.TurretAimAzi; float t = 0; float maxt = 30; Vector2 aimPos = Vector2.Zero; bool canShoot = false; while (t < maxt) { aimPos = enemyPos + enemyVel * t; float timeRota = Math.Abs(MathTools.AngTransInPI(MathTools.AziFromRefPos(aimPos - orderServer.TurretAxePos) - curTurretAzi) / orderServer.MaxRotaTurretSpeed); float timeShell = (Vector2.Distance(aimPos, orderServer.TurretAxePos) - orderServer.TurretLength) / orderServer.ShellSpeed; if (MathTools.FloatEqual(timeRota + timeShell, t, 0.05f)) { canShoot = true; break; } t += 0.05f; } if (canShoot) { aimmingTime = t; aimmingPos = aimPos; aimming = true; action.AddOrder(new OrderRotaTurretToPos(aimPos, 0, delegate(IActionOrder order) { orderServer.Fire(); aimming = false; }, true)); } }
private void SearchPath() { Vector2 curPos = orderServer.Pos; float curAzi = MathTools.AngTransInPI(orderServer.Azi); float aimAzi = MathTools.AziFromRefPos(aimPos - curPos); bool aimObstruct = false; bool curObstruct = false; bool crossPi = false; bool crossZero = false; float minAziMinus = 0; float maxAziMinus = -MathHelper.Pi; float minAziPlus = MathHelper.Pi; float maxAziPlus = 0; foreach (EyeableBorderObjInfo borderObjInfo in orderServer.EyeableBorderObjInfos) { //if (!((SceneCommonObjInfo)borderObjInfo.EyeableInfo.ObjInfo.SceneInfo).isTankObstacle) // continue; if (InputHandler.JustPressKey(Microsoft.Xna.Framework.Input.Keys.B)) { //borderObjInfo.UpdateConvexHall(); foreach (BordPoint bordP in borderObjInfo.Border.VisiBorder) { Vector2 logicP = Vector2.Transform(ConvertHelper.PointToVector2(bordP.p), borderObjInfo.EyeableInfo.CurTransMatrix); float azi = MathTools.AziFromRefPos(logicP - curPos); if (azi < 0) { minAziMinus = Math.Min(minAziMinus, azi); maxAziMinus = Math.Max(maxAziMinus, azi); } else { minAziPlus = Math.Min(minAziPlus, azi); maxAziPlus = Math.Max(maxAziPlus, azi); } if (MathTools.FloatEqualZero(MathTools.AngTransInPI(azi - MathHelper.Pi), 0.1f)) { crossPi = true; } if (MathTools.FloatEqualZero(azi, 0.1f)) { crossZero = true; } if (MathTools.FloatEqual(azi, aimAzi, 0.1f) && Vector2.Distance(logicP, curPos) < Vector2.Distance(aimPos, curPos)) { aimObstruct = true; } if (MathTools.FloatEqual(curAzi, azi, 0.1f) && Vector2.Distance(logicP, curPos) < Vector2.Distance(aimPos, curPos)) { curObstruct = true; } } } } if (!aimObstruct) { if (!MathTools.FloatEqual(curAzi, aimAzi, 0.1f)) { action.AddOrder(new OrderMoveToPosDirect(aimPos)); } else { orderServer.ForwardSpeed = orderServer.MaxForwardSpeed; } } else if (!curObstruct) { orderServer.ForwardSpeed = orderServer.MaxForwardSpeed; } else { orderServer.ForwardSpeed = 0; float aziEage1 = 0; float aziEage2 = 0; if (!crossZero && !crossPi) { if (minAziMinus == 0) { aziEage1 = minAziPlus; aziEage2 = maxAziPlus; } else { aziEage1 = minAziMinus; aziEage2 = maxAziMinus; } } else if (crossZero) { aziEage1 = minAziMinus; aziEage2 = maxAziPlus; } else if (crossPi) { aziEage1 = minAziPlus; aziEage2 = maxAziMinus; } else { } float curAimAzi = 0; if (Math.Abs(MathTools.AngTransInPI(curAzi - aziEage1)) < Math.Abs(MathTools.AngTransInPI(curAzi - aziEage2))) { curAimAzi = aziEage1 - 0.1f; } else { curAimAzi = aziEage2 + 0.1f; } rotaing = true; action.AddOrder(new OrderRotaToAzi(curAimAzi, 0, delegate(IActionOrder order) { rotaing = false; orderServer.ForwardSpeed = orderServer.MaxForwardSpeed; }, false)); } }
public void Update(float seconds) { #region OLDCODE //if (InputHandler.IsKeyDown( Keys.W )) //{ // orderServer.ForwardSpeed = 1000; //} //else if (InputHandler.IsKeyDown( Keys.S )) //{ // orderServer.ForwardSpeed = -1000; //} //else //{ // orderServer.ForwardSpeed = 0; //} //if (InputHandler.IsKeyDown( Keys.D )) //{ // orderServer.TurnRightSpeed = 20; //} //else if (InputHandler.IsKeyDown( Keys.A )) //{ // orderServer.TurnRightSpeed = -20; //} //else //{ // orderServer.TurnRightSpeed = 0; //} #endregion aimmingTime -= seconds; List <IEyeableInfo> eyeableInfos = orderServer.GetEyeableInfo(); if (eyeableInfos.Count != 0) { orderServer.TurnRaderWiseSpeed = 0; if (eyeableInfos[0] is ItemEyeableInfo) { lastRaderRotaWise = MathTools.AziFromRefPos(((ItemEyeableInfo)eyeableInfos[0]).Pos - orderServer.Pos) > 0; action.AddOrder(new OrderRotaRaderToPos(((ItemEyeableInfo)eyeableInfos[0]).Pos)); } else if (eyeableInfos[0] is TankSinTur.TankCommonEyeableInfo) { lastRaderRotaWise = MathTools.AziFromRefPos(((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Pos - orderServer.Pos) > 0; action.AddOrder(new OrderRotaRaderToPos(((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Pos)); } if (eyeableInfos[0] is ItemEyeableInfo) { lastItemPos = ((ItemEyeableInfo)eyeableInfos[0]).Pos; vel = ((ItemEyeableInfo)eyeableInfos[0]).Vel; } else if (eyeableInfos[0] is TankSinTur.TankCommonEyeableInfo) { lastItemPos = ((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Pos; vel = ((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Vel; } if (!aimming) { float curTurretAzi = orderServer.TurretAimAzi; float t = 0; float maxt = 30; bool canShoot = false; while (t < maxt) { aimPos = lastItemPos + vel * t; float timeRota = Math.Abs(MathTools.AngTransInPI(MathTools.AziFromRefPos(aimPos - orderServer.TurretAxePos) - curTurretAzi) / orderServer.MaxRotaTurretSpeed); float timeShell = (Vector2.Distance(aimPos, orderServer.TurretAxePos) - orderServer.TurretLength) / orderServer.ShellSpeed; if (MathTools.FloatEqual(timeRota + timeShell, t, 0.03f)) { canShoot = true; break; } t += 0.03f; } if (canShoot) { aimming = true; aimmingTime = t; action.AddOrder(new OrderRotaTurretToPos(aimPos, 0, delegate(IActionOrder order) { orderServer.Fire(); aimming = false; }, false)); } else { action.AddOrder(new OrderRotaTurretToPos(lastItemPos)); } } else { if ((lastItemPos + aimmingTime * vel - aimPos).Length() > 4) { aimming = false; } } } else { orderServer.TurnRaderWiseSpeed = orderServer.MaxRotaRaderSpeed * (lastRaderRotaWise ? 1 : -1); } action.Update(seconds); }