Skip to content

Commit fd1b98e

Browse files
committed
Move to pointer based system for storing players and units
1 parent 86561ba commit fd1b98e

File tree

2 files changed

+80
-61
lines changed

2 files changed

+80
-61
lines changed

src/client.zig

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,10 @@ pub const Client = struct {
182182
const x = try std.fmt.parseUnsigned(u32, it.next().?, 10);
183183
const y = try std.fmt.parseUnsigned(u32, it.next().?, 10);
184184

185-
var unit = self.game.?.units.get(id).?;
185+
const unit = self.game.?.units.get(id).?;
186186
self.game.?.board.getField(unit.x, unit.y).unit_id = null;
187187
unit.x = x;
188188
unit.y = y;
189-
try self.game.?.units.put(id, unit);
190189
self.game.?.board.getField(x, y).unit_id = id;
191190
},
192191
@intFromEnum(Message.Type.attack) => {
@@ -197,13 +196,9 @@ pub const Client = struct {
197196
const hp = try std.fmt.parseUnsigned(u32, it.next().?, 10);
198197

199198
if (hp == 0) {
200-
const unit = self.game.?.units.get(id2).?;
201-
self.game.?.board.getField(unit.x, unit.y).unit_id = null;
202-
_ = self.game.?.units.remove(id2);
199+
self.game.?.deleteUnit(id2);
203200
} else {
204-
var unit = self.game.?.units.get(id2).?;
205-
unit.hp = hp;
206-
try self.game.?.units.put(id2, unit);
201+
self.game.?.units.get(id2).?.hp = hp;
207202
}
208203
},
209204
@intFromEnum(Message.Type.mine) => {
@@ -223,19 +218,19 @@ pub const Client = struct {
223218
const player = self.game.?.findPlayer(&self.name).?;
224219
var it = player.units.valueIterator();
225220
while (it.next()) |unit| {
226-
const fieldc = self.game.?.board.getClosestResourceFieldPosition(unit.x, unit.y);
221+
const fieldc = self.game.?.board.getClosestResourceFieldPosition(unit.*.x, unit.*.y);
227222
if (fieldc) |fc| {
228-
const cds = coordOps.towards(unit.x, unit.y, fc.x, fc.y);
223+
const cds = coordOps.towards(unit.*.x, unit.*.y, fc.x, fc.y);
229224
const max_len = 10; // TO DO figure out max_len from config sent by server
230225
var buf: [max_len]u8 = undefined;
231226
try self.send("m");
232-
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.id}));
227+
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.*.id}));
233228
try self.send(" ");
234229
try self.send(try std.fmt.bufPrint(&buf, "{}", .{cds.x}));
235230
try self.send(" ");
236231
try self.send(try std.fmt.bufPrint(&buf, "{}", .{cds.y}));
237232
try self.send("\n");
238-
print("move from {d} {d}\n", .{ unit.x, unit.y });
233+
print("move from {d} {d}\n", .{ unit.*.x, unit.*.y });
239234
}
240235
}
241236
},

src/game.zig

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const print = std.debug.print;
44
const distance = @import("coordinateOps.zig").distance;
55
const coords = @import("coordinateOps.zig").coords;
66

7-
pub const Unit = struct { id: u32 = undefined, hp: u32 = undefined, x: u32 = undefined, y: u32 = undefined, owner: u32 = undefined };
7+
pub const Unit = struct { id: u32 = undefined, hp: u32 = undefined, x: u32 = undefined, y: u32 = undefined, owner: ?*Player = undefined };
88

99
pub const Field = struct { unit_id: ?u32, res_hp: ?u32 };
1010

@@ -67,7 +67,7 @@ pub const Board = struct {
6767
for (0..self.x) |xi| {
6868
const f = self.getField(@intCast(xi), @intCast(yi));
6969
if (f.unit_id) |id| {
70-
print("{d}", .{game.units.get(id).?.owner});
70+
print("{c}", .{game.units.get(id).?.owner.?.name.items[0]});
7171
} else print("-", .{});
7272
}
7373
print("\n", .{});
@@ -93,20 +93,14 @@ pub const Board = struct {
9393

9494
pub const Player = struct {
9595
name: std.ArrayList(u8),
96-
units: std.AutoHashMap(u32, Unit),
97-
id: u32,
96+
units: std.AutoHashMap(u32, *Unit),
9897

99-
pub fn init(name: []const u8, id: u32, allocator: std.mem.Allocator) !Player {
100-
var p = Player{ .name = try std.ArrayList(u8).initCapacity(allocator, name.len), .units = std.AutoHashMap(u32, Unit).init(allocator), .id = id };
98+
pub fn init(name: []const u8, allocator: std.mem.Allocator) !Player {
99+
var p = Player{ .name = try std.ArrayList(u8).initCapacity(allocator, name.len), .units = std.AutoHashMap(u32, *Unit).init(allocator) };
101100
p.name.appendSliceAssumeCapacity(name);
102101
return p;
103102
}
104103

105-
pub fn newUnit(self: *Player, unit: *Unit) !void {
106-
unit.owner = self.id;
107-
try self.units.put(unit.id, unit.*);
108-
}
109-
110104
pub fn deinit(self: *Player) void {
111105
self.name.deinit();
112106
self.units.deinit();
@@ -115,92 +109,122 @@ pub const Player = struct {
115109

116110
pub const Game = struct {
117111
board: Board,
118-
players: std.ArrayList(Player),
119-
units: std.AutoHashMap(u32, Unit),
120-
nextPlayerId: u32 = 0,
112+
players: std.ArrayList(*Player),
113+
units: std.AutoHashMap(u32, *Unit),
114+
allocator: std.mem.Allocator,
121115

122116
pub fn init(x: u32, y: u32, allocator: std.mem.Allocator) !Game {
123-
return Game{ .board = try Board.init(x, y, allocator), .units = std.AutoHashMap(u32, Unit).init(allocator), .players = std.ArrayList(Player).init(allocator) };
117+
return Game{ .board = try Board.init(x, y, allocator), .units = std.AutoHashMap(u32, *Unit).init(allocator), .players = std.ArrayList(*Player).init(allocator), .allocator = allocator };
124118
}
125119

126120
pub fn findPlayer(self: *Game, playerName: []const u8) ?*Player {
127-
for (self.players.items) |*player| {
121+
for (self.players.items) |player| {
128122
if (std.mem.eql(u8, player.name.items, playerName)) return player;
129123
}
130124
return null;
131125
}
132126

133-
pub fn findPlayerArrayPosition(self: *Game, playerName: []const u8) ?u32 {
134-
for (self.players.items, 0..) |*player, id| {
127+
fn findPlayerArrayPosition(self: *Game, playerName: []const u8) ?u32 {
128+
for (self.players.items, 0..) |player, id| {
135129
if (std.mem.eql(u8, player.name.items, playerName)) return @intCast(id);
136130
}
137131
return null;
138132
}
139133

140134
pub fn newPlayer(self: *Game, playerName: []const u8) !void {
141135
std.debug.assert(self.findPlayer(playerName) == null);
142-
try self.players.append(try Player.init(playerName, self.nextPlayerId, self.players.allocator));
143-
self.nextPlayerId += 1;
144-
}
145136

146-
pub fn printPlayerNames(self: *Game) void {
147-
print("players:\n", .{});
148-
for (self.players.items) |*player| {
149-
print("{s} id:{d}\n", .{ player.name.items, player.id });
150-
}
137+
const player = try self.allocator.create(Player);
138+
errdefer self.allocator.destroy(player);
139+
player.* = try Player.init(playerName, self.players.allocator);
140+
141+
try self.players.append(player);
151142
}
152143

153144
pub fn deletePlayer(self: *Game, playerName: []const u8) error{OutOfMemory}!void {
154145
const arid = self.findPlayerArrayPosition(playerName).?;
155-
var pl = self.players.items[arid];
156-
const pid = pl.id;
157-
pl.deinit();
158-
_ = self.players.orderedRemove(arid);
146+
var player = self.players.items[arid];
159147

160-
var toRemove = std.ArrayList(u32).init(self.units.allocator);
148+
var toRemove = std.ArrayList(u32).init(self.allocator);
161149
defer toRemove.deinit();
162150

163151
var it = self.units.valueIterator();
164152
while (it.next()) |unit| {
165-
if (unit.owner == pid) try toRemove.append(unit.id);
153+
if (unit.*.owner == player) try toRemove.append(unit.*.id);
166154
}
167155

168156
for (toRemove.items) |uid| {
169-
const x = self.units.get(uid).?.x;
170-
const y = self.units.get(uid).?.y;
171-
self.board.getField(x, y).unit_id = null;
172-
_ = self.units.remove(uid);
157+
self.deleteUnit(uid);
173158
}
159+
160+
player.deinit();
161+
self.allocator.destroy(player);
162+
_ = self.players.orderedRemove(arid);
174163
}
175164

176-
pub fn clear(self: *Game) void {
177-
self.board.clear();
165+
pub fn printPlayerNames(self: *Game) void {
166+
print("players:\n", .{});
167+
for (self.players.items) |player| {
168+
print("{s}\n", .{player.name.items});
169+
}
170+
}
171+
172+
/// unit paremeter should be partially filed (with hp, x, y and id, owner should be left undefined)
173+
pub fn newUnit(self: *Game, playerName: []const u8, unit: Unit) !void {
174+
std.debug.assert(self.findPlayer(playerName) != null);
178175

179-
for (self.players.items) |*player| {
176+
const unitPtr = try self.allocator.create(Unit);
177+
errdefer self.allocator.destroy(unitPtr);
178+
unitPtr.* = unit;
179+
180+
var player = self.findPlayer(playerName).?;
181+
unitPtr.owner = player;
182+
try player.units.put(unitPtr.id, unitPtr);
183+
try self.units.put(unitPtr.id, unitPtr);
184+
self.board.getField(unitPtr.x, unitPtr.y).unit_id = unitPtr.id;
185+
}
186+
187+
pub fn deleteUnit(self: *Game, id: u32) void {
188+
const unit = self.units.get(id).?;
189+
const player = unit.owner.?;
190+
191+
self.board.getField(unit.x, unit.y).unit_id = null;
192+
_ = player.units.remove(unit.id);
193+
_ = self.units.remove(id);
194+
195+
self.allocator.destroy(unit);
196+
}
197+
198+
pub fn clear(self: *Game) void {
199+
for (self.players.items) |player| {
180200
player.deinit();
201+
self.allocator.destroy(player);
181202
}
182203
self.players.shrinkRetainingCapacity(0);
183204

184-
const alloc = self.units.allocator;
205+
var it = self.units.valueIterator();
206+
while (it.next()) |unit| {
207+
self.allocator.destroy(unit.*);
208+
}
185209
self.units.deinit();
186-
self.units = std.AutoHashMap(u32, Unit).init(alloc);
187-
}
210+
self.units = std.AutoHashMap(u32, *Unit).init(self.allocator);
188211

189-
pub fn newUnit(self: *Game, playerName: []const u8, unit: Unit) !void {
190-
var u = unit;
191-
var pl = self.findPlayer(playerName).?;
192-
try pl.newUnit(&u);
193-
try self.units.put(u.id, u);
194-
self.board.getField(u.x, u.y).unit_id = u.id;
212+
self.board.clear();
195213
}
196214

197215
pub fn deinit(self: *Game) void {
198-
for (self.players.items) |*player| {
216+
for (self.players.items) |player| {
199217
player.deinit();
218+
self.allocator.destroy(player);
200219
}
201220
self.players.deinit();
202221

222+
var it = self.units.valueIterator();
223+
while (it.next()) |unit| {
224+
self.allocator.destroy(unit.*);
225+
}
203226
self.units.deinit();
227+
204228
self.board.deinit();
205229
}
206230
};

0 commit comments

Comments
 (0)