summaryrefslogtreecommitdiff
path: root/pokemontools/vba/autoplayer.py
blob: 2d2e8afc2e60fdce13c0de40ceb2f6a228c4ffc0 (plain)
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
# -*- coding: utf-8 -*-
"""
Programmatic speedrun of Pokémon Crystal
"""
from __future__ import print_function
from __future__ import absolute_import
import os

import pokemontools.configuration as configuration

# bring in the emulator and basic tools
from . import vba as _vba

def skippable(func):
    """
    Makes a function skippable.

    Saves the state before and after the function runs. Pass "skip=True" to the
    function to load the previous save state from when the function finished.
    """
    def wrapped_function(*args, **kwargs):
        self = args[0]
        skip = True
        override = True

        if "skip" in kwargs.keys():
            skip = kwargs["skip"]
            del kwargs["skip"]

        if "override" in kwargs.keys():
            override = kwargs["override"]
            del kwargs["override"]

        # override skip if there's no save
        if skip:
            full_name = func.__name__ + "-end.sav"
            if not os.path.exists(os.path.join(self.config.save_state_path, full_name)):
                skip = False

        return_value = None

        if not skip:
            if override:
                self.cry.save_state(func.__name__ + "-start", override=override)

            return_value = func(*args, **kwargs)

            if override:
                self.cry.save_state(func.__name__ + "-end", override=override)
        elif skip:
            self.cry.vba.state = self.cry.load_state(func.__name__ + "-end")

        return return_value
    return wrapped_function

class Runner(object):
    """
    ``Runner`` is used to represent a set of functions that control an instance
    of the emulator. This allows for automated runs of games.
    """
    pass

class SpeedRunner(Runner):
    def __init__(self, cry=None, config=None):
        super(SpeedRunner, self).__init__()

        self.cry = cry

        if not config:
            config = configuration.Config()

        self.config = config

    def setup(self):
        """
        Configure this ``Runner`` instance to contain a reference to an active
        emulator session.
        """
        if not self.cry:
            self.cry = _vba.crystal(config=self.config)

    def main(self):
        """
        Main entry point for complete control of the game as the main player.
        """
        # get past the opening sequence
        self.skip_intro(skip=True)

        # walk to mom and handle her text
        self.handle_mom(skip=True)

        # walk outside into new bark town
        self.walk_into_new_bark_town(skip=True)

        # walk to elm and do whatever he wants
        self.handle_elm("totodile", skip=True)

        self.new_bark_level_grind(17, skip=False)

    @skippable
    def skip_intro(self, stop_at_name_selection=False):
        """
        Skip the game boot intro sequence.
        """

        # copyright sequence
        self.cry.nstep(400)

        # skip the ditto sequence
        self.cry.vba.press("a")
        self.cry.nstep(100)

        # skip the start screen
        self.cry.vba.press("start")
        self.cry.nstep(100)

        # click "new game"
        self.cry.vba.press("a", hold=50, after=1)

        # skip text up to "Are you a boy? Or are you a girl?"
        self.cry.text_wait()

        # select "Boy"
        self.cry.vba.press("a", hold=50, after=1)

        # text until "What time is it?"
        self.cry.text_wait()

        # select 10 o'clock
        self.cry.vba.press("a", hold=50, after=1)

        # yes i mean it
        self.cry.vba.press("a", hold=50, after=1)

        # "How many minutes?" 0 min.
        self.cry.vba.press("a", hold=50, after=1)

        # "Who! 0 min.?" yes/no select yes
        self.cry.vba.press("a", hold=50, after=1)

        # read text until name selection
        self.cry.text_wait()

        if stop_at_name_selection:
            return

        # select "Chris"
        self.cry.vba.press("d", hold=10, after=1)
        self.cry.vba.press("a", hold=50, after=1)

        def overworldcheck():
            """
            A basic check for when the game starts.
            """
            return self.cry.vba.memory[0xcfb1] != 0

        # go until the introduction is done
        self.cry.text_wait(callback=overworldcheck)

        return

    @skippable
    def handle_mom(self):
        """
        Walk to mom. Handle her speech and questions.
        """

        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")

        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")

        self.cry.move("d")
        self.cry.move("d")

        # move into mom's line of sight
        self.cry.move("d")

        # let mom talk until "What day is it?"
        self.cry.text_wait()

        # "What day is it?" Sunday
        self.cry.vba.press("a", hold=10) # Sunday

        self.cry.text_wait()

        # "SUNDAY, is it?" yes/no
        self.cry.vba.press("a", hold=10) # yes

        self.cry.text_wait()

        # "Is it Daylight Saving Time now?" yes/no
        self.cry.vba.press("a", hold=10) # yes

        self.cry.text_wait()

        # "AM DST, is that OK?" yes/no
        self.cry.vba.press("a", hold=10) # yes

        # text until "know how to use the PHONE?" yes/no
        self.cry.text_wait()

        # press yes
        self.cry.vba.press("a", hold=10)

        # wait until mom is done talking
        self.cry.text_wait()

        # wait until the script is done running
        self.cry.wait_for_script_running()

        return

    @skippable
    def walk_into_new_bark_town(self):
        """
        Walk outside after talking with mom.
        """

        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("l")
        self.cry.move("l")

        # walk outside
        self.cry.move("d")

    @skippable
    def handle_elm(self, starter_choice):
        """
        Walk to Elm's Lab and get a starter.
        """

        # walk to the lab
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("u")
        self.cry.move("u")

        # walk into the lab
        self.cry.move("u")

        # talk to elm
        self.cry.text_wait()

        # "that I recently caught." yes/no
        self.cry.vba.press("a", hold=10) # yes

        # talk to elm some more
        self.cry.text_wait()

        # talking isn't done yet..
        self.cry.text_wait()
        self.cry.text_wait()
        self.cry.text_wait()

        # wait until the script is done running
        self.cry.wait_for_script_running()

        # move toward the pokeballs
        self.cry.move("r")

        # move to cyndaquil
        self.cry.move("r")

        moves = 0

        if starter_choice.lower() == "cyndaquil":
            moves = 0
        elif starter_choice.lower() == "totodile":
            moves = 1
        else:
            moves = 2

        for each in range(0, moves):
            self.cry.move("r")

        # face the pokeball
        self.cry.move("u")

        # select it
        self.cry.vba.press("a", hold=10, after=0)

        # wait for the image to pop up
        self.cry.text_wait()

        # wait for the image to close
        self.cry.text_wait()

        # wait for the yes/no box
        self.cry.text_wait()

        # press yes
        self.cry.vba.press("a", hold=10, after=0)

        # wait for elm to talk a bit
        self.cry.text_wait()

        # TODO: why didn't that finish his talking?
        self.cry.text_wait()

        # give a nickname? yes/no
        self.cry.vba.press("d", hold=10, after=0) # move to "no"
        self.cry.vba.press("a", hold=10, after=0) # no

        # TODO: why didn't this wait until he was completely done?
        self.cry.text_wait()
        self.cry.text_wait()

        # get the phone number
        self.cry.text_wait()

        # talk with elm a bit more
        self.cry.text_wait()

        # wait until the script is done running
        self.cry.wait_for_script_running()

        # move down
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")

        # move into the researcher's line of sight
        self.cry.move("d")

        # get the potion from the person
        self.cry.text_wait()
        self.cry.text_wait()

        # wait for the script to end
        self.cry.wait_for_script_running()

        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")

        # go outside
        self.cry.move("d")

        return

    @skippable
    def new_bark_level_grind(self, level, walk_to_grass=True):
        """
        Do level grinding in New Bark.

        Starting just outside of Elm's Lab, do some level grinding until the
        first partymon level is equal to the given value..
        """

        # walk to the grass area
        if walk_to_grass:
            self.new_bark_level_grind_walk_to_grass(skip=False)

        last_direction = "u"

        # walk around in the grass until a battle happens
        while self.cry.vba.memory[0xd22d] == 0:
            if last_direction == "u":
                direction = "d"
            else:
                direction = "u"

            self.cry.move(direction)

            last_direction = direction

        # wait for wild battle to completely start
        self.cry.text_wait()

        attacks = 5

        while attacks > 0:
            # FIGHT
            self.cry.vba.press("a", hold=10, after=1)

            # wait to select a move
            self.cry.text_wait()

            # SCRATCH
            self.cry.vba.press("a", hold=10, after=1)

            # wait for the move to be over
            self.cry.text_wait()

            hp = self.cry.get_enemy_hp()
            print("enemy hp is: " + str(hp))

            if hp == 0:
                print("enemy hp is zero, exiting")
                break
            else:
                print("enemy hp is: " + str(hp))

            attacks = attacks - 1

        while self.cry.vba.memory[0xd22d] != 0:
            self.cry.vba.press("a", hold=10, after=1)

        # wait for the map to finish loading
        self.cry.vba.step(count=50)

        # This is used to handle any additional textbox that might be up on the
        # screen. The debug parameter is set to True so that max_wait is
        # enabled. This might be a textbox that is still waiting around because
        # of some faint during the battle. I am not completely sure why this
        # happens.
        self.cry.text_wait(max_wait=30, debug=True)

        print("okay, back in the overworld")

        cur_hp = ((self.cry.vba.memory[0xdd01] << 8) | self.cry.vba.memory[0xdd02])
        move_pp = self.cry.vba.memory[0xdcf6] # move 1 pp

        # if pokemon health is >20, just continue
        # if move 1 PP is 0, just continue
        if cur_hp > 20 and move_pp > 5 and self.cry.vba.memory[0xdcfe] < level:
            self.cry.move("u")
            return self.new_bark_level_grind(level, walk_to_grass=False, skip=False)

        # move up
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")

        # move into new bark town
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")

        # move up
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")

        # move to the door
        self.cry.move("r")
        self.cry.move("r")
        self.cry.move("r")

        # walk in
        self.cry.move("u")

        # move up to the healing thing
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("u")
        self.cry.move("l")
        self.cry.move("l")

        # face it
        self.cry.move("u")

        # interact
        self.cry.vba.press("a", hold=10, after=1)

        # wait for yes/no box
        self.cry.text_wait()

        # press yes
        self.cry.vba.press("a", hold=10, after=1)

        # TODO: when is healing done?

        # wait until the script is done running
        self.cry.wait_for_script_running()

        # wait for it to be really really done
        self.cry.vba.step(count=50)

        self.cry.move("r")
        self.cry.move("r")

        # move to the door
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")

        # walk out
        self.cry.move("d")

        # check partymon1 level
        if self.cry.vba.memory[0xdcfe] < level:
            self.new_bark_level_grind(level, skip=False)
        else:
            return

    @skippable
    def new_bark_level_grind_walk_to_grass(self):
        """
        Move to just above the grass from outside Elm's lab.
        """

        self.cry.move("d")
        self.cry.move("d")

        self.cry.move("l")
        self.cry.move("l")

        self.cry.move("d")
        self.cry.move("d")

        self.cry.move("l")
        self.cry.move("l")

        # move to route 29 past the trees
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")
        self.cry.move("l")

        # move to just above the grass
        self.cry.move("d")
        self.cry.move("d")
        self.cry.move("d")

def bootstrap(runner=None, cry=None):
    """
    Setup the initial game and return the state. This skips the intro and
    performs some other actions to get the game to a reasonable starting state.
    """
    if not runner:
        runner = SpeedRunner(cry=cry)
    runner.setup()

    # skip=False means always run the skip_intro function regardless of the
    # presence of a saved after state.
    runner.skip_intro(skip=True)

    # keep a reference of the current state
    state = runner.cry.vba.state

    runner.cry.vba.shutdown()

    return state

def main():
    """
    Setup a basic ``SpeedRunner`` instance and then run the runner.
    """
    runner = SpeedRunner()
    runner.setup()
    return runner.main()

if __name__ == "__main__":
    main()