monotonicity

A city-building and transport simulation game written in Lean

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
import Lean.Data.Json
import Lens
import Raylean

open Lens
open Raylean Types

namespace Raylean

/-- This is in Raylib but not Raylean so let's just define it ourselves -/
def drawCubeV (pos size : Vector3) (color : Color) :=
  drawCube pos size.x size.y size.z color

def drawCubeWiresV (pos size : Vector3) (color : Color) :=
  drawCubeWires pos size.x size.y size.z color

end Raylean

def fps := 60
def screenWidth := 960
def screenHeight := 640

/-- Grid side length -/
def N := 500

/-- Grid height -/
def M := 20

def origin : Vector3 := N.toFloat / 20, M.toFloat / 20, N.toFloat / 20

instance : Add Vector3 where
  add a b := a.x + b.x, a.y + b.y, a.z + b.z

instance : Sub Vector3 where
  sub a b := a.x - b.x, a.y - b.y, a.z - b.z

instance : HDiv Vector3 Float Vector3 where
  hDiv a b := a.x / b, a.y / b, a.z / b

structure Nat3 where
  x : Nat
  y : Nat
  z : Nat
deriving Lean.ToJson, Lean.FromJson

inductive BuildingVariant
  | house
  | apartment
  | office
  | shop
  | factory
deriving Lean.ToJson, Lean.FromJson

def BuildingVariant.ofString? : String  Option BuildingVariant
  | "h" | "house" => some .house
  | "a" | "apartment" => some .apartment
  | "o" | "office" => some .office
  | "s" | "shop" => some .shop
  | "f" | "factory" => some .factory
  | _ => none

structure Building where
  pos : Nat3
  size : Nat3
  variant : BuildingVariant
deriving Lean.ToJson, Lean.FromJson

def Building.occupants (b : Building) :=
  match b.variant with
  | .house => 1
  | .apartment => b.size.x * b.size.y * b.size.z / 2
  | .office => 0
  | .shop => 0
  | .factory => 0

def Building.color (b : Building) :=
  match b.variant with
  | .house => Color.Raylean.red
  | .apartment => Color.Raylean.orange
  | .office => Color.Raylean.blue
  | .shop => Color.Raylean.purple
  | .factory => Color.Raylean.green

structure Vehicle where
  pos : Nat3
  dest : Nat3


inductive Road
  | none
  | low
  | high
deriving Lean.ToJson, Lean.FromJson

instance [Lean.ToJson α] : Lean.ToJson (Vector α n) where
  toJson := Array.toJson  Vector.toArray

instance [Lean.FromJson α] : Lean.FromJson (Vector α n) where
  fromJson? j := do
    let A : Array α  Array.fromJson? j
    if h : A.size = n then
      return h  A.toVector
    else
      throw s!"expected size {n}, got {A.size}"

structure Point where
  roads : Vector Road 24
  occupant : Option Nat
deriving Lean.ToJson, Lean.FromJson

def Point.empty : Point :=
  .replicate 24 .none, none

abbrev Grid := Vector (Vector (Vector Point (M + 1)) (N + 1)) (N + 1)

-- TODO: routing table for each building, reverse grid
structure State where
  rng : Nat
  day : Nat
  time : Nat
  grid : Grid
  buildings : Array Building
deriving Lean.ToJson, Lean.FromJson

makeLenses State

-- This is a big TODO, probably want to use a state monad here?
def tick (s : State) : State := Id.run do
  return s


def myroad : Road := .low

def serialized := Lean.toJson myroad |>.compress

def blah : Except String Road := Lean.Json.parse serialized >>= Lean.fromJson?

def Nat3.toVector3 (p : Nat3) : Vector3 :=
  p.x.toFloat / 10, p.y.toFloat / 10, p.z.toFloat / 10

instance : ToString Vector3 := fun v  s!"({v.1}, {v.2}, {v.3})"

def render (s : State) : IO Unit := do
  for b in s.buildings do
    let p := b.pos.toVector3
    let s := b.size.toVector3
    let o := p - origin + s / 2.0
    drawCubeV o s b.color
    drawCubeWiresV o s .black

def getInput (stdin : IO.FS.Stream) := do
  IO.print "> "
  return ( stdin.getLine).trimAsciiEnd.toString

-- TODO error messages
-- Load game
-- Save game
-- Build roads
def handleCmd (s : State) (cmd : String) :=
  match cmd.split ' ' |>.toStringList with
  | "b" :: variant :: dims =>
    let variant := BuildingVariant.ofString? variant
    if h : dims.length = 6 && variant.isSome then
      let dims := dims.map String.toNat!
      have : dims.length = 6 := by grind
      over State.Lens.buildings (fun (b : Array Building)  b.push dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], variant.get (by grind)) s
    else
      s
  | _ =>
    s

def main : IO Unit := do
  -- This constant is FLAG_WINDOW_HIGHDPI || FLAG_WINDOW_RESIZABLE
  -- https://github.com/raysan5/raylib/blob/aaacda6e147031f2af0cfb6c1fd7e64d761ddb1f/src/raylib.h#L567
  setConfigFlags 0x00002004
  initWindow screenWidth screenHeight "LeanTTD"
  setTargetFPS fps
  let mut camera : Camera3D := {
    position := 10, 10, 10
    target := 0, 0, 0
    up := 0, 1, 0
    fovy := 45
    projection := .perspective
  }
  let mut s : State := {
    rng := 0
    day := 0
    time := 0
    grid := Vector.replicate _ (Vector.replicate _ (Vector.replicate _ .empty))
    buildings := #[250, 10, 250, 20, 5, 5, .apartment]
  }
  let stdin  IO.getStdin
  let mut task  IO.asTask <| getInput stdin
  while !( windowShouldClose) do
    camera  updateCamera camera .thirdPerson
    if  IO.hasFinished task then
      let cmd  (.ofExcept task.get)
      s := handleCmd s cmd
      IO.println <| Lean.toJson s.buildings
      task  IO.asTask <| getInput stdin
    renderFrame do
      drawFPS (screenWidth - 100) 10
      clearBackground Color.white
      renderWithCamera camera do
        drawGrid (N / 20) 1
        render s
  closeWindow