-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.c
99 lines (91 loc) · 2.55 KB
/
world.c
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* world.c :+: :+: */
/* +:+ */
/* By: jandre-d <[email protected]> +#+ */
/* +#+ */
/* Created: 2019/02/25 13:12:49 by jandre-d #+# #+# */
/* Updated: 2019/03/19 16:57:55 by jandre-d ######## odam.nl */
/* */
/* ************************************************************************** */
#include "world.h"
#include "object.h"
#include "input.h"
#include <unistd.h>
#include <fcntl.h>
static void world_list_add(t_object_elem *obj, t_world *world)
{
t_object_elem *use_me;
if (world->object_list == NULL)
world->object_list = obj;
else
{
use_me = world->object_list;
while (use_me->next != NULL)
use_me = use_me->next;
use_me->next = obj;
}
world->selected_obj = obj;
}
int world_load_object(char *filename, t_world *world)
{
t_input *input;
t_object_elem *obj;
int32_t fd;
fd = open(filename, O_RDONLY);
input = input_get(fd);
if (fd != -1)
close(fd);
if (input == NULL)
{
ft_putstr("map \"");
ft_putstr(filename);
ft_putstr("\" is not formatted correctly\n");
return (-1);
}
obj = object_elem_from_input(input);
input_free(input);
if (obj == NULL)
return (-1);
world_list_add(obj, world);
return (0);
}
void wold_select_next_object(t_world *world)
{
if (world->selected_obj->next == NULL)
{
world->selected_obj = world->object_list;
}
else
{
world->selected_obj = world->selected_obj->next;
}
}
int world_remove_object(char *filename, t_world *world)
{
t_object_elem *travers_me;
t_object_elem *free_me;
if (world->object_list != NULL && filename == world->object_list->filename)
{
free_me = world->object_list;
world->object_list = world->object_list->next;
object_free(free_me);
}
else
{
travers_me = world->object_list;
while (travers_me != NULL && travers_me->next != NULL)
{
if (filename == travers_me->next->filename)
{
free_me = travers_me->next;
travers_me->next = travers_me->next->next;
object_free(free_me);
return (0);
}
travers_me = travers_me->next;
}
}
return (0);
}