Ken 905 Posted May 14, 2019 Share Posted May 14, 2019 M2 Download Center This is the hidden content, please Sign In or Sign Up ( Internal ) Hey everyone, it's a nice day to share something I just wrote Grid class in python language. You can use it as fast moving into the window with this or something else. Spoiler """ This module was written by Ken for metin2dev.org (Please don't try to change it.) Note: Use reset function instead of deleting the object and creating it again. (That's more easy) """ class Grid: """ Args: width (int): Grid's width. height (int): Grid's height. Attributes: grid (list): The list will hold the position empty or not information. width (int): Grid's width height (int): Grid's height """ def __init__(self, width, height): self.grid = [False] * (width * height) self.width = width self.height = height def __str__(self): output = "Grid {}x{} Information\n".format(self.width, self.height) for row in range(self.height): for col in range(self.width): output += "Status of %d: " % (row * self.width + col) output += "NotEmpty, " if self.grid[row * self.width + col] else "Empty, " output += "\n" return output def find_blank(self, width, height): """ Args: width (int): The item's width you can call width item.GetItemSize()[0] height (int): The item's height you can call width item.GetItemSize()[1] Returns: int: The return value would be an int if successful. Otherwise -1. """ if width > self.width or height > self.height: return -1 for row in range(self.height): for col in range(self.width): index = row * self.width + col if self.is_empty(index, width, height): return index return -1 def put(self, pos, width, height): """ Args: pos (int): Position of the item to put. width (int): The item's width you can call width item.GetItemSize()[0] height (int): The item's height you can call width item.GetItemSize()[1] Returns: bool: The return value. True for success, False otherwise. """ if not self.is_empty(pos, width, height): return False for row in range(height): start = pos + (row * self.width) self.grid[start] = True col = 1 while col < width: self.grid[start + col] = True col += 1 return True def clear(self, pos, width, height): """ Args: pos (int): Position of the item to put. width (int): The item's width you can call width item.GetItemSize()[0] height (int): The item's height you can call width item.GetItemSize()[1] Returns: There is nothing to return """ if pos < 0 or pos >= (self.width * self.height): return for row in range(height): start = pos + (row * self.width) self.grid[start] = True col = 1 while col < width: self.grid[start + col] = False col += 1 def is_empty(self, pos, width, height): """ Args: pos (int): Position of the item to put. width (int): The item's width you can call width item.GetItemSize()[0] height (int): The item's height you can call width item.GetItemSize()[1] Returns: bool: The return value. True for success, False otherwise. """ if pos < 0: return False row = pos // self.width if (row + height) > self.height: return False if (pos + width) > ((row * self.width) + self.width): return False for row in range(height): start = pos + (row * self.width) if self.grid[start]: return False col = 1 while col < width: if self.grid[start + col]: return False col += 1 return True def get_size(self): """ Returns: int: The return value will give you maximum capacity of grid. (width * height) """ return self.width * self.height def reset(self): """ With this function, you can reset instead of deleting it and create again. """ self.grid = [False] * (self.width * self.height) Usage: from grid import Grid import item import chat grid = Grid(width=5, height=9) item.SelectItem(11299) (width, height) = item.GetItemSize() available_position = grid.find_blank(width, height) if available_position == -1: chat.AppendChat(chat.CHAT_TYPE_INFO, "There is no available position.") return chat.AppendChat(chat.CHAT_TYPE_INFO, "Available position is %d" % (available_position)) Best Regards Ken 12 1 4 18 Do not be sorry, be better. Link to comment Share on other sites More sharing options...
Active Member Exygo 1097 Posted May 14, 2019 Active Member Share Posted May 14, 2019 Something useful in comparison with the topics made by others. https://www.youtube.com/channel/UCQ8mAeda9TWq6SsTzB53emw/videos Link to comment Share on other sites More sharing options...
Premium WeedHex 636 Posted May 14, 2019 Premium Share Posted May 14, 2019 Very useful for safety works. Link to comment Share on other sites More sharing options...
Johnny69 368 Posted September 2, 2019 Share Posted September 2, 2019 # In def Clear self.grid[start] = True # Change to: self.grid[start] = False 1 Link to comment Share on other sites More sharing options...
Recommended Posts