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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
local tne

-- level is the number of levels up *from the caller*
-- we want to take our location from, defaulting to 1.
local function csloc(level)
  if level == nil then level = 0 end
  -- to debug.getinfo,
  --   1 = this very function (csloc)
  --   2 = the caller
  -- etc.
  -- we want our level = 0 to mean the caller of csloc,
  -- level = 1 to be the caller of that, etc.  So add 2.
  local info = debug.getinfo(level+2, "Sl")
  local filename
  if string.sub(info.source, 1, 1) == "@" then
    filename = string.sub(info.source, 2)
  else
    filename = "data:" .. info.source
  end
  return
  {
    filename = filename,
    line_number = info.currentline
  }
end

local tne

local function fraction(num, den)
  if den == nil then den = 1 end
  return tne(num)/tne(den)
end

local function funcapp(name, arguments, sloc)
  local fixed_arguments = {}
  for k,v in pairs(arguments) do
    fixed_arguments[k] = tne(v)
  end
  return tne{
    type = "function-application",
    source_location = sloc or csloc(1),
    function_name = name,
    arguments = fixed_arguments
  }
end

local noise_expression_metatable =
{
  __add = function(lhs, rhs)
    return tne{
      type = "function-application",
      source_location = csloc(1),
      function_name = "add",
      arguments = { tne(lhs), tne(rhs) }
    }
  end,
  __sub = function(lhs, rhs)
    return tne{
      type = "function-application",
      source_location = csloc(1),
      function_name = "subtract",
      arguments = { tne(lhs), tne(rhs) }
    }
  end,
  __unm = function(val)
    return tne{
      type = "function-application",
      source_location = csloc(1),
      function_name = "subtract",
      arguments = { tne(0), tne(val) }
    }
  end,
  __mul = function(lhs, rhs)
    return tne{
      type = "function-application",
      source_location = csloc(1),
      function_name = "multiply",
      arguments = { tne(lhs), tne(rhs) }
    }
  end,
  __div = function(lhs, rhs)
    return tne{
      type = "function-application",
      source_location = csloc(1),
      function_name = "divide",
      arguments = { tne(lhs), tne(rhs) }
    }
  end,
  __pow = function(lhs, rhs)
    return tne{
      type = "function-application",
      source_location = csloc(1),
      function_name = "exponentiate",
      arguments = { tne(lhs), tne(rhs) }
    }
  end
  -- Missing: __lt, __le, __eq because Lua forces the return values to be boolean, so these cannot be implemented to return a noise expression.
  --   Use less_than(), less_or_equal(), equals() instead.
}

function fixne(v)
  if v.type == nil then
    error("Tried to create noise expression with no 'type'")
  end
  if v.source_location == nil then
    error("Noise expression has no 'source_location'")
  end

  setmetatable(v, noise_expression_metatable)
  -- if v.expression_id == nil then
  --   v.expression_id = id_expression(v)
  -- end
  return v
end

function log2(power)
  return funcapp("log2", { power })
end

local function fmod(lhs, rhs)
  return funcapp("modulo", { lhs, rhs })
end

local function floor(value)
  return funcapp("floor", { value })
end

local function ceil(value)
  return funcapp("ceil", { value })
end

local function band(...)
  return funcapp("bitwise-and", { ... })
end

local function bor(...)
  return funcapp("bitwise-or", { ... })
end

local function bxor(...)
  return funcapp("bitwise-xor", { ... })
end

local function bnot(value)
  return funcapp("bitwise-not", { value })
end

local function sin(value)
  return funcapp("sin", { value })
end

local function cos(value)
  return funcapp("cos", { value })
end

local function atan2(y, x)
  return funcapp("atan2", { y, x })
end

-- This results in a number that is '0' for 'false' or '1' for 'true'.
local function less_than(lhs, rhs)
  return funcapp("less-than", { lhs, rhs })
end

-- This results in a number that is '0' for 'false' or '1' for 'true'.
local function less_or_equal(lhs, rhs)
  return funcapp("less-or-equal", { lhs, rhs })
end

-- This results in a number that is '0' for 'false' or '1' for 'true'.
local function equals(lhs, rhs)
  return funcapp("equals", { lhs, rhs })
end

-- 'to noise expression'
-- turns simple values into noise expressions and
-- adds a metatable so you can do arithmetic operations on noise expressions
function tne(v, sloc)
  if type(v) == "number" then
    return fixne{
      type = "literal-number",
      source_location = sloc or csloc(1),
      literal_value = v
    }
  elseif type(v) == "boolean" then
    return fixne{
      type = "literal-boolean",
      source_location = sloc or csloc(1),
      literal_value = v
    }
  elseif type(v) == "string" then
    return tne{
      type = "literal-string",
      literal_value = v
    }
  elseif type(v) == "table" then
    if v.type == nil then
      error("Can't turn table without 'type' property into noise expression")
    end
    if v.source_location == nil then
      v.source_location = sloc or csloc(1)
    end
    return fixne(v)
  else
    error("Can't turn "..type(v).." into noise expression")
  end
end

local function nfvar(name, sloc)
  return tne{
    type = "variable",
    source_location = sloc or csloc(1),
    variable_name = name
  }
end

local function literal_object(obj, sloc)
  return tne{
    type = "literal-object",
    source_location = sloc or csloc(1),
    literal_value = obj
  }
end

local function literal_string(str, sloc)
  return tne{
    type = "literal-string",
    source_location = sloc or csloc(1),
    literal_value = str
  }
end

local function absolute_value(x)
  return funcapp("absolute-value", {x})
end

local function autoplace_probability(autoplace)
  return funcapp("autoplace-probability", { literal_object(autoplace) })
end

local function autoplace_richness(autoplace)
  return funcapp("autoplace-richness", { literal_object(autoplace) })
end

local function random_penalty(source, random_penalty_amplitude, opts)
  if opts == nil then opts = {} end
  if random_penalty_amplitude == nil then random_penalty_amplitude = 1 end
  return tne{
    type = "function-application",
    function_name = "random-penalty",
    arguments =
    {
      x = opts.x or nfvar("x"),
      y = opts.y or nfvar("y"),
      source = tne(source),
      amplitude = tne(random_penalty_amplitude)
    }
  }
end

local function random_between(lower, upper)
  return random_penalty(upper, upper-lower)
end

local function random(amplitude)
  return random_penalty(amplitude, amplitude)
end

-- Call this to...define a noise function using lua syntax.
-- Your lua function will be passed x, y, tile properties, and map properties.
-- The arguments are 'noise expression' objects to which arithmetic operations may be applied.
local function define_noise_function( func )
  local x = nfvar("x")
  local y = nfvar("y")
  local tile_props =
  {
    x = x,
    y = y,
    distance = nfvar("distance"),
    tier = nfvar("tier_from_start")
  }
  local map_props =
  {
    seed = nfvar("map_seed"),
    width = nfvar("map_width"),
    height = nfvar("map_height"),
    starting_area_radius = nfvar("starting_area_radius"),
    segmentation_multiplier = nfvar("segmentation_multiplier"),
    terrace_elevation_offset = nfvar("terrace_elevation_offset"),
    terrace_elevation_interval = nfvar("terrace_elevation_interval"),
    wlc_elevation_offset = nfvar("wlc_elevation_offset"), -- add this to your (presumably centered around 0) elevation to correct water coverage
    wlc_elevation_minimum = nfvar("wlc_elevation_minimum"), -- minimum elevation to be applied to areas outside the starting lake *after* the offset
    water_level = nfvar("water_level"),
    finite_water_level = nfvar("finite_water_level")
  }
  return tne(func(x,y,tile_props,map_props), csloc(0)) -- TODO: Pass in sloc of the function, if we know it
end

local function clamp(v, min, max, sloc)
  return funcapp(
    "clamp",
    {
      tne(v, sloc),
      tne(min, sloc),
      tne(max, sloc)
    },
    sloc
  )
end

local function reduce(reducer, list)
  local result = list[1]
  for i=2,#list do
    result = reducer(result, list[i])
  end
  return result
end

local function max(...)
  local sloc = csloc(1)
  return reduce(function(a,b)
    return clamp(a, b, math.huge, sloc)
  end, {...})
end

local function min(...)
  local sloc = csloc(1)
  return reduce(function(a,b)
    return clamp(a, -math.huge, b, sloc)
  end, {...})
end

local function ridge(v, min, max, sloc)
  return tne{
    type = "function-application",
    function_name = "ridge",
    source_location = sloc or csloc(1),
    arguments =
    {
      tne(v),
      tne(min),
      tne(max)
    }
  }
end

local function terrace(v, offset, width, strength)
  return tne{
    type = "function-application",
    function_name = "terrace",
    arguments =
    {
      tne(v), tne(offset), tne(width), tne(strength)
    }
  }
end

-- Terrace at elevations at which the game will place cliffs
-- if change in elevation is steep enough.
-- strength = 0: no-op; strength = 1: vertical slopes between terrace elevations
local function terrace_for_cliffs(v, strength, map)
  if strength == nil then
    strength = clamp(nfvar("cliffiness"), 0, 1)
  end
  return max(
    terrace(v, map.terrace_elevation_offset, map.terrace_elevation_interval, strength),
    min(v, 4) -- Prevent area below the first cliff from going under water
  )
end

local function make_array(list)
  local value_expressions = {}

  for i=1,#list do
    value_expressions[i] = tne(list[i])
  end

  return tne{
    type = "array-construction",
    value_expressions = value_expressions
  }
end

local function make_point_list(list)
  local value_expressions = {}

  for i=1,#list do
    value_expressions[i] = make_array(list[i])
  end

  return tne{
    type = "array-construction",
    value_expressions = value_expressions
  }
end

local function distance_from(x, y, points, max_distance)
  local arguments =
  {
    x = x,
    y = y,
    points = points
  }
  if max_distance then
    arguments["maximum_distance"] = tne(max_distance)
  end
  -- todo: misspell to test compilation crashing gracefully
  return funcapp("distance-from-nearest-point", arguments)
end

local function get_control_setting(name)
  local vars = {}
  for i,attr in ipairs{"frequency","size","richness"} do
    for j,attrattr in ipairs{"multiplier"} do
      vars[attr.."_"..attrattr] = nfvar("control-setting:"..name..":"..attr..":"..attrattr)
    end
  end
  return vars
end

local function if_else_chain(...)
  local args = {...}
  local arg_count = 0
  local arg_expressions = {}
  for i,ar in ipairs(args) do
    arg_expressions[i] = tne(ar)
    arg_count = arg_count + 1
  end
  if arg_count % 2 ~= 1 then
    error("if_else_chain requires an odd number of arguments")
  end
  return tne
  {
    type = "if-else-chain",
    arguments = arg_expressions
  }
end

local function literal_object(val, sloc)
  return tne
  {
    type = "literal-object",
    source_location = sloc or csloc(1),
    literal_value = val
  }
end

local function literal_expression(x)
  return tne
  {
    type = "literal-expression",
    literal_value = tne(x)
  }
end

local function noise_layer_name_to_id(name)
  return funcapp("noise-layer-name-to-id", {tne(name)})
end

local function delimit_procedure(expression)
  return tne
  {
    type = "procedure-delimiter",
    expression = tne(expression)
  }
end

local function compile_time_log(...)
  local args = {...}
  arg_expressions = {}
  for i,arg in ipairs(args) do
    if type(arg) == "string" then
      arg = literal_string(arg)
    end
    arg_expressions[i] = tne(arg)
  end
  return funcapp("compile-time-log", arg_expressions)
end

-- Useful for preventing tail-calls
-- because we want to not lose part of the stacktrace
-- in order that csloc() gives the desired result
local function ident(x)
  return x
end

return
{
  csloc = csloc,
  to_noise_expression = tne,
  define_noise_function = define_noise_function,
  clamp = clamp,
  compile_time_log = compile_time_log,
  ident = ident,
  min = min,
  max = max,
  ridge = ridge,
  terrace = terrace,
  terrace_for_cliffs = terrace_for_cliffs,
  make_array = make_array,
  make_point_list = make_point_list,
  distance_from = distance_from,
  var = nfvar,
  get_control_setting = get_control_setting,
  absolute_value = absolute_value,
  autoplace_probability = autoplace_probability,
  autoplace_richness = autoplace_richness,
  fraction = fraction,
  function_application = funcapp,
  if_else_chain = if_else_chain,
  literal_expression = literal_expression,
  literal_object = literal_object,
  literal_string = literal_string,
  noise_layer_name_to_id = noise_layer_name_to_id,
  random = random,
  random_between = random_between,
  random_penalty = random_penalty,
  delimit_procedure = delimit_procedure,
  log2 = log2,
  fmod = fmod,
  floor = floor,
  ceil = ceil,
  band = band,
  bor = bor,
  bxor = bxor,
  bnot = bnot,
  sin = sin,
  cos = cos,
  atan2 = atan2,
  less_than = less_than,
  less_or_equal = less_or_equal,
  equals = equals
}