eolib 0.6.1
A core C library for writing applications related to Endless Online
Loading...
Searching...
No Matches
sequencer.c
Go to the documentation of this file.
1#include "eolib/sequencer.h"
2#include "eolib/data.h"
3#include "eolib/rng.h"
4
6{
7 EoSequencer seq;
8 seq.start = start;
9 seq.counter = 0;
10 return seq;
11}
12
14{
15 if (!sequencer)
16 {
17 return EO_NULL_PTR;
18 }
19
20 sequencer->counter = (sequencer->counter + 1) % 10;
21 if (out_value)
22 {
23 *out_value = sequencer->start + sequencer->counter;
24 }
25
26 return EO_SUCCESS;
27}
28
33
35{
36 /* Protocol formula: start = s1 * 7 + s2 - 13
37 * Inverse of the eo_sequence_init_bytes encoding (seq1*7 + seq2 == start + 13). */
38 return s1 * 7 + s2 - 13;
39}
40
42{
43 return s1 - s2;
44}
45
47{
48 int32_t seq1_min;
49 int32_t seq1_max;
50 int32_t range;
51 int32_t seq1;
52 int32_t seq2;
53
54 if (!out_bytes)
55 {
56 return EO_NULL_PTR;
57 }
58
59 /* Encode start as: start + 13 = seq1 * 7 + seq2
60 * where seq1 and seq2 are both valid single EO bytes (0..EO_CHAR_MAX-1).
61 * Pick a random seq1 in the valid range and derive seq2. */
62 seq1_min = (start - (EO_CHAR_MAX - 1) + 13 + 6) / 7;
63 if (seq1_min < 0)
64 {
65 seq1_min = 0;
66 }
67
68 seq1_max = (start + 13) / 7;
69 if (seq1_max > (EO_CHAR_MAX - 1))
70 {
71 seq1_max = EO_CHAR_MAX - 1;
72 }
73
74 if (seq1_max < seq1_min)
75 {
77 }
78
79 range = seq1_max - seq1_min + 1;
80 seq1 = (range > 0) ? eo_rand_range(seq1_min, seq1_max) : seq1_min;
81 seq2 = start - seq1 * 7 + 13;
82
83 if (seq2 < 0 || seq2 > (EO_CHAR_MAX - 1))
84 {
86 }
87
88 out_bytes[0] = (uint8_t)seq1;
89 out_bytes[1] = (uint8_t)seq2;
90 return EO_SUCCESS;
91}
92
94{
95 int32_t low;
96 int32_t high;
97 int32_t range;
98 int32_t seq1;
99 int32_t seq2;
100
101 if (!out_bytes)
102 {
103 return EO_NULL_PTR;
104 }
105
106 low = start;
107 if (low < 0)
108 {
109 low = 0;
110 }
111
112 high = start + (EO_CHAR_MAX - 1);
113 if (high > (EO_CHAR_MAX - 1))
114 {
115 high = EO_CHAR_MAX - 1;
116 }
117
118 if (high < low)
119 {
121 }
122
123 range = high - low + 1;
124 seq1 = (range > 0) ? eo_rand_range(low, high) : low;
125 seq2 = seq1 - start;
126
127 if (seq2 < 0 || seq2 > (EO_CHAR_MAX - 1))
128 {
130 }
131
132 out_bytes[0] = (uint8_t)seq1;
133 out_bytes[1] = (uint8_t)seq2;
134 return EO_SUCCESS;
135}
#define EO_CHAR_MAX
Definition data.h:10
EoResult
Definition result.h:12
@ EO_NULL_PTR
Definition result.h:22
@ EO_INVALID_SEQUENCE_RANGE
Definition result.h:72
@ EO_SEQUENCE_OUT_OF_RANGE
Definition result.h:80
@ EO_SUCCESS
Definition result.h:14
uint32_t eo_rand_range(uint32_t min, uint32_t max)
Definition rng.c:38
EoResult eo_sequence_ping_bytes(int32_t start, uint8_t *out_bytes)
Definition sequencer.c:93
EoResult eo_sequencer_next(EoSequencer *sequencer, int32_t *out_value)
Definition sequencer.c:13
int32_t eo_generate_sequence_start(void)
Definition sequencer.c:29
int32_t eo_sequence_start_from_ping(int32_t s1, int32_t s2)
Definition sequencer.c:41
EoResult eo_sequence_init_bytes(int32_t start, uint8_t *out_bytes)
Definition sequencer.c:46
int32_t eo_sequence_start_from_init(int32_t s1, int32_t s2)
Definition sequencer.c:34
EoSequencer eo_sequencer_init(int32_t start)
Definition sequencer.c:5
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78
int32_t start
Definition sequencer.h:10
int32_t counter
Definition sequencer.h:11