aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/opendeco.c
blob: 8e22e4b71545e6f535768c51291c121ee5df78e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* SPDX-License-Identifier: MIT-0 */

#include <errno.h>
#include <locale.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>

#include "deco.h"
#include "opendeco-cli.h"
#include "opendeco-conf.h"
#include "output.h"
#include "schedule.h"

#define MOD_OXY (abs_depth(xsw_to_bar(msw_or_fsw(6, 20))))

#define RMV_DIVE_DEFAULT 20
#define RMV_DECO_DEFAULT 15
#define SHOW_TRAVEL_DEFAULT 0

double RMV_DIVE = RMV_DIVE_DEFAULT;
double RMV_DECO = RMV_DECO_DEFAULT;

int SHOW_TRAVEL = SHOW_TRAVEL_DEFAULT;

static struct gas_usage {
    const gas_t *gas;
    double usage;
} gas_usage[10];

int register_gas_use(double depth, double time, const gas_t *gas, double rmv)
{
    double usage = depth * time * rmv;

    for (int i = 0; i < 10; i++) {
        if (!gas_usage[i].gas) {
            gas_usage[i] = (struct gas_usage){
                .gas = gas,
                .usage = usage,
            };
            return 0;
        } else if (gas_usage[i].gas == gas) {
            gas_usage[i].usage += usage;
            return 0;
        }
    }

    return -ENOSPC;
}

void print_gas_use(void)
{
    static char gasbuf[12];

    wprintf(L"\nRMV dive: %.2f%lc/min\n", RMV_DIVE, LTR);
    wprintf(L"RMV deco: %.2f%lc/min\n\n", RMV_DECO, LTR);

    wprintf(L"Gas use:\n", RMV_DECO, LTR);
    for (int i = 0; i < 10; i++) {
        if (gas_usage[i].gas) {
            format_gas(gasbuf, len(gasbuf), gas_usage[i].gas);
            strcat(gasbuf, ":");
            wprintf(L"%-12s%5i%lc\n", gasbuf, (int) ceil(gas_usage[i].usage), LTR);
        }
    }
}

void print_segment_callback_fn(const decostate_t *ds, waypoint_t wp, segtype_t type, void *arg)
{
    static double last_depth;
    static double runtime;

    wchar_t sign;

    /* first time initialization */
    if (!last_depth)
        last_depth = SURFACE_PRESSURE;

    runtime += wp.time;

    if (wp.depth < last_depth)
        sign = ASC;
    else if (wp.depth > last_depth)
        sign = DEC;
    else
        sign = LVL;

    if (SHOW_TRAVEL || type != SEG_TRAVEL)
        print_planline(sign, wp.depth, wp.time, runtime, wp.gas);

    /* register gas use */
    double avg_seg_depth = wp.depth == last_depth ? last_depth : (wp.depth + last_depth) / 2;
    double rmv = type == SEG_DIVE ? RMV_DIVE : RMV_DECO;

    register_gas_use(avg_seg_depth, wp.time, wp.gas, rmv);

    last_depth = wp.depth;
}

int parse_gasses(gas_t **gasses, char *str)
{
    if (!str) {
        *gasses = NULL;
        return 0;
    }

    /* count number of gasses in string */
    int nof_gasses = 1;

    for (int c = 0; str[c]; c++)
        if (str[c] == ',')
            nof_gasses++;

    /* allocate gas array */
    gas_t *deco_gasses = malloc(nof_gasses * sizeof(gas_t));

    /* fill gas array */
    char *gas_str = NULL;
    int gas_idx = 0;

    for (;;) {
        if (!gas_str)
            gas_str = strtok(str, ",");
        else
            gas_str = strtok(NULL, ",");

        if (!gas_str)
            break;

        if (scan_gas(&deco_gasses[gas_idx], gas_str)) {
            wprintf(L"Invalid gas (%s). Aborting!\n", gas_str);
            exit(EXIT_FAILURE);
        }

        gas_idx++;
    }

    *gasses = deco_gasses;
    return nof_gasses;
}

void apply_arguments(struct arguments *arguments)
{
    SURFACE_PRESSURE = arguments->SURFACE_PRESSURE;
    SWITCH_INTERMEDIATE = arguments->SWITCH_INTERMEDIATE;
    LAST_STOP_AT_SIX = arguments->LAST_STOP_AT_SIX;
    RMV_DIVE = arguments->RMV_DIVE;
    RMV_DECO = arguments->RMV_DECO;
    SHOW_TRAVEL = arguments->SHOW_TRAVEL;
    UNITS = arguments->UNITS;
}

int main(int argc, char *argv[])
{
    setlocale(LC_ALL, "en_US.utf8");

    /* get conf and cli options */
    char *gas_default = strdup("Air");
    char *decogasses_default = strdup("");

    if (!gas_default || !decogasses_default) {
        errno = ENOMEM;
        perror(__func__);
        exit(EXIT_FAILURE);
    }

    struct arguments arguments = {
        .depth = -1,
        .time = -1,
        .gas = gas_default,
        .gflow = 30,
        .gfhigh = 75,
        .decogasses = decogasses_default,
        .SURFACE_PRESSURE = SURFACE_PRESSURE_DEFAULT,
        .SWITCH_INTERMEDIATE = SWITCH_INTERMEDIATE_DEFAULT,
        .LAST_STOP_AT_SIX = LAST_STOP_AT_SIX_DEFAULT,
        .RMV_DIVE = RMV_DIVE_DEFAULT,
        .RMV_DECO = RMV_DECO_DEFAULT,
        .SHOW_TRAVEL = SHOW_TRAVEL_DEFAULT,
        .UNITS = UNITS_DEFAULT,
    };

    opendeco_conf_parse("opendeco.toml", &arguments);
    opendeco_argp_parse(argc, argv, &arguments);

    /* apply global options */
    apply_arguments(&arguments);

    /* setup */
    decostate_t ds;
    decostate_t ds_p5;
    gas_t bottom_gas;
    gas_t *deco_gasses;

    double depth;
    const gas_t *gas;

    double dec_per_min = xsw_to_bar(msw_or_fsw(9, 30));
    int nof_gasses = parse_gasses(&deco_gasses, arguments.decogasses);

    init_decostate(&ds, arguments.gflow, arguments.gfhigh, xsw_to_bar(msw_or_fsw(3, 10)));

    if (scan_gas(&bottom_gas, arguments.gas)) {
        wprintf(L"Invalid gas (%s). Aborting!\n", arguments.gas);
        exit(EXIT_FAILURE);
    }

    /* override oxygen mod */
    for (int i = 0; i < nof_gasses; i++)
        if (gas_o2(&deco_gasses[i]) == 100)
            deco_gasses[i].mod = MOD_OXY;

    /* simulate dive */
    double descent_time = xsw_to_bar(arguments.depth) / dec_per_min;
    double bottom_time = max(1, arguments.time - descent_time);

    waypoint_t waypoints[] = {
        {.depth = abs_depth(xsw_to_bar(arguments.depth)), .time = descent_time, &bottom_gas},
        {.depth = abs_depth(xsw_to_bar(arguments.depth)), .time = bottom_time,  &bottom_gas},
    };

    waypoint_callback_t print_segment_callback = {
        .fn = &print_segment_callback_fn,
        .arg = NULL,
    };

    print_planhead();

    simulate_dive(&ds, waypoints, len(waypoints), &print_segment_callback);

    depth = waypoints[len(waypoints) - 1].depth;
    gas = waypoints[len(waypoints) - 1].gas;
    ds_p5 = ds;

    /* calculate deco */
    decoinfo_t di = calc_deco(&ds, depth, gas, deco_gasses, nof_gasses, &print_segment_callback);

    /* calculate @+5 TTS */
    add_segment_const(&ds_p5, depth, 5, gas);
    decoinfo_t di_p5 = calc_deco(&ds_p5, depth, gas, deco_gasses, nof_gasses, NULL);

    /* output deco info and disclaimer */
    wprintf(L"\nNDL: %imin TTS: %imin TTS @+5: %imin\n", (int) floor(di.ndl), (int) ceil(di.tts),
            (int) ceil(di_p5.tts));
    print_planfoot(&ds);
    print_gas_use();
    print_disclaimer();

    /* cleanup */
    free(deco_gasses);
    free(arguments.gas);
    free(arguments.decogasses);

    return 0;
}