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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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
local util = require("new-hope-util")
local final_screen = require("final_screen")
require("story")

local car_supplies =
{
  ["piercing-rounds-magazine"] =  200,
  ["steel-plate"] = 200,
  ["iron-gear-wheel"] = 250,
  ["electronic-circuit"] = 500,
  ["solar-panel"] = 200
}

local get_car_contents = function()
  local cars = global.cars
  local contents = {}
  for k, car in pairs (cars) do
    if not car.valid then
      cars[k] = nil
    else
      for name, count in pairs(car.get_inventory(defines.inventory.car_trunk).get_contents()) do
        contents[name] = (contents[name] or 0) + count
      end
      for name, count in pairs(car.get_inventory(defines.inventory.car_ammo).get_contents()) do
        contents[name] = (contents[name] or 0) + count
      end
    end
  end
  return contents
end

local update_materials_gui = function(gui)
  local contents = get_car_contents()
  local table = gui.holding_table
  if not table then
    table = gui.add{type = "table", column_count = 3, style = "bordered_table", name = "holding_table"}
  end

  local items = game.item_prototypes

  for item, count in pairs(car_supplies) do
    if items[item] then
      local count_label = table[item]
      if not count_label then
        local sprite = table.add{type = "sprite", sprite = "item/"..item}
        sprite.style.width = 32
        sprite.style.height = 32
        local item_label = table.add{type = "label", caption = items[item].localised_name, style = "bold_label"}
        item_label.style.horizontally_stretchable = false
        count_label = table.add{type = "label", name = item}
      end
      local current_count = contents[item] or 0
      count_label.caption = current_count.."/"..count
      if current_count >= count then
        count_label.style = "bold_green_label"
      else
        count_label.style = "label"
      end
    end
  end

end

local car_content_check = function()
  local car_contents = get_car_contents()
  for item, count in pairs (car_supplies) do
    if not car_contents[item] then return end
    if car_contents[item] < count then return end
  end
  return true
end

local researched_technology_list =
{
  ["automation-2"] = true,
  ["automation"] = true,
  ["automobilism"] = true,
  ["electronics"] = true,
  ["engine"] = true,
  ["fast-inserter"] = true,
  ["heavy-armor"] = true,
  ["logistic-science-pack"] = true,
  ["logistics-2"] = true,
  ["logistics"] = true,
  ["military"] = true,
  ["optics"] = true,
  ["physical-projectile-damage-1"] = true,
  ["steel-axe"] = true,
  ["steel-processing"] = true,
  ["stone-wall"] = true,
  ["gun-turret"] = true,
  ["weapon-shooting-speed-1"] = true
}

local enabled_technology_list =
{
  ["advanced-material-processing"] = true,
  ["automated-rail-transportation"] = true,
  ["concrete"] = true,
  ["electronics"] = true,
  ["electric-energy-distribution-1"] = true,
  ["engine"] = true,
  ["gate"] = true,
  ["landfill"] = true,
  ["military-2"] = true,
  ["physical-projectile-damage-2"] = true,
  ["rail-signals"] = true,
  ["railway"] = true,
  ["research-speed-1"] = true,
  ["research-speed-2"] = true,
  ["solar-energy"] = true,
  ["stronger-explosives-1"] = true,
  ["toolbelt"] = true,
  ["weapon-shooting-speed-2"] = true
}

local spawn_position = {-23, 5}

script.on_init(function()
  global.story = story_init()
  game.forces.player.set_spawn_position(spawn_position, 1)
end)

local set_remnants_permament = function()
  for k, remnant in pairs (game.surfaces[1].find_entities_filtered{type = {"corpse", "rail-remnants"}}) do
    if remnant.name:find("remnant") then
      remnant.corpse_expires = false
    end
  end
end

local chart_areas = function()
  local surface = game.surfaces[1]
  local force = game.forces.player
  for k, entity in pairs (surface.find_entities_filtered{force = force, area = {{-500, -250},{500, 250}}}) do
    local position = entity.position
    force.chart(surface, {{position.x - 16, position.y - 16}, {position.x + 16, position.y + 16}})
  end
  force.chart(surface, {{spawn_position[1] - 200, spawn_position[2] - 200}, {spawn_position[1] + 200, spawn_position[2] + 200}})
end

local init = function()

  game.map_settings.enemy_expansion.enabled = false
  game.map_settings.enemy_evolution.enabled = false
  game.forces.enemy.evolution_factor = 0
  game.surfaces[1].daytime = 0.8

  local force = game.forces.player
  force.reset_recipes()
  force.disable_all_prototypes()

  util.set_technologies_enabled(force, enabled_technology_list)
  util.set_technologies_researched(force, researched_technology_list)

  force.reset_technology_effects()
  force.clear_chart()
  force.maximum_following_robot_count = 10

  util.verify_techs(force)

  global.cars = global.cars or {}
  game.players[1].clear_recipe_notifications()
end

local player_item_list =
{
  ["assembling-machine-1"] = 10,
  ["coal"] = 40,
  ["copper-plate"] = 100,
  ["electric-mining-drill"] = 5,
  ["electronic-circuit"] = 100,
  ["submachine-gun"] = 1,
  ["firearm-magazine"] = 40,
  ["shotgun"] = 1,
  ["shotgun-shell"] = 20,
  ["heavy-armor"] = 1,
  ["inserter"] = 20,
  ["iron-gear-wheel"] = 50,
  ["iron-plate"] = 200,
  ["lab"] = 5,
  ["long-handed-inserter"] = 10,
  ["pipe"] = 40,
  ["small-electric-pole"] = 20,
  ["small-lamp"] = 20,
  ["stone"] = 200,
  ["transport-belt"] = 50,
  ["wood"] = 50
}

local car_item_list =
{
  ["iron-plate"] = 400,
  ["copper-plate"] = 200,
  ["gun-turret"] = 20,
  ["firearm-magazine"] = 200,
  ["steel-plate"] = 50,
  ["coal"] = 100
}

local train_stop_locale =
{
  dropoff_stop = {"iron-processing-stop"},
  mine_stop = {"iron-mine-stop"}
}

local translate_stops = function(player)
  player.request_translation(train_stop_locale.dropoff_stop)
  player.request_translation(train_stop_locale.mine_stop)
end

local on_player_created = function(event)
  local player = game.get_player(event.player_index)
  util.insert_safe(player.character, player_item_list)
  if event.player_index == 1 then
    translate_stops(player)
  end
end

local on_string_translated = function(event)
  if not event.translated then return end
  if event.localised_string[1] == train_stop_locale.dropoff_stop[1] then
    local train_stop = game.surfaces[1].find_entity("train-stop", {-29, -7})
    if train_stop then
      train_stop.backer_name = event.result
    end
    return
  end
  if event.localised_string[1] == train_stop_locale.mine_stop[1] then
    local train_stop = game.surfaces[1].find_entity("train-stop", {-423, -145})
    if train_stop then
      train_stop.backer_name = event.result
    end
    return
  end
end

local get_angle = function(position_1, position_2)
  local d_x = (position_2[1] or position_2.x) - (position_1[1] or position_1.x)
  local d_y = (position_2[2] or position_2.y) - (position_1[2] or position_1.y)
  return math.atan2(d_y, d_x)
end

local get_orientation = function(source_position, target_position)

  -- Angle in rads
  local angle = get_angle(target_position, source_position)

  -- Convert to orientation
  local orientation =  (angle / (2 * math.pi)) - 0.25
  if orientation < 0 then orientation = orientation + 1 end
  if orientation > 1 then orientation = orientation - 1 end

  return orientation

end

--local car_waypoints =
--{
--  {50, 100},
--  {45, 55},
--  {15, 30},
--  {-35, 20},
--  {-30, 5},
--}

local car_waypoints =
{
  {-200, 20},
  {-165, -6},
  {-122, -5},
  {-95, -5},
  {-88, -0},
  {-82, 0},
  {-50, 3},
  {-25, 5}
}

local default_delay = 5
local think_with_delay = function(string, seconds)
  return
  {
    condition = story_elapsed_check(seconds or default_delay),
    action = function()
      game.print(util.think_string(string))
    end
  }
end

local update_car_driving = function()

  local car = global.car
  local waypoint = global.car_waypoints[1]

  if not waypoint then
    car.speed = car.speed * 0.95
    car.riding_state =
    {
      acceleration = defines.riding.acceleration.braking,
      direction = defines.riding.direction.straight
    }
    return
  end

  local distance = util.distance(car.position, {x = waypoint[1], y = waypoint[2]})
  if distance < 2 then
    table.remove(global.car_waypoints, 1)
    return
  end

  local direction

  local target_orientation = get_orientation(car.position, waypoint)
  if math.abs(car.orientation - target_orientation) <= 1/128 then
    car.orientation = target_orientation
    direction = defines.riding.direction.straight
  else

    local change = car.orientation - target_orientation
    if change > 0.5 then
      change = -1 + change
    end
    if change < -0.5 then
      change = 1 + change
    end

    if change > 0 then
      --direction = defines.riding.direction.left
      car.orientation = car.orientation - (0.08 * change)
    else
      car.orientation = car.orientation - (0.08 * change)
      --direction = defines.riding.direction.right
    end

  end

  car.riding_state =
  {
    acceleration = defines.riding.acceleration.accelerating,
    direction = defines.riding.direction.straight
  }
end

story_table =
{
  {
    {
      init = function()
        init()
      end
    },
    {
      init = function()
        global.car_waypoints = car_waypoints
        for k, waypoint in pairs (car_waypoints) do
          -- Debug visualisation to see car waypoints.
          --game.surfaces[1].set_tiles{{name = "concrete", position = waypoint}}
        end
        for k, player in pairs (game.players) do
          if player.character then player.character.destroy() end
        end
        local start_position = global.car_waypoints[1]
        table.remove(global.car_waypoints, 1)
        local driving_car = game.surfaces[1].create_entity{name = "car", position = start_position, force = "player"}
        global.cars =
        {
          [driving_car.unit_number] = driving_car
        }
        local dude = game.surfaces[1].create_entity{name = "character", force = "player", position = start_position}
        driving_car.set_passenger(dude)
        global.dude = dude
        global.car = driving_car
        global.car.orientation = get_orientation(global.car.position, global.car_waypoints[1])
        global.car.speed = 0.3
        util.insert_safe(global.car, car_item_list)
        global.car.color = { r = 0.869, g = 0.5  , b = 0.130, a = 0.5 }
        for k, player in pairs (game.players) do
          player.set_controller
          {
            type = defines.controllers.cutscene,
            waypoints =
            {
              {
                target = global.car,
                transition_time = 0,
                time_to_wait = 1000,
                zoom = 1.5
              }
            },
            start_position = global.car.position,
            start_zoom = 1.5
          }
        end
      end,
      update = update_car_driving,
      condition = function(event)
        if event.tick % 60 ~= 0 then return end
        return global.car.speed == 0
      end
    },
    {
      init = function()
        chart_areas()
        if global.dude and global.dude.valid then
          global.dude.destroy()
        end
        for k, player in pairs (game.players) do
          if player.controller_type == defines.controllers.cutscene then
            player.exit_cutscene()
          end
          if player.character then player.character.destroy() end
          player.teleport({x = global.car.position.x + 1, y = global.car.position.y - 1})
          player.create_character()
          util.insert_safe(player, player_item_list)
          player.zoom = 0.75
          player.set_controller
          {
            type = defines.controllers.cutscene,
            waypoints =
            {
              {
                target = player.character,
                transition_time = 150,
                time_to_wait = 30,
                zoom = 0.75
              }
            },
            start_position = global.car.position,
            start_zoom = 1.5
          }
        end
      end,
      condition = story_elapsed_check(3)
    },
    think_with_delay({"think-arrived-1"}, 2),
    think_with_delay({"think-arrived-2"}, 6),
    think_with_delay({"think-arrived-3"}, 7),
    think_with_delay({"think-arrived-4"}, 8),
    think_with_delay({"think-arrived-5"}, 7),
    {
      condition = story_elapsed_check(2)
    },
    {
      init = function()
        set_goal({"goal-repair-base-and-research-railway"})
      end,
      condition = function(event)
        if event.tick % 60 ~= 0 then return end
        local force = game.forces.player
        return force.technologies["automated-rail-transportation"].researched == true
      end,
      action = function()
        set_goal("")
      end
    },
    think_with_delay({"think-recover-railway"}),
    {
      condition = story_elapsed_check(6)
    },
    {
      init = function(event)
        set_goal({"goal-set-up-train"})
      end,
      condition = function(event)
        if event.name ~= defines.events.on_train_changed_state then return end
        local train = event.train
        if not (train and train.valid) then return end
        if train.manual_mode then return end
        if train.state ~= defines.train_state.wait_station then return end
        if #train.cargo_wagons == 0 then return end
        local schedule = train.schedule
        if not schedule then return end
        return #schedule.records >= 2
      end,
      action = function(event)
        set_goal()
      end
    },
    think_with_delay({"think-gather-supplies-1"}, 5),
    think_with_delay({"think-gather-supplies-2"}, 6),
    think_with_delay({"think-gather-supplies-3"}, 7),
    think_with_delay({"think-gather-supplies-4"}, 7),
    {
      condition = story_elapsed_check(5)
    },
    {
      init = function(event)
        set_goal({"goal-get-supplies"})
        set_info({custom_function = update_materials_gui})
      end,
      condition = function(event)
        if event.tick % 60 ~= 0 then return end
        set_info({custom_function = update_materials_gui})
        return car_content_check()
      end,
      action = function()
        set_goal("")
        set_info()
      end
    },
    think_with_delay({"think-ready-to-go"}, 5),
    {
      condition = story_elapsed_check(5)
    },
    {
      init = function()
        if not game.is_multiplayer() then
          game.tick_paused = true
        end
        for k, player in pairs (game.connected_players) do
          final_screen.create(player)
        end
        game.play_sound({path = "utility/game_won"})
      end,
      condition = function()
        return false
      end
    }
  }
}

skippity = function()
  for k, player in pairs (game.connected_players) do
    final_screen.create(player)
  end
end

story_init_helpers(story_table)

local story_events =
{
  defines.events.on_tick,
  defines.events.on_train_changed_state
}

local build_events =
{
  defines.events.on_built_entity,
  defines.events.on_robot_built_entity,
  defines.events.script_raised_built,
  defines.events.script_raised_revive
}

local on_built_entity = function(event)

  local entity = event.entity or event.created_entity
  if not (entity and entity.valid) then return end

  if entity.name == "car" then
    global.cars[entity.unit_number] = entity
  end

end

local on_player_driving_changed_state = function(event)
  -- If they find the car in the ruin, only consider it for the objectives if they get in.
  local car = event.entity
  if not (car and car.valid) then return end

  if car.name ~= "car" then return end

  global.cars[car.unit_number] = car

end

script.on_event(story_events, function(event)
  story_update(global.story, event)
end)

script.on_event(defines.events.on_player_created, on_player_created)

script.on_event(build_events, on_built_entity)
script.on_event(defines.events.on_player_driving_changed_state, on_player_driving_changed_state)
script.on_event(defines.events.on_gui_click, final_screen.on_gui_click)
script.on_event(defines.events.on_gui_closed, final_screen.on_gui_closed)
script.on_event(defines.events.on_string_translated, on_string_translated)