Skip to content

Commit 60f237b

Browse files
committed
Follow only unoccupied resource fileds
1 parent 0da73a7 commit 60f237b

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

src/client.zig

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub const Client = struct {
4141
try std.io.getStdOut().writer().print("server closed the connection\n", .{});
4242
return false;
4343
}
44-
print("MSG: {s}\n", .{buff.items});
44+
// print("MSG: {s}\n", .{buff.items});
4545

4646
try self.parse(buff.items, allocator);
4747
return true;
@@ -70,7 +70,6 @@ pub const Client = struct {
7070
try self.send("n");
7171
try self.send(&(self.name));
7272
try self.send("\n");
73-
print("x: {d}, y: {d}\n", .{ self.game.?.board.x, self.game.?.board.y });
7473
}
7574
},
7675
@intFromEnum(Message.Type.yes) => {
@@ -119,8 +118,6 @@ pub const Client = struct {
119118
unit.y = try std.fmt.parseUnsigned(u32, it3.next().?, 10);
120119
unit.hp = try std.fmt.parseUnsigned(u32, it3.next().?, 10);
121120
try self.game.?.newUnit(playerName, unit);
122-
123-
print("{d} {d} {d} {d}\n", .{ self.game.?.units.get(unit.id).?.id, unit.x, self.game.?.units.get(unit.id).?.y, unit.hp });
124121
}
125122
}
126123
self.game.?.printPlayerNames();
@@ -148,7 +145,6 @@ pub const Client = struct {
148145
const hp = try std.fmt.parseUnsigned(u32, it.next().?, 10);
149146

150147
self.game.?.board.getField(x, y).res_hp = hp;
151-
// self.game.?.board.printResources();
152148
},
153149
@intFromEnum(Message.Type.unit) => {
154150
var it = std.mem.tokenizeAny(u8, buff[1..], " \n");
@@ -158,9 +154,7 @@ pub const Client = struct {
158154
const x = try std.fmt.parseUnsigned(u32, it.next().?, 10);
159155
const y = try std.fmt.parseUnsigned(u32, it.next().?, 10);
160156

161-
print("{s} got new unit\n", .{playerName});
162157
try self.game.?.newUnit(playerName, Unit{ .id = id, .x = x, .y = y, .hp = 100 });
163-
self.game.?.board.printUnits(&self.game.?);
164158
},
165159
@intFromEnum(Message.Type.leave) => {
166160
const playerName = buff[1..]; // player name
@@ -170,10 +164,8 @@ pub const Client = struct {
170164
self.state = State.Ready;
171165
try self.send("j\n");
172166
} else {
173-
print("{s} left\n", .{playerName});
174167
try self.game.?.deletePlayer(playerName);
175168
}
176-
self.game.?.board.printUnits(&self.game.?);
177169
},
178170
@intFromEnum(Message.Type.lost) => {
179171
const playerName = buff[1..]; // player name
@@ -227,22 +219,22 @@ pub const Client = struct {
227219
},
228220
@intFromEnum(Message.Type.join) => {
229221
try self.game.?.newPlayer(buff[1..]);
230-
print("{s} joined\n", .{buff[1..]});
231222
},
232223
@intFromEnum(Message.Type.tick) => {
233224
const player = self.game.?.findPlayer(&self.name).?;
234225
var it = player.units.valueIterator();
235226
while (it.next()) |unit| {
236-
const fieldc = self.game.?.board.getClosestResourceFieldPosition(unit.*.x, unit.*.y);
237-
if (fieldc) |fc| {
238-
const cds = coordOps.towards(unit.*.x, unit.*.y, fc.x, fc.y);
239-
const max_len = 10; // TO DO figure out max_len from config sent by server
240-
var buf: [max_len]u8 = undefined;
241-
if (cds.x == unit.*.x and cds.y == unit.*.y) {
242-
try self.send("d");
243-
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.*.id}));
244-
try self.send("\n");
245-
} else {
227+
const max_len = 10; // TO DO figure out max_len from config sent by server
228+
var buf: [max_len]u8 = undefined;
229+
230+
if (self.game.?.board.getField(unit.*.x, unit.*.y).res_hp != null) {
231+
try self.send("d");
232+
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.*.id}));
233+
try self.send("\n");
234+
} else {
235+
const fieldc = self.game.?.board.getClosestUnoccupiedResourceFieldPosition(unit.*.x, unit.*.y);
236+
if (fieldc) |fc| {
237+
const cds = coordOps.towards(unit.*.x, unit.*.y, fc.x, fc.y);
246238
try self.send("m");
247239
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.*.id}));
248240
try self.send(" ");
@@ -254,9 +246,7 @@ pub const Client = struct {
254246
}
255247
}
256248
},
257-
else => {
258-
print("got invalid message type character {c}\n", .{t});
259-
},
249+
else => {},
260250
}
261251
}
262252

src/coordinateOps.zig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ pub fn towards(sx: u32, sy: u32, tx: u32, ty: u32) coords {
2626
return cds;
2727
}
2828

29-
pub fn same(x1: u32, y1: u32, x2: u32, y2: u32) bool {
30-
return (x1 == x2 and y1 == y2);
31-
}
32-
3329
pub const coords = struct {
3430
x: u32,
3531
y: u32,
@@ -42,7 +38,7 @@ pub const coords = struct {
4238
return ops.towards(sc.x, sc.y, tc.x, tc.y);
4339
}
4440

45-
pub fn same(c1: coords, c2: coords) bool {
41+
pub fn eql(c1: coords, c2: coords) bool {
4642
return (c1.x == c2.x and c1.y == c2.y);
4743
}
4844
};

src/game.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub const Board = struct {
2727
return &self.fields[x * self.y + y];
2828
}
2929

30-
pub fn getClosestResourceFieldPosition(self: *Board, x: u32, y: u32) ?coords {
30+
pub fn getClosestUnoccupiedResourceFieldPosition(self: *Board, x: u32, y: u32) ?coords {
3131
// TO DO write optimal version
3232
//var dist: u32 = 0;
3333
//const curField = self.getField(x, y);
@@ -41,7 +41,7 @@ pub const Board = struct {
4141
for (0..self.y) |yi| {
4242
for (0..self.x) |xi| {
4343
const f = self.getField(@intCast(xi), @intCast(yi));
44-
if (f.res_hp != null) {
44+
if (f.res_hp != null and f.unit_id == null) {
4545
const dist = distance(x, y, @intCast(xi), @intCast(yi));
4646
if (dist < minDist) {
4747
minDist = dist;
@@ -63,6 +63,7 @@ pub const Board = struct {
6363
}
6464

6565
pub fn printUnits(self: *Board, game: *Game) void {
66+
print("units:\n", .{});
6667
for (0..self.y) |yi| {
6768
for (0..self.x) |xi| {
6869
const f = self.getField(@intCast(xi), @intCast(yi));
@@ -75,6 +76,7 @@ pub const Board = struct {
7576
}
7677

7778
pub fn printResources(self: *Board) void {
79+
print("resources:\n", .{});
7880
for (0..self.y) |yi| {
7981
for (0..self.x) |xi| {
8082
const f = self.getField(@intCast(xi), @intCast(yi));

0 commit comments

Comments
 (0)