Directory: | source/ |
---|---|
File: | source/world_system.c |
Date: | 2023-12-18 09:27:49 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 31 | 31 | 100.0% |
Branches: | 6 | 6 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Filename: world_system.c | ||
3 | * Path: source | ||
4 | * Created Date: Sunday, January 15th 2023, 3:59:16 pm | ||
5 | * Author: osvegn | ||
6 | * | ||
7 | * Copyright (c) 2023 ECS | ||
8 | */ | ||
9 | |||
10 | #include "world.h" | ||
11 | #include "world_logger.h" | ||
12 | |||
13 | 37 | static unsigned int find_system_by_type(vector_t *system_list, unsigned int type) | |
14 | { | ||
15 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 2 taken 28 times.
|
49 | for (unsigned int i = 0; i < system_list->size(system_list); i++) { |
16 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 12 times.
|
21 | if (type == (*(system_t *)(system_list->at(system_list, i))).type) { |
17 | 9 | return i; | |
18 | } | ||
19 | } | ||
20 | 28 | return system_list->size(system_list); | |
21 | } | ||
22 | |||
23 | /// @brief It finds the index of a system in a list of systems | ||
24 | /// @param system_list The list of systems to search through. | ||
25 | /// @param system The system to find. | ||
26 | /// @return The index of the system in the system_list vector, or list size if not found. | ||
27 | 23 | static unsigned int find_system(vector_t *system_list, system_t *system) | |
28 | { | ||
29 | 23 | return find_system_by_type(system_list, system->type); | |
30 | } | ||
31 | |||
32 | 23 | int world_add_system(world_t *world, system_t *system) | |
33 | { | ||
34 | 23 | unsigned int index = find_system(&world->system_list, system); | |
35 | |||
36 | 23 | log_info("Adding system of type %d to world", system->type); | |
37 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 22 times.
|
23 | if (index < world->system_list.size(&world->system_list)) { |
38 | 1 | return -1; | |
39 | } | ||
40 | 22 | world->system_list.emplace_back(&world->system_list, system); | |
41 | 22 | return 0; | |
42 | } | ||
43 | |||
44 | 6 | int world_remove_system_by_type(world_t *world, unsigned int type) | |
45 | { | ||
46 | 6 | unsigned int index = find_system_by_type(&world->system_list, type); | |
47 | |||
48 | 6 | log_info("Removing system of type %d from world", type); | |
49 | 6 | return (world->system_list.erase(&world->system_list, index)); | |
50 | } | ||
51 | |||
52 | 3 | int world_remove_system(world_t *world, system_t *system) | |
53 | { | ||
54 | 3 | return world_remove_system_by_type(world, system->type); | |
55 | } | ||
56 | |||
57 | 4 | bool world_contains_system_by_type(world_t *world, unsigned int type) | |
58 | { | ||
59 | 4 | unsigned int index = find_system_by_type(&world->system_list, type); | |
60 | 4 | unsigned int size = world->system_list.size(&world->system_list); | |
61 | |||
62 | 4 | return (index < size); | |
63 | } | ||
64 | |||
65 | 2 | bool world_contains_system(world_t *world, system_t *system) | |
66 | { | ||
67 | 2 | return (world_contains_system_by_type(world, system->type)); | |
68 | } | ||
69 | |||
70 | 4 | system_t *world_get_system_by_type(world_t *world, unsigned int type) | |
71 | { | ||
72 | 4 | unsigned int index = find_system_by_type(&world->system_list, type); | |
73 | |||
74 | 4 | return world->system_list.at(&world->system_list, index); | |
75 | } | ||
76 | |||
77 | 2 | system_t *world_get_system(world_t *world, system_t *system) | |
78 | { | ||
79 | 2 | return (world_get_system_by_type(world, system->type)); | |
80 | } | ||
81 |