aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_warp_level.c
blob: 0954b1c2c95da9bfec64759ef21ea5722f260270 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_warp_level.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/28 20:54:28 by joelecle          #+#    #+#             */
/*   Updated: 2020/02/28 20:54:29 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <pthread.h>

static void
	ft_player_keepings(t_cub *cl)
{
	int16_t		tmp_life;
	uint8_t		tmp_has[3];
	uint16_t	tmp_ammo[3];
	int8_t		tmp_handles;

	tmp_life = cl->plist.life;
	tmp_has[0] = cl->plist.has_weapon[0];
	tmp_has[1] = cl->plist.has_weapon[1];
	tmp_has[2] = cl->plist.has_weapon[2];
	tmp_ammo[0] = cl->plist.ammo[0];
	tmp_ammo[1] = cl->plist.ammo[1];
	tmp_ammo[2] = cl->plist.ammo[2];
	tmp_handles = cl->plist.handles_weapon;
	cl->plist = ft_init_player();
	if (!cl->isdead)
		cl->plist.life = tmp_life;
	cl->plist.has_weapon[0] = tmp_has[0];
	cl->plist.has_weapon[1] = tmp_has[1];
	cl->plist.has_weapon[2] = tmp_has[2];
	cl->plist.ammo[0] = tmp_ammo[0];
	cl->plist.ammo[1] = tmp_ammo[1];
	cl->plist.ammo[2] = tmp_ammo[2];
	cl->plist.handles_weapon = tmp_handles;
}

static int8_t
	ft_reinit_some(t_cub *cl)
{
	int8_t	i;

	ft_player_keepings(cl);
	cl->f_rgb = ft_init_rgb();
	cl->c_rgb = ft_init_rgb();
	cl->rlist = ft_init_s_ray();
	ft_del_tex(cl);
	cl->walltexgood = 0;
	ft_del_map(&cl->mlist);
	ft_del_sprites_lists(cl);
	if (ft_init_sprites(&cl->sprites) < 0)
		return (-1);
	if (ft_init_map(&cl->mlist) < 0)
		return (-1);
	i = 0;
	while (i < 5)
	{
		cl->key_input[i] = -1;
		i++;
	}
	return (0);
}

static void
	ft_treat_music(uint8_t isoldmus, char *tmp_mup, t_cub *cl)
{
	if (isoldmus && !cl->mlist.ismusic)
	{
		pthread_cancel(cl->mtid);
		pthread_join(cl->mtid, NULL);
		system(FT_SND_TERM_CMD);
		cl->isoldmus = 0;
	}
	else if (isoldmus && cl->mlist.ismusic
		&& ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1))
	{
		pthread_cancel(cl->mtid);
		pthread_join(cl->mtid, NULL);
		system(FT_SND_TERM_CMD);
		ft_enable_music(cl);
	}
	else if (isoldmus && cl->mlist.ismusic
		&& !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1))
		return ;
	else if (!isoldmus && cl->mlist.ismusic)
		ft_enable_music(cl);
}

int8_t
	ft_warp_level(char *path, t_cub *cl)
{
	char	*next_path;
	char	*tmp_mup;
	uint8_t	isoldmus;

	if (!(next_path = (char *)malloc((ft_strlen(path) + 1) * sizeof(char))))
		return (-1);
	ft_sprintf(next_path, "%s", path);
	if ((isoldmus = cl->mlist.ismusic))
		tmp_mup = ft_strdup(cl->mlist.music_path);
	if (ft_reinit_some(cl) < 0)
		return (-1);
	ft_parse_map(next_path, cl);
	ft_treat_music(isoldmus, tmp_mup, cl);
	cl->isdead = (cl->isdead == 1) ? (0) : (cl->isdead);
	if (isoldmus)
		ft_memdel((void*)&tmp_mup);
	ft_wall_tex_init(cl);
	ft_weap_tex_init(cl);
	ft_num_tex_init(cl);
	ft_memdel((void*)&next_path);
	return (0);
}