Skip to content

Commit

Permalink
Rewrite nextFreeId to not use a goto
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jun 30, 2017
1 parent fc0b71b commit 5f1f8e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
21 changes: 7 additions & 14 deletions fCraft/Commands/CpeCommands.cs
Expand Up @@ -159,8 +159,13 @@ static class CpeCommands {
string skinString1 = (cmd.Next() ?? entityName); string skinString1 = (cmd.Next() ?? entityName);
if (skinString1 != null) skinString1 = ParseSkin(skinString1); if (skinString1 != null) skinString1 = ParseSkin(skinString1);


entity = Entity.Create(entityName, skinString1, requestedModel, player.World, player.Position, getNewID(player.World)); sbyte newEntityId = Entity.NextFreeID(player.World.Name);
player.Message("Successfully created entity {0}&S with id:{1} and skin {2}.", entity.Name, entity.ID, entity.Skin, entity.Name); if (newEntityId == Packet.SelfId) {
player.Message("No more free ids left! Remove an entity first.");
} else {
entity = Entity.Create(entityName, skinString1, requestedModel, player.World, player.Position, newEntityId);
player.Message("Successfully created entity {0}&S with id:{1} and skin {2}.", entity.Name, entity.ID, entity.Skin, entity.Name);
}
break; break;
case "remove": case "remove":
player.Message("{0} was removed from {1}", entity.Name, player.World.Name); player.Message("{0} was removed from {1}", entity.Name, player.World.Name);
Expand Down Expand Up @@ -273,18 +278,6 @@ static class CpeCommands {
} }
} }


public static sbyte getNewID(World world) {
sbyte i = 1;
go:
foreach (Entity entity in Entity.AllIn(world)) {
if (entity.ID == i) {
i++;
goto go;
}
}
return i;
}

#endregion #endregion


#region ChangeModel #region ChangeModel
Expand Down
20 changes: 19 additions & 1 deletion fCraft/World/Entity.cs
Expand Up @@ -155,7 +155,23 @@ public sealed class Entity {
EntityList.Clear(); EntityList.Clear();
LoadAll(); LoadAll();
SpawnAll(); SpawnAll();
}

public unsafe static sbyte NextFreeID(string world) {
byte* used = stackalloc byte[128];
for (int i = 0; i < 128; i++)
used[i] = 0;

foreach (Entity entity in Entity.AllIn(world)) {
if (entity.ID >= 0) used[entity.ID] = 1;
}

for (int i = 0; i <= sbyte.MaxValue; i++ ) {
if (used[i] == 0) return (sbyte)i;
}
return Packet.SelfId;
} }



public static void SaveAll(bool verbose) { public static void SaveAll(bool verbose) {
try { try {
Expand Down Expand Up @@ -216,7 +232,9 @@ public sealed class Entity {
if (entity.World == null) continue; if (entity.World == null) continue;


sbyte id; sbyte id;
if (!sbyte.TryParse(entityData[3], out id)) { id = CpeCommands.getNewID(world); } if (!sbyte.TryParse(entityData[3], out id)) id = NextFreeID(entity.World);
if (id == Packet.SelfId) continue;

entity.ID = id; entity.ID = id;
entity.Name = entityData[0] ?? entity.ID.ToString(); entity.Name = entityData[0] ?? entity.ID.ToString();
entity.Skin = entityData[1] ?? entity.Name; entity.Skin = entityData[1] ?? entity.Name;
Expand Down

0 comments on commit 5f1f8e1

Please sign in to comment.