saving and loading map parser prototype #4
							
								
								
									
										35
									
								
								prototypes/saves_prototypes/README
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								prototypes/saves_prototypes/README
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
# Prototypes Saves files
 | 
			
		||||
 | 
			
		||||
This prototype is aiming at defining a structure for saving "map" files.
 | 
			
		||||
 | 
			
		||||
The objective is to have it quite modular so that if we want to add more level we can easily do so.
 | 
			
		||||
 | 
			
		||||
Another objective is that theses files are as small as possible without missing information.
 | 
			
		||||
 | 
			
		||||
## What needs to be saved
 | 
			
		||||
 | 
			
		||||
We need to have the shape of the map itself and the shape and number of each pieces it is a minimum
 | 
			
		||||
 | 
			
		||||
I also want to define an header (and maybe a footer) to the file so that it is easier to recover if any corruption occure to a drive
 | 
			
		||||
(we all been trough that)
 | 
			
		||||
 | 
			
		||||
## Method
 | 
			
		||||
 | 
			
		||||
I would like to save a file as byte so I need to have a bitewise parser. This is the objective of this prototype
 | 
			
		||||
 | 
			
		||||
## Structure
 | 
			
		||||
 | 
			
		||||
The map file should have the following structure:
 | 
			
		||||
- The file should be named <map_name>.shmap (where shmap stand for shape map)
 | 
			
		||||
- The file should start with the ascii characters S, M then S (0x534D53)
 | 
			
		||||
- The file should end with the ascii characters S, M then E (0x534D45)
 | 
			
		||||
- First we define the map shape
 | 
			
		||||
    1) the map should always be a square. the lenght of this square will be the first octet after the header.
 | 
			
		||||
    2) next we have s x s (where s is the size of the square) bits (1/0) where
 | 
			
		||||
        - 0 represent an empty cell (where we can place a shape)
 | 
			
		||||
        - 1 represent a filled cell (where we can't place a shape)
 | 
			
		||||
- Next we define the amount of shape for this level with a number on one octet
 | 
			
		||||
- Next we define each shapes with the same method defined previously for a map
 | 
			
		||||
 | 
			
		||||
With all that we should have all that is needed for a level to work... further information could be added later this is just a prototype atm
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										56
									
								
								prototypes/saves_prototypes/parser.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								prototypes/saves_prototypes/parser.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,56 @@
 | 
			
		||||
class MapNotSquareException(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
class  PieceNotSquareException(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
class save_Parser:
 | 
			
		||||
    def __init__(self, filename:str ):
 | 
			
		||||
        self.filename = filename
 | 
			
		||||
        self.map_shape = [[0]]
 | 
			
		||||
        self.pieces = list()
 | 
			
		||||
 | 
			
		||||
    def define_map(self, map_shape):
 | 
			
		||||
        size = len(map_shape)
 | 
			
		||||
        for row in map_shape:
 | 
			
		||||
            if size != len(row):
 | 
			
		||||
                raise MapNotSquareException
 | 
			
		||||
        self.map_shape = map_shape
 | 
			
		||||
 | 
			
		||||
    def add_piece(self, piece_shape):
 | 
			
		||||
        size = len(piece_shape)
 | 
			
		||||
        for row in piece_shape:
 | 
			
		||||
            if size != len(row):
 | 
			
		||||
                raise PieceNotSquareException
 | 
			
		||||
        self.pieces.append(piece_shape)
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return str(self.map_shape)
 | 
			
		||||
 | 
			
		||||
    def shape_to_bytes(self, shape):
 | 
			
		||||
        bytes_ammount = len(shape) // 8
 | 
			
		||||
        bytes_trash = len(shape) % 8
 | 
			
		||||
        tray = 0
 | 
			
		||||
 | 
			
		||||
    def load(self):
 | 
			
		||||
        """
 | 
			
		||||
        load the file and prepare to parse informations
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
    def save(self):
 | 
			
		||||
        """
 | 
			
		||||
        save parsed info to the file
 | 
			
		||||
        """
 | 
			
		||||
        save_data = b''
 | 
			
		||||
        save_data += b'SMS'
 | 
			
		||||
        save_data += bytes(len(map_shape))
 | 
			
		||||
        save_data += shape_to_bytes(self.map_shape)
 | 
			
		||||
        save_data += bytes()
 | 
			
		||||
        save_data += b'SME'
 | 
			
		||||
        with open(self.filename, mode='bw') as file:
 | 
			
		||||
            file.write(save_data)
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    p = save_Parser('test.map')
 | 
			
		||||
    p.define_map([[0,1],[0,1]])
 | 
			
		||||
    p.save()
 | 
			
		||||
							
								
								
									
										1
									
								
								prototypes/saves_prototypes/test.map
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								prototypes/saves_prototypes/test.map
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
SMSSME
 | 
			
		||||
		Reference in New Issue
	
	Block a user