Example #1
0
    def main_loop(self):
        """
        Perform maintenance operations. The main operation is routing table
        maintenance where staled nodes are added/probed/replaced/removed as
        needed. The routing management module specifies the implementation
        details.  This includes keeping track of queries that have not been
        responded for a long time (timeout) with the help of
        querier.Querier. The routing manager and the lookup manager will be
        informed of those timeouts.

        This method is designed to be used as minitwisted's heartbeat handler.

        """

        queries_to_send = []
        current_ts = time.time()
        #TODO: I think this if should be removed
        # At most, 1 second between calls to main_loop after the first call
        if current_ts >= self._next_main_loop_call_ts:
            self._next_main_loop_call_ts = current_ts + 1
        else:
            # It's too early
            return self._next_main_loop_call_ts, []
        # Retry failed lookup (if any)
        queries_to_send.extend(self._try_do_lookup())
        
        # Take care of timeouts
        if current_ts >= self._next_timeout_ts:
            (self._next_timeout_ts,
             timeout_queries) = self._querier.get_timeout_queries()
            for query in timeout_queries:
                queries_to_send.extend(self._on_timeout(query))

        # Routing table maintenance
        if time.time() >= self._next_maintenance_ts:
            (maintenance_delay,
             queries,
             maintenance_lookup) = self._routing_m.do_maintenance()
            self._next_maintenance_ts = current_ts + maintenance_delay
            self._next_main_loop_call_ts = min(self._next_main_loop_call_ts,
                                               self._next_maintenance_ts)
            queries_to_send.extend(queries)
            if maintenance_lookup:
                target, rnodes = maintenance_lookup
                lookup_obj = self._lookup_m.maintenance_lookup(target)
                queries_to_send.extend(lookup_obj.start(rnodes))
            
        # Auto-save routing table
        if current_ts >= self._next_save_state_ts:
            state.save(self._my_id,
                       self._routing_m.get_main_rnodes(),
                       self.state_filename)
            self._next_save_state_ts = current_ts + SAVE_STATE_DELAY
            self._next_main_loop_call_ts = min(self._next_main_loop_call_ts,
                                               self._next_maintenance_ts,
                                               self._next_timeout_ts,
                                               self._next_save_state_ts)
        # Return control to reactor
        datagrams_to_send = self._register_queries(queries_to_send)
        return self._next_main_loop_call_ts, datagrams_to_send
Example #2
0
 def test_load_save_state(self):
     filename = "test_logs/state.dat"
     my_id = tc.CLIENT_ID
     # TODO: change state
     state.save(my_id, [], filename)
     # TODO:check file
     state.load(filename)
Example #3
0
def init():
	global controls, cursor, point, cps
	controls = [
		parts.Conduit((1,)),
		parts.Conduit((2,)),
		parts.Conduit((3,)),
		parts.Conduit((1,2,)),
		parts.Conduit((1,3,)),
		parts.Conduit((2,3,)),
	]
	cps = [
		F(854 - 130 + 90 * (j % 3 - 1) - 25, 80 + 100 * (j // 3), 50)
		for j in range(len(controls))
	]

	del buttons[:]
	buttons.append(button.Button("Remove All", F(720, 390, 120, 30), fontsize = F(22)))
	buttons.append(button.Button("Depart", F(720, 430, 120, 30), fontsize = F(22)))
	buttons.extend(
		button.Button("buy" + s, (x - F(15), y + F(55), F(80), F(40)), fontsize = F(14))
		for (x, y, _), s in zip(cps, "1 2 3 12 13 23".split())
	)
	for j, modulename in enumerate(state.state.available):
		controls.append(parts.Module(modulename))
		cps.append((mx0 + F(85) * (j // 4), my0 + F(85) * (j % 4), mscale))
	cursor = None
	point = None
	gamescene.setshroud((0,0,0))
	state.state.you.hp = state.state.you.maxhp
	state.save()
Example #4
0
def predict():
    saved = state.load('model')
    #saved = None
    if debug_mode:
        saved = None
    if saved == None:
        train, y, test, _ = data.get()
        ftrain, ftest, _ = fea_1.get()
        ftrain2, ftest2, _ = fea_2.get()
        train = pd.concat([train, ftrain, ftrain2], axis=1)
        test = pd.concat([test, ftest, ftest2], axis=1)
        print(train.shape, test.shape)

        z = pd.DataFrame()
        z['id'] = test.id
        z['y'] = 0

        v = pd.DataFrame()
        v['id'] = train.id
        v['y'] = y
        cv, _ = run(train, y, test, v, z)
        state.save('model', (v, z, cv, None))
    else:
        v, z, cv, _ = saved
    return v, z, cv, _
Example #5
0
    def update(self, events):
	'''
        Updates the screen when an event happens 

        @param - list of game events
        '''
        for event in events:
            if not hasattr(event, 'key'): 
                continue
            if event.type == KEYDOWN:
                if event.key == K_p:
                    State.pop_screen()
		elif event.key == K_RETURN:
			self.sounds['select'].play()
			if self.currLine == PauseScreenLines.Resume:
				State.pop_screen()
			if self.currLine == PauseScreenLines.Save:
                            self.tileMap.save(self.player)
                            save(USER_SAVES_DIR + State.save_name)
			elif self.currLine == PauseScreenLines.Quit:
				State.pop_screen()
				State.pop_screen()
		elif event.key == K_i:
		    State.push_screen(InventoryScreen(self.player))
		else:
		    super(PauseScreen, self).interact(event)
Example #6
0
def predict():
    saved = state.load('model')
    #saved = None
    if debug_mode:
        saved = None
    if saved == None:
        train, y, test, _ = data.get()
        ftrain, ftest, _ = fea_1.get()
        ftrain2, ftest2, _ = fea_2.get()
        train = pd.concat([train, ftrain, ftrain2], axis=1)
        test = pd.concat([test, ftest, ftest2], axis=1)
        print(train.shape, test.shape)

        z = pd.DataFrame()
        z['id'] = test.id
        z['y'] = 0

        v = pd.DataFrame()
        v['id'] = train.id
        v['y'] = y
        cv, _ = run(train, y, test, v, z)
        state.save('model', (v, z, cv, None))
    else:
        v, z, cv, _ = saved
    return v, z, cv, _
Example #7
0
 def test_load_save_state(self):
     filename = 'test_logs/state.dat'
     my_id = tc.CLIENT_ID
     #TODO: change state
     state.save(my_id, [], filename)
     #TODO:check file
     state.load(filename)
Example #8
0
    def main_loop(self):
        """
        Perform maintenance operations. The main operation is routing table
        maintenance where staled nodes are added/probed/replaced/removed as
        needed. The routing management module specifies the implementation
        details.  This includes keeping track of queries that have not been
        responded for a long time (timeout) with the help of
        querier.Querier. The routing manager and the lookup manager will be
        informed of those timeouts.

        This method is designed to be used as minitwisted's heartbeat handler.

        """

        queries_to_send = []
        current_ts = time.time()
        #TODO: I think this if should be removed
        # At most, 1 second between calls to main_loop after the first call
        if current_ts >= self._next_main_loop_call_ts:
            self._next_main_loop_call_ts = current_ts + 1
        else:
            # It's too early
            return self._next_main_loop_call_ts, []
        # Retry failed lookup (if any)
        queries_to_send.extend(self._try_do_lookup())
        
        # Take care of timeouts
        if current_ts >= self._next_timeout_ts:
            (self._next_timeout_ts,
             timeout_queries) = self._querier.get_timeout_queries()
            for query in timeout_queries:
                queries_to_send.extend(self._on_timeout(query))

        # Routing table maintenance
        if time.time() >= self._next_maintenance_ts:
            (maintenance_delay,
             queries,
             maintenance_lookup) = self._routing_m.do_maintenance()
            self._next_maintenance_ts = current_ts + maintenance_delay
            self._next_main_loop_call_ts = min(self._next_main_loop_call_ts,
                                               self._next_maintenance_ts)
            queries_to_send.extend(queries)
            if maintenance_lookup:
                target, rnodes = maintenance_lookup
                lookup_obj = self._lookup_m.maintenance_lookup(target)
                queries_to_send.extend(lookup_obj.start(rnodes))
            
        # Auto-save routing table
        if current_ts >= self._next_save_state_ts:
            state.save(self._my_id,
                       self._routing_m.get_main_rnodes(),
                       self.state_filename)
            self._next_save_state_ts = current_ts + SAVE_STATE_DELAY
            self._next_main_loop_call_ts = min(self._next_main_loop_call_ts,
                                               self._next_maintenance_ts,
                                               self._next_timeout_ts,
                                               self._next_save_state_ts)
        # Return control to reactor
        datagrams_to_send = self._register_queries(queries_to_send)
        return self._next_main_loop_call_ts, datagrams_to_send
Example #9
0
def check_events():
    """ Check android-specific pause event. """
    if android.check_pause():
        android.hide_keyboard()
        # save emulator state
        state.console_state.screen.save_state()
        state.save()
        state.console_state.screen.clear_saved_state()
        # hibernate; we may not wake up
        android.wait_for_resume()
        return True
    return False
Example #10
0
def check_events():
    """ Check android-specific pause event. """
    if android.check_pause():
        android.hide_keyboard()
        # save emulator state
        state.console_state.screen.save_state()
        state.save()
        state.console_state.screen.clear_saved_state()
        # hibernate; we may not wake up
        android.wait_for_resume()
        return True
    return False
Example #11
0
def handleclick():
	global cursor
	if isinstance(point, tuple):
		if cursor:
			icon = cursor.nearest(point)
			if state.state.canaddpart(icon):
				state.state.addpart(icon)
				sound.play("build")
				cursor = None
			else:
				sound.play("cantbuild")
		else:
			part = state.state.partat(map(int, point))
			if part:
				state.state.removepart(part)
				sound.play("unbuild")
	elif isinstance(point, basestring):
		if point == "Remove All":
			state.state.removeall()
			sound.play("unbuild")
		elif point == "Depart" and state.state.cango():
			scene.pop()
			gamescene.makebuttons()
		elif point.startswith("buy"):
			cname = "conduit-" + point[3:]
			state.state.buy(cname)
	elif isinstance(point, parts.Conduit):
		if cursor is point:
			for j in range(len(controls)):
				if controls[j] is cursor:
					controls[j] = cursor = cursor.rotate(1)
		elif state.state.unused[point.name]:
			cursor = point
		else:
			sound.play("cantpick")
	elif isinstance(point, parts.Part):
		if cursor is point:
			cursor = None
		elif point.name in state.state.unlocked:
			if state.state.unused[point.name]:
				cursor = point
			else:
				sound.play("cantpick")
		else:
			state.state.unlock(point.name)
	elif point is None:
		cursor = None
	state.save()
Example #12
0
def predict():
    saved = state.load('model')
    #saved = None
    if saved == None:
        train, y, test, _ = data.get()
        z = pd.DataFrame()
        z['id'] = test.id
        z['y'] = 0

        v = pd.DataFrame()
        v['id'] = train.id
        v['y'] = y
        cv, _ = run(train, y, test, v, z)
        state.save('model', (v, z, cv, None))
    else:
        v, z, cv, _ = saved
    return v, z, cv, _
def predict():
    saved = state.load('model')
    #saved = None
    if saved == None:
        train, y, test, _ = data.get()
        z = pd.DataFrame()
        z['id'] = test.id
        z['y'] = 0

        v = pd.DataFrame()
        v['id'] = train.id
        v['y'] = y
        cv, _ = run(train, y, test, v, z)
        state.save('model', (v, z, cv, None))
    else:
        v, z, cv, _ = saved
    return v, z, cv, _
Example #14
0
 def run(self, command, run, quit, wait):
     """ Interactive interpreter session. """
     if command:
         self.store_line(command)
         self.loop()
     if run:
         # position the pointer at start of program and enter execute mode
         flow.jump(None)
         state.basic_state.parse_mode = True
         state.console_state.screen.cursor.reset_visibility()
     try:
         try:
             while True:
                 self.loop()
                 if quit and state.console_state.keyb.buf.is_empty():
                     break
         except error.Exit:
             # pause before exit if requested
             if wait:
                 signals.video_queue.put(signals.Event(signals.VIDEO_SET_CAPTION, 'Press a key to close window'))
                 signals.video_queue.put(signals.Event(signals.VIDEO_SHOW_CURSOR, False))
                 state.console_state.keyb.pause = True
                 # this performs a blocking keystroke read if in pause state
                 events.check_events()
         finally:
             # close interfaces
             signals.video_queue.put(signals.Event(signals.VIDEO_QUIT))
             signals.message_queue.put(signals.Event(signals.AUDIO_QUIT))
             # persist unplayed tones in sound queue
             state.console_state.tone_queue_store = [
                     signals.save_queue(q) for q in signals.tone_queue]
             state.save()
             # close files if we opened any
             devices.close_files()
             devices.close_devices()
     except error.Reset:
         # delete state if resetting
         state.delete()
Example #15
0
def start_basic():
    """ Load & run programs and commands and hand over to interactive mode. """
    import program
    import run
    import error
    import state
    import devices
    import disk
    import cassette
    import reset
    import sound
    import audio
    do_reset = False
    backend, console = None, None
    exit_error = ''
    try:
        # resume from saved emulator state if requested and available
        resume = config.get('resume') and state.load()
        # choose the video and sound backends
        backend, console = prepare_console()
        # greet, load and run only if not resuming
        if resume:
            # override selected settings from command line
            cassette.override()
            disk.override()
            # suppress double prompt
            if not state.basic_state.execute_mode:
                state.basic_state.prompt = False
            run.start('', False, config.get('quit'))
        else:
            # load/run program
            config.options['run'] = config.get(0) or config.get('run')
            prog = config.get('run') or config.get('load')
            if prog:
                # on load, accept capitalised versions and default extension
                with open_native_or_dos_filename(prog) as progfile:
                    program.load(progfile)
                reset.clear()
            print_greeting(console)
            # start the interpreter (and get out if we ran with -q)
            run.start(config.get('exec'), config.get('run'),
                      config.get('quit'))
    except error.RunError as e:
        exit_error = e.message
    except error.Exit:
        # pause before exit if requested
        if config.get('wait'):
            backend.video_queue.put(
                backend.Event(backend.VIDEO_SET_CAPTION,
                              'Press a key to close window'))
            backend.video_queue.put(
                backend.Event(backend.VIDEO_SHOW_CURSOR, False))
            state.console_state.keyb.pause = True
            # this performs a blocking keystroke read if in pause state
            backend.check_events()
    except error.Reset:
        do_reset = True
    except KeyboardInterrupt:
        if config.get('debug'):
            raise
    except Exception as e:
        exit_error = "Unhandled exception\n%s" % traceback.format_exc()
    finally:
        try:
            audio.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing audio: %s', e)
        try:
            # fix the terminal on exit (important for ANSI terminals)
            # and save display interface state into screen state
            state.console_state.screen.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing screen: %s', e)
        # delete state if resetting
        if do_reset:
            state.delete()
            if plat.system == 'Android':
                shutil.rmtree(plat.temp_dir)
        else:
            state.save()
        try:
            # close files if we opened any
            devices.close_files()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing files: %s', e)
        try:
            devices.close_devices()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing devices: %s', e)
        if exit_error:
            logging.error(exit_error)
Example #16
0
def start_basic():
    """ Load & run programs and commands and hand over to interactive mode. """
    import program
    import run
    import error
    import state
    import devices
    import disk
    import cassette
    import reset
    import sound
    import audio
    do_reset = False
    backend, console = None, None
    exit_error = ''
    try:
        # resume from saved emulator state if requested and available
        resume = config.get('resume') and state.load()
        # choose the video and sound backends
        backend, console = prepare_console()
        # greet, load and run only if not resuming
        if resume:
            # override selected settings from command line
            cassette.override()
            disk.override()
            # suppress double prompt
            if not state.basic_state.execute_mode:
                state.basic_state.prompt = False
            run.start('', False, config.get('quit'))
        else:
            # load/run program
            config.options['run'] = config.get(1) or config.get('run')
            prog = config.get('run') or config.get('load')
            if prog:
                # on load, accept capitalised versions and default extension
                with open_native_or_dos_filename(prog) as progfile:
                    program.load(progfile)
                reset.clear()
            print_greeting(console)
            # start the interpreter (and get out if we ran with -q)
            run.start(config.get('exec'), config.get('run'), config.get('quit'))
    except error.RunError as e:
        exit_error = error.get_message(e.err)
    except error.Exit:
        # pause before exit if requested
        if config.get('wait'):
            backend.video_queue.put(backend.Event(backend.VIDEO_SET_CAPTION, 'Press a key to close window'))
            backend.video_queue.put(backend.Event(backend.VIDEO_SHOW_CURSOR, False))
            state.console_state.keyb.pause = True
            # this performs a blocking keystroke read if in pause state
            backend.check_events()
    except error.Reset:
        do_reset = True
    except KeyboardInterrupt:
        if config.get('debug'):
            raise
    except Exception as e:
        print traceback.print_exc()
        exit_error = "Unhandled exception\n%s" % traceback.format_exc()
    finally:
        try:
            audio.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing audio: %s', e)
        try:
            # fix the terminal on exit (important for ANSI terminals)
            # and save display interface state into screen state
            state.console_state.screen.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing screen: %s', e)
        # delete state if resetting
        if do_reset:
            state.delete()
            if plat.system == 'Android':
                shutil.rmtree(plat.temp_dir)
        else:
            state.save()
        try:
            # close files if we opened any
            devices.close_files()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing files: %s', e)
        try:
            devices.close_devices()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing devices: %s', e)
        if exit_error:
            logging.error(exit_error)
Example #17
0
def run(train, y, test, v, z):
    #cname = sys._getframe().f_code.co_name
    cname = 'p'
    train.drop('id', axis=1, inplace=True)
    test.drop('id', axis=1, inplace=True)
    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval
    dtrain = xgb.DMatrix(train, y)

    def step_xgb(params):
        cv = xgb.cv(params=params,
                    dtrain=dtrain,
                    num_boost_round=10000,
                    early_stopping_rounds=50,
                    nfold=10,
                    seed=params['seed'])
        score = cv.ix[len(cv) - 1, 0]
        print(cname, score, len(cv), params)
        return dict(loss=score, status=STATUS_OK)

    space_xgb = dict(max_depth=hp.choice('max_depth', range(2, 9)),
                     subsample=hp.quniform('subsample', 0.6, 1, 0.05),
                     colsample_bytree=hp.quniform('colsample_bytree', 0.6, 1,
                                                  0.05),
                     learning_rate=hp.quniform('learning_rate', 0.005, 0.1,
                                               0.005),
                     min_child_weight=hp.quniform('min_child_weight', 1, 6, 1),
                     gamma=hp.quniform('gamma', 0.5, 10, 0.05),
                     reg_alpha=hp.quniform('reg_alpha', 0, 1, 0.001),
                     objective='binary:logistic',
                     eval_metric='logloss',
                     seed=1,
                     silent=1)
    trs = state.load('xgb_trials')
    if trs == None or debug_mode:
        tr = Trials()
    else:
        tr, _ = trs
    if len(tr.trials) > 0:
        print('reusing %d trials, best was:' % (len(tr.trials)),
              space_eval(space_xgb, tr.argmin))
        best = tr.argmin
    while len(tr.trials) < 15:
        best = fmin(step_xgb,
                    space_xgb,
                    algo=tpe.suggest,
                    max_evals=len(tr.trials) + 1,
                    trials=tr)
        state.save('xgb_trials', (tr, space_xgb))
    xgb_params = space_eval(space_xgb, best)
    print(xgb_params)

    N_splits = 9
    N_seeds = 3

    skf = model_selection.StratifiedKFold(n_splits=N_splits, shuffle=True)
    dtest = xgb.DMatrix(test)
    cv = []
    for s in range(N_seeds):
        scores = []
        cname2 = cname + str(s)
        v[cname2], z[cname2] = 0, 0
        xgb_params['seed'] = s + 4242
        for n, (itrain, ival) in enumerate(skf.split(train, y)):
            dtrain = xgb.DMatrix(train.ix[itrain], y[itrain])
            dvalid = xgb.DMatrix(train.ix[ival], y[ival])
            watch = [(dtrain, 'train'), (dvalid, 'valid')]
            clf = xgb.train(xgb_params,
                            dtrain,
                            10000,
                            watch,
                            early_stopping_rounds=100,
                            verbose_eval=False)

            p = clf.predict(dvalid)
            v.loc[ival, cname2] += p
            score = metrics.log_loss(y[ival], p)
            z[cname2] += clf.predict(dtest)
            print(
                cname, 'seed %d step %d of %d: ' %
                (xgb_params['seed'], n + 1, skf.n_splits), score, state.now())
            scores.append(score)
        z[cname2] /= N_splits
        cv.append(np.mean(scores))
        print('seed %d loss: ' % (xgb_params['seed']), scores, np.mean(scores),
              np.std(scores))
        z['y'] = z[cname2]

    print('cv:', cv, np.mean(cv), np.std(cv))
    return cv, None
def run(train, y, test, v, z):
    np.random.seed(1)
    #cname = sys._getframe().f_code.co_name
    train = train.values
    test = test.values

    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval
    space_stack = hp.choice('stacking by', [
            dict( type = 'BayesianRidge' ),
            dict( type = 'Lars' ),
            dict( type = 'LinearRegression' ),
            dict( type = 'Ridge' ),
            dict( type = 'SGDRegressor', random_state = 1 ),
            dict( type = 'XGBRegressor',
                 max_depth = hp.choice('max_depth', range(2, 8)),
                 subsample = hp.quniform('subsample', 0.6, 1, 0.05),
                 colsample_bytree = hp.quniform('colsample_bytree', 0.6, 1, 0.05),
                 learning_rate = hp.quniform('learning_rate', 0.005, 0.03, 0.005),
                 min_child_weight = hp.quniform('min_child_weight', 1, 6, 1),
                 gamma = hp.quniform('gamma', 0, 10, 0.05),
                 reg_alpha = hp.quniform('alpha', 0, 1, 0.0001),
                 ),
                                            ])

    def get_lr(params):
        t = params['type']
        del params['type']

        if t == 'BayesianRidge':
            lr = linear_model.BayesianRidge(**params)
        elif t == 'Lars':
            lr = linear_model.Lars(**params)
        elif t == 'LinearRegression':
            lr = linear_model.LinearRegression(**params)
        elif t == 'Ridge':
            lr = linear_model.Ridge(**params)
        elif t == 'SGDRegressor':
            lr = linear_model.SGDRegressor(**params)
        elif t == 'XGBRegressor':
            lr = xgb.XGBRegressor(**params)

        return lr

    def step(params):
        print(params, end = ' ')
        cv = model_selection.cross_val_score(get_lr(params),
                                             train, y,
                                             cv=10,
                                             scoring=metrics.make_scorer(metrics.log_loss))
        score = np.mean(cv)
        print(score)
        return dict(loss=score, status=STATUS_OK)

    trs = state.load('trials')
    if trs == None:
        tr = Trials()
    else:
        tr, _ = trs
    if len(tr.trials) > 0:
        best = tr.argmin
        print('reusing %d trials, best was:'%(len(tr.trials)), space_eval(space_stack, best))
    mt = max(50, len(tr.trials) + 1)
    while len(tr.trials) < min(50, mt):
        best = fmin(step, space_stack, algo=tpe.suggest, max_evals=len(tr.trials) + 1, trials = tr)
        state.save('trials', (tr, space_stack))
    params = space_eval(space_stack, best)

    print('best params:', params)
    lr = get_lr(params)
    cv = model_selection.cross_val_score(lr,
                                         train, y,
                                         cv=10,
                                         scoring=metrics.make_scorer(metrics.log_loss))
    lr.fit(train, y)
    z['p'] = np.clip(lr.predict(test), 1e-5, 1-1e-5)
    z['y'] = z['p']
    v['p'] = model_selection.cross_val_predict(lr,
                                         train, y,
                                         cv=10)
    print('cv:', np.mean(cv), np.std(cv))
    return cv, None
Example #19
0
def start_basic():
    """ Load & run programs and commands and hand over to interactive mode. """
    import program
    import run
    import error
    import state
    import devices
    import disk
    import cassette
    import reset
    import sound
    do_reset = False
    backend, console = None, None
    try:
        # resume from saved emulator state if requested and available
        resume = config.get('resume') and state.load()
        # choose the video and sound backends
        backend, console = prepare_console()
        # greet, load and run only if not resuming
        if resume:
            # override selected settings from command line
            cassette.override()
            disk.override()
            # suppress double prompt
            if not state.basic_state.execute_mode:
                state.basic_state.prompt = False
            run.start('', False, config.get('quit'))
        else:
            # load/run program
            config.options['run'] = config.get(0) or config.get('run')
            prog = config.get('run') or config.get('load')
            if prog:
                # on load, accept capitalised versions and default extension
                with open_native_or_dos_filename(prog) as progfile:
                    program.load(progfile)
                reset.clear()
            print_greeting(console)
            # start the interpreter (and get out if we ran with -q)
            run.start(config.get('exec'), config.get('run'),
                      config.get('quit'))
    except error.RunError as e:
        msg = error.get_message(e.err)
        if console and config.get('wait'):
            console.write_error_message(msg, None)
        if backend and backend.video:
            # close terminal to avoid garbled error message
            backend.video.close()
            backend.video = None
        logging.error(msg)
    except error.Exit:
        pass
    except error.Reset:
        do_reset = True
    except KeyboardInterrupt:
        if config.get('debug'):
            raise
    except Exception as e:
        logging.error("Unhandled exception\n%s", traceback.format_exc())
    finally:
        try:
            # fix the terminal on exit (important for ANSI terminals)
            # and save display interface state into screen state
            state.console_state.screen.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing screen: %s', e)
        # delete state if resetting
        if do_reset:
            state.delete()
            if plat.system == 'Android':
                shutil.rmtree(plat.temp_dir)
        else:
            state.save()
        try:
            # close files if we opened any
            devices.close_files()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing files: %s', e)
        try:
            devices.close_devices()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing devices: %s', e)
        try:
            sound.audio.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing audio: %s', e)
Example #20
0
    logging.info(myyaml.dump(common.dump.vars_seq([hyperparameters, miscglobals])))

    #validate(0)
    diagnostics.diagnostics(cnt, m)
#    diagnostics.visualizedebug(cnt, m, rundir)
    while 1:
        logging.info("STARTING EPOCH #%d" % epoch)
        for ebatch in get_train_minibatch:
            cnt += len(ebatch)
        #    print [wordmap.str(id) for id in e]

            noise_sequences, weights = corrupt.corrupt_examples(m, ebatch)
            m.train(ebatch, noise_sequences, weights)

            #validate(cnt)
            if cnt % (int(1000./HYPERPARAMETERS["MINIBATCH SIZE"])*HYPERPARAMETERS["MINIBATCH SIZE"]) == 0:
                logging.info("Finished training step %d (epoch %d)" % (cnt, epoch))
#                print ("Finished training step %d (epoch %d)" % (cnt, epoch))
            if cnt % (int(100000./HYPERPARAMETERS["MINIBATCH SIZE"])*HYPERPARAMETERS["MINIBATCH SIZE"]) == 0:
                diagnostics.diagnostics(cnt, m)
                if os.path.exists(os.path.join(rundir, "BAD")):
                    logging.info("Detected file: %s\nSTOPPING" % os.path.join(rundir, "BAD"))
                    sys.stderr.write("Detected file: %s\nSTOPPING\n" % os.path.join(rundir, "BAD"))
                    sys.exit(0)
            if cnt % (int(HYPERPARAMETERS["VALIDATE_EVERY"]*1./HYPERPARAMETERS["MINIBATCH SIZE"])*HYPERPARAMETERS["MINIBATCH SIZE"]) == 0:
                state.save(m, cnt, epoch, get_train_minibatch, rundir, newkeystr)
                diagnostics.visualizedebug(cnt, m, rundir, newkeystr)
#                validate(cnt)
        get_train_minibatch = examples.TrainingMinibatchStream()
        epoch += 1
Example #21
0
def run(state, train, y, test, v, z):
    #cname = sys._getframe().f_code.co_name
    cname = 'p'
    train.drop('id', axis=1, inplace=True)
    test.drop('id', axis=1, inplace=True)
    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval
    def step_rf(params):
        clf = ensemble.RandomForestRegressor(**params)
        cv = model_selection.cross_val_score(clf,
                                             train, y,
                                             scoring=metrics.make_scorer(metrics.log_loss),
                                             cv = 10,
                                             n_jobs = -2)
        score = np.mean(cv)
        print(cname, score, params)
        return dict(loss=score, status=STATUS_OK)
    space_rf = dict(
        n_estimators = hp.choice('n_estimators', range(50, 1500)),
        #criterion = hp.choice('criterion', ["gini", "entropy"]),
        min_samples_split = hp.choice('min_samples_split', range(2, 10)),
        min_samples_leaf = hp.choice('min_samples_leaf', range(1, 10)),
        max_features = hp.choice('max_features', range(1, 16)),
        random_state = 1
        )
    trs = state.load('rf_trials')
    if trs == None or debug_mode:
        tr = Trials()
    else:
        tr, _ = trs
    if len(tr.trials) > 0:
        print('reusing %d trials, best was:'%(len(tr.trials)), space_eval(space_rf, tr.argmin))
        best = tr.argmin
    while len(tr.trials) < 15:
        best = fmin(step_rf, space_rf, algo=tpe.suggest, max_evals=len(tr.trials) + 1, trials = tr)
        state.save('et_trials', (tr, space_rf))
    rf_params = space_eval(space_rf, best)
    print(rf_params)

    N_splits = 9
    N_seeds = 3

    skf = model_selection.StratifiedKFold(n_splits=N_splits, shuffle=True)
    cv = []
    for s in range(N_seeds):
        scores = []
        cname2 = cname + str(s)
        v[cname2], z[cname2] = 0, 0
        rf_params['random_state'] = s + 4242
        for n, (itrain, ival) in enumerate(skf.split(train, y)):
            clf = ensemble.RandomForestRegressor(**rf_params)
            clf.fit(train.ix[itrain], y[itrain])
            p = clf.predict(train.ix[ival])
            v.loc[ival, cname2] += p
            score = metrics.log_loss(y[ival], p)
            z[cname2]  += clf.predict(test)
            print(cname, 'seed %d step %d of %d: '%(rf_params['random_state'], n+1, skf.n_splits), score, state.now())
            scores.append(score)
        z[cname2] /= N_splits
        cv.append(np.mean(scores))
        print('seed %d loss: '%(rf_params['random_state']), scores, np.mean(scores), np.std(scores))
        z['y'] = z[cname2]

    print('cv:', cv, np.mean(cv), np.std(cv))
    return cv, None
Example #22
0
            bh -= gbh * LR
        w2 -= gw2 * LR
        b2 -= gb2 * LR
    
    #    o = graph.validatefn(x, N.array([y]), w1, b1, w2, b2)
    #    (kl, softmax, argmax, presquashh) = o
    ##    print "new KL=%.3f, softmax=%s, argmax=%d" % (kl, softmax, argmax)
    #    print "new KL=%.3f, argmax=%d" % (kl, argmax)
    
    if cnt % HYPERPARAMETERS["examples per validation"] == 0:
        valacc, valstd = validate()
        sys.stderr.write("After %d training examples, validation accuracy: %.2f%%, stddev: %.2f%% (former best=%.2f%% at %d)\n" % (cnt, valacc*100, valstd*100, best_validation_accuracy*100, best_validation_at))
        if best_validation_accuracy < valacc:
            best_validation_accuracy = valacc
            best_validation_at = cnt
            sys.stderr.write("NEW BEST VALIDATION ACCURACY. Saving state.\n")
            if HLAYERS == 2:
                state.save((w1, b1, wh, bh, w2, b2), rundir, best_validation_accuracy, best_validation_at)
            else:
                state.save((w1, b1, w2, b2), rundir, best_validation_accuracy, best_validation_at)
        elif cnt > 2*best_validation_at and cnt >= HYPERPARAMETERS["minimum training updates"]:
            sys.stderr.write("Have not beaten best validation accuracy for a while. Terminating training...\n")
            sys.stderr.write(stats() + "\n")
            break
    if cnt % 1000 == 0:
        sys.stderr.write("After %d training examples, training accuracy %s\n" % (cnt, mvgavg_accuracy))
        sys.stderr.write("After %d training examples, training loss %s\n" % (cnt, mvgavg_loss))
        sys.stderr.write(stats() + "\n")

#graph.COMPILE_MODE.print_summary()
Example #23
0
def run(train, y, test, v, z):
    np.random.seed(1)
    #cname = sys._getframe().f_code.co_name
    train = train.values
    test = test.values

    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval
    space_stack = hp.choice('stacking by', [
        dict(type='BayesianRidge'),
        dict(type='Lars'),
        dict(type='LinearRegression'),
        dict(type='Ridge'),
        dict(type='SGDRegressor', random_state=1),
        dict(
            type='XGBRegressor',
            max_depth=hp.choice('max_depth', range(2, 8)),
            subsample=hp.quniform('subsample', 0.6, 1, 0.05),
            colsample_bytree=hp.quniform('colsample_bytree', 0.6, 1, 0.05),
            learning_rate=hp.quniform('learning_rate', 0.005, 0.03, 0.005),
            min_child_weight=hp.quniform('min_child_weight', 1, 6, 1),
            gamma=hp.quniform('gamma', 0, 10, 0.05),
            reg_alpha=hp.quniform('alpha', 0, 1, 0.0001),
        ),
    ])

    def get_lr(params):
        t = params['type']
        del params['type']

        if t == 'BayesianRidge':
            lr = linear_model.BayesianRidge(**params)
        elif t == 'Lars':
            lr = linear_model.Lars(**params)
        elif t == 'LinearRegression':
            lr = linear_model.LinearRegression(**params)
        elif t == 'Ridge':
            lr = linear_model.Ridge(**params)
        elif t == 'SGDRegressor':
            lr = linear_model.SGDRegressor(**params)
        elif t == 'XGBRegressor':
            lr = xgb.XGBRegressor(**params)

        return lr

    def step(params):
        print(params, end=' ')
        cv = model_selection.cross_val_score(get_lr(params),
                                             train,
                                             y,
                                             cv=10,
                                             scoring=metrics.make_scorer(
                                                 metrics.log_loss))
        score = np.mean(cv)
        print(score)
        return dict(loss=score, status=STATUS_OK)

    trs = state.load('trials')
    if trs == None:
        tr = Trials()
    else:
        tr, _ = trs
    if len(tr.trials) > 0:
        best = tr.argmin
        print('reusing %d trials, best was:' % (len(tr.trials)),
              space_eval(space_stack, best))
    mt = max(50, len(tr.trials) + 1)
    while len(tr.trials) < min(50, mt):
        best = fmin(step,
                    space_stack,
                    algo=tpe.suggest,
                    max_evals=len(tr.trials) + 1,
                    trials=tr)
        state.save('trials', (tr, space_stack))
    params = space_eval(space_stack, best)

    print('best params:', params)
    lr = get_lr(params)
    cv = model_selection.cross_val_score(lr,
                                         train,
                                         y,
                                         cv=10,
                                         scoring=metrics.make_scorer(
                                             metrics.log_loss))
    lr.fit(train, y)
    z['p'] = np.clip(lr.predict(test), 1e-5, 1 - 1e-5)
    z['y'] = z['p']
    v['p'] = model_selection.cross_val_predict(lr, train, y, cv=10)
    print('cv:', np.mean(cv), np.std(cv))
    return cv, None
Example #24
0
                aux__ = string_helpers.string_used_words(used_letters)
                print("LETRAS YA USADAS: {}".format(aux__))
                print(grafico.draw_sprite(lives, graphic_word))
                
                if lives > 0:
                    print("Quedan {} lives!!".format(lives))
                else:  ## si perdiste
                    print("Que en paz descanse...")
                    print("La palabra era... \"{}\"".format(hidden_word))
                    if score > 0 :
                        print("Tienes un total de {} puntos".format(score))
                    _continue = input("Tu puntaje es {}! \n Sigues jugando? (y = SI / n = NO)".format(score))
                    if _continue == "n":  ## Al ingresar 'y' va a emplezar de nuevo
                        play = False

to_save = state.save(score)

with open(score_file, "r") as f:
    for line in f:
        to_save += line

#convertir esto en print tabla con valores de mayor a menor
print(to_save)

with open(score_file, "w") as f:
    f.write(to_save)




def run(train, y, test, v, z):
    #cname = sys._getframe().f_code.co_name
    cname = 'p'
    train.drop('id', axis=1, inplace=True)
    test.drop('id', axis=1, inplace=True)
    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval
    dtrain = xgb.DMatrix(train, y)
    def step_xgb(params):
        cv = xgb.cv(params=params,
                    dtrain=dtrain,
                    num_boost_round=10000,
                    early_stopping_rounds=50,
                    nfold=10,
                    seed=params['seed'])
        score = cv.ix[len(cv)-1, 0]
        print(cname, score, len(cv), params)
        return dict(loss=score, status=STATUS_OK)
    space_xgb = dict(
            max_depth = hp.choice('max_depth', range(2, 8)),
            subsample = hp.quniform('subsample', 0.6, 1, 0.05),
            colsample_bytree = hp.quniform('colsample_bytree', 0.6, 1, 0.05),
            learning_rate = hp.quniform('learning_rate', 0.005, 0.03, 0.005),
            min_child_weight = hp.quniform('min_child_weight', 1, 6, 1),
            gamma = hp.quniform('gamma', 0.5, 10, 0.05),

            objective = 'binary:logistic',
            eval_metric = 'logloss',
            seed = 1,
            silent = 1
        )
    trs = state.load('xgb_trials')
    if trs == None:
        tr = Trials()
    else:
        tr, _ = trs
    if len(tr.trials) > 0:
        print('reusing %d trials, best was:'%(len(tr.trials)), space_eval(space_xgb, tr.argmin))
        best = tr.argmin
    while len(tr.trials) < 15:
        best = fmin(step_xgb, space_xgb, algo=tpe.suggest, max_evals=len(tr.trials) + 1, trials = tr)
        state.save('xgb_trials', (tr, space_xgb))
    xgb_params = space_eval(space_xgb, best)
    print(xgb_params)

    N_splits = 9
    N_seeds = 1

    skf = model_selection.StratifiedKFold(n_splits=N_splits, shuffle=True)
    dtest = xgb.DMatrix(test)
    for s in range(N_seeds):
        scores = []
        cname2 = cname + str(s)
        v[cname2], z[cname2] = 0, 0
        xgb_params['seed'] = s + 4242
        for n, (itrain, ival) in enumerate(skf.split(train, y)):
            dtrain = xgb.DMatrix(train.ix[itrain], y[itrain])
            dvalid = xgb.DMatrix(train.ix[ival], y[ival])
            watch = [(dtrain, 'train'), (dvalid, 'valid')]
            clf = xgb.train(xgb_params, dtrain, 10000, watch, early_stopping_rounds=100, verbose_eval=False)

            p = clf.predict(dvalid)
            v.loc[ival, cname2] += p
            score = metrics.log_loss(y[ival], p)
            z[cname2]  += clf.predict(dtest)
            print(cname, 'seed %d step %d of %d: '%(xgb_params['seed'], n+1, skf.n_splits), score, state.now())
            scores.append(score)
        z[cname2] /= N_splits

    cv = scores
    z['y'] = z[cname2]
    print('validation loss: ', cv, np.mean(cv), np.std(cv))

    return cv, None
Example #26
0
            # The following is code for training on bilingual examples.
            # TODO: Monolingual examples?

            correct_sequences, noise_sequences, weights = ebatch_to_sequences(ebatch)
            translation_model[source_language].train(correct_sequences, noise_sequences, weights)

            #validate(translation_model, cnt)
            if int(cnt/1000) > int(lastcnt/1000):
                logging.info("Finished training step %d (epoch %d)" % (cnt, epoch))
#                print ("Finished training step %d (epoch %d)" % (cnt, epoch))
            if int(cnt/10000) > int(lastcnt/10000):
                for l1 in translation_model:
                    diagnostics.diagnostics(cnt, translation_model[l1])
                if os.path.exists(os.path.join(rundir, "BAD")):
                    logging.info("Detected file: %s\nSTOPPING" % os.path.join(rundir, "BAD"))
                    sys.stderr.write("Detected file: %s\nSTOPPING\n" % os.path.join(rundir, "BAD"))
                    sys.exit(0)
            if int(cnt/HYPERPARAMETERS["VALIDATE_EVERY"]) > int(lastcnt/HYPERPARAMETERS["VALIDATE_EVERY"]):
                validate(translation_model, cnt)
                pass
#                for l1 in translation_model:
#                    diagnostics.visualizedebug(cnt, translation_model[l1], rundir, newkeystr)

        validate(translation_model, cnt)
#        get_train_minibatch = w2w.examples.get_training_minibatch_online()
        get_train_minibatch = w2w.examples.get_training_minibatch_cached()
        epoch += 1

        state.save(translation_model, cnt, lastcnt, epoch, rundir, newkeystr)
#       validate(cnt)
Example #27
0
def start_basic():
    """ Load & run programs and commands and hand over to interactive mode. """
    import program
    import run
    import error
    import state
    import devices
    import disk
    import cassette
    import reset
    import sound
    do_reset = False
    backend, console = None, None
    try:
        # resume from saved emulator state if requested and available
        resume = config.get('resume') and state.load()
        # choose the video and sound backends
        backend, console = prepare_console()
        # greet, load and run only if not resuming
        if resume:
            # override selected settings from command line
            cassette.override()
            disk.override()
            # suppress double prompt
            if not state.basic_state.execute_mode:
                state.basic_state.prompt = False
            run.start('', False, config.get('quit'))
        else:
            # load/run program
            config.options['run'] = config.get(0) or config.get('run')
            prog = config.get('run') or config.get('load')
            if prog:
                # on load, accept capitalised versions and default extension
                with open_native_or_dos_filename(prog) as progfile:
                    program.load(progfile)
                reset.clear()
            print_greeting(console)
            # start the interpreter (and get out if we ran with -q)
            run.start(config.get('exec'), config.get('run'), config.get('quit'))
    except error.RunError as e:
        msg = error.get_message(e.err)
        if console and config.get('wait'):
            console.write_error_message(msg, None)
        if backend and backend.video:
            # close terminal to avoid garbled error message
            backend.video.close()
            backend.video = None
        logging.error(msg)
    except error.Exit:
        pass
    except error.Reset:
        do_reset = True
    except KeyboardInterrupt:
        if config.get('debug'):
            raise
    except Exception as e:
        logging.error("Unhandled exception\n%s", traceback.format_exc())
    finally:
        try:
            # fix the terminal on exit (important for ANSI terminals)
            # and save display interface state into screen state
            state.console_state.screen.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing screen: %s', e)
        # delete state if resetting
        if do_reset:
            state.delete()
            if plat.system == 'Android':
                shutil.rmtree(plat.temp_dir)
        else:
            state.save()
        try:
            # close files if we opened any
            devices.close_files()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing files: %s', e)
        try:
            devices.close_devices()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing devices: %s', e)
        try:
            sound.audio.close()
        except (NameError, AttributeError) as e:
            logging.debug('Error on closing audio: %s', e)
Example #28
0
        logging.info("INITIALIZING TRAINING STATE")

    logging.info(myyaml.dump(common.dump.vars_seq([hyperparameters, miscglobals])))

    #validate(0)
    diagnostics.diagnostics(cnt, m)
#    diagnostics.visualizedebug(cnt, m, rundir)
    while 1:
        logging.info("STARTING EPOCH #%d" % epoch)
        for ebatch in get_train_minibatch:
            cnt += len(ebatch)
        #    print [wordmap.str(id) for id in e]
            m.train(ebatch)

            #validate(cnt)
            if cnt % (int(1000./HYPERPARAMETERS["MINIBATCH SIZE"])*HYPERPARAMETERS["MINIBATCH SIZE"]) == 0:
                logging.info("Finished training step %d (epoch %d)" % (cnt, epoch))
#                print ("Finished training step %d (epoch %d)" % (cnt, epoch))
            if cnt % (int(100000./HYPERPARAMETERS["MINIBATCH SIZE"])*HYPERPARAMETERS["MINIBATCH SIZE"]) == 0:
                diagnostics.diagnostics(cnt, m)
                if os.path.exists(os.path.join(rundir, "BAD")):
                    logging.info("Detected file: %s\nSTOPPING" % os.path.join(rundir, "BAD"))
                    sys.stderr.write("Detected file: %s\nSTOPPING\n" % os.path.join(rundir, "BAD"))
                    sys.exit(0)
            if cnt % (int(HYPERPARAMETERS["VALIDATE_EVERY"]*1./HYPERPARAMETERS["MINIBATCH SIZE"])*HYPERPARAMETERS["MINIBATCH SIZE"]) == 0:
                state.save(m, cnt, epoch, get_train_minibatch, rundir, newkeystr)
                diagnostics.visualizedebug(cnt, m, rundir, newkeystr)
#                validate(cnt)
        get_train_minibatch = examples.TrainingMinibatchStream()
        epoch += 1
Example #29
0
def run(state, train, y, test, v, z):
    #cname = sys._getframe().f_code.co_name
    cname = 'p'
    train.drop('id', axis=1, inplace=True)
    test.drop('id', axis=1, inplace=True)
    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval

    def step_et(params):
        clf = ensemble.ExtraTreesRegressor(**params)
        cv = model_selection.cross_val_score(clf,
                                             train,
                                             y,
                                             scoring=metrics.make_scorer(
                                                 metrics.log_loss),
                                             cv=10,
                                             n_jobs=-2)
        score = np.mean(cv)
        print(cname, score, params)
        return dict(loss=score, status=STATUS_OK)

    space_et = dict(
        n_estimators=hp.choice('n_estimators', range(50, 1500)),
        #criterion = hp.choice('criterion', ["gini", "entropy"]),
        min_samples_split=hp.choice('min_samples_split', range(2, 10)),
        min_samples_leaf=hp.choice('min_samples_leaf', range(1, 10)),
        max_features=hp.choice('max_features', range(1, 16)),
        random_state=1)
    trs = state.load('et_trials')
    if trs == None or debug_mode:
        tr = Trials()
    else:
        tr, _ = trs
    if len(tr.trials) > 0:
        print('reusing %d trials, best was:' % (len(tr.trials)),
              space_eval(space_et, tr.argmin))
        best = tr.argmin
    while len(tr.trials) < 15:
        best = fmin(step_et,
                    space_et,
                    algo=tpe.suggest,
                    max_evals=len(tr.trials) + 1,
                    trials=tr)
        state.save('et_trials', (tr, space_et))
    et_params = space_eval(space_et, best)
    print(et_params)

    N_splits = 9
    N_seeds = 3

    skf = model_selection.StratifiedKFold(n_splits=N_splits, shuffle=True)
    cv = []
    for s in range(N_seeds):
        scores = []
        cname2 = cname + str(s)
        v[cname2], z[cname2] = 0, 0
        et_params['random_state'] = s + 4242
        for n, (itrain, ival) in enumerate(skf.split(train, y)):
            clf = ensemble.ExtraTreesRegressor(**et_params)
            clf.fit(train.ix[itrain], y[itrain])
            p = clf.predict(train.ix[ival])
            v.loc[ival, cname2] += p
            score = metrics.log_loss(y[ival], p)
            z[cname2] += clf.predict(test)
            print(
                cname, 'seed %d step %d of %d: ' %
                (et_params['random_state'], n + 1, skf.n_splits), score,
                state.now())
            scores.append(score)
        z[cname2] /= N_splits
        cv.append(np.mean(scores))
        print('seed %d loss: ' % (et_params['random_state']), scores,
              np.mean(scores), np.std(scores))
        z['y'] = z[cname2]

    print('cv:', cv, np.mean(cv), np.std(cv))
    return cv, None
Example #30
0
def think(dt, events, kpress, mpos):

	global alpha, playing, ending, lastsave
	if playing and not ending:
		alpha = min(2 * dt + alpha, 1)
	elif ending:
		alpha -= 2 * dt
		if alpha <= -6:
			hud.endtitle.settext()
			if not hud.endtitle:
				state.removesave()
				scene.pop()
				return
	else:
		alpha -= 2 * dt
		if alpha <= 0:
			scene.pop()
			return
	sound.setvolume(alpha)

	lastsave += dt
	if settings.savetime is not None and lastsave > settings.savetime:
		lastsave = 0
		state.save()

	if kpress[K_ESCAPE]:
		state.save()
		scene.pop()
		scene.pop()
		playing = False

	dx = ((kpress[K_RIGHT]) - (kpress[K_LEFT])) * dt
	dy = ((kpress[K_UP]) - (kpress[K_DOWN])) * dt
	dr = ((kpress[K_e] or kpress[K_d]) - (kpress[K_a])) * dt
	dA = ((kpress[K_COMMA] or kpress[K_w]) - (kpress[K_o] or kpress[K_s])) * dt

	for event in events:
		if event.type == QUIT:
			state.save()
			scene.pop()
			scene.pop()
			playing = False
		if event.type == MOUSEBUTTONDOWN:
			if event.button == 4:
				camera.zoom /= 1.08
			elif event.button == 5:
				camera.zoom *= 1.08
			else:
				px, py = mpos
				if hud.click((px, settings.sy - py)):
					continue
				p = camera.screentoworld((settings.sx - px, settings.sy - py))
				if p and cursor.tobuild:
					phat = p.norm()
					if state.canbuild(cursor.tobuild, phat) and not state.builderror(cursor.tobuild):
						f = camera.eye().rej(phat).norm()
						state.build(cursor.tobuild(p.norm(), f))
						cursor.tobuild = None
					else:
						sound.play("error")
				elif cursor.pointingto:
					if cursor.unbuild:
						state.unbuild(cursor.pointingto)
					elif cursor.disable:
						state.toggleenable(cursor.pointingto)
				else:
					cursor.dragging = True
		if event.type == MOUSEBUTTONUP:
			cursor.dragging = False
		if event.type == MOUSEMOTION:
			relx, rely = event.rel
			if event.buttons[0]:
				dx -= relx * settings.dragfactor / settings.sy
				dy += rely * settings.dragfactor / settings.sy
			if event.buttons[2]:
				dr -= relx * settings.dragfactor / settings.sy
				dA += rely * settings.dragfactor / settings.sy
#			camera.seek((0, 0, 1))
		if event.type == KEYDOWN and event.key == K_CAPSLOCK:
			settings.swaparrows = not settings.swaparrows
		if event.type == KEYDOWN and event.key == K_F3 and settings.unlocked >= 99:
			if settings.level == 3:
				scene.pushunder(scenes.final)
			ending = True
			hud.endtitle.settext("Mission Complete")
		if event.type == KEYDOWN and event.key == K_F12:
			graphics.screenshot()
		if event.type == KEYDOWN and event.key == K_INSERT:
			camera.zoom /= 1.2
		if event.type == KEYDOWN and event.key == K_DELETE:
			camera.zoom *= 1.2
		if event.type == KEYDOWN and event.key == K_SPACE:
			things.Asteroid(100)

	camera.zoom = min(max(camera.zoom, 30), 200)


	if bool(kpress[K_LSHIFT]) != bool(settings.swaparrows):
		dx, dy, dr, dA = dr, dA, dx, dy
	
	camera.move(40. * dx / state.R, 40. * dy / state.R, 2.5 * dr, 0.7 * dA)
	camera.think(dt)
			

	px, py = mpos
	hud.point((px, settings.sy - py))

	dtobj = dt * settings.speedup

	for obj in state.thinkers():
		obj.think(dtobj)

	hud.think(dt)
	state.think(dtobj)
	
	hud.setstatus()

	if not ending:
		if state.checklose():
			ending = True
			hud.endtitle.settext("Mission Failed")
		elif state.checkwin(dt):
			if settings.level == 3:
				scene.pushunder(scenes.final)
			sound.play("success")
			ending = True
			hud.endtitle.settext("Mission Complete")
			settings.unlocked = max(settings.unlocked, settings.level + 1)
			settings.save()
Example #31
0
def clickon(bname):
	soundname = buttons[bname].soundname
	if bname == "buildw":
		if mode == "main":
			buildwmode()
		elif mode == "buildw":
			soundname = "back"
			mainmode()
	elif bname == "buildb":
		if mode == "main":
			buildbmode()
		elif mode == "buildb":
			soundname = "back"
			mainmode()
	elif bname == "buildr":
		if mode == "main":
			buildrmode()
		elif mode == "buildr":
			soundname = "back"
			mainmode()
	elif bname.startswith("build"):
		cursor.tobuild = buttons[bname].btype
	if bname == "launch":
		if mode == "main":
			launchmode()
		elif mode == "launch":
			soundname = "back"
			mainmode()
	elif bname.startswith("launch"):
		if state.launcherror(buttons[bname].ltype):
			soundname = "error"
		else:
			state.launch(buttons[bname].ltype)
			soundname = "launch"

	if bname == "help":
		if mode == "main":
			soundname = "back"
			helpmode()
		elif mode == "help":
			mainmode()
	elif bname == "helpmission":
		labels["helppage"].settext(info.missionhelp[settings.level])
	elif bname.startswith("help"):
		labels["helppage"].settext(info.help[buttons[bname].helpname])

	if bname == "unbuild":
		if mode == "main":
			unbuildmode()
		elif mode == "unbuild":
			soundname = "back"
			mainmode()

	if bname == "unlaunch":
		if mode == "main":
			unlaunchmode()
		elif mode == "unlaunch":
			soundname = "back"
			mainmode()
	elif bname.startswith("unlaunch"):
		state.unlaunch(buttons[bname].ltype)

	if bname == "disable":
		if mode == "main":
			disablemode()
		elif mode == "disable":
			soundname = "back"
			mainmode()

	if bname == "back":
		soundname = "back"
		cursor.tobuild = None
		mainmode()
	
	if bname == "bcancel":
		soundname = "back"
		cursor.tobuild = None

	if bname == "quit":
		soundname = "back"
		quitmode()

	if bname == "abandonquit":
		soundname = "back"
		state.removesave()
		scene.top().playing = False

	if bname == "savequit":
		soundname = "back"
		state.save()
		scene.pop()
		scene.pop()

	if bname == "quitgame":
		scene.top().playing = False


	if bname.startswith("level"):
		scene.top().playing = False
		scene.top().selected = buttons[bname].n

	if bname == "selectlevel":
		if mode == "level":
			soundname = "back"
			menumode()
		else:
			levelmode()
	if bname == "options":
		if mode == "options":
			soundname = "back"
			menumode()
		else:
			optionsmode()
	if bname == "credits":
		if mode == "credits":
			soundname = "back"
			menumode()
		else:
			creditsmode()

	if bname == "soundtoggle":
		settings.sound = not settings.sound
		settings.save()
		sound.setvolume()
	if bname == "musictoggle":
		settings.music = not settings.music
		settings.save()
		sound.setvolume()
	if bname == "fullscreentoggle":
		settings.fullscreen = not settings.fullscreen
		settings.save()
	if bname == "wsizesmall":
		settings.wsize = 640, 360
		settings.save()
	if bname == "wsizemedium":
		settings.wsize = 854, 480
		settings.save()
	if bname == "wsizelarge":
		settings.wsize = 1280, 720
		settings.save()
	if bname == "wsizevlarge":
		settings.wsize = 1920, 1080
		settings.save()
	if mode == "options":
		buttons["soundtoggle"].words = "SOUND: %s" % ("ON" if settings.sound else "OFF")
		buttons["musictoggle"].words = "MUSIC: %s" % ("ON" if settings.music else "OFF")
		buttons["fullscreentoggle"].words = "FULLSCREEN: %s" % ("ON" if settings.fullscreen else "OFF")
		for sname in "small medium large vlarge".split():
			b = buttons["wsize" + sname]
			size = map(int, b.words.split()[-1].split("x"))
			b.color = (180, 255, 180) if tuple(size) == tuple(settings.wsize) else (180, 180, 180)

	sound.play(soundname)