@@ -6,8 +6,9 @@ const Message = @import("message.zig");
66const game = @import ("game.zig" );
77const Game = game .Game ;
88const Unit = game .Unit ;
9- const Config = game . Config ;
9+ const Config = @import ( " Config.zig" ) ;
1010const coordOps = @import ("coordinateOps.zig" );
11+ const NameIterator = @import ("NameIterator.zig" );
1112
1213const singleReadSize = 50 ;
1314
@@ -17,7 +18,7 @@ pub const Client = struct {
1718 stream : Stream ,
1819 state : State ,
1920 game : ? Game = null ,
20- name : [ 7 ] u8 = [ 7 ] u8 { 'z' , 'i' , 'g' , 'b' , 'o' , 't' , '!' } ,
21+ nameIter : NameIterator = undefined ,
2122
2223 /// sets up TCP connection
2324 /// after init .game is still null, .game will be initialized in .parse() if client recieves c message from server
@@ -79,29 +80,34 @@ pub const Client = struct {
7980 };
8081
8182 self .game = try Game .init (c , allocator );
83+
84+ self .nameIter = NameIterator .init (self .game .? .allowedNameCharacters ).? ;
85+
8286 try self .send ("n" );
83- try self .send (&(self .name ));
87+ try self .send (self .nameIter .current ());
88+ print ("requested name: {s}\n " , .{self .nameIter .current ()});
8489 try self .send ("\n " );
8590 }
8691 },
8792 @intFromEnum (Message .Type .yes ) = > {
8893 if (self .state == State .Connected ) {
8994 self .state = State .Ready ;
90- try std .io .getStdOut ().writer ().print ("named self: {s}\n joining...\n " , .{self .name });
95+ try std .io .getStdOut ().writer ().print ("named self: {s}\n joining...\n " , .{self .nameIter . current () });
9196 try self .send ("j\n " );
9297 }
9398 },
9499 @intFromEnum (Message .Type .no ) = > {
95100 if (self .state == State .Connected ) {
96- const last = self .name .len - 1 ;
97- if (self .name [last ] == '~' ) {
101+ const newName = self .nameIter .next ();
102+ if (newName ) | name | {
103+ try self .send ("n" );
104+ try self .send (name );
105+ try self .send ("\n " );
106+ print ("requested name: {s}\n " , .{name });
107+ } else {
98108 try std .io .getStdOut ().writer ().print ("unable to set a name\n look into mini-rts-server .config file and make sure characters z, i, g, b, o, t are set as valid name characters or contact your mini-rts-server administrator\n also make sure not too many zigbots are playing at the same time\n " , .{});
99109 return error .UnableToNameSelf ;
100110 }
101- self .name [last ] += 1 ;
102- try self .send ("n" );
103- try self .send (& self .name );
104- try self .send ("\n " );
105111 }
106112 },
107113 @intFromEnum (Message .Type .queue ) = > {
@@ -169,11 +175,11 @@ pub const Client = struct {
169175 const x = try std .fmt .parseUnsigned (u32 , it .next ().? , 10 );
170176 const y = try std .fmt .parseUnsigned (u32 , it .next ().? , 10 );
171177
172- try self .game .? .newUnit (playerName , Unit { .id = id , .x = x , .y = y , .hp = 100 });
178+ try self .game .? .newUnit (playerName , Unit { .id = id , .x = x , .y = y , .hp = self . game .? . unitHp });
173179 },
174180 @intFromEnum (Message .Type .leave ) = > {
175181 const playerName = buff [1.. ]; // player name
176- if (std .mem .eql (u8 , playerName , & self .name )) {
182+ if (std .mem .eql (u8 , playerName , self .nameIter . current () )) {
177183 try std .io .getStdOut ().writer ().print ("lost all units\n rejoining...\n " , .{});
178184 self .game .? .clear ();
179185 self .state = State .Ready ;
@@ -191,7 +197,7 @@ pub const Client = struct {
191197 },
192198 @intFromEnum (Message .Type .win ) = > {
193199 const playerName = buff [1.. ]; // player name
194- std .debug .assert (std .mem .eql (u8 , playerName , & self .name ));
200+ std .debug .assert (std .mem .eql (u8 , playerName , self .nameIter . current () ));
195201 try std .io .getStdOut ().writer ().print ("won the game\n rejoining...\n " , .{});
196202 self .game .? .clear ();
197203 self .state = State .Ready ;
@@ -236,7 +242,7 @@ pub const Client = struct {
236242 try self .game .? .newPlayer (buff [1.. ]);
237243 },
238244 @intFromEnum (Message .Type .tick ) = > {
239- const player = self .game .? .findPlayer (& self .name ).? ;
245+ const player = self .game .? .findPlayer (self .nameIter . current () ).? ;
240246 var it = player .units .valueIterator ();
241247 while (it .next ()) | unit | {
242248 const max_len = 10 ; // TO DO figure out max_len from config sent by server
@@ -273,6 +279,7 @@ pub const Client = struct {
273279
274280 pub fn deinit (self : * Client ) void {
275281 self .stream .close ();
282+ self .game .? .deinit ();
276283 }
277284};
278285
0 commit comments