eolib 0.5.0
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
13EoResult eo_sequencer_next(EoSequencer *sequencer, int32_t *out_value)
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
30{
31 return eo_rand_range(0, EO_CHAR_MAX - 9);
32}
33
34int32_t eo_sequence_start_from_init(int32_t s1, int32_t s2)
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
41int32_t eo_sequence_start_from_ping(int32_t s1, int32_t s2)
42{
43 return s1 - s2;
44}
45
46EoResult eo_sequence_init_bytes(int32_t start, uint8_t *out_bytes)
47{
48 if (!out_bytes)
49 {
50 return EO_NULL_PTR;
51 }
52
53 // Encode start as: start + 13 = seq1 * 7 + seq2
54 // where seq1 and seq2 are both valid single EO bytes (0..EO_CHAR_MAX-1).
55 // Pick a random seq1 in the valid range and derive seq2.
56 int32_t seq1_min = (start - (EO_CHAR_MAX - 1) + 13 + 6) / 7;
57 if (seq1_min < 0)
58 {
59 seq1_min = 0;
60 }
61
62 int32_t seq1_max = (start + 13) / 7;
63 if (seq1_max > (EO_CHAR_MAX - 1))
64 {
65 seq1_max = EO_CHAR_MAX - 1;
66 }
67
68 if (seq1_max < seq1_min)
69 {
71 }
72
73 int32_t range = seq1_max - seq1_min + 1;
74 int32_t seq1 = (range > 0) ? eo_rand_range(seq1_min, seq1_max) : seq1_min;
75 int32_t seq2 = start - seq1 * 7 + 13;
76
77 if (seq2 < 0 || seq2 > (EO_CHAR_MAX - 1))
78 {
80 }
81
82 out_bytes[0] = (uint8_t)seq1;
83 out_bytes[1] = (uint8_t)seq2;
84 return EO_SUCCESS;
85}
86
87EoResult eo_sequence_ping_bytes(int32_t start, uint8_t *out_bytes)
88{
89 if (!out_bytes)
90 {
91 return EO_NULL_PTR;
92 }
93
94 int32_t low = start;
95 if (low < 0)
96 {
97 low = 0;
98 }
99
100 int32_t high = start + (EO_CHAR_MAX - 1);
101 if (high > (EO_CHAR_MAX - 1))
102 {
103 high = EO_CHAR_MAX - 1;
104 }
105
106 if (high < low)
107 {
109 }
110
111 int32_t range = high - low + 1;
112 int32_t seq1 = (range > 0) ? eo_rand_range(low, high) : low;
113 int32_t seq2 = seq1 - start;
114
115 if (seq2 < 0 || seq2 > (EO_CHAR_MAX - 1))
116 {
118 }
119
120 out_bytes[0] = (uint8_t)seq1;
121 out_bytes[1] = (uint8_t)seq2;
122 return EO_SUCCESS;
123}
#define EO_CHAR_MAX
Definition data.h:11
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:87
EoResult eo_sequencer_next(EoSequencer *sequencer, int32_t *out_value)
Definition sequencer.c:13
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
int32_t eo_generate_sequence_start()
Definition sequencer.c:29
EoSequencer eo_sequencer_init(int32_t start)
Definition sequencer.c:5
int32_t start
Definition sequencer.h:10
int32_t counter
Definition sequencer.h:11