Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Filename: component_speed.c |
3 |
|
|
* Path: sources/Component |
4 |
|
|
* Created Date: Sunday, September 17th 2023, 7:35:03 am |
5 |
|
|
* Author: osvegn |
6 |
|
|
* |
7 |
|
|
* Copyright (c) 2023 our_rpg |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "json.h" |
11 |
|
|
#include "components.h" |
12 |
|
|
#include "world_logger.h" |
13 |
|
|
#include <stdlib.h> |
14 |
|
|
|
15 |
|
|
static int component_is_speed(const component_t *component) |
16 |
|
|
{ |
17 |
|
✗ |
if (component->type == C_SPEED) |
18 |
|
|
return 1; |
19 |
|
✗ |
log_error("Component is not speed."); |
20 |
|
|
return 0; |
21 |
|
|
} |
22 |
|
|
|
23 |
|
✗ |
int component_speed_constructor(component_t *component, void *data) |
24 |
|
|
{ |
25 |
|
|
int rvalue = 0; |
26 |
|
|
|
27 |
|
✗ |
component->type = C_SPEED; |
28 |
|
✗ |
component->data = malloc(sizeof(int)); |
29 |
|
✗ |
if (!component->data) { |
30 |
|
✗ |
log_fatal("Could not allocate memory for speed component."); |
31 |
|
✗ |
return -1; |
32 |
|
|
} |
33 |
|
✗ |
if (!data) { |
34 |
|
|
data = &(int){5}; |
35 |
|
|
} |
36 |
|
✗ |
rvalue = component_speed_set(component, data); |
37 |
|
✗ |
log_info("Speed component created."); |
38 |
|
✗ |
return rvalue; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
✗ |
int component_speed_constructor_from_json(component_t *component, void *data) |
42 |
|
|
{ |
43 |
|
✗ |
json_object *obj = json_tokener_parse(data); |
44 |
|
|
int speed = 0; |
45 |
|
|
|
46 |
|
✗ |
speed = json_object_get_int(obj); |
47 |
|
✗ |
return component_speed_constructor(component, &speed); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
✗ |
int component_speed_set(component_t *component, void *data) |
51 |
|
|
{ |
52 |
|
✗ |
if (!component_is_speed(component) || !component->data || !data) |
53 |
|
✗ |
return -1; |
54 |
|
✗ |
memcpy(component->data, data, sizeof(int)); |
55 |
|
✗ |
return 0; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
✗ |
void *component_speed_get(const component_t *component) |
59 |
|
|
{ |
60 |
|
|
if (!component_is_speed(component)) |
61 |
|
✗ |
return 0; |
62 |
|
✗ |
return component->data; |
63 |
|
|
} |
64 |
|
|
|