| Directory: | source/ |
|---|---|
| File: | source/world_resource.c |
| Date: | 2023-12-18 09:27:49 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 32 | 32 | 100.0% |
| Branches: | 8 | 8 | 100.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Filename: world_resource.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 | /// @brief It finds the index of a resource type in a resource list. | ||
| 14 | /// @param resource_list The list of resources on which find the corresponding type. | ||
| 15 | /// @param type The type of resource to find. | ||
| 16 | /// @return The index of the resource type in the resource list, or list size if not found. | ||
| 17 | 38 | static unsigned int find_resource_by_type(vector_t *resource_list, unsigned int type) | |
| 18 | { | ||
| 19 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 2 taken 29 times.
|
50 | for (unsigned int i = 0; i < resource_list->size(resource_list); i++) { |
| 20 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 12 times.
|
21 | if (type == (*(resource_t *)(resource_list->at(resource_list, i))).type) { |
| 21 | 9 | return i; | |
| 22 | } | ||
| 23 | } | ||
| 24 | 29 | return resource_list->size(resource_list); | |
| 25 | } | ||
| 26 | |||
| 27 | /// @brief It finds the index of a resource in a resource list | ||
| 28 | /// @param resource_list The list of resources that we're going to be adding to. | ||
| 29 | /// @param resource The resource to find. | ||
| 30 | /// @return The index of the resource in the resource list, or list size if not found. | ||
| 31 | 24 | static unsigned int find_resource(vector_t *resource_list, resource_t *resource) | |
| 32 | { | ||
| 33 | 24 | return (find_resource_by_type(resource_list, resource->type)); | |
| 34 | } | ||
| 35 | |||
| 36 | 24 | int world_add_resource(world_t *world, resource_t *resource) | |
| 37 | { | ||
| 38 | 24 | unsigned int index = find_resource(&world->resource_list, resource); | |
| 39 | |||
| 40 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 23 times.
|
24 | if (index < world->resource_list.size(&world->resource_list)) { |
| 41 | 1 | return -1; | |
| 42 | } | ||
| 43 | 23 | log_info("Adding resource of type %d to world", resource->type); | |
| 44 | 23 | return (world->resource_list.emplace_back(&world->resource_list, resource)); | |
| 45 | } | ||
| 46 | |||
| 47 | 6 | int world_remove_resource_by_type(world_t *world, unsigned int type) | |
| 48 | { | ||
| 49 | 6 | unsigned int index = find_resource_by_type(&world->resource_list, type); | |
| 50 | |||
| 51 | 6 | log_info("Removing resource of type %d from world", type); | |
| 52 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
|
6 | if (index < world->resource_list.size(&world->resource_list)) { |
| 53 | 4 | return (world->resource_list.erase(&world->resource_list, index)); | |
| 54 | } | ||
| 55 | 2 | return -1; | |
| 56 | } | ||
| 57 | |||
| 58 | 3 | int world_remove_resource(world_t *world, resource_t *resource) | |
| 59 | { | ||
| 60 | 3 | return (world_remove_resource_by_type(world, resource->type)); | |
| 61 | } | ||
| 62 | |||
| 63 | 4 | bool world_contains_resource_by_type(world_t *world, unsigned int type) | |
| 64 | { | ||
| 65 | 4 | unsigned int index = find_resource_by_type(&world->resource_list, type); | |
| 66 | 4 | unsigned int size = world->resource_list.size(&world->resource_list); | |
| 67 | |||
| 68 | 4 | return (index < size); | |
| 69 | } | ||
| 70 | |||
| 71 | 2 | bool world_contains_resource(world_t *world, resource_t *resource) | |
| 72 | { | ||
| 73 | 2 | return (world_contains_resource_by_type(world, resource->type)); | |
| 74 | } | ||
| 75 | |||
| 76 | 4 | resource_t *world_get_resource_by_type(world_t *world, unsigned int type) | |
| 77 | { | ||
| 78 | 4 | unsigned int index = find_resource_by_type(&world->resource_list, type); | |
| 79 | |||
| 80 | 4 | return world->resource_list.at(&world->resource_list, index); | |
| 81 | } | ||
| 82 | |||
| 83 | 2 | resource_t *world_get_resource(world_t *world, resource_t *resource) | |
| 84 | { | ||
| 85 | 2 | return (world_get_resource_by_type(world, resource->type)); | |
| 86 | } | ||
| 87 |