Missile no longer uses a separate thread

This commit is contained in:
UnknownShadow200 2017-06-13 16:56:04 +10:00
parent 1690baaa25
commit 4b2eb16806
4 changed files with 39 additions and 35 deletions

View File

@ -15,11 +15,11 @@
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Threading;
using MCGalaxy.Drawing.Ops;
using MCGalaxy.Maths;
using System;
using System.Collections.Generic;
using MCGalaxy.Drawing.Ops;
using MCGalaxy.Maths;
using MCGalaxy.Tasks;
namespace MCGalaxy.Commands.Fun {
public sealed class CmdMissile : WeaponCmd {
@ -42,9 +42,9 @@ namespace MCGalaxy.Commands.Fun {
args.ending = bp.ending;
args.pos = MakePos(p);
Thread gunThread = new Thread(() => DoShoot(args));
gunThread.Name = "MCG_Missile";
gunThread.Start();
SchedulerTask task = new SchedulerTask(MissileCallback, args,
TimeSpan.FromMilliseconds(100), true);
p.CriticalTasks.Add(task);
}
class MissileArgs {
@ -52,6 +52,7 @@ namespace MCGalaxy.Commands.Fun {
public ExtBlock block;
public EndType ending;
public Vec3U16 pos;
public bool Moving = true;
public List<Vec3U16> previous = new List<Vec3U16>();
public List<Vec3U16> allBlocks = new List<Vec3U16>();
@ -59,30 +60,39 @@ namespace MCGalaxy.Commands.Fun {
public int iterations;
}
void DoShoot(MissileArgs args) {
static void MissileCallback(SchedulerTask task) {
MissileArgs args = (MissileArgs)task.State;
Player p = args.player;
if (args.Moving) { PerformMove(args); return; }
if (args.ending == EndType.Teleport) {
args.ending = EndType.Normal;
int index = args.previous.Count - 3;
if (index >= 0 && index < args.previous.Count)
DoTeleport(p, args.previous[index]);
}
if (args.previous.Count > 0) {
Vec3U16 pos = args.previous[0];
args.previous.RemoveAt(0);
p.level.Blockchange(pos.X, pos.Y, pos.Z, ExtBlock.Air, true);
}
task.Repeating = args.previous.Count > 0;
}
static void PerformMove(MissileArgs args) {
while (true) {
args.iterations++;
Vec3U16 target = MissileTarget(args);
FindNext(target, ref args.pos, args.buffer);
if (args.iterations <= 3) continue;
if (!MoveMissile(args, args.pos, target)) break;
Thread.Sleep(100);
}
if (args.ending == EndType.Teleport) {
int index = args.previous.Count - 3;
if (index >= 0 && index < args.previous.Count)
DoTeleport(p, args.previous[index]);
}
foreach (Vec3U16 pos1 in args.previous) {
p.level.Blockchange(pos1.X, pos1.Y, pos1.Z, ExtBlock.Air, true);
Thread.Sleep(100);
args.Moving = MoveMissile(args, args.pos, target);
return;
}
}
static Vec3U16 MissileTarget(MissileArgs args) {
Player p = args.player;
Vec3U16 start = MakePos(p);
@ -147,7 +157,7 @@ namespace MCGalaxy.Commands.Fun {
return (Vec3U16)p.Pos.BlockCoords;
}
void FindNext(Vec3U16 lookedAt, ref Vec3U16 pos, List<Vec3S32> buffer) {
static void FindNext(Vec3U16 lookedAt, ref Vec3U16 pos, List<Vec3S32> buffer) {
LineDrawOp.DrawLine(pos.X, pos.Y, pos.Z, 2, lookedAt.X, lookedAt.Y, lookedAt.Z, buffer);
Vec3U16 end = (Vec3U16)buffer[buffer.Count - 1];
pos.X = end.X; pos.Y = end.Y; pos.Z = end.Z;

View File

@ -125,8 +125,5 @@ namespace MCGalaxy {
}
return list;
}
[Obsolete("Group parameter is completely ignored.")]
public static PlayerList Load(string path, Group grp) { return Load(path); }
}
}

View File

@ -248,11 +248,6 @@ namespace MCGalaxy {
}
}
[Obsolete("Use LevelInfo.Loaded.Add()")]
public static void addLevel(Level level) {
LevelInfo.Loaded.Add(level);
}
public void PlayerListUpdate() {
if (Server.s.OnPlayerListChange != null) Server.s.OnPlayerListChange(Player.players);
}
@ -335,10 +330,6 @@ namespace MCGalaxy {
internal void SettingsUpdate() {
if (OnSettingsUpdate != null) OnSettingsUpdate();
}
public static string FindColor(string name) {
return Group.findPlayerGroup(name).color;
}
/// <summary> Sets the main level of the server that new players spawn in. </summary>
/// <returns> true if main level was changed, false if not
@ -367,7 +358,9 @@ namespace MCGalaxy {
long end = GC.GetTotalMemory(false);
double deltaKB = (start - end) / 1024.0;
if (deltaKB >= 100.0) {
Server.s.Log("GC performed (freed " + deltaKB.ToString("F2") + " KB)", true);
string track = (end / 1024.0).ToString("F2");
string delta = deltaKB.ToString("F2");
Server.s.Log("GC performed (tracking " + track + " KB, freed " + delta + " KB)", true);
}
}
}

View File

@ -73,9 +73,13 @@ namespace MCGalaxy.Tasks {
}
SchedulerTask[] tasks = p.CriticalTasks.Items;
DateTime now = DateTime.UtcNow;
for (int i = 0; i < tasks.Length; i++) {
SchedulerTask task = tasks[i];
if (now < task.NextRun) continue;
task.Callback(task);
task.NextRun = now.Add(task.Delay);
if (task.Repeating) continue;
p.CriticalTasks.Remove(task);