-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_world.c
129 lines (98 loc) · 3.61 KB
/
client_world.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
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
/*
Copyright (c) 2006 Florian Wesch <[email protected]>. All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "renderer.h"
#include "client_world.h"
#include "misc.h"
static int initialized;
static client_maptile_t *map;
static client_world_info_t info;
#define MAPTILE(x, y) (map[(y) * info.width + (x)])
const client_world_info_t *client_world_get_info() {
return initialized ? &info : NULL;
}
const client_maptile_t *client_world_get() {
return initialized ? map : NULL;
}
const client_maptile_t *client_world_get_tile(int x, int y) {
return &MAPTILE(x, y);
}
void client_world_destroy() {
if (!initialized)
return;
free(map);
initialized = 0;
}
void client_world_from_network(packet_t *packet) {
if (!initialized) PROTOCOL_ERROR();
uint8_t x; uint8_t y;
if (!packet_read08(packet, &x)) PROTOCOL_ERROR();
if (!packet_read08(packet, &y)) PROTOCOL_ERROR();
if (x >= info.width) PROTOCOL_ERROR();
if (y >= info.height) PROTOCOL_ERROR();
uint8_t food_type;
if (!packet_read08(packet, &food_type)) PROTOCOL_ERROR();
uint8_t food = food_type & 0x0F;
if (food > 10) PROTOCOL_ERROR();
MAPTILE(x, y).food = food;
uint8_t type = (food_type & 0xF0) >> 4;
if (type >= TILE_LAST_DEFINED) PROTOCOL_ERROR();
MAPTILE(x,y).type = type;
uint8_t gfx;
if (!packet_read08(packet, &gfx)) PROTOCOL_ERROR();
if (gfx >= TILE_GFX_LAST_DEFINED) PROTOCOL_ERROR();
MAPTILE(x,y).gfx = gfx;
renderer_world_changed(x, y);
}
void client_world_info_from_network(packet_t *packet) {
if (initialized)
client_world_destroy();
uint8_t w, h, kx, ky;
if (!packet_read08(packet, &w)) PROTOCOL_ERROR();
if (!packet_read08(packet, &h)) PROTOCOL_ERROR();
if (!packet_read08(packet, &kx)) PROTOCOL_ERROR();
if (!packet_read08(packet, &ky)) PROTOCOL_ERROR();
info.width = w;
info.height = h;
info.koth_x = kx;
info.koth_y = ky;
if (info.koth_x >= info.width) PROTOCOL_ERROR();
if (info.koth_y >= info.height) PROTOCOL_ERROR();
map = malloc(info.width * info.height * sizeof(client_maptile_t));
initialized = 1;
// Tile Texturen setzen
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
MAPTILE(x,y).food = 0;
MAPTILE(x,y).type = TILE_SOLID;
MAPTILE(x,y).gfx = TILE_GFX_SOLID;
}
}
renderer_world_info_changed(&info);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
renderer_world_changed(x, y);
}
}
}
void client_world_init() {
initialized = 0;
}
void client_world_shutdown() {
client_world_destroy();
}