aboutsummaryrefslogtreecommitdiff
path: root/bsq/terrain.c
blob: b2ef10ac5145ecf3b358724bc8a3fedc80f8b18b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   terrain.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <charles.cabergs@gmail.com>       +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/07/23 08:56:45 by cacharle          #+#    #+#             */
/*   Updated: 2019/07/24 12:06:11 by samzur           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>
#include <unistd.h>
#include "include.h"

/*
** Free the terrain
*/

void	destroy_terrain(t_terrain *terrain)
{
	int y;

	if (terrain == NULL)
		return ;
	if (terrain->matrix == NULL)
		return ;
	y = 0;
	while (y < terrain->rows)
	{
		if (terrain->matrix[y] == NULL)
			break ;
		free(terrain->matrix[y++]);
	}
	free(terrain->matrix);
}

void	print_terrain(t_parsed_terrain *pterrain)
{
	int		terrain_len;
	char	*terrain_str;

	terrain_str = (char*)malloc(sizeof(char) * pterrain->terrain->rows
						* (pterrain->terrain->columns + 1));
	terrain_len = terrain_to_string(pterrain, terrain_str);
	write(STDOUT_FILENO, terrain_str, terrain_len);
	free(terrain_str);
}

int		terrain_to_string(t_parsed_terrain *pterrain, char *str)
{
	int	i;
	int j;
	int	str_index;

	str_index = 0;
	i = 0;
	while (i < pterrain->terrain->rows)
	{
		j = 0;
		while (j < pterrain->terrain->columns)
		{
			if (pterrain->terrain->matrix[i][j] >= EMPTY)
				str[str_index] = pterrain->empty;
			else if (pterrain->terrain->matrix[i][j] == OBSTACLE)
				str[str_index] = pterrain->obstacle;
			else if (pterrain->terrain->matrix[i][j] == FILLED)
				str[str_index] = pterrain->filled;
			j++;
			str_index++;
		}
		i++;
		str[str_index] = '\n';
		str_index++;
	}
	return (str_index);
}