def complete_dot(): line_num = util.get_cursor_line_num() line = util.get_line(line_num) col_num = util.get_cursor_col_num() token_chain = script.get_token_chain(line, line_num, col_num - 2) if token_chain: last_token = token_chain[-1] last_token_type = type(last_token) # Complete statically accessible items in classes. if last_token_type is script.ClassToken: # Class is defined by Godot. if last_token.line == -1: c = classes.get_class(last_token.name) # Manually add an entry for 'new()' for core types. if not c.is_built_in(): new_func = classes.GodotMethod("new", c.get_name(), [], None) append_completion(build_completion(new_func, c.get_name())) _add_class_items(c, _CONSTANTS) else: for decl in script.iter_static_decls(last_token.line, script.ANY_DECLS): append_completion(build_completion(decl)) return # Treat 'self' like we're accessing script variables, but exclude globals. if last_token_type is script.VariableToken and last_token.name == "self": complete_script(include_globals=False) return # Complete enum values. if last_token_type is script.EnumToken: values = script.get_enum_values(last_token.line) if values: for value in values: append_completion(build_completion(value)) return c_name = None flags = None if len(token_chain ) == 1 and last_token_type is script.SuperAccessorToken: c_name = script.get_extended_class(line_num) flags = _METHODS elif last_token_type is script.MethodToken: c_name = last_token.returns elif last_token_type is script.VariableToken: c_name = last_token.type if c_name: _add_class_items(classes.get_class(c_name), flags)
def complete_method_signatures(): c = classes.get_class(script.get_extended_class()) while c: for method in c.iter_methods(): d = build_completion(method, c.get_name()) if not d: continue mapped_args = map(lambda a: a.name, method.args) d["word"] = "{}({}):".format(method.name, ", ".join(mapped_args)) append_completion(d) c = c.get_inherited_class()
def add_race_class(): race_list = sorted([ race[0].replace("_", " ") for race in inspect.getmembers(races, inspect.isclass) if not race[1].__subclasses__() ]) class_list = sorted([ class_[0].replace("_", " ") for class_ in inspect.getmembers(classes, inspect.isclass) if issubclass(class_[1], classes.Class) and not class_[1].__subclasses__() ]) class_choice = class_list[simple_choice(class_list)] classes.get_class(char, class_choice).add_class_features(char) race_choice = race_list[simple_choice(race_list)] races.get_race(char, race_choice).add_race_modifiers(char) char.update() return char
def _args_to_vars(func_decl): vars = [] method = None extended_class = classes.get_class(get_extended_class(func_decl.line)) if extended_class: method = extended_class.get_method(func_decl.name) for i, arg in enumerate(func_decl.args): arg_type = None if method: method_arg = method.args[i] if method_arg: arg_type = method_arg.type vars.append(VarDecl(func_decl.line, arg, arg_type)) return vars
def complete_script(include_globals): # Complete user decls. down_search_start = 1 for decl in script.iter_decls(util.get_cursor_line_num(), direction=-1): decl_type = type(decl) if decl_type == script.ClassDecl: down_search_start = decl.line elif decl_type != script.FuncDecl: append_completion(build_completion(decl)) for decl in script.iter_decls(down_search_start, direction=1): append_completion(build_completion(decl)) # Complete extended class. c = classes.get_class(script.get_extended_class()) _add_class_items(c) # Complete global scope. if include_globals: _add_class_items(classes.get_global_scope())
def game(width, height, fullscreen, lvl_name): FPS = 120 WIDTH = width HEIGHT = height running = True drawing = False newturnanimation = False screen_3 = pygame.display.set_mode((width, height), fullscreen) clock = pygame.time.Clock() board = classes.board board.clear() board.set_level(lvl_name) board.resize(WIDTH, HEIGHT) DELTA = board.get_delta() hud = classes.hud bu = False players = [player_1, player_2, player_3, player_4] = board.get_players() turns = { player_1: player_2, player_2: player_3, player_3: player_4, player_4: player_1 } turned = players[board.get_first_turned()] next_turned = turns[turned] while not next_turned.active: next_turned = turns[next_turned] turned.update() button_town = classes.Button((WIDTH - 275, 30), 'Build Town') button_gun = classes.Button((WIDTH - 275, 90), 'Set Gun') button_block = classes.Button((WIDTH - 275, 150), 'Set Block') button_destroy = classes.Button((WIDTH - 275, 210), 'Destroy') button_end_turn = classes.Button((WIDTH - 275, HEIGHT - 80), 'End Turn') buttons = [ button_town, button_gun, button_block, button_destroy, button_end_turn ] players = [player_1, player_2, player_3, player_4] pygame.mouse.set_visible(0) DELTA = board.resize(WIDTH, HEIGHT) classes.resize(DELTA, button_town, button_gun, button_block, button_destroy, button_end_turn) hud.resize() hud.set_turned_text(str(board.get_first_turned() + 1), turned) x, y, a, b = 0, 0, 0, 0 turned_gun = None ch = 0 choosen = '.' while running: ch += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: exit(0) if event.type == pygame.MOUSEBUTTONDOWN: sprites.click.play() x, y = event.pos if 0 <= x < DELTA[0] * len( board.board[0]) and 0 <= y < DELTA[1] * len( board.board) and turned.action != 0: x1, y1 = board.get_click(event.pos) if board.board[y1][x1] is not None: if board.board[y1][x1].player == turned: if board.board[y1][ x1].built or choosen == 'Destroy': if choosen == 'Destroy': board.destroy_object(y1, x1) turned.use_action() hud.update_actions() for button in buttons: button.unpress() choosen = '.' else: if board.board[y1][ x1].__class__ == classes.Gun: turned_gun = board.board[y1][x1] else: board.board[y1][x1].build() turned.use_action() hud.update_actions() elif board.board[y1][x1].__class__ == classes.Gun: if turned_gun is not None and turned_gun.can_destroy( y1, x1): board.attack(y1, x1, turned) turned_gun = None turned.use_action() hud.update_actions() elif board.board[y1][x1].__class__ == classes.Block: if turned_gun is not None and turned_gun.can_destroy( y1, x1): board.attack(y1, x1, turned) turned_gun = None turned.use_action() hud.update_actions() elif board.board[y1][x1].__class__ == classes.Town: if turned_gun is not None and turned_gun.can_destroy( y1, x1): board.attack(y1, x1, turned) turned_gun = None turned.use_action() hud.update_actions() else: if choosen in 'GunTownBlock' and board.possible( y1, x1, turned): board.board[y1][x1] = classes.get_class( choosen, (y1, x1), turned) turned.add_own(board.board[y1][x1]) turned.update_max_actions() turned.use_action() hud.update_actions() board.board[y1][x1].build() choosen = '.' else: find = False for button in buttons: button.unpress() if button.crossing((x, y)): choosen = button.press() if choosen != 'Destroy' and choosen != 'End Turn': choosen = choosen.split()[1] find = True if not find: choosen = '.' if choosen == 'End Turn': newturnanimation = True sprites.newturn.play() choosen = '.' button_end_turn.unpress() turned.update(turn=False) turned = next_turned next_turned = turns[next_turned] while not next_turned.active: next_turned = turns[next_turned] turned.update() hud.set_turned_text(str(players.index(turned) + 1), turned) if event.type == pygame.MOUSEMOTION: x, y = event.pos for button in buttons: button.crossing(event.pos) if 0 <= x < DELTA[0] * len( board.board[0]) and 0 <= y < DELTA[1] * len( board.board): b, a = board.get_click(event.pos) else: a, b = -1, -1 if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: if not esc.menu(screen_3, board): running = False if newturnanimation: count += 1 if count == 60: newturnanimation = False if not newturnanimation: count = 0 if not running: break screen_3.fill((0, 0, 0)) hud.update_hud(screen_3) if (ch // 80) % 2 == 0: board.update(screen_3, True) else: board.update(screen_3) for button in buttons: button.render(screen_3) hud.draw_info((a, b), screen_3, board.board[a][b]) if newturnanimation: screen_3.blit(sprites.newturnanim[count // 6], (0, 0)) if pygame.mouse.get_focused(): screen_3.blit(sprites.mouse.image, (x, y)) k = 0 for c in players: if c.active: k += 1 if k == 1: for c in players: if c.active: fun.win(c, screen_3, WIDTH, HEIGHT) bu = True if bu: break pygame.display.flip() clock.tick(FPS)
def link(pool,replace_objects_only = False,preserve_input_pool=True): """ Reverse operation to unlink. """ used = [] t = time.time() # First a shallow copy pool = pool.copy() # first replace all occurences of object dictioniaries with their respective objects # since all objects appear only once, this is a safe. for k,v in pool.iteritems(): #at this point we can make copies of objects, since they are uniquely referenced here if preserve_input_pool: if type(v) is dict or type(v) is np.ndarray: pool[k]=v.copy() elif type(v) is list: pool[k]=list(v) cls=get_class(k) if cls is not None: logger.debug('Found %s' % cls) # check if the value is a dict. if type(v) is dict: #try: logger.debug('Try calling class "_from_dict()" method') pool[k] = cls._from_dict(pool[k]) #except: # logger.debug('Attempt failed. Invoking class instance without arguments') # inst = cls() # inst.__dict__ = pool[k] if replace_objects_only : return pool calls = [] keys = pool.keys() def _unpool(obj): calls.append(None) #for k,v in pool.iteritems(): #print obj if str(obj) in keys: # replace key by object. # as these keys ALWAYS refer to objects and not to other keys, no further checking is needed obj = pool[obj] if type(obj) in _dict_like and id(obj) not in used: used.append(id(obj)) for k,v in obj.iteritems(): obj[k]= _unpool(v) elif type(obj) in _list_like and id(obj) not in used: used.append(id(obj)) for k,v in enumerate(obj): obj[k] = _unpool(v) elif type(obj) in _ptypy and id(obj) not in used: used.append(id(obj)) for k,v in obj.__dict__.iteritems(): obj.__dict__[k]= _unpool(v) return obj else: return obj # Reestablish references, start from root out = _unpool(pool['root']) t = time.time()-t logger.info('Converted flat pool to cross-linked structure in %.3f sec.\n\ %d recursions were needed,\n\ %d objects were used\n\ %d objects are mutable.' % (t,len(calls),len(pool)-1,len(used))) return out
def get_token_chain(line, line_num, start_col): i = start_col paren_count = 0 is_method = False end_col = None # Find the name of the token, skipping over any text in parentheses. while True: i -= 1 char = line[i] if char == ")": is_method = True paren_count += 1 elif char == "(": paren_count -= 1 if paren_count == 0: start_col = i continue if paren_count <= 0 and not (char.isalnum() or char == "_"): end_col = i + 1 break elif i == 0: end_col = i break name = line[end_col:start_col] if not name: if util.get_syn_attr(col_num=i + 1) == "gdString": return [VariableToken(None, "String")] else: # return [SuperAccessorToken()] chain = None if line[i] == ".": chain = get_token_chain(line, line_num, i) if not chain: return # If this is the beginning of the chain, search global scope. # TODO: search user funcs and vars with type annotations. if (not chain or type(chain[-1]) is SuperAccessorToken or chain[-1].name == "self") and is_method: extended_class = classes.get_class(get_extended_class(line_num)) if extended_class: method = extended_class.get_method(name, search_global=True) if method: return [ MethodToken(name, method.returns, method.args, method.qualifiers) ] decl = find_decl(line_num, name, FUNC_DECLS) if decl: return [MethodToken(name, None, decl.args, None)] elif not chain or chain[-1].name == "self": if not chain and name == "self": return [VariableToken(name, None)] decl = find_decl(line_num, name, ENUM_DECLS | CLASS_DECLS) if decl: decl_type = type(decl) if decl_type is EnumDecl: return [EnumToken(name, decl.line)] elif decl_type is ClassDecl: return [ClassToken(name, decl.line)] else: extended_class = classes.get_class(get_extended_class(line_num)) if extended_class: member = extended_class.get_member(name, search_global=True) if member: return [VariableToken(name, member.type)] c = classes.get_class(name) if c: return [ClassToken(name, -1)] # Not the beginning of a chain, so get the type of the previous token. else: prev_token = chain[-1] prev_token_type = type(prev_token) prev_class = None if prev_token_type is VariableToken: prev_class = classes.get_class(prev_token.type) elif prev_token_type is MethodToken: prev_class = classes.get_class(prev_token.returns) elif prev_token_type is ClassToken: if is_method and name == "new": if not (prev_token.line == -1 and classes.get_class(prev_token.name).is_built_in()): chain.append(MethodToken(name, prev_token.name, None, None)) return chain for decl in iter_static_decls(prev_token.line, ANY_DECLS): if decl.name == name: decl_type = type(decl) if decl_type is ClassDecl: chain.append(ClassToken(name, decl.line)) return chain elif decl_type is FuncDecl and decl.static: chain.append(MethodToken(name, None, decl.args, None)) return chain return if not prev_class: return if is_method: method = prev_class.get_method(name) if method: chain.append( MethodToken(name, method.returns, method.args, method.qualifiers)) return chain else: member = prev_class.get_member(name) if member: chain.append(VariableToken(name, member.type)) return chain