Jump to content

Grid Class Python


Recommended Posts

  • Bronze

M2 Download Center

This is the hidden content, please
( 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

  • Metin2 Dev 10
  • Smile Tear 1
  • Good 4
  • Love 18

Do not be sorry, be better.

Link to comment
Share on other sites

  • 3 months later...

Announcements



  • Similar Content

  • Similar Content

  • Similar Content

  • Tags

  • Activity

    1. 1

      Problem with sidebar by vegas

    2. 4

      [Discussion] Rain - The First Person that leaked the M2 Files

    3. 4

      [Discussion] Rain - The First Person that leaked the M2 Files

    4. 0

      [Client] All tabs opening after quest/npc talk or even shop // PORTS

    5. 0

      Arrow does not hide at warp

    6. 1

      Problem with sidebar by vegas

    7. 58

      Discord Rich Presence

    8. 145

      Full Costume Mount System

  • Recently Browsing

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.