예제 #1
0
    def step(self, state: StateFile):
        fuel_loc = state.fuel_loc.read()
        dump_loc = state.dump_loc.read()
        last_checkup_time = state.last_checkup.read()
        check_every_n_seconds = state.check_every_n_seconds.read()

        # ALWAYS scan for tree nodes!
        self.scan_for_nodes(state)

        # Basic maintenance
        routines.maybe_refuel(self, fuel_loc)
        routines.dump_if_full(self, dump_loc, range(4, 17))

        # Resupply on dirt or saplings as necessary
        self.maybe_resupply(state)

        # Priority 0: Make sure any tree chopping tasks are finished
        self.chop_trees(state)

        # Priority 1: Make sure trees are planted in the first place
        self.plant_trees(state)

        # Priority 2: Wait for trees to grow
        if time() - last_checkup_time < check_every_n_seconds:
            self.move_toward(fuel_loc, destructive=self.destructive)
            self.debug("Waiting...")
            os.sleep(30)
            raise StepFinished
        else:
            self.debug("Writing tree nodes!")
            state.tree_nodes.write(self.get_tree_corners(height_offset=4))
            state.placed_dirt.write([])
            state.placed_saplings.write([])
            state.confirmed_not_tree.write([])
            state.last_checkup.write(time())
예제 #2
0
 def run(self):
     self.debug("Starting loop!")
     while True:
         start_time = time()
         try:
             with self.state as state:
                 self.step(state)
         except StepFinished:
             pass
         except Exception as e:
             self.debug(
                 f"Turtle fatal exception! "
                 f"Turtle: {self.computer_id}", type(e), e)
             if not isinstance(e, lua_errors.TurtleBlockedError):
                 os.sleep(5)
         # Throttle the turtles maximum runs per second
         throttle_time = 1 / self.RUNS_PER_SECOND - (time() - start_time)
         if throttle_time > 0:
             os.sleep(throttle_time)
예제 #3
0
    'Use advanced computer for colors\n'
    'Screen will be cleared'
)

assert term.getSize() == (51, 19)
assert term.isColor() is True
assert term.clear() is None
assert term.setCursorPos(1, 1) is None
assert term.getCursorPos() == (1, 1)
assert term.write('Alpha') is None
assert term.getCursorPos() == (6, 1)
assert term.setCursorBlink(False) is None
assert term.getCursorBlink() is False
assert term.setCursorBlink(True) is None
assert term.getCursorBlink() is True
os.sleep(2)

_lib.term_step('You must have seen word Alpha with blinking cursor')

assert term.clear() is None
for offs, (tc, bc) in enumerate((
    (colors.lime, colors.green),
    (colors.yellow, colors.brown),
    (colors.red, colors.orange),
), start=1):
    assert term.setTextColor(tc) is None
    assert term.getTextColor() == tc
    assert term.setBackgroundColor(bc) is None
    assert term.getBackgroundColor() == bc
    assert term.setCursorPos(offs * 2, offs) is None
    assert term.getCursorPos() == (offs * 2, offs)
bbbbbbbbbbbbbbbbbbbbbb399399bbbbbbbbbbbbbbbbbbbbb
'''.strip()

assert _lib.get_class_table(paintutils) == _lib.get_object_table('paintutils')

with fs.open('img.nfp', 'w') as f:
    f.write(pixels)

int_pixels = paintutils.loadImage('img.nfp')
assert len(int_pixels) > 0
assert len(int_pixels[0]) > 0
assert paintutils.parseImage(pixels.encode('ascii')) == int_pixels

assert paintutils.drawImage(int_pixels, 1, 1) is None

os.sleep(2)

term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
term.setBackgroundColor(colors.green)

by = 3
bx = 3

assert paintutils.drawPixel(bx, by) is None
assert paintutils.drawPixel(bx + 1, by, colors.red) is None

bx += 5

assert paintutils.drawLine(bx, by, bx + 3, by + 3) is None
예제 #5
0
m = peripheral.wrap(side)

tbl = _lib.get_object_table(f'peripheral.wrap("{side}")')
assert _lib.get_class_table(CCSpeaker) == tbl

for _ in range(48):
    assert m.playNote(
        random.choice([
            'bass',
            'basedrum',
            'bell',
            'chime',
            'flute',
            'guitar',
            'hat',
            'snare',
            'xylophone',
            'iron_xylophone',
            'pling',
            'banjo',
            'bit',
            'didgeridoo',
            'cow_bell',
        ]), 3, random.randint(0, 24)) is True
    os.sleep(0.2)

assert m.playSound('minecraft:entity.player.levelup') is True

print('You must have heard notes and sounds')
print('Test finished successfully')
def breaks_slow(etype):
    os.sleep(3)
    raise etype
def breaks_fast(etype):
    os.sleep(0.5)
    raise etype
            partial('fast', os.version),
            partial('bomb', breaks_slow, IndexError),
        )
assert tags == {'fast', 'bomb'}
print('waitForAll waits for bomb OK')

for name, fn in all_parallels:
    tags.clear()
    with assert_takes_time(0.4, 1.2):
        with assert_raises(ValueError):
            fn(
                partial('v', breaks_fast, ValueError),
                partial('s', os.sleep, 2),
                partial('i', breaks_slow, IndexError),
            )
    os.sleep(4)
    assert tags == {'v', 's', 'i'}
    print(name + ' handles error OK')

for name, fn in all_parallels:
    tags.clear()
    with assert_takes_time(1.5, 3):
        fn(
            partial('1_s', os.sleep, 2),
            partial(
                '1_p',
                fn,
                partial('2_s', os.sleep, 2),
                partial(
                    '2_p',
                    fn,
예제 #9
0
assert m.setCursorBlink(False) is None
for offs, (tc, bc) in enumerate((
    (colors.lime, colors.green),
    (colors.yellow, colors.brown),
    (colors.red, colors.orange),
),
                                start=1):
    assert m.setTextColor(tc) is None
    assert m.getTextColor() == tc
    assert m.setBackgroundColor(bc) is None
    assert m.getBackgroundColor() == bc
    assert m.setCursorPos(offs, offs) is None
    assert m.getCursorPos() == (offs, offs)
    assert m.write('text') is None
assert m.setBackgroundColor(colors.black) is None
os.sleep(1)
for i in range(2):
    assert m.scroll(-1) is None
    os.sleep(0.5)
for i in range(2):
    assert m.scroll(1) is None
    os.sleep(0.5)

_lib.step('You must have seen three texts with different colors scrolling')

assert m.setTextColor(colors.white) is None
assert m.setBackgroundColor(colors.black) is None
assert m.clear() is None
for i in range(1, 5):
    assert m.setCursorPos(1, i) is None
    assert m.write((str(i) + '  ') * 4) is None
예제 #10
0
        if e[0] == timer_id:
            print('Timer reached')
            break

timer_id = os.startTimer(20)
assert isinstance(timer_id, int)
assert os.cancelTimer(timer_id) is None
assert os.cancelTimer(timer_id) is None

alarm_id = os.setAlarm(0.0)
assert isinstance(alarm_id, int)
assert os.cancelAlarm(alarm_id) is None
assert os.cancelAlarm(alarm_id) is None

with _lib.assert_takes_time(1.5, 3):
    assert os.sleep(2) is None

assert (os.version()).startswith('CraftOS ')
assert isinstance(os.getComputerID(), int)

assert os.setComputerLabel(None) is None
assert os.getComputerLabel() is None
assert os.setComputerLabel('altair') is None
assert os.getComputerLabel() == 'altair'
assert os.setComputerLabel(None) is None
assert os.getComputerLabel() is None

assert isinstance(os.epoch(), int)
assert isinstance(os.day(), int)
assert isinstance(os.time(), (int, float))
assert isinstance(os.clock(), (int, float))