File size: 22,378 Bytes
898c672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
require "story"
require "advanced-signals"

function on_player_created(event)
  local player = game.players[event.player_index]
  player.game_view_settings =
  {
    show_side_menu = false,
    show_research_info = false,
    show_alert_gui = false,
    show_minimap = false
  }
  game.permissions.get_group(0).set_allows_action(defines.input_action.remove_cables, false)
  game.permissions.get_group(0).set_allows_action(defines.input_action.open_production_gui, false)
  game.permissions.get_group(0).set_allows_action(defines.input_action.open_tips_and_tricks_gui, false)
  game.permissions.get_group(0).set_allows_action(defines.input_action.open_logistic_gui, false)
  game.permissions.get_group(0).set_allows_action(defines.input_action.open_technology_gui, false)
  player.force.disable_all_prototypes()
end

local clear_vis = function()
  --clear all render render_ids
  for _, id in pairs(global.render_ids) do
    rendering.destroy(id)
  end
end

local add_train_label = function(backer_tag,label_text,color)
  local loco = nil
  for _, train in pairs(surface().find_entities_filtered({name='locomotive'})) do
    if train.backer_name == backer_tag then
      loco = train
    end
  end
  if loco then
    loco.color = {r=1,g=0.6,b=0,a=0.5}
    game.print('adding label')
    local label_id = rendering.draw_text({
      surface = loco.surface,
      target = loco,
      color = {1,1,1},
      text = "red train",
      offset = {0,-2}
    })
    table.insert(global.render_ids,label_id)
  end
end

local add_train_labels = function(trains)
  for _, train in pairs(trains) do
    local ent = surface().find_entities_filtered({name='locomotive',position=train.position})
    if ent then
      game.print('adding label')
      local label_id = rendering.draw_text({
        surface = surface(),
        target = ent,
        color = {1,1,1},
        text = "red train",
        offset = {0,-2}
      })
      table.insert(global.render_ids,label_id)
    end
  end
end

local add_labels = function(label_data)
  for _, data in pairs(label_data) do
    local matching_ents = surface().find_entities_filtered({name=data.entity,position=data.position})
    if #matching_ents>0 then
      for _, ent in pairs(matching_ents) do
        local label_id = rendering.draw_text({
          surface = surface(),
          target = ent,
          color = {1,1,1},
          text = {"label."..data.locale},
          target_offset = {0,1},
          alignment = 'center'
        })
        table.insert(global.render_ids,label_id)
      end
    end
  end
end

story_table =
{
  {
    {
      init = function()
        global.render_ids = {}
        for k, entity in pairs (surface().find_entities()) do
          if entity.name == "locomotive" then
            entity.insert"coal"
          end
          entity.minable = false
          entity.operable = false
          entity.rotatable = false
        end
        player().character.destroy()
        player().set_quick_bar_slot(1,'rail-chain-signal')
        player().set_quick_bar_slot(2,'rail-signal')
        player().game_view_settings = {show_rail_block_visualisation = true}
      end
    },
    {
      init = function()
        clear_vis()
        set_goal("", false)
        set_info({text = {"chain-green"}})
        set_info({custom_function = add_run_trains_button, append = true})
        find_gui_recursive(player().gui, "reset_all").destroy()
        set_continue_button_style(function (button)
          if button.valid then
            button.enabled = true
          end
        end)
        clear_surface()
        global.this_puzzle = setup.chain_green.entities
        global.this_puzzle_param = nil
        global.this_puzzle_labels = {}
        global.this_puzzle_trains = {}
        for k, entity in pairs (global.this_puzzle) do
          entity.minable = false
          entity.operable = false
          if entity.name == "locomotive" then
            if entity.schedule then
              entity.manual_mode = true
            end
            table.insert(global.this_puzzle_trains, entity)
          end
          if entity.label then
            print("found ent with label "..entity.name)
             table.insert(global.this_puzzle_labels,{
               name = entity.name,
               position = entity.position,
               locale = entity.label
              })
          end
        end
        recreate_entities(global.this_puzzle)
        loop_trains(0)
        add_labels(global.this_puzzle_labels)
      end,
      condition = function()
        return global.continue
      end
    },
    {
      init = function()
        set_goal("", false)
        set_info({text = {"chain-blue-go"}})
        set_info({custom_function = add_run_trains_button, append = true})
        find_gui_recursive(player().gui, "reset_all").destroy()
        set_continue_button_style(function (button)
          if button.valid then
            button.enabled = true
          end
        end)
        clear_surface()
        global.this_puzzle = setup.chain_blue_go.entities
        global.this_puzzle_param = nil
        global.this_puzzle_trains = {}
        for k, entity in pairs (global.this_puzzle) do
          entity.minable = false
          entity.operable = false
          if entity.name == "locomotive" then
            if entity.schedule then
              entity.manual_mode = true
            end
            table.insert(global.this_puzzle_trains, entity)
          end
        end
        recreate_entities(global.this_puzzle)
        loop_trains(0)
      end,
      condition = function()
        return global.continue
      end
    },
    {
      init = function()
        set_goal("", false)
        set_info({text = {"chain-blue-stop"}})
        set_info({custom_function = add_run_trains_button, append = true})
        find_gui_recursive(player().gui, "reset_all").destroy()
        set_continue_button_style(function (button)
          if button.valid then
            button.enabled = true
          end
        end)
        clear_surface()
        global.this_puzzle = setup.chain_blue_stop.entities
        global.this_puzzle_param = nil
        global.this_puzzle_trains = {}
        for k, entity in pairs (global.this_puzzle) do
          entity.minable = false
          entity.operable = false
          if entity.name == "locomotive" then
            if entity.schedule then
              entity.manual_mode = true
            end
            table.insert(global.this_puzzle_trains, entity)
          end
        end
        recreate_entities(global.this_puzzle)
        loop_trains(0)
      end,
      condition = function()
        return global.continue
      end
    },
    {
      init = function()
        set_goal("", false)
        set_info({text = {"chain-red"}})
        set_info({custom_function = add_run_trains_button, append = true})
        find_gui_recursive(player().gui, "reset_all").destroy()
        set_continue_button_style(function (button)
          if button.valid then
            button.enabled = true
          end
        end)
        clear_surface()
        global.this_puzzle = setup.chain_red.entities
        global.this_puzzle_param = nil
        global.this_puzzle_trains = {}
        for k, entity in pairs (global.this_puzzle) do
          entity.minable = false
          entity.operable = false
          if entity.name == "locomotive" then
            if entity.schedule then
              entity.manual_mode = true
            end
            table.insert(global.this_puzzle_trains, entity)
          end
        end
        recreate_entities(global.this_puzzle)
        loop_trains(0)
      end,
      condition = function()
        return global.continue
      end
    },
    {
      init = function()
        clear_surface()
        set_goal("", false)
        set_info({text = {"deadlock-1"}})
        set_info({text = {"deadlock-2"}, append = true})
        set_info({custom_function = add_button, append = true})
        global.this_puzzle_trains = {}
        for k, entity in pairs (setup.deadlock_1.entities) do
          if entity.name == "locomotive" or entity.name == "fluid-wagon" then
            table.insert(global.this_puzzle_trains, entity)
          elseif entity.name == "rail-signal" then
            entity.minable = true
          end
        end
        global.this_puzzle = setup.deadlock_1.entities
        global.this_puzzle_param = setup.deadlock_1.param
        recreate_entities(global.this_puzzle, global.this_puzzle_param)
        loop_trains(9*60)
      end,
      condition = function()
        return global.continue
      end
    },
    {
      init = function()
        clear_surface()
        global.this_puzzle = setup.deadlock_1.entities
        global.this_puzzle_param = setup.deadlock_1.param
        global.this_puzzle_trains = {}
        global.this_puzzle_labels = {}
        clear_vis()
        for k, entity in pairs (global.this_puzzle) do
          if entity.name == "locomotive" or entity.name == "fluid-wagon" then
            table.insert(global.this_puzzle_trains, entity)
          elseif entity.name == "rail-signal" then
            entity.minable = true
          end
        end
        for k, entity in pairs (recreate_entities(global.this_puzzle, global.this_puzzle_param)) do
          if entity.name == "rail-signal" and
            (
              (
                entity.position.x == -4.5 and
                entity.position.y == 2.5
              )
              or
              (
                entity.position.x == -2.5 and
                entity.position.y == -2.5
              )
            )
          then
            entity.minable = true
            local X = entity.position.x
            local Y = entity.position.y
            local D = entity.direction
            entity.destroy()
            surface().create_entity{name = "rail-chain-signal", position = {X,Y}, direction = D}
            table.insert(global.this_puzzle_labels,{
              name = 'rail-chain-signal',
              position = {X,Y},
              locale = 'rail-chain-signal'
            })
          end
        end
        add_labels(global.this_puzzle_labels)
        set_goal()
        set_info({text = {"chain-signal-1"}})
        set_info({text = {"chain-signal-2"}, append = true})
        set_info({custom_function = add_button, append = true})
        loop_trains(8*60)
      end,
      condition = function()
        return global.continue
      end
    },
    {
      init = function()
        clear_surface()
        global.required_chain_signals = 4
        global.required_rail_signals = 0
        set_goal({"fix-intersection"})
        set_info({custom_function = add_run_trains_button})
        player().game_view_settings = {show_rail_block_visualisation = false}
        global.this_puzzle_trains = {}
        for k, entity in pairs (setup.deadlock_2.entities) do
          if entity.name == "locomotive" or entity.name == "fluid-wagon" then
            entity.manual_mode = true
            table.insert(global.this_puzzle_trains, entity)
          elseif entity.name == "rail-signal" then
            entity.minable = true
          end
        end
        global.this_puzzle = setup.deadlock_2.entities
        global.this_puzzle_param = setup.deadlock_2.param
        recreate_entities(global.this_puzzle, global.this_puzzle_param)
      end,
      condition = function()
        return puzzle_condition_red()
      end
    },
    {
      init = function()
        clear_surface()
        global.required_chain_signals = 8
        set_goal({"fix-intersection"})
        set_info({custom_function = add_run_trains_button})
        global.this_puzzle_trains = {}
        for k, entity in pairs (setup.intersection_1.entities) do
          if entity.name == "locomotive" or entity.name == "fluid-wagon" then
            entity.manual_mode = true
            table.insert(global.this_puzzle_trains, entity)
          elseif entity.name == "rail-signal" then
            entity.minable = true
          end
        end
        global.this_puzzle = setup.intersection_1.entities
        global.this_puzzle_param = setup.intersection_1.param
        recreate_entities(global.this_puzzle, global.this_puzzle_param)
      end,
      condition = function()
        return puzzle_condition_red()
      end
    },
    {
      init = function()
        clear_surface()
        global.required_chain_signals = 4
        set_goal({"fix-intersection"})
        set_info({custom_function = add_run_trains_button})
        global.this_puzzle_trains = {}
        for k, entity in pairs (setup.intersection_2.entities) do
          if entity.name == "locomotive" or entity.name == "fluid-wagon" then
            entity.manual_mode = true
            table.insert(global.this_puzzle_trains, entity)
          elseif entity.name == "rail-signal" then
            entity.minable = true
          end
        end
        global.this_puzzle = setup.intersection_2.entities
        global.this_puzzle_param = setup.intersection_2.param
        recreate_entities(global.this_puzzle, global.this_puzzle_param)
      end,
      condition = function()
        return puzzle_condition_red()
      end
    },
    {
      init = function()
        clear_surface()
        global.required_chain_signals = 12
        global.required_rail_signals = 12
        set_goal({"fix-intersection-2"})
        set_info({custom_function = add_run_trains_button})
        global.this_puzzle_trains = {}
        global.this_puzzle = setup.intersection_3.entities
        global.this_puzzle_param = setup.intersection_3.param
        for k, entity in pairs (global.this_puzzle) do
          if entity.name == "locomotive" or entity.name == "fluid-wagon" then
            entity.manual_mode = true
            table.insert(global.this_puzzle_trains, entity)
          elseif entity.name == "rail-signal" then
            entity.minable = true
          end
        end
        recreate_entities(global.this_puzzle, global.this_puzzle_param)
      end,
      condition = function()
        return puzzle_condition_all()
      end
    },
    {
      init = function()
        player().set_controller{type = defines.controllers.god}
        for k, entity in pairs (surface().find_entities()) do
          entity.minable = true
          entity.operable = true
          entity.rotatable = true
        end
        player().insert({name='rail',count=1000})
        player().insert({name='rail-signal',count=30})
        player().insert({name='rail-chain-signal',count=30})
        player().insert({name='train-stop',count=10})
        player().insert({name='locomotive',count=3})
        player().insert({name='cargo-wagon',count=3})
        player().insert({name='coal',count=100})
        player().set_quick_bar_slot(2,'rail-signal')
        player().set_quick_bar_slot(3,'rail')
        player().set_quick_bar_slot(4,'train-stop')
        player().set_quick_bar_slot(5,'locomotive')
        player().set_quick_bar_slot(6,'cargo-wagon')
        set_info{text = {"finish-info"}}
        set_info{custom_function = function(flow) add_button(flow).caption = {"finish"} end, append = true}
        set_goal(nil, false)
      end,
      condition = function()
        return global.continue
      end
    }
  }
}

story_init_helpers(story_table)

script.on_init(function()
  surface().always_day = true
  game.forces.player.manual_mining_speed_modifier = 4
  game.forces.player.disable_all_prototypes()
  global.story = story_init()
end)

script.on_event(defines.events.on_tick, function(event)
  story_update(global.story, event, "")
  limit_camera({0,0}, 20)
  loop_trains()
end)

script.on_event(defines.events.on_gui_click, function (event)
  story_update(global.story, event, "")
end)

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

story_gui_click = function(event)
  local element = event.element
  if not element.valid then return end
  local player = game.players[event.player_index]
  local name = element.name

  if name == "start_trains" then
    if not element.enabled then return end
    for k, train in pairs (surface().find_entities_filtered{name = "locomotive"}) do
      if train.train.schedule then
        train.train.manual_mode = false
      end
    end
    element.enabled = false
    global.save_inventory = game.create_inventory(100)
    player.clear_cursor()
    for name, count in pairs (player.get_main_inventory().get_contents()) do
      global.save_inventory.insert({name = name, count = count})
    end
    player.clear_items_inside()

    player.set_controller{type = defines.controllers.ghost}
    return
  end

  if name == "reset_trains" then
    for k, train in pairs (surface().find_entities_filtered{name = "locomotive"}) do
      train.destroy()
    end
    for k, train in pairs (surface().find_entities_filtered{name = "fluid-wagon"}) do
      train.destroy()
    end
    recreate_entities(global.this_puzzle_trains, global.this_puzzle_param)
    for k, child in pairs (element.parent.children) do
      if child.name ~= "story_continue_button" then
        child.enabled = true
      end
    end
    if player.controller_type ~= defines.controllers.god then
      player.set_controller{type = defines.controllers.god}
      if global.save_inventory then
        for name, count in pairs (global.save_inventory.get_contents()) do
          player.insert({name = name, count = count})
        end
        global.save_inventory.destroy()
        global.save_inventory = nil
      end
    end
    return
  end

  if name == "reset_all" then
    clear_surface()
    recreate_entities(global.this_puzzle, global.this_puzzle_param)
    for k, child in pairs (element.parent.children) do
      if child.name ~= "story_continue_button" then
        child.enabled = true
      end
    end

    if player.controller_type ~= defines.controllers.god then
      player.set_controller{type = defines.controllers.god}
    end

    player.clear_items_inside()
    if global.required_chain_signals > 0 then player.insert{name = "rail-chain-signal", count = global.required_chain_signals} end
    if global.required_rail_signals > 0 then player.insert{name = "rail-signal", count = global.required_rail_signals} end
    return
  end

end

function clear_surface()
  local entities = surface().find_entities()
  for k, entity in pairs (entities) do
    if entity.valid and entity.name ~= "character" then
      entity.destroy()
    end
  end
  for k, entity in pairs (surface().find_entities()) do
    if entity.valid and entity.name ~= "character" then
      entity.destroy()
    end
  end
end

function add_run_trains_button(gui)
  gui.add{type = "line", direction = "horizontal"}
  local flow = gui.add{type = "table", column_count = 2}
  flow.style.horizontal_spacing = 2
  flow.style.vertical_spacing = 2
  flow.style.horizontally_stretchable = true
  local button = flow.add{type = "button", name = "start_trains", caption = {"start-trains"}}
  button.style.horizontally_stretchable = true
  local button = flow.add{type = "button", name = "reset_trains", caption = {"reset-trains"}}
  button.style.horizontally_stretchable = true
  local button = flow.add{type = "button", name = "reset_all", caption = {"reset-all"}}
  button.style.horizontally_stretchable = true
  local button = add_button(flow)
  button.style.horizontally_stretchable = true
  set_continue_button_style(function (button)
    if button.valid then
      button.enabled = false
    end
  end)
  local player = player()
  player.set_controller{type = defines.controllers.god}
  player.remove_item"rail-chain-signal"
  if global.required_chain_signals and global.required_chain_signals > 0 then
    player.insert{name = "rail-chain-signal", count = global.required_chain_signals}
  end
  if global.required_rail_signals and global.required_rail_signals > 0 then
    player.insert{name = "rail-signal", count = global.required_rail_signals}
  end
  global.intermission = 0
  global.loop_interval = 0
  global.loop_tick = nil
end

function puzzle_condition_red()
  if global.continue then return true end
  for k, train in pairs (surface().find_entities_filtered{name = "locomotive"}) do
    if train.train.speed ~= 0 then
      return false
    end
    if train.color == nil then
      if train.train.state ~= defines.train_state.wait_station then
        return false
      end
      if train.health ~= 1000 then
        return false
      end
    end
    if train.train.state == defines.train_state.no_path then
      return false
    end
  end
  for k, wagon in pairs (surface().find_entities_filtered{name = "fluid-wagon"}) do
    if wagon.health ~= 600 then
      return false
    end
  end
  global.intermission = global.intermission + 1
  if global.intermission == 90 then
    flash_goal()
    set_continue_button_style(function (button)
      if button.valid then
        button.enabled = true
      end
    end)
  end
end

function puzzle_condition_all()
  if global.continue then return true end
  for k, train in pairs (surface().find_entities_filtered{name = "locomotive"}) do
    if train.train.speed ~= 0 then
      return false
    end

    if train.train.state ~= defines.train_state.wait_station then
      return false
    end
    if train.health ~= 1000 then
      return false
    end

    if train.train.state == defines.train_state.no_path then
      return false
    end
  end
  for k, wagon in pairs (surface().find_entities_filtered{name = "fluid-wagon"}) do
    if wagon.health ~= 600 then
      return false
    end
  end
  global.intermission = global.intermission + 1
  if global.intermission == 90 then
    flash_goal()
    set_continue_button_style(function (button)
      if button.valid then
        button.enabled = true
      end
    end)
  end
end

function loop_trains(interval)
  if interval then
    global.loop_interval = interval
    global.loop_tick = game.tick + global.loop_interval
    return
  end
  if not global.loop_tick then return end
  if game.tick ~= global.loop_tick then return end
  for k, train in pairs (surface().find_entities_filtered{name = "locomotive"}) do
    train.destroy()
  end
  for k, train in pairs (surface().find_entities_filtered{name = "fluid-wagon"}) do
    train.destroy()
  end
  recreate_entities(global.this_puzzle_trains, global.this_puzzle_param)
  global.loop_tick = game.tick + global.loop_interval
end