Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Filename: /workspaces/our_rpg/lib/ECS/source/world_resource.c |
3 |
|
|
* Path: /workspaces/our_rpg/lib/ECS/source |
4 |
|
|
* Created Date: Sunday, January 15th 2023, 3:59:16 pm |
5 |
|
|
* Author: osvegn |
6 |
|
|
* |
7 |
|
|
* Copyright (c) 2023 our_rpg |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "world.h" |
11 |
|
|
|
12 |
|
|
/// @brief It finds the index of a resource in a resource list |
13 |
|
|
/// @param resource_list The list of resources that we're going to be adding to. |
14 |
|
|
/// @param resource The resource to find. |
15 |
|
|
/// @return The index of the resource in the resource list, or list size if not found. |
16 |
|
✗ |
static unsigned int find_resource(vector_t *resource_list, resource_t *resource) |
17 |
|
|
{ |
18 |
|
✗ |
for (unsigned int i = 0; i < resource_list->size(resource_list); i++) { |
19 |
|
✗ |
if (resource->type == (*(resource_t *)(resource_list->at(resource_list, i))).type) { |
20 |
|
✗ |
return i; |
21 |
|
|
} |
22 |
|
|
} |
23 |
|
✗ |
return resource_list->size(resource_list); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
/// @brief It finds the index of a resource type in a resource list. |
27 |
|
|
/// @param resource_list The list of resources on which find the corresponding type. |
28 |
|
|
/// @param type The type of resource to find. |
29 |
|
|
/// @return The index of the resource type in the resource list, or list size if not found. |
30 |
|
✗ |
static unsigned int find_resource_by_type(vector_t *resource_list, unsigned int type) |
31 |
|
|
{ |
32 |
|
✗ |
for (unsigned int i = 0; i < resource_list->size(resource_list); i++) { |
33 |
|
✗ |
if (type == (*(resource_t *)(resource_list->at(resource_list, i))).type) { |
34 |
|
✗ |
return i; |
35 |
|
|
} |
36 |
|
|
} |
37 |
|
✗ |
return resource_list->size(resource_list); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
✗ |
int world_add_resource(world_t *world, resource_t *resource) |
41 |
|
|
{ |
42 |
|
✗ |
unsigned int index = find_resource(&world->resource_list, resource); |
43 |
|
|
|
44 |
|
✗ |
if (index < world->resource_list.size(&world->resource_list)) { |
45 |
|
|
return -1; |
46 |
|
|
} |
47 |
|
✗ |
world->resource_list.emplace_back(&world->resource_list, resource); |
48 |
|
✗ |
return 0; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
✗ |
int world_remove_resource(world_t *world, resource_t *resource) |
52 |
|
|
{ |
53 |
|
✗ |
unsigned int index = find_resource(&world->resource_list, resource); |
54 |
|
|
|
55 |
|
✗ |
if (index < world->resource_list.size(&world->resource_list)) { |
56 |
|
✗ |
world->resource_list.erase(&world->resource_list, index); |
57 |
|
✗ |
return 0; |
58 |
|
|
} |
59 |
|
|
return -1; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
✗ |
int world_remove_resource_by_type(world_t *world, unsigned int type) |
63 |
|
|
{ |
64 |
|
✗ |
unsigned int index = find_resource_by_type(&world->resource_list, type); |
65 |
|
|
|
66 |
|
✗ |
if (index < world->resource_list.size(&world->resource_list)) { |
67 |
|
✗ |
world->resource_list.erase(&world->resource_list, index); |
68 |
|
✗ |
return 0; |
69 |
|
|
} |
70 |
|
|
return -1; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
✗ |
bool world_contains_resource(world_t *world, resource_t *resource) |
74 |
|
|
{ |
75 |
|
✗ |
unsigned int index = find_resource(&world->resource_list, resource); |
76 |
|
|
|
77 |
|
✗ |
if (index < world->resource_list.size(&world->resource_list)) { |
78 |
|
✗ |
return true; |
79 |
|
|
} |
80 |
|
|
return false; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
✗ |
bool world_contains_resource_by_type(world_t *world, unsigned int type) |
84 |
|
|
{ |
85 |
|
✗ |
unsigned int index = find_resource_by_type(&world->resource_list, type); |
86 |
|
|
|
87 |
|
✗ |
if (index < world->resource_list.size(&world->resource_list)) { |
88 |
|
✗ |
return true; |
89 |
|
|
} |
90 |
|
|
return false; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
✗ |
resource_t *world_get_resource(world_t *world, resource_t *resource) |
94 |
|
|
{ |
95 |
|
✗ |
unsigned int index = find_resource(&world->resource_list, resource); |
96 |
|
|
|
97 |
|
✗ |
if (index < world->resource_list.size(&world->resource_list)) { |
98 |
|
✗ |
return world->resource_list.at(&world->resource_list, index); |
99 |
|
|
} |
100 |
|
|
return 0; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
✗ |
resource_t *world_get_resource_by_type(world_t *world, unsigned int type) |
104 |
|
|
{ |
105 |
|
✗ |
unsigned int index = find_resource_by_type(&world->resource_list, type); |
106 |
|
|
|
107 |
|
✗ |
if (index < world->resource_list.size(&world->resource_list)) { |
108 |
|
✗ |
return world->resource_list.at(&world->resource_list, index); |
109 |
|
|
} |
110 |
|
|
return 0; |
111 |
|
|
} |
112 |
|
|
|