@@ -3,7 +3,7 @@ open Gmap
3
3
4
4
let debug = false
5
5
6
- let add_mountain_pixel = function
6
+ let pixel_apply_mountain = function
7
7
| Foothills_pixel -> Hills_pixel
8
8
| Hills_pixel -> Mountain_pixel
9
9
| Ocean_pixel
@@ -147,7 +147,7 @@ let add_mountains_list r area =
147
147
This particular function needs to access the map, and since resources can only
148
148
be in certain tiles, it may fail and need to try again and again.
149
149
*)
150
- let add_resource area ~map ~land_type ~resource_pixel ~wanted_tile ~random_seed ~r =
150
+ let add_resource area ~map ~land_pixel ~resource_pixel ~wanted_tile ~random_seed ~r =
151
151
let rec loop () =
152
152
let x = Random. int 256 r in
153
153
let y = Random. int 192 r in
@@ -159,21 +159,21 @@ let add_resource area ~map ~land_type ~resource_pixel ~wanted_tile ~random_seed
159
159
| Europe -> x
160
160
in
161
161
let rec attempt i x y =
162
- if i > = 2 then false else
163
- let offset = calc_offset x y in
164
- let tile = map.(offset) in
162
+ if i > = 2 then None else
163
+ let pixel = Gmap. get_pixel ~map ~x ~y in
165
164
let possible_tile = tile_of_pixel ~x ~y ~random_seed ~pixel: resource_pixel in
166
- if Gmap. equal_tile tile land_type && Gmap. equal_tile possible_tile wanted_tile then (
167
- map.(offset) < - possible_tile ;
168
- true
165
+ if Gmap. equal_pixel pixel land_pixel && Gmap. equal_tile possible_tile wanted_tile then (
166
+ Gmap. set_pixel ~map ~x ~y ~pixel: resource_pixel ~random_seed ;
167
+ Some (x, y)
169
168
) else
170
169
let x = if y mod 2 = 1 then x + 1 else x - 1 in
171
170
attempt (i + 1 ) x (y + 1 )
172
171
in
173
- if attempt 0 x y then ()
174
- else loop () (* Keep trying *)
172
+ match attempt 0 x y with
173
+ | None -> loop ()
174
+ | x -> x
175
175
in
176
- loop ()
176
+ loop () |> Option. get_exn_or " Impossible failure reached "
177
177
178
178
(* A general list of resources to add *)
179
179
let add_resources_list area =
@@ -192,7 +192,7 @@ let add_resources_list area =
192
192
(Clear_pixel , OilWell_pixel , OilWell , count);
193
193
]
194
194
195
- let upgrade_city_pixel = function
195
+ let pixel_apply_city = function
196
196
| Clear_pixel
197
197
| Woods_pixel
198
198
| Farm_pixel -> Village_pixel
@@ -260,17 +260,62 @@ let load_city_list ?(debug=false) area =
260
260
cities
261
261
262
262
type t = {
263
+ area : area ;
263
264
mountains : (int * int ) list ;
264
265
resources : (pixel * pixel * tile * int ) list ;
265
266
cities : (int * int ) list ;
266
267
current : [`Mountains | `Resources | `Cities ];
268
+ new_pixels : (int * int * pixel ) list ;
269
+ random_seed : int ;
267
270
}
268
271
269
- let init r area cities =
272
+ let init r area cities ~ random_seed =
270
273
let mountains = add_mountains_list r area in
271
274
let resources = add_resources_list area in
272
275
let cities = add_city_list r area cities in
273
276
let current = `Mountains in
274
- {mountains; resources; cities; current}
277
+ let new_pixels = [] in
278
+ {area; mountains; resources; cities; current; new_pixels; random_seed}
279
+
280
+ (* Perform a step of updating the map *)
281
+ let update_map_step r v map =
282
+ let random_seed = v.random_seed in
283
+ let is_done = false in
284
+ match v.current with
285
+ | `Mountains ->
286
+ begin match v.mountains with
287
+ | (x , y )::rest ->
288
+ let pixel = Gmap. get_pixel ~map ~x ~y in
289
+ let pixel = pixel_apply_mountain pixel in
290
+ Gmap. set_pixel ~map ~x ~y ~pixel ~random_seed ;
291
+ let new_pixels = (x, y, pixel)::v.new_pixels in
292
+ {v with mountains= rest; new_pixels}, is_done
293
+ | _ ->
294
+ {v with current= `Resources }, is_done
295
+ end
296
+ | `Cities ->
297
+ begin match v.cities with
298
+ | (x , y )::rest ->
299
+ let pixel = Gmap. get_pixel ~map ~x ~y in
300
+ let pixel = pixel_apply_city pixel in
301
+ Gmap. set_pixel ~map ~x ~y ~pixel ~random_seed ;
302
+ let new_pixels = (x, y, pixel)::v.new_pixels in
303
+ {v with cities= rest; new_pixels}, is_done
304
+ | _ -> v, true
305
+ end
306
+ | `Resources ->
307
+ begin match v.resources with
308
+ | (_ , _ , _ , 0 )::rest ->
309
+ {v with resources= rest}, is_done
310
+ | (land_pixel , resource_pixel , wanted_tile , num )::rest ->
311
+ let x, y =
312
+ add_resource v.area ~map ~land_pixel ~resource_pixel ~wanted_tile ~random_seed ~r
313
+ in
314
+ let new_pixels = (x, y, resource_pixel)::v.new_pixels in
315
+ let resources = (land_pixel, resource_pixel, wanted_tile, num-1 )::rest in
316
+ {v with resources; new_pixels}, is_done
317
+ | _ -> {v with current= `Cities }, is_done
318
+ end
319
+
275
320
276
321
0 commit comments