1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify |
8 | * 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 |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation, either version 3 of the License, or |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
11 | * option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
20 | * |
21 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | #include <global.h> |
25 | #include <global.h> |
… | |
… | |
32 | #include <algorithm> |
33 | #include <algorithm> |
33 | #include <functional> |
34 | #include <functional> |
34 | |
35 | |
35 | playervec players; |
36 | playervec players; |
36 | |
37 | |
37 | void |
|
|
38 | display_motd (const object *op) |
|
|
39 | { |
|
|
40 | char buf[MAX_BUF]; |
|
|
41 | char motd[HUGE_BUF]; |
|
|
42 | FILE *fp; |
|
|
43 | int comp; |
|
|
44 | int size; |
|
|
45 | |
|
|
46 | sprintf (buf, "%s/%s", settings.confdir, settings.motd); |
|
|
47 | if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) |
|
|
48 | return; |
|
|
49 | |
|
|
50 | motd[0] = '\0'; |
|
|
51 | size = 0; |
|
|
52 | |
|
|
53 | while (fgets (buf, MAX_BUF, fp)) |
|
|
54 | { |
|
|
55 | if (*buf == '#') |
|
|
56 | continue; |
|
|
57 | |
|
|
58 | strncat (motd + size, buf, HUGE_BUF - size); |
|
|
59 | size += strlen (buf); |
|
|
60 | } |
|
|
61 | |
|
|
62 | draw_ext_info (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_MOTD, MSG_SUBTYPE_NONE, motd, NULL); |
|
|
63 | close_and_delete (fp, comp); |
|
|
64 | } |
|
|
65 | |
|
|
66 | void |
|
|
67 | send_rules (const object *op) |
|
|
68 | { |
|
|
69 | char buf[MAX_BUF]; |
|
|
70 | char rules[HUGE_BUF]; |
|
|
71 | FILE *fp; |
|
|
72 | int comp; |
|
|
73 | int size; |
|
|
74 | |
|
|
75 | sprintf (buf, "%s/%s", settings.confdir, settings.rules); |
|
|
76 | if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) |
|
|
77 | return; |
|
|
78 | |
|
|
79 | rules[0] = '\0'; |
|
|
80 | size = 0; |
|
|
81 | |
|
|
82 | while (fgets (buf, MAX_BUF, fp)) |
|
|
83 | { |
|
|
84 | if (*buf == '#') |
|
|
85 | continue; |
|
|
86 | |
|
|
87 | if (size + strlen (buf) >= HUGE_BUF) |
|
|
88 | { |
|
|
89 | LOG (llevDebug, "Warning, rules size is > %d bytes.\n", HUGE_BUF); |
|
|
90 | break; |
|
|
91 | } |
|
|
92 | |
|
|
93 | strncat (rules + size, buf, HUGE_BUF - size); |
|
|
94 | size += strlen (buf); |
|
|
95 | } |
|
|
96 | |
|
|
97 | draw_ext_info (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_RULES, rules, NULL); |
|
|
98 | close_and_delete (fp, comp); |
|
|
99 | } |
|
|
100 | |
|
|
101 | void |
|
|
102 | send_news (const object *op) |
|
|
103 | { |
|
|
104 | char buf[MAX_BUF]; |
|
|
105 | char news[HUGE_BUF]; |
|
|
106 | char subject[MAX_BUF]; |
|
|
107 | FILE *fp; |
|
|
108 | int comp; |
|
|
109 | int size; |
|
|
110 | |
|
|
111 | sprintf (buf, "%s/%s", settings.confdir, settings.news); |
|
|
112 | if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) |
|
|
113 | return; |
|
|
114 | |
|
|
115 | news[0] = '\0'; |
|
|
116 | subject[0] = '\0'; |
|
|
117 | size = 0; |
|
|
118 | |
|
|
119 | while (fgets (buf, MAX_BUF, fp)) |
|
|
120 | { |
|
|
121 | if (*buf == '#') |
|
|
122 | continue; |
|
|
123 | |
|
|
124 | if (*buf == '%') |
|
|
125 | { /* send one news */ |
|
|
126 | if (size > 0) |
|
|
127 | draw_ext_info_format (NDI_UNIQUE | NDI_GREEN, 0, op, |
|
|
128 | MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS, |
|
|
129 | "INFORMATION: %s\n%s", (char *)"%s\n%s", |
|
|
130 | subject, news); /*send previously read news */ |
|
|
131 | |
|
|
132 | strcpy (subject, buf + 1); |
|
|
133 | strip_endline (subject); |
|
|
134 | size = 0; |
|
|
135 | news[0] = '\0'; |
|
|
136 | } |
|
|
137 | else |
|
|
138 | { |
|
|
139 | if (size + strlen (buf) >= HUGE_BUF) |
|
|
140 | { |
|
|
141 | LOG (llevDebug, "Warning, one news item has size > %d bytes.\n", HUGE_BUF); |
|
|
142 | break; |
|
|
143 | } |
|
|
144 | strncat (news + size, buf, HUGE_BUF - size); |
|
|
145 | size += strlen (buf); |
|
|
146 | } |
|
|
147 | } |
|
|
148 | |
|
|
149 | draw_ext_info_format (NDI_UNIQUE | NDI_GREEN, 0, op, |
|
|
150 | MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS, "INFORMATION: %s\n%s\n", (char *)"%s\n%s", subject, news); |
|
|
151 | close_and_delete (fp, comp); |
|
|
152 | } |
|
|
153 | |
|
|
154 | /* This loads the first map an puts the player on it. */ |
38 | /* This loads the first map an puts the player on it. */ |
155 | static void |
39 | static void |
156 | set_first_map (object *op) |
40 | set_first_map (object *op) |
157 | { |
41 | { |
158 | op->contr->maplevel = first_map_path; |
42 | op->contr->maplevel = first_map_path; |
… | |
… | |
252 | esrv_send_inventory (ob, ob); |
136 | esrv_send_inventory (ob, ob); |
253 | esrv_add_spells (this, 0); |
137 | esrv_add_spells (this, 0); |
254 | |
138 | |
255 | activate (); |
139 | activate (); |
256 | |
140 | |
257 | send_rules (ob); |
|
|
258 | send_news (ob); |
|
|
259 | display_motd (ob); |
|
|
260 | |
|
|
261 | INVOKE_PLAYER (CONNECT, this); |
141 | INVOKE_PLAYER (CONNECT, this); |
262 | INVOKE_PLAYER (LOGIN, this); |
142 | INVOKE_PLAYER (LOGIN, this); |
263 | } |
143 | } |
264 | |
144 | |
265 | void |
145 | void |
… | |
… | |
281 | ns->reset_stats (); |
161 | ns->reset_stats (); |
282 | ns->pl = 0; |
162 | ns->pl = 0; |
283 | ns = 0; |
163 | ns = 0; |
284 | } |
164 | } |
285 | |
165 | |
286 | observe = ob; |
166 | // this is important for the player scheduler to get the correct refcount |
|
|
167 | // when ns = 0 |
|
|
168 | observe = viewpoint = ob; |
287 | |
169 | |
288 | deactivate (); |
170 | deactivate (); |
289 | } |
171 | } |
290 | |
172 | |
291 | // the need for this function can be explained |
173 | // the need for this function can be explained |
292 | // by load_object not returning the object |
174 | // by load_object not returning the object |
293 | void |
175 | void |
294 | player::set_object (object *op) |
176 | player::set_object (object *op) |
295 | { |
177 | { |
296 | ob = observe = op; |
178 | ob = observe = viewpoint = op; |
297 | ob->contr = this; /* this aren't yet in archetype */ |
179 | ob->contr = this; /* this aren't yet in archetype */ |
298 | |
180 | |
299 | ob->speed = 1.0f; |
181 | ob->speed = 1.0f; |
300 | ob->speed_left = 0.5f; |
182 | ob->speed_left = 0.5f; |
301 | |
183 | |
… | |
… | |
330 | } |
212 | } |
331 | |
213 | |
332 | void |
214 | void |
333 | player::set_observe (object *op) |
215 | player::set_observe (object *op) |
334 | { |
216 | { |
335 | observe = op ? op : ob; |
217 | observe = viewpoint = op ? op : ob; |
|
|
218 | do_los = 1; |
|
|
219 | } |
|
|
220 | |
|
|
221 | void |
|
|
222 | player::set_viewpoint (object *op) |
|
|
223 | { |
|
|
224 | viewpoint = op ? op : (object *)observe; |
336 | do_los = 1; |
225 | do_los = 1; |
337 | } |
226 | } |
338 | |
227 | |
339 | player::player () |
228 | player::player () |
340 | { |
229 | { |
… | |
… | |
369 | { |
258 | { |
370 | ob->destroy_inv (false); |
259 | ob->destroy_inv (false); |
371 | ob->destroy (); |
260 | ob->destroy (); |
372 | } |
261 | } |
373 | |
262 | |
374 | ob = observe = 0; |
263 | ob = observe = viewpoint = 0; |
375 | } |
264 | } |
376 | |
265 | |
377 | player::~player () |
266 | player::~player () |
378 | { |
267 | { |
379 | /* Clear item stack */ |
268 | /* Clear item stack */ |
… | |
… | |
1467 | LOG (llevError, "Range: bow without activated bow (%s).\n", &op->name); |
1356 | LOG (llevError, "Range: bow without activated bow (%s).\n", &op->name); |
1468 | return 0; |
1357 | return 0; |
1469 | } |
1358 | } |
1470 | |
1359 | |
1471 | // optimisation: move object to top so we will find it quickly again |
1360 | // optimisation: move object to top so we will find it quickly again |
1472 | if (bow->below) |
1361 | splay (bow); |
1473 | { |
|
|
1474 | bow->remove (); |
|
|
1475 | op->insert (bow); |
|
|
1476 | } |
|
|
1477 | } |
1362 | } |
1478 | |
1363 | |
1479 | if (!bow->race || !bow->skill) |
1364 | if (!bow->race || !bow->skill) |
1480 | { |
1365 | { |
1481 | new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s is broken.", &bow->name); |
1366 | new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s is broken.", &bow->name); |
… | |
… | |
1770 | } |
1655 | } |
1771 | |
1656 | |
1772 | return true; |
1657 | return true; |
1773 | } |
1658 | } |
1774 | |
1659 | |
1775 | /* find_key |
1660 | static object * |
1776 | * We try to find a key for the door as passed. If we find a key |
|
|
1777 | * and successfully use it, we return the key, otherwise NULL |
|
|
1778 | * This function merges both normal and locked door, since the logic |
|
|
1779 | * for both is the same - just the specific key is different. |
|
|
1780 | * pl is the player, |
|
|
1781 | * inv is the objects inventory to searched |
|
|
1782 | * door is the door we are trying to match against. |
|
|
1783 | * This function can be called recursively to search containers. |
|
|
1784 | */ |
|
|
1785 | object * |
|
|
1786 | find_key (object *pl, object *container, object *door) |
1661 | find_key_ (object *pl, object *container, object *door) |
1787 | { |
1662 | { |
1788 | object *tmp, *key; |
1663 | object *tmp, *key; |
1789 | |
1664 | |
1790 | /* Should not happen, but sanity checking is never bad */ |
1665 | /* Should not happen, but sanity checking is never bad */ |
1791 | if (!container->inv) |
1666 | if (!container->inv) |
… | |
… | |
1794 | /* First, lets try to find a key in the top level inventory */ |
1669 | /* First, lets try to find a key in the top level inventory */ |
1795 | for (tmp = container->inv; tmp; tmp = tmp->below) |
1670 | for (tmp = container->inv; tmp; tmp = tmp->below) |
1796 | { |
1671 | { |
1797 | if (door->type == DOOR && tmp->type == KEY) |
1672 | if (door->type == DOOR && tmp->type == KEY) |
1798 | break; |
1673 | break; |
|
|
1674 | |
1799 | /* For sanity, we should really check door type, but other stuff |
1675 | /* For sanity, we should really check door type, but other stuff |
1800 | * (like containers) can be locked with special keys |
1676 | * (like containers) can be locked with special keys |
1801 | */ |
1677 | */ |
1802 | if (tmp->slaying && tmp->type == SPECIAL_KEY && tmp->slaying == door->slaying) |
1678 | if (tmp->slaying && tmp->type == SPECIAL_KEY && tmp->slaying == door->slaying) |
1803 | break; |
1679 | break; |
… | |
… | |
1809 | * a key, return |
1685 | * a key, return |
1810 | */ |
1686 | */ |
1811 | if (!tmp) |
1687 | if (!tmp) |
1812 | { |
1688 | { |
1813 | for (tmp = container->inv; tmp; tmp = tmp->below) |
1689 | for (tmp = container->inv; tmp; tmp = tmp->below) |
1814 | { |
|
|
1815 | /* No reason to search empty containers */ |
1690 | /* No reason to search empty containers */ |
1816 | if (tmp->type == CONTAINER && tmp->inv) |
1691 | if (tmp->type == CONTAINER && tmp->inv) |
1817 | { |
|
|
1818 | if ((key = find_key (pl, tmp, door))) |
1692 | if ((key = find_key_ (pl, tmp, door))) |
1819 | return key; |
1693 | return key; |
1820 | } |
|
|
1821 | } |
|
|
1822 | |
1694 | |
1823 | if (!tmp) |
1695 | if (!tmp) |
1824 | return NULL; |
1696 | return 0; |
1825 | } |
1697 | } |
1826 | |
1698 | |
1827 | /* We get down here if we have found a key. Now if its in a container, |
1699 | /* We get down here if we have found a key. Now if its in a container, |
1828 | * see if we actually want to use it |
1700 | * see if we actually want to use it |
1829 | */ |
1701 | */ |
1830 | if (pl != container) |
1702 | if (pl != container) |
1831 | { |
1703 | { |
1832 | /* Only let players use keys in containers */ |
1704 | /* Only let players use keys in containers */ |
1833 | if (!pl->contr) |
1705 | if (!pl->contr) |
1834 | return NULL; |
1706 | return 0; |
|
|
1707 | |
1835 | /* cases where this fails: |
1708 | /* cases where this fails: |
1836 | * If we only search the player inventory, return now since we |
1709 | * If we only search the player inventory, return now since we |
1837 | * are not in the players inventory. |
1710 | * are not in the players inventory. |
1838 | * If the container is not active, return now since only active |
1711 | * If the container is not active, return now since only active |
1839 | * containers can be used. |
1712 | * containers can be used. |
… | |
… | |
1843 | * inv must have been an container and must have been active. |
1716 | * inv must have been an container and must have been active. |
1844 | * |
1717 | * |
1845 | * Change the color so that the message doesn't disappear with |
1718 | * Change the color so that the message doesn't disappear with |
1846 | * all the others. |
1719 | * all the others. |
1847 | */ |
1720 | */ |
1848 | if (pl->contr->usekeys == key_inventory || |
1721 | if (pl->contr->usekeys == key_inventory |
1849 | !QUERY_FLAG (container, FLAG_APPLIED) || |
1722 | || !QUERY_FLAG (container, FLAG_APPLIED) |
1850 | (pl->contr->usekeys == keyrings && container->race != shstr_keys)) |
1723 | || (pl->contr->usekeys == keyrings && container->race != shstr_keys)) |
1851 | { |
1724 | { |
1852 | new_draw_info_format (NDI_UNIQUE | NDI_BROWN, 0, pl, |
1725 | new_draw_info_format (NDI_UNIQUE | NDI_BROWN, 0, pl, |
1853 | "The %s in your %s vibrates as you approach the door", query_name (tmp), query_name (container)); |
1726 | "The %s in your %s vibrates as you approach the door", query_name (tmp), query_name (container)); |
1854 | return NULL; |
1727 | return NULL; |
1855 | } |
1728 | } |
1856 | } |
1729 | } |
1857 | |
1730 | |
1858 | return tmp; |
1731 | return tmp; |
|
|
1732 | } |
|
|
1733 | |
|
|
1734 | /* find_key |
|
|
1735 | * We try to find a key for the door as passed. If we find a key |
|
|
1736 | * and successfully use it, we return the key, otherwise NULL |
|
|
1737 | * This function merges both normal and locked door, since the logic |
|
|
1738 | * for both is the same - just the specific key is different. |
|
|
1739 | * pl is the player, |
|
|
1740 | * inv is the objects inventory to searched |
|
|
1741 | * door is the door we are trying to match against. |
|
|
1742 | * This function can be called recursively to search containers. |
|
|
1743 | */ |
|
|
1744 | object * |
|
|
1745 | find_key (object *pl, object *container, object *door) |
|
|
1746 | { |
|
|
1747 | if (door->slaying && is_match_expr (door->slaying)) |
|
|
1748 | { |
|
|
1749 | // for match expressions, we try to find the key by applying the match |
|
|
1750 | // to the op itself, which is supposed to find the "key", instead |
|
|
1751 | // of searching through containers ourselves. |
|
|
1752 | |
|
|
1753 | return match_one (door->slaying, container, door, pl, pl); |
|
|
1754 | } |
|
|
1755 | else |
|
|
1756 | return find_key_ (pl, container, door); |
1859 | } |
1757 | } |
1860 | |
1758 | |
1861 | /* moved door processing out of move_player_attack. |
1759 | /* moved door processing out of move_player_attack. |
1862 | * returns 1 if player has opened the door with a key |
1760 | * returns 1 if player has opened the door with a key |
1863 | * such that the caller should not do anything more, |
1761 | * such that the caller should not do anything more, |
… | |
… | |
1913 | * going to try and move (not fire weapons). |
1811 | * going to try and move (not fire weapons). |
1914 | */ |
1812 | */ |
1915 | bool |
1813 | bool |
1916 | move_player_attack (object *op, int dir) |
1814 | move_player_attack (object *op, int dir) |
1917 | { |
1815 | { |
|
|
1816 | if (!op->contr->braced && op->speed_left > 0.f && move_ob (op, dir, op)) |
|
|
1817 | { |
|
|
1818 | --op->speed_left; |
|
|
1819 | return true; |
|
|
1820 | } |
|
|
1821 | |
1918 | int on_battleground; |
1822 | int on_battleground; |
1919 | |
1823 | |
1920 | sint16 nx = freearr_x[dir] + op->x; |
1824 | sint16 nx = freearr_x[dir] + op->x; |
1921 | sint16 ny = freearr_y[dir] + op->y; |
1825 | sint16 ny = freearr_y[dir] + op->y; |
1922 | |
1826 | |
1923 | on_battleground = op_on_battleground (op, 0, 0); |
1827 | on_battleground = op_on_battleground (op, 0, 0); |
1924 | |
1828 | |
1925 | if (out_of_map (op->map, nx, ny)) |
1829 | if (out_of_map (op->map, nx, ny)) |
1926 | return false; |
1830 | return false; |
1927 | |
|
|
1928 | if (!op->contr->braced && op->speed_left > 0.f && move_ob (op, dir, op)) |
|
|
1929 | { |
|
|
1930 | --op->speed_left; |
|
|
1931 | return true; |
|
|
1932 | } |
|
|
1933 | |
1831 | |
1934 | /* If braced, or can't move to the square, and it is not out of the |
1832 | /* If braced, or can't move to the square, and it is not out of the |
1935 | * map, attack it. Note order of if statement is important - don't |
1833 | * map, attack it. Note order of if statement is important - don't |
1936 | * want to be calling move_ob if braced, because move_ob will move the |
1834 | * want to be calling move_ob if braced, because move_ob will move the |
1937 | * player. This is a pretty nasty hack, because if we could |
1835 | * player. This is a pretty nasty hack, because if we could |