-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
103 lines (82 loc) · 2.06 KB
/
main.c
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
// main.c - main().
#if !defined(TESTS)
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <process.h>
#include "bstrlib.h"
#include "net.h"
#include "protocol.h"
#include "server.h"
#include "game.h"
#include "landscape.h"
#include "debug.h"
static void __stop(int unused);
static Landscape *l = NULL;
static bstring input = NULL;
//int main(int argc, char *argv[], char *envp[])
int main(int argc, char *argv[])
{
puts("Starting morrigan.");
unsigned short port = 0;
if (1 < argc)
{
port = (unsigned short) atoi(argv[1]);
if (!port)
{
port = PORT;
}
printf("Using port: %d.\n", port);
}
check(SIG_ERR != signal(SIGINT, __stop), "Failed to set signal handler.", "");
check(SIG_ERR != signal(SIGTERM, __stop), "Failed to set signal handler.", "");
srand((unsigned) (time(NULL) ^ _getpid()));
l = landscape_load("land.dat", 32, 1.0);
check(l, "Failed to load landscape.", "");
check(net_start(port), "Failed to start network interface.", "");
check(server_start(), "Failed to start server.", "");
check(game_start(l, server_get_clients()), "Failed to start game.", "");
do
{
printf(">");
input = bgets(fgetc, stdin, '\n');
if (NULL == input || 0 == strcmp("exit\n", bdata(input)))
{
break;
}
bdestroy(input);
input = NULL;
} while(true);
__stop(0);
return EXIT_SUCCESS;
error:
fprintf(stderr, "Error exit.\n");
__stop(0);
return EXIT_FAILURE;
}
static void __stop(int unused)
{
#pragma ref unused
static bool stopped = false;
if (stopped)
{
return;
}
stopped = true;
puts("Stopping morrigan.");
if (input)
{
bdestroy(input);
}
net_stop();
game_stop();
server_stop();
if (l)
{
landscape_destroy(l);
l = NULL;
}
}
#endif