@@ -4,7 +4,7 @@ const print = std.debug.print;
44const distance = @import ("coordinateOps.zig" ).distance ;
55const 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
99pub 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
9494pub 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
116110pub 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