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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
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
require("story")
require('util')

local think = function(thought)
  game.players[1].print({"","[img=entity/character][color=orange]",{"engineer-title"},": [/color]",{"think-"..thought}})
end

local set_arrow = function(arrow_settings)
  if global.arrow then
    global.arrow.destroy()
  end
  if arrow_settings == nil then
    global.arrow = nil
  else
    global.arrow = game.players[1].surface.create_entity(arrow_settings)
  end
end

local on_player_created = function(event)
  local player = game.players[event.player_index]
  player.disable_recipe_groups()
  player.disable_recipe_subgroups()
  player.minimap_enabled = false
  player.force.disable_all_prototypes()
  player.force.disable_research()
  player.surface.always_day = true -- Don't bother the player with night in this early mission
  player.game_view_settings =
  {
    show_hotkey_suggestions = false
  }
  local recipe_list = player.force.recipes
  recipe_list["iron-plate"].enabled = true
  recipe_list["copper-plate"].enabled = true
  recipe_list["stone-furnace"].enabled = true
  recipe_list["wooden-chest"].enabled = true
  recipe_list["iron-gear-wheel"].enabled = true
  recipe_list["burner-mining-drill"].enabled = true
  recipe_list["transport-belt"].enabled = true
  recipe_list["burner-inserter"].enabled = true
  game.players[1].clear_recipe_notifications()
  if player.character then
    player.character.insert{name = "coal", count = 20}
  end

end

local init = function()

  global.story = story_init()
  game.map_settings.pollution.enabled = false

  global.inserter_chest = game.get_entity_by_tag("inserter-chest")  or error("Inserter chest missing")
  global.inserter_chest_position = global.inserter_chest.position
  global.inserter_chest_position.energy = 0

  global.inserter_furnace = game.get_entity_by_tag("inserter-furnace") or error("Inserter furnace missing")
  global.inserter_furnace_position = global.inserter_furnace.position
end

function check_for_player_death(event)
  if event.name == defines.events.on_player_died then
    game.set_game_state({game_finished=true, player_won=false, can_continue=false})
  end
end

local story_table =
{
  ["update-functions"]=
  {
   ["check-inserter-1"]=
   function(event, story)
     if not global.inserter_chest.valid then
       story_jump_to(story, "build-the-accidentally-mined-inserter")
     end
   end,
   ["check-inserter-2"]=
   function(event, story)
     if not global.inserter_furnace.valid then
       story_jump_to(story, "build-the-accidentally-mined-inserter-2")
     end
   end
  },
  {
    {
      init = function(event, story)
        story_add_update(story, "check-inserter-1")
      end,
      condition = story_elapsed_check(3),
      action = function()
        think("found-mining-site")
      end
    },
    {
      condition = story_elapsed_check(4),
      action = function()
        think("robotic-arm")
      end
    },
    {
      condition = story_elapsed_check(4.6),
      action =
      function(event,story)
        if global.inserter_chest.energy > 0 then
          story_jump_to(story,"inserter-explaination")
        else
          story_show_message_dialog{text={"msg-inserter-no-power"},
                                    point_to={type="entity", entity=global.inserter_chest}}
        end
      end
    },
    {
      condition = story_elapsed_check(1),
      action =
      function()
        set_goal({"goal-open-machine-gui"})
        game.players[1].set_gui_arrow({type="entity", entity=global.inserter_chest})
      end
    },
    {
      name = "wait-to-open-inserter-1",
      condition = function() return global.inserter_chest == game.players[1].opened end,
      action =
      function()
        set_goal({"goal-insert-fuel-into-inserter-1"})
        game.players[1].clear_gui_arrow()
      end
    },
    {
      condition = function() return global.inserter_chest.energy > 0 end,
      action = function()
        set_goal({"goal-close-inserter-gui"})
      end
    },
    {
      name = 'inserter-explaination',
      condition = function() return game.players[1].opened == nil end,
      action =
      function()
        set_goal("")
        local shown_text;
        if game.players[1].input_method == defines.input_method.game_controller then
          shown_text = "msg-inserter-introduction-1-controller"
        else
          shown_text = "msg-inserter-introduction-1"
        end
        story_show_message_dialog{text={shown_text},
                                 image = "inserter-explanation.png",
                                 point_to={type="entity", entity=global.inserter_chest}}
      end
    },
    {
      action =
      function(event, story)
          story_show_message_dialog{text={"msg-inserter-introduction-2"},
                                 image = "inserter-usage-explanation.png",
                                 point_to={type="entity", entity=global.inserter_chest}}
        story_remove_update(story, "check-inserter-1")
        game.players[1].game_view_settings.update_entity_selection = true
      end
    },
    {
      condition = story_elapsed_check(5),
      action = function()
        local pistol_chest = game.get_entity_by_tag('pistol-chest')
        if pistol_chest and pistol_chest.valid then
          set_goal({"goal-inspect-chest"})
          global.arrow = game.players[1].surface.create_entity{name="orange-arrow-with-circle", position = pistol_chest.position}
        end
      end
    },
    {

      condition = function()
        local pistol_chest = game.get_entity_by_tag('pistol-chest')
        return pistol_chest == nil or (pistol_chest.get_inventory(defines.inventory.chest).is_empty() and
        game.players[1].cursor_stack.valid_for_read == false)
      end,
      action = function()
        if global.arrow then global.arrow.destroy() end
        set_goal({"goal-close-inventory"})
      end
    },
    {
      condition = function()
        return game.players[1].opened == nil
      end,
      action = function()
        think("chest-content-useful")
        set_goal("")
      end
    },
    {
      condition = story_elapsed_check(2),
      action = function(event, story)
        if not (game.players[1].character.get_inventory(defines.inventory.character_guns)[1].valid_for_read and
          game.players[1].character.get_inventory(defines.inventory.character_guns)[1].name == "pistol") then
          local shown_text;
          if game.players[1].input_method == defines.input_method.game_controller then
            shown_text = "msg-gun-equipment-controller"
          else
            shown_text = "msg-gun-equipment"
          end
          story_show_message_dialog
          {
            text = {shown_text},
            point_to =
            {
              type = "item_stack",
              inventory_index = defines.inventory.character_guns,
              item_stack_index = 1,
              source = "player-equipment-bar"
            }
          }
        end
        if game.players[1].input_method ~= defines.input_method.game_controller then
          story_jump_to(story, "equip-start")
        end
      end
    },    
    {
      init = function()
        game.players[1].set_goal_description({"goal-controller-open-character-gui"})
      end,
      condition = function (event)
        return game.players[1].opened_gui_type == defines.gui_type.controller
      end
    },
    {
      name = 'wait-for-window-to-open',
      condition = story_elapsed_check(0.0000001),
    },
    {
      init = function()
        game.show_message_dialog({text={"msg-controller-character-tab"}, point_to= {type="active_window"}})
      end,
      condition = story_elapsed_check(0.0000001),
    },
    {
      name = 'equip-start',
      condition = story_elapsed_check(0.5),
      action = function()
        if not (game.players[1].character.get_inventory(defines.inventory.character_guns)[1].valid_for_read and
          game.players[1].character.get_inventory(defines.inventory.character_guns)[1].name == "pistol") then
          set_goal({"goal-equip-gun"})
          if game.players[1].input_method ~= defines.input_method.game_controller then
            game.players[1].set_gui_arrow{type = "item_stack", inventory_index = defines.inventory.character_guns, item_stack_index = 1, source="player-equipment-bar"}
          else
            game.players[1].character.clear_gui_arrow()
          end
        end
      end
    },
    {
      condition = function() return game.players[1].character.get_inventory(defines.inventory.character_guns)[1].valid_for_read and
          game.players[1].character.get_inventory(defines.inventory.character_guns)[1].name == "pistol" end,
      action = function()
        if game.players[1].character.get_inventory(defines.inventory.character_ammo).get_item_count("firearm-magazine") == 0 then
          set_goal({"goal-equip-ammo"})
          if game.players[1].input_method ~= defines.input_method.game_controller then
            game.players[1].set_gui_arrow{type = "item_stack", inventory_index = defines.inventory.character_ammo, item_stack_index = 1, source="player-equipment-bar"}
          else
            game.players[1].character.clear_gui_arrow()
          end
        end
      end
    },
    {
      condition = function() return game.players[1].character.get_inventory(defines.inventory.character_ammo)[1].valid_for_read and
          game.players[1].character.get_inventory(defines.inventory.character_ammo)[1].name == "firearm-magazine" end,
      action = function()
        set_goal("")
        game.players[1].character.clear_gui_arrow()
      end
    },
    {
      init = function()
        set_goal({"goal-close-inventory"})
      end,
      condition = function()
        return game.players[1].opened == nil and game.players[1].opened_self == false
      end,
      action = function()
        set_goal("")
        if game.players[1].surface.count_entities_filtered({name='small-biter'}) < 2 then
          local pos = {
            x=0,
            y=game.players[1].surface.map_gen_settings.height/-2
          }
          for k=1,4 do
            game.players[1].surface.create_entity({
              name='small-biter',
              position=game.players[1].surface.find_non_colliding_position('small-biter',pos,5,0.1)
            })
          end
        end
      end
    },
    {
      condition = story_elapsed_check(2),
      action =
      function()
        think("creepers-coming")
        game.players[1].character.clear_gui_arrow()
      end
    },
    {
      condition = story_elapsed_check(3),
      action = function()
        local shown_text;
        if game.players[1].input_method == defines.input_method.game_controller then
          shown_text = "msg-shooting-controller"
        else
          shown_text = "msg-shooting"
        end
        story_show_message_dialog
        {
          text = {shown_text},
          point_to = {type = "entity", entity = game.players[1].character}
        }
        global.biters_killed = 0
      end
    },
    {
      condition = story_elapsed_check(1.5),
      action = function()
        set_goal({"kill-creepers"})
      end
    },
    {
      init = function()
        game.surfaces[1].create_entity({name='small-biter',position={x=-51,y=-55}})
        game.surfaces[1].create_entity({name='small-biter',position={x=-55,y=-55}})

        game.players[1].surface.set_multi_command
        {
          command =
          {
            type=defines.command.attack,
            target=game.players[1].character,
            distraction=defines.distraction.none
          },
          unit_count = 2
        }
      end,
      update = function(event)
        if event.name == defines.events.on_entity_died and
           event.entity.name == "small-biter" then
          global.biters_killed = global.biters_killed + 1
        end
      end,
      condition = function() return global.biters_killed >= 2 end,
      action = function()
        set_goal("")
        think("creepers-dead")
      end
    },
    {
      condition = story_elapsed_check(3),
      action = function()
        set_goal("")
        think("prepare")
      end
    },
    {
      init = function(event, story) story_add_update(story, "check-inserter-2") end,
      condition = story_elapsed_check(5),
      action = function(event, story)
        if global.inserter_furnace.direction == defines.direction.east then
          story_jump_to(story,"more-machines")
        else
          story_show_message_dialog
          {
            text = {"msg-inserter-2-reversed"},
            point_to = {type="entity", entity = global.inserter_furnace}
          }
        end
      end
    },
    {
      condition = story_elapsed_check(0.5),
      action = function()
        game.players[1].set_gui_arrow({type="entity", entity=global.inserter_furnace})
        if game.players[1].input_method == defines.input_method.game_controller then
          set_goal({"goal-rotate-inserter-2-controller"})
        else
          set_goal({"goal-rotate-inserter-2"})
        end
      end
    },
    {
      name = "wait-to-rotate-inserter-2",
      condition = function()
          return global.inserter_furnace.drop_target ~= nil and
                 global.inserter_furnace.drop_target.name == "stone-furnace"
        end,
      action = function(event, story)
        set_goal("")
        game.players[1].clear_gui_arrow()
        think("inserter2-working")
        story_remove_update(story, "check-inserter-2")
      end
    },
    {
      condition = story_elapsed_check(3),
      action = function()
        game.players[1].clear_gui_arrow()
        story_show_message_dialog
        {
          text = {"msg-rotations-explained"},
          point_to = {type = "entity", entity = game.players[1].character}
        }
      end
    },
    {
      name = 'more-machines',
      condition = story_elapsed_check(3),
      action = function()
        think("need-more-machines")
      end
    },
    {
      condition = story_elapsed_check(4)
    },
    {
      init = function(event, story)
        global.mined_stone_count = 0
        story_remove_update(story, "check-inserter-1")
        story_remove_update(story, "check-inserter-2")
      end,
      update = (function()
        --jesus...
        --yeah i know what you mean...
        local only_update = false

        return function(event)

          progress = event.name == defines.events.on_built_entity and
                     (event.created_entity.name == "burner-mining-drill" or
                      event.created_entity.name == "stone-furnace")

          if event.name == defines.events.on_player_mined_item and
             event.item_stack.name == "stone" then
             global.mined_stone_count = global.mined_stone_count + 1
          end

          if global.mined_stone_count >= 5 and global.advice_to_mine_stone == nil then
            think("automated-stone-mining")
            global.advice_to_mine_stone = true
          end

          set_goal
          (
            {
              "goal-build-machines",
              game.players[1].force.get_entity_count("burner-mining-drill"),
              10,
              game.players[1].force.get_entity_count("stone-furnace"),
              5
            },
            true
          )

          only_update = true
        end
      end)(),
      condition = function()
        return game.players[1].force.get_entity_count("burner-mining-drill") >= 10 and
               game.players[1].force.get_entity_count("stone-furnace") >= 5
      end,
      action = function()
        think("got-machines")
        set_goal("")
      end
    },
    {
      condition = story_elapsed_check(5),
      action = function()
        think("need-more-resources")
      end
    },
    {
      update = (function()
        local only_update = false
        return function(event)
          local iron_plate_count = game.players[1].character.get_item_count("iron-plate")
          local copperplatecount = game.players[1].character.get_item_count("copper-plate")
          local coal_count = game.players[1].character.get_item_count("coal")
          set_goal
          (
            {
              "goal-get-resources",
              iron_plate_count,
              150,
              copperplatecount,
              50,
              coal_count,
              75
            },
            only_update
          )
          only_update = true
        end
      end)(),
      condition = function()
        return game.players[1].character.get_item_count("iron-plate") >= 150 and
               game.players[1].character.get_item_count("copper-plate") >= 50 and
               game.players[1].character.get_item_count("coal") >= 75
      end,
      action = function()
        think("got-resources-1")
        set_goal("")
      end
    },
    {
      condition = story_elapsed_check(5),
      action = function()
        think("time-to-move-on")
      end
    },
    {
      condition = story_elapsed_check(5),
      action = function()
        game.set_game_state({game_finished=true, player_won=true, can_continue=true,next_level='level-03'})
      end
    }
  },
  {
    {
     name = "build-the-accidentally-mined-inserter",
     init = function(event, story)
       story_remove_update(story, "check-inserter-1")
     end,
     action = function()
       story_show_message_dialog
       {
         text={"msg-mined-inserter-instead-of-open"},
         point_to = {type = "position", position = global.inserter_chest_position}}
       set_arrow({name="orange-arrow-with-circle", position = global.inserter_chest_position})
     end
    },
    {
      action = function()
        set_goal({"goal-build-inserter-back"})
      end
    },
    {
      condition = function(event)
        if event.name == defines.events.on_built_entity and
           event.created_entity.name == "burner-inserter" then

          if not (event.created_entity.position.x == global.inserter_chest_position.x and
            event.created_entity.position.y == global.inserter_chest_position.y) then
            story_show_message_dialog
            {
              text = {"msg-inserter-1-wrong-position"},
              point_to = {type = "entity", entity = event.created_entity}
            }
            return false
          else
            event.created_entity.direction = defines.direction.west
            global.inserter_chest = event.created_entity
            return true
          end
        end
      end,
      action = function(event, story)
        set_arrow()
        story_jump_to(story, "wait-to-open-inserter-1")
        story_add_update(story, "check-inserter-1")
      end
    }
  },
  {
    {
     name = "build-the-accidentally-mined-inserter-2",
     init = function(event, story)
       story_remove_update(story, "check-inserter-2")
     end,
     action = function()
       story_show_message_dialog
       {
         text = {"msg-mined-inserter-instead-of-open-2"},
         point_to = {type="position", position = global.inserter_furnace_position}
       }
       set_arrow({name="orange-arrow-with-circle", position = global.inserter_furnace_position})
     end
    },
    {
      action = function()
        set_goal({"goal-build-inserter-back"})
      end
    },
    {
      condition = function(event)
        if event.name == defines.events.on_built_entity and
           event.created_entity.name == "burner-inserter" then

          if not table.compare(event.created_entity.position, global.inserter_furnace_position or {0,0}) then
            story_show_message_dialog
            {
              text = {"msg-inserter-2-wrong-position"},
              point_to = {type = "entity", entity = event.created_entity}
            }
            return false
          end

          global.inserter_furnace = event.created_entity
          return true
        end
      end,
      action = function(event, story)
        set_arrow()
        story_jump_to(story, "wait-to-rotate-inserter-2")
        story_add_update(story, "check-inserter-2")
      end
    }
  }
}

story_init_helpers(story_table)

local story_events =
{
  defines.events.on_tick,
  defines.events.on_entity_died,
  defines.events.on_built_entity,
  defines.events.on_player_mined_item,
  defines.events.on_player_died
}

script.on_event(story_events, function(event)
  if game.players[1].character then
    check_for_player_death(event)
    story_update(global.story, event, "level-03")
  end
end)

script.on_init(init)

script.on_event(defines.events.on_player_created, function(event)
  on_player_created(event)
end)