1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
|
|
6 | * Copyright (C) 1992 Frank Tore Johansen |
|
|
7 | * |
5 | * |
8 | * This program is free software; you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * it under the terms of the GNU General Public License as published by |
7 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation; either version 2 of the License, or |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
9 | * option) any later version. |
12 | * |
10 | * |
13 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
17 | * |
|
|
18 | * You should have received a copy of the GNU General Public License |
|
|
19 | * along with this program; if not, write to the Free Software |
|
|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
21 | * |
15 | * |
22 | * The maintainer of this code can be reached at <crossfire@schmorp.de> |
16 | * You should have received a copy of the Affero GNU General Public License |
|
|
17 | * and the GNU General Public License along with this program. If not, see |
|
|
18 | * <http://www.gnu.org/licenses/>. |
|
|
19 | * |
|
|
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
21 | */ |
24 | |
|
|
25 | |
22 | |
26 | #include <global.h> |
23 | #include <global.h> |
27 | #include <stdio.h> |
24 | #include <stdio.h> |
28 | |
25 | |
29 | #include "face.h" |
26 | #include "face.h" |
30 | #include "crc.h" |
27 | #include "crc.h" |
31 | |
28 | |
32 | faceidx blank_face, empty_face; |
29 | faceidx blank_face, empty_face, magicmouth_face; |
33 | |
30 | |
34 | facehash_t facehash; |
31 | facehash_t facehash; |
35 | std::vector<faceinfo> faces; |
32 | std::vector<faceinfo> faces; |
36 | std::vector<facedata> face32, face64; |
|
|
37 | |
33 | |
38 | /** |
34 | static std::vector<faceidx> faces_freelist; |
39 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
35 | |
40 | */ |
36 | faceidx face_alloc () |
41 | struct smoothing : zero_initialised |
|
|
42 | { |
37 | { |
43 | uint16 id; |
38 | faceidx idx; |
44 | uint16 smooth; |
|
|
45 | }; |
|
|
46 | |
39 | |
47 | /** |
40 | if (!faces_freelist.empty ()) |
48 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
41 | { |
49 | * entries. It is sorted by smooth[].id. |
42 | idx = faces_freelist.back (); |
50 | */ |
43 | faces_freelist.pop_back (); |
51 | static struct smoothing *smooth = NULL; |
44 | } |
52 | int nrofsmooth = 0; |
45 | else |
|
|
46 | { |
|
|
47 | idx = faces.size (); |
53 | |
48 | |
54 | /* the only thing this table is used for now is to |
49 | if (!idx) // skip index 0 |
55 | * translate the colorname in the magicmap field of the |
50 | idx = 1; |
56 | * face into a numeric index that is then sent to the |
|
|
57 | * client for magic map commands. The order of this table |
|
|
58 | * must match that of the NDI colors in include/newclient.h. |
|
|
59 | */ |
|
|
60 | static const char *const colorname[] = { |
|
|
61 | "black", /* 0 */ |
|
|
62 | "white", /* 1 */ |
|
|
63 | "blue", /* 2 */ |
|
|
64 | "red", /* 3 */ |
|
|
65 | "orange", /* 4 */ |
|
|
66 | "light_blue", /* 5 */ |
|
|
67 | "dark_orange", /* 6 */ |
|
|
68 | "green", /* 7 */ |
|
|
69 | "light_green", /* 8 */ |
|
|
70 | "grey", /* 9 */ |
|
|
71 | "brown", /* 10 */ |
|
|
72 | "yellow", /* 11 */ |
|
|
73 | "khaki" /* 12 */ |
|
|
74 | }; |
|
|
75 | |
51 | |
76 | static int |
52 | faces.resize (idx + 1); |
77 | compar_smooth (const struct smoothing *a, const struct smoothing *b) |
53 | } |
78 | { |
54 | |
79 | if (a->id < b->id) |
|
|
80 | return -1; |
|
|
81 | if (b->id < a->id) |
|
|
82 | return 1; |
|
|
83 | return 0; |
55 | return idx; |
84 | } |
56 | } |
85 | |
57 | |
86 | /* |
58 | void |
87 | * Returns the matching color in the coloralias if found, |
59 | faceinfo::unref () |
88 | * 0 otherwise. Note that 0 will actually be black, so there is no |
|
|
89 | * way the calling function can tell if an error occurred or not |
|
|
90 | */ |
|
|
91 | static uint8 |
|
|
92 | find_color (const char *name) |
|
|
93 | { |
60 | { |
94 | uint8 i; |
61 | if (--refcnt) |
|
|
62 | return; |
95 | |
63 | |
96 | for (i = 0; i < sizeof (colorname) / sizeof (*colorname); i++) |
64 | refcnt = 1; |
97 | if (!strcmp (name, colorname[i])) |
|
|
98 | return i; |
|
|
99 | |
65 | |
100 | LOG (llevError, "Unknown color: %s\n", name); |
|
|
101 | return 0; |
|
|
102 | } |
66 | } |
103 | |
67 | |
104 | int |
68 | faceidx |
105 | face_find (const char *name, int defidx) |
69 | face_find (const char *name, faceidx defidx) |
106 | { |
70 | { |
|
|
71 | if (!name) |
|
|
72 | return defidx; |
|
|
73 | |
107 | facehash_t::iterator i = facehash.find (name); |
74 | facehash_t::iterator i = facehash.find (name); |
108 | |
75 | |
109 | return i == facehash.end () |
76 | return i == facehash.end () |
110 | ? defidx : i->second; |
77 | ? defidx : i->second; |
111 | } |
78 | } |
112 | |
79 | |
113 | facedata * |
80 | faceinfo * |
114 | face_data (int idx, int faceset) |
81 | face_info (faceidx idx) |
115 | { |
82 | { |
116 | return &(faceset ? face64 : face32)[idx]; |
83 | assert (0 < (faceidx)-1); // faceidx must be unsigned |
|
|
84 | |
|
|
85 | if (idx >= faces.size ()) |
|
|
86 | return 0; |
|
|
87 | |
|
|
88 | return &faces [idx]; |
117 | } |
89 | } |
118 | |
90 | |
119 | /* Reads the smooth file to know how to smooth datas. |
91 | facedata * |
120 | * the smooth file if made of 2 elements lines. |
92 | faceinfo::data (int faceset) const |
121 | * lines starting with # are comment |
|
|
122 | * the first element of line is face to smooth |
|
|
123 | * the next element is the 16x2 faces picture |
|
|
124 | * used for smoothing |
|
|
125 | */ |
|
|
126 | int |
|
|
127 | ReadSmooth (void) |
|
|
128 | { |
93 | { |
129 | char buf[MAX_BUF], *p, *q; |
94 | if (!face [faceset].chksum_len) |
130 | FILE *fp; |
95 | faceset = 0; |
131 | int smoothcount = 0; |
|
|
132 | |
96 | |
133 | sprintf (buf, "%s/smooth", settings.datadir); |
97 | return (facedata *)(face + faceset); |
134 | LOG (llevDebug, "Reading smooth from %s...\n", buf); |
|
|
135 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
136 | { |
|
|
137 | LOG (llevError, "Cannot open smooth file %s: %s\n", strerror (errno)); |
|
|
138 | exit (-1); |
|
|
139 | } |
|
|
140 | |
|
|
141 | /* First count how many smooth we have, so we can allocate correctly */ |
|
|
142 | while (fgets (buf, MAX_BUF, fp) != NULL) |
|
|
143 | if (buf[0] != '#' && buf[0] != '\n') |
|
|
144 | smoothcount++; |
|
|
145 | rewind (fp); |
|
|
146 | |
|
|
147 | smooth = new smoothing[smoothcount]; |
|
|
148 | |
|
|
149 | while (nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp) != NULL) |
|
|
150 | { |
|
|
151 | if (*buf == '#') |
|
|
152 | continue; |
|
|
153 | p = strchr (buf, ' '); |
|
|
154 | if (!p) |
|
|
155 | continue; |
|
|
156 | *p = '\0'; |
|
|
157 | q = buf; |
|
|
158 | smooth[nrofsmooth].id = face_find (q); |
|
|
159 | q = p + 1; |
|
|
160 | smooth[nrofsmooth].smooth = face_find (q); |
|
|
161 | nrofsmooth++; |
|
|
162 | } |
|
|
163 | fclose (fp); |
|
|
164 | |
|
|
165 | LOG (llevDebug, "done (got %d smooth entries)\n", nrofsmooth); |
|
|
166 | qsort (smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
|
|
167 | return nrofsmooth; |
|
|
168 | } |
98 | } |
169 | |
99 | |
170 | /** |
100 | facedata * |
171 | * Find the smooth face for a given face. |
101 | face_data (faceidx idx, int faceset) |
172 | * |
|
|
173 | * @param face the face to find the smoothing face for |
|
|
174 | * |
|
|
175 | * @param smoothed return value: set to smooth face |
|
|
176 | * |
|
|
177 | * @return 1=smooth face found, 0=no smooth face found |
|
|
178 | */ |
|
|
179 | int |
|
|
180 | FindSmooth (uint16 face, uint16 * smoothed) |
|
|
181 | { |
102 | { |
182 | struct smoothing *bp, tmp; |
103 | if (faceinfo *f = face_info (idx)) |
|
|
104 | return f->data (faceset); |
183 | |
105 | |
184 | tmp.id = face; |
106 | return 0; |
185 | bp = (struct smoothing *) bsearch |
|
|
186 | (&tmp, smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
|
|
187 | (*smoothed) = 0; |
|
|
188 | if (bp) |
|
|
189 | (*smoothed) = bp->smooth; |
|
|
190 | return bp ? 1 : 0; |
|
|
191 | } |
107 | } |
192 | |
108 | |