1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * Crossfire TRT is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
11 | * (at your 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 GNU General Public License |
19 | * along with this program; if not, write to the Free Software |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
21 | * |
20 | * |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
23 | */ |
22 | */ |
24 | |
23 | |
25 | #include <global.h> |
24 | #include <global.h> |
26 | #include <sproto.h> |
25 | #include <sproto.h> |
27 | #include <spells.h> |
26 | #include <spells.h> |
… | |
… | |
92 | && npc->enemy->type != PLAYER |
91 | && npc->enemy->type != PLAYER |
93 | && npc->enemy->type != GOLEM) |
92 | && npc->enemy->type != GOLEM) |
94 | npc->enemy = NULL; |
93 | npc->enemy = NULL; |
95 | |
94 | |
96 | } |
95 | } |
|
|
96 | |
97 | return can_detect_enemy (npc, npc->enemy, rv) ? npc->enemy : NULL; |
97 | return can_detect_enemy (npc, npc->enemy, rv) ? npc->enemy : NULL; |
98 | } |
98 | } |
99 | |
99 | |
100 | /* Returns the nearest living creature (monster or generator). |
100 | /* Returns the nearest living creature (monster or generator). |
101 | * Modified to deal with tiled maps properly. |
101 | * Modified to deal with tiled maps properly. |
… | |
… | |
145 | } |
145 | } |
146 | |
146 | |
147 | return 0; |
147 | return 0; |
148 | } |
148 | } |
149 | |
149 | |
150 | |
|
|
151 | /* Tries to find an enmy for npc. We pass the range vector since |
150 | /* Tries to find an enemy for npc. We pass the range vector since |
152 | * our caller will find the information useful. |
151 | * our caller will find the information useful. |
153 | * Currently, only move_monster calls this function. |
152 | * Currently, only move_monster calls this function. |
154 | * Fix function so that we always make calls to get_rangevector |
153 | * Fix function so that we always make calls to get_rangevector |
155 | * if we have a valid target - function as not doing so in |
154 | * if we have a valid target - function as not doing so in |
156 | * many cases. |
155 | * many cases. |
157 | */ |
156 | */ |
158 | |
|
|
159 | object * |
157 | object * |
160 | find_enemy (object *npc, rv_vector * rv) |
158 | find_enemy (object *npc, rv_vector * rv) |
161 | { |
159 | { |
162 | object *attacker, *tmp = NULL; |
160 | object *attacker, *tmp = NULL; |
163 | |
161 | |
… | |
… | |
169 | { |
167 | { |
170 | tmp = find_nearest_living_creature (npc); |
168 | tmp = find_nearest_living_creature (npc); |
171 | |
169 | |
172 | if (tmp) |
170 | if (tmp) |
173 | get_rangevector (npc, tmp, rv, 0); |
171 | get_rangevector (npc, tmp, rv, 0); |
|
|
172 | |
174 | return tmp; |
173 | return tmp; |
175 | } |
174 | } |
176 | |
175 | |
177 | /* Here is the main enemy selection. |
176 | /* Here is the main enemy selection. |
178 | * We want this: if there is an enemy, attack him until its not possible or |
177 | * We want this: if there is an enemy, attack him until its not possible or |
… | |
… | |
231 | /* Sees if this monster should wake up. |
230 | /* Sees if this monster should wake up. |
232 | * Currently, this is only called from move_monster, and |
231 | * Currently, this is only called from move_monster, and |
233 | * if enemy is set, then so should be rv. |
232 | * if enemy is set, then so should be rv. |
234 | * returns 1 if the monster should wake up, 0 otherwise. |
233 | * returns 1 if the monster should wake up, 0 otherwise. |
235 | */ |
234 | */ |
236 | |
|
|
237 | int |
235 | int |
238 | check_wakeup (object *op, object *enemy, rv_vector * rv) |
236 | check_wakeup (object *op, object *enemy, rv_vector * rv) |
239 | { |
237 | { |
240 | int radius = op->stats.Wis > MIN_MON_RADIUS ? op->stats.Wis : MIN_MON_RADIUS; |
238 | int radius = op->stats.Wis > MIN_MON_RADIUS ? op->stats.Wis : MIN_MON_RADIUS; |
241 | |
239 | |
… | |
… | |
289 | } |
287 | } |
290 | |
288 | |
291 | /* |
289 | /* |
292 | * Move-monster returns 1 if the object has been freed, otherwise 0. |
290 | * Move-monster returns 1 if the object has been freed, otherwise 0. |
293 | */ |
291 | */ |
294 | |
|
|
295 | int |
292 | int |
296 | move_monster (object *op) |
293 | move_monster (object *op) |
297 | { |
294 | { |
298 | int dir, diff, pre_att_dir; /* elmex: pre_att_dir remembers the direction before attack movement */ |
295 | int dir, diff, pre_att_dir; /* elmex: pre_att_dir remembers the direction before attack movement */ |
299 | object *owner, *enemy, *part, *oph = op; |
296 | object *owner, *enemy, *part, *oph = op; |
… | |
… | |
973 | if (!(--wand->stats.food)) |
970 | if (!(--wand->stats.food)) |
974 | { |
971 | { |
975 | if (wand->arch) |
972 | if (wand->arch) |
976 | { |
973 | { |
977 | CLEAR_FLAG (wand, FLAG_ANIMATE); |
974 | CLEAR_FLAG (wand, FLAG_ANIMATE); |
978 | wand->face = wand->arch->clone.face; |
975 | wand->face = wand->arch->face; |
979 | wand->set_speed (0); |
976 | wand->set_speed (0); |
980 | } |
977 | } |
981 | } |
978 | } |
982 | /* Success */ |
979 | /* Success */ |
983 | return 1; |
980 | return 1; |
… | |
… | |
1208 | case ROD: |
1205 | case ROD: |
1209 | flag = QUERY_FLAG (monster, FLAG_USE_RANGE); |
1206 | flag = QUERY_FLAG (monster, FLAG_USE_RANGE); |
1210 | break; |
1207 | break; |
1211 | |
1208 | |
1212 | case SPELLBOOK: |
1209 | case SPELLBOOK: |
1213 | flag = (monster->arch != NULL && QUERY_FLAG ((&monster->arch->clone), FLAG_CAST_SPELL)); |
1210 | flag = monster->arch && QUERY_FLAG (monster->arch, FLAG_CAST_SPELL); |
1214 | break; |
1211 | break; |
1215 | |
1212 | |
1216 | case SCROLL: |
1213 | case SCROLL: |
1217 | flag = QUERY_FLAG (monster, FLAG_USE_SCROLL); |
1214 | flag = QUERY_FLAG (monster, FLAG_USE_SCROLL); |
1218 | break; |
1215 | break; |
… | |
… | |
1287 | void |
1284 | void |
1288 | monster_check_apply (object *mon, object *item) |
1285 | monster_check_apply (object *mon, object *item) |
1289 | { |
1286 | { |
1290 | int flag = 0; |
1287 | int flag = 0; |
1291 | |
1288 | |
1292 | if (item->type == SPELLBOOK && mon->arch && (QUERY_FLAG ((&mon->arch->clone), FLAG_CAST_SPELL))) |
1289 | if (item->type == SPELLBOOK && mon->arch && (QUERY_FLAG (mon->arch, FLAG_CAST_SPELL))) |
1293 | { |
1290 | { |
1294 | SET_FLAG (mon, FLAG_CAST_SPELL); |
1291 | SET_FLAG (mon, FLAG_CAST_SPELL); |
1295 | return; |
1292 | return; |
1296 | } |
1293 | } |
1297 | |
1294 | |
… | |
… | |
1669 | * properly. I also so odd code in place that checked for x distance |
1666 | * properly. I also so odd code in place that checked for x distance |
1670 | * OR y distance being within some range - that seemed wrong - both should |
1667 | * OR y distance being within some range - that seemed wrong - both should |
1671 | * be within the valid range. MSW 2001-08-05 |
1668 | * be within the valid range. MSW 2001-08-05 |
1672 | * Returns 0 if enemy can not be detected, 1 if it is detected |
1669 | * Returns 0 if enemy can not be detected, 1 if it is detected |
1673 | */ |
1670 | */ |
1674 | |
|
|
1675 | int |
1671 | int |
1676 | can_detect_enemy (object *op, object *enemy, rv_vector * rv) |
1672 | can_detect_enemy (object *op, object *enemy, rv_vector * rv) |
1677 | { |
1673 | { |
1678 | int radius = MIN_MON_RADIUS, hide_discovery; |
1674 | int radius = MIN_MON_RADIUS, hide_discovery; |
1679 | |
1675 | |
… | |
… | |
1710 | /* use this for invis also */ |
1706 | /* use this for invis also */ |
1711 | hide_discovery = op->stats.Int / 5; |
1707 | hide_discovery = op->stats.Int / 5; |
1712 | |
1708 | |
1713 | /* Determine Detection radii */ |
1709 | /* Determine Detection radii */ |
1714 | if (!enemy->hide) /* to detect non-hidden (eg dark/invis enemy) */ |
1710 | if (!enemy->hide) /* to detect non-hidden (eg dark/invis enemy) */ |
1715 | radius = (op->stats.Wis / 5) + 1 > MIN_MON_RADIUS ? (op->stats.Wis / 5) + 1 : MIN_MON_RADIUS; |
1711 | radius = max (MIN_MON_RADIUS, op->stats.Wis / 5 + 1); |
1716 | else |
1712 | else |
1717 | { /* a level/INT/Dex adjustment for hiding */ |
1713 | { /* a level/INT/Dex adjustment for hiding */ |
1718 | object *sk_hide; |
|
|
1719 | int bonus = (op->level / 2) + (op->stats.Int / 5); |
1714 | int bonus = op->level / 2 + op->stats.Int / 5; |
1720 | |
1715 | |
1721 | if (enemy->type == PLAYER) |
1716 | if (enemy->type == PLAYER) |
1722 | { |
1717 | { |
1723 | if ((sk_hide = find_skill_by_number (enemy, SK_HIDING))) |
1718 | if (object *sk_hide = find_skill_by_number (enemy, SK_HIDING)) |
1724 | bonus -= sk_hide->level; |
1719 | bonus -= sk_hide->level; |
1725 | else |
1720 | else |
1726 | { |
1721 | { |
1727 | LOG (llevError, "can_detect_enemy() got hidden player w/o hiding skill!\n"); |
1722 | LOG (llevError, "can_detect_enemy() got hidden player w/o hiding skill!\n"); |
1728 | make_visible (enemy); |
1723 | make_visible (enemy); |
… | |
… | |
1737 | } /* else creature has modifiers for hiding */ |
1732 | } /* else creature has modifiers for hiding */ |
1738 | |
1733 | |
1739 | /* Radii stealth adjustment. Only if you are stealthy |
1734 | /* Radii stealth adjustment. Only if you are stealthy |
1740 | * will you be able to sneak up closer to creatures */ |
1735 | * will you be able to sneak up closer to creatures */ |
1741 | if (QUERY_FLAG (enemy, FLAG_STEALTH)) |
1736 | if (QUERY_FLAG (enemy, FLAG_STEALTH)) |
1742 | radius = radius / 2, hide_discovery = hide_discovery / 3; |
1737 | { |
|
|
1738 | radius /= 2; |
|
|
1739 | hide_discovery /= 3; |
|
|
1740 | } |
1743 | |
1741 | |
1744 | /* Radii adjustment for enemy standing in the dark */ |
1742 | /* Radii adjustment for enemy standing in the dark */ |
1745 | if (op->map->darkness > 0 && !stand_in_light (enemy)) |
1743 | if (op->map->darkness > 0 && !stand_in_light (enemy)) |
1746 | { |
1744 | { |
1747 | /* on dark maps body heat can help indicate location with infravision |
1745 | /* on dark maps body heat can help indicate location with infravision |
… | |
… | |
1765 | * may have for their map - in that way, creatures at the edge will |
1763 | * may have for their map - in that way, creatures at the edge will |
1766 | * do something. Note that the distance field in the |
1764 | * do something. Note that the distance field in the |
1767 | * vector is real distance, so in theory this should be 18 to |
1765 | * vector is real distance, so in theory this should be 18 to |
1768 | * find that. |
1766 | * find that. |
1769 | */ |
1767 | */ |
1770 | if (radius > 13) |
1768 | // note that the above reasoning was utter bullshit even at the time it was written |
1771 | radius = 13; |
1769 | // we use 25, lets see if we have the cpu time for it |
|
|
1770 | radius = min (25, radius); |
1772 | |
1771 | |
1773 | /* Enemy in range! Now test for detection */ |
1772 | /* Enemy in range! Now test for detection */ |
1774 | if ((int) rv->distance <= radius) |
1773 | if (rv->distance <= radius) |
1775 | { |
1774 | { |
1776 | /* ah, we are within range, detected? take cases */ |
1775 | /* ah, we are within range, detected? take cases */ |
1777 | if (!enemy->invisible) /* enemy in dark squares... are seen! */ |
1776 | if (!enemy->invisible) /* enemy in dark squares... are seen! */ |
1778 | return 1; |
1777 | return 1; |
1779 | |
1778 | |
1780 | /* hidden or low-quality invisible */ |
1779 | /* hidden or low-quality invisible */ |
1781 | if (enemy->hide && (rv->distance <= 1) && (rndm (100) <= hide_discovery)) |
1780 | if (enemy->hide && rv->distance <= 1 && rndm (100) <= hide_discovery) |
1782 | { |
1781 | { |
1783 | make_visible (enemy); |
1782 | make_visible (enemy); |
|
|
1783 | |
1784 | /* inform players of new status */ |
1784 | /* inform players of new status */ |
1785 | if (enemy->type == PLAYER && player_can_view (enemy, op)) |
1785 | if (enemy->type == PLAYER && player_can_view (enemy, op)) |
1786 | new_draw_info_format (NDI_UNIQUE, 0, enemy, "You are discovered by %s!", &op->name); |
1786 | new_draw_info_format (NDI_UNIQUE, 0, enemy, "You are discovered by %s!", &op->name); |
|
|
1787 | |
1787 | return 1; /* detected enemy */ |
1788 | return 1; /* detected enemy */ |
1788 | } |
1789 | } |
1789 | else if (enemy->invisible) |
1790 | else if (enemy->invisible) |
1790 | { |
1791 | { |
1791 | /* Change this around - instead of negating the invisible, just |
1792 | /* Change this around - instead of negating the invisible, just |
1792 | * return true so that the mosnter that managed to detect you can |
1793 | * return true so that the monster that managed to detect you can |
1793 | * do something to you. Decreasing the duration of invisible |
1794 | * do something to you. Decreasing the duration of invisible |
1794 | * doesn't make a lot of sense IMO, as a bunch of stupid creatures |
1795 | * doesn't make a lot of sense IMO, as a bunch of stupid creatures |
1795 | * can then basically negate the spell. The spell isn't negated - |
1796 | * can then basically negate the spell. The spell isn't negated - |
1796 | * they just know where you are! |
1797 | * they just know where you are! |
1797 | */ |
1798 | */ |
1798 | if ((rndm (50)) <= hide_discovery) |
1799 | if (rndm (50) <= hide_discovery) |
1799 | { |
1800 | { |
1800 | if (enemy->type == PLAYER) |
1801 | if (enemy->type == PLAYER) |
1801 | { |
|
|
1802 | new_draw_info_format (NDI_UNIQUE, 0, enemy, "You see %s noticing your position.", query_name (op)); |
1802 | new_draw_info_format (NDI_UNIQUE, 0, enemy, "You see %s noticing your position.", query_name (op)); |
1803 | } |
1803 | |
1804 | return 1; |
1804 | return 1; |
1805 | } |
1805 | } |
1806 | } |
1806 | } |
1807 | } /* within range */ |
1807 | } /* within range */ |
1808 | |
1808 | |
… | |
… | |
1816 | * other side of a wall (!). |
1816 | * other side of a wall (!). |
1817 | */ |
1817 | */ |
1818 | int |
1818 | int |
1819 | stand_in_light (object *op) |
1819 | stand_in_light (object *op) |
1820 | { |
1820 | { |
1821 | sint16 nx, ny; |
|
|
1822 | maptile *m; |
|
|
1823 | |
|
|
1824 | if (!op) |
1821 | if (!op) |
1825 | return 0; |
1822 | return 0; |
1826 | |
1823 | |
1827 | if (op->glow_radius > 0) |
1824 | if (op->glow_radius > 0) |
1828 | return 1; |
1825 | return 1; |
1829 | |
1826 | |
1830 | if (op->map) |
1827 | if (op->map) |
1831 | { |
1828 | { |
1832 | int x, y, x1, y1; |
|
|
1833 | |
|
|
1834 | /* Check the spaces with the max light radius to see if any of them |
1829 | /* Check the spaces with the max light radius to see if any of them |
1835 | * have lights, and if any of them light the player enough, then return 1. |
1830 | * have lights, and if any of them light the player enough, then return 1. |
1836 | */ |
1831 | */ |
1837 | for (x = op->x - MAX_LIGHT_RADII; x <= op->x + MAX_LIGHT_RADII; x++) |
1832 | for (int x = op->x - MAX_LIGHT_RADII; x <= op->x + MAX_LIGHT_RADII; x++) |
1838 | { |
1833 | { |
1839 | for (y = op->y - MAX_LIGHT_RADII; y <= op->y + MAX_LIGHT_RADII; y++) |
1834 | for (int y = op->y - MAX_LIGHT_RADII; y <= op->y + MAX_LIGHT_RADII; y++) |
1840 | { |
1835 | { |
1841 | m = op->map; |
1836 | maptile *m = op->map; |
1842 | nx = x; |
1837 | sint16 nx = x; |
1843 | ny = y; |
1838 | sint16 ny = y; |
1844 | |
1839 | |
1845 | if (get_map_flags (m, &m, nx, ny, &nx, &ny) & P_OUT_OF_MAP) |
1840 | if (xy_normalise (m, nx, ny)) |
1846 | continue; |
1841 | if (idistance (x - op->x, y - op->y) < m->at (nx, ny).light) |
1847 | |
|
|
1848 | x1 = abs (x - op->x) * abs (x - op->x); |
|
|
1849 | y1 = abs (y - op->y) * abs (y - op->y); |
|
|
1850 | if (isqrt (x1 + y1) < GET_MAP_LIGHT (m, nx, ny)) |
|
|
1851 | return 1; |
1842 | return 1; |
1852 | } |
1843 | } |
1853 | } |
1844 | } |
1854 | } |
1845 | } |
1855 | |
1846 | |
1856 | return 0; |
1847 | return 0; |