mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-24 05:03:34 -04:00
Make bot movement not use 90% cpu on mono and also make it consistent movement speed regardless of position sending interval, partially addresses #205.
This commit is contained in:
parent
036e758e38
commit
5e4a189511
@ -114,7 +114,7 @@ namespace MCGalaxy.Bots {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool DoSpeed(PlayerBot bot) {
|
static bool DoSpeed(PlayerBot bot) {
|
||||||
bot.movementSpeed = (int)Math.Round(24m / 100m * bot.Waypoints[bot.cur].seconds);
|
bot.movementSpeed = (int)Math.Round(3m / 100m * bot.Waypoints[bot.cur].seconds);
|
||||||
if (bot.movementSpeed == 0) bot.movementSpeed = 1;
|
if (bot.movementSpeed == 0) bot.movementSpeed = 1;
|
||||||
bot.NextInstruction(); return false;
|
bot.NextInstruction(); return false;
|
||||||
}
|
}
|
||||||
|
@ -47,12 +47,11 @@ namespace MCGalaxy {
|
|||||||
public ushort[] pos = new ushort[3], oldpos = new ushort[3], foundPos = new ushort[3];
|
public ushort[] pos = new ushort[3], oldpos = new ushort[3], foundPos = new ushort[3];
|
||||||
public byte[] rot = new byte[2], oldrot = new byte[2], foundRot = new byte[2];
|
public byte[] rot = new byte[2], oldrot = new byte[2], foundRot = new byte[2];
|
||||||
public bool movement = false;
|
public bool movement = false;
|
||||||
public int movementSpeed = 24;
|
public int movementSpeed = 3;
|
||||||
internal bool jumping = false;
|
internal bool jumping = false;
|
||||||
internal int currentjump = 0;
|
internal int currentjump = 0;
|
||||||
|
|
||||||
System.Timers.Timer botTimer = new System.Timers.Timer(100);
|
System.Timers.Timer botTimer = new System.Timers.Timer(100);
|
||||||
System.Timers.Timer moveTimer = new System.Timers.Timer(100 / 24);
|
|
||||||
internal System.Timers.Timer jumpTimer = new System.Timers.Timer(95);
|
internal System.Timers.Timer jumpTimer = new System.Timers.Timer(95);
|
||||||
|
|
||||||
public PlayerBot(string n, Level lvl, ushort x, ushort y, ushort z, byte rotx, byte roty) {
|
public PlayerBot(string n, Level lvl, ushort x, ushort y, ushort z, byte rotx, byte roty) {
|
||||||
@ -67,8 +66,6 @@ namespace MCGalaxy {
|
|||||||
|
|
||||||
botTimer.Elapsed += BotTimerFunc;
|
botTimer.Elapsed += BotTimerFunc;
|
||||||
botTimer.Start();
|
botTimer.Start();
|
||||||
moveTimer.Elapsed += MoveTimerFunc;
|
|
||||||
moveTimer.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Script handling
|
#region Script handling
|
||||||
@ -81,9 +78,9 @@ namespace MCGalaxy {
|
|||||||
if (hunt) Instructions.DoHunt(this);
|
if (hunt) Instructions.DoHunt(this);
|
||||||
} else {
|
} else {
|
||||||
bool doNextInstruction = !DoInstruction();
|
bool doNextInstruction = !DoInstruction();
|
||||||
if (cur == Waypoints.Count) cur = 0;
|
if (cur == Waypoints.Count) cur = 0;
|
||||||
|
|
||||||
if (!doNextInstruction) return;
|
if (!doNextInstruction) return;
|
||||||
DoInstruction();
|
DoInstruction();
|
||||||
if (cur == Waypoints.Count) cur = 0;
|
if (cur == Waypoints.Count) cur = 0;
|
||||||
}
|
}
|
||||||
@ -112,10 +109,7 @@ namespace MCGalaxy {
|
|||||||
if (cur == Waypoints.Count) cur = 0;
|
if (cur == Waypoints.Count) cur = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveTimerFunc(object sender, ElapsedEventArgs e) {
|
void DoMove() {
|
||||||
moveTimer.Interval = Server.updateTimer.Interval / movementSpeed;
|
|
||||||
if (!movement) return;
|
|
||||||
|
|
||||||
if ((pos[1] - 19) % 32 != 0 && !jumping)
|
if ((pos[1] - 19) % 32 != 0 && !jumping)
|
||||||
pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32));
|
pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32));
|
||||||
|
|
||||||
@ -144,7 +138,7 @@ namespace MCGalaxy {
|
|||||||
} else if (Block.Walkthrough(b1) && Block.Walkthrough(b2)) {
|
} else if (Block.Walkthrough(b1) && Block.Walkthrough(b2)) {
|
||||||
pos[0] += (ushort)dx; // Stay on current level
|
pos[0] += (ushort)dx; // Stay on current level
|
||||||
pos[2] += (ushort)dz;
|
pos[2] += (ushort)dz;
|
||||||
} else if (Block.Walkthrough(b) && Block.Walkthrough(b1)) {
|
} else if (Block.Walkthrough(b) && Block.Walkthrough(b1)) {
|
||||||
pos[0] += (ushort)dx; // Drop a level
|
pos[0] += (ushort)dx; // Drop a level
|
||||||
pos[1] -= (ushort)32;
|
pos[1] -= (ushort)32;
|
||||||
pos[2] += (ushort)dz;
|
pos[2] += (ushort)dz;
|
||||||
@ -198,7 +192,6 @@ namespace MCGalaxy {
|
|||||||
bot.GlobalDespawn();
|
bot.GlobalDespawn();
|
||||||
|
|
||||||
bot.botTimer.Stop();
|
bot.botTimer.Stop();
|
||||||
bot.moveTimer.Stop();
|
|
||||||
bot.jumpTimer.Stop();
|
bot.jumpTimer.Stop();
|
||||||
if (save)
|
if (save)
|
||||||
BotsFile.RemoveBot(bot);
|
BotsFile.RemoveBot(bot);
|
||||||
@ -236,9 +229,12 @@ namespace MCGalaxy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update() { }
|
|
||||||
|
|
||||||
void UpdatePosition() {
|
void UpdatePosition() {
|
||||||
|
if (movement) {
|
||||||
|
for (int i = 0; i < movementSpeed; i++)
|
||||||
|
DoMove();
|
||||||
|
}
|
||||||
|
|
||||||
byte[] packet = Entities.GetPositionPacket(this);
|
byte[] packet = Entities.GetPositionPacket(this);
|
||||||
oldpos = pos; oldrot = rot;
|
oldpos = pos; oldrot = rot;
|
||||||
if (packet == null) return;
|
if (packet == null) return;
|
||||||
@ -248,13 +244,10 @@ namespace MCGalaxy {
|
|||||||
if (p.level == level) p.SendRaw(packet);
|
if (p.level == level) p.SendRaw(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region == Misc ==
|
|
||||||
static byte FreeId()
|
static byte FreeId() {
|
||||||
{
|
for (byte i = 127; i >= 64; i--) {
|
||||||
for (byte i = 127; i >= 64; i--)
|
foreach (PlayerBot b in playerbots) {
|
||||||
{
|
|
||||||
foreach (PlayerBot b in playerbots)
|
|
||||||
{
|
|
||||||
if (b.id == i) { goto Next; }
|
if (b.id == i) { goto Next; }
|
||||||
} return i;
|
} return i;
|
||||||
Next: continue;
|
Next: continue;
|
||||||
@ -281,7 +274,6 @@ namespace MCGalaxy {
|
|||||||
return Extensions.FindMatches<PlayerBot>(pl, name, out matches, Bots.Items,
|
return Extensions.FindMatches<PlayerBot>(pl, name, out matches, Bots.Items,
|
||||||
b => true, b => b.name, "bots");
|
b => true, b => b.name, "bots");
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
public static void GlobalUpdatePosition() {
|
public static void GlobalUpdatePosition() {
|
||||||
PlayerBot[] bots = Bots.Items;
|
PlayerBot[] bots = Bots.Items;
|
||||||
|
@ -29,7 +29,7 @@ namespace MCGalaxy.Bots {
|
|||||||
|
|
||||||
PlayerBot.Pos newPos = new PlayerBot.Pos();
|
PlayerBot.Pos newPos = new PlayerBot.Pos();
|
||||||
try { bot.Waypoints.Clear(); } catch { }
|
try { bot.Waypoints.Clear(); } catch { }
|
||||||
bot.cur = 0; bot.countdown = 0; bot.movementSpeed = 12;
|
bot.cur = 0; bot.countdown = 0; bot.movementSpeed = 3;
|
||||||
|
|
||||||
foreach (string line in codes) {
|
foreach (string line in codes) {
|
||||||
if (line == "" || line[0] == '#') continue;
|
if (line == "" || line[0] == '#') continue;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user