|
36 | 36 | JERICHO_PATH = importlib.resources.files("jericho") |
37 | 37 | FROTZ_LIB_PATH = os.path.join(JERICHO_PATH, 'libfrotz.so') |
38 | 38 |
|
| 39 | +# The buffer size for actions in frotz. -2 to allow for newline and null terminator. |
| 40 | +INPUT_BUFFER_SIZE = 200 - 2 |
| 41 | + |
39 | 42 | # Function to unload a shared library. |
40 | 43 | dlclose_func = CDLL(None).dlclose # This WON'T work on Win |
41 | 44 | dlclose_func.argtypes = [c_void_p] |
@@ -361,6 +364,10 @@ class UnsupportedGameWarning(UserWarning): |
361 | 364 | pass |
362 | 365 |
|
363 | 366 |
|
| 367 | +class TruncatedInputActionWarning(UserWarning): |
| 368 | + pass |
| 369 | + |
| 370 | + |
364 | 371 | class FrotzEnv(): |
365 | 372 | """ |
366 | 373 | The Frotz Environment is a fast interface to Z-Machine games. |
@@ -465,9 +472,19 @@ def step(self, action): |
465 | 472 | the immediate reward, a boolean indicating whether the game is over,\ |
466 | 473 | and a dictionary of info. |
467 | 474 | :rtype: string, float, boolean, dictionary |
| 475 | +
|
| 476 | + Note: |
| 477 | + - The action is converted to bytes and truncated to 198 characters. |
468 | 478 | ''' |
| 479 | + action_bytes = action.encode('utf-8') |
| 480 | + if len(action_bytes) > INPUT_BUFFER_SIZE: |
| 481 | + action_bytes = action_bytes[:INPUT_BUFFER_SIZE] |
| 482 | + msg = ("Once converted to bytes, actions should have less than 198 characters." |
| 483 | + " Action '{}' was truncated to '{}'.".format(action, action_bytes.decode())) |
| 484 | + warnings.warn(msg, TruncatedInputActionWarning) |
| 485 | + |
469 | 486 | old_score = self.frotz_lib.get_score() |
470 | | - next_state = self.frotz_lib.step((action+'\n').encode('utf-8')).decode('cp1252') |
| 487 | + next_state = self.frotz_lib.step(action_bytes + b'\n').decode('cp1252') |
471 | 488 | score = self.frotz_lib.get_score() |
472 | 489 | reward = score - old_score |
473 | 490 | return next_state, reward, (self.game_over() or self.victory()),\ |
|
0 commit comments