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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
#[cfg(feature = "worldgen")]
use crate::rtsim::RtSim;
use crate::{
client::Client,
comp::{
agent::{Agent, AgentEvent, Sound, SoundKind},
loot_owner::LootOwner,
skillset::SkillGroupKind,
BuffKind, BuffSource, PhysicsState,
},
error,
events::entity_creation::handle_create_npc,
pet::tame_pet,
state_ext::StateExt,
sys::terrain::{NpcData, SpawnEntityData, SAFE_ZONE_RADIUS},
Server, Settings, SpawnPoint,
};
#[cfg(feature = "worldgen")]
use common::rtsim::{Actor, RtSimEntity};
use common::{
assets::AssetExt,
combat::{
self, AttackSource, DamageContributor, DeathEffect, DeathEffects,
BASE_PARRIED_POISE_PUNISHMENT,
},
comp::{
self,
aura::{self, EnteredAuras},
buff,
chat::{KillSource, KillType},
inventory::item::{AbilityMap, MaterialStatManifest},
item::flatten_counted_items,
loot_owner::LootOwnerKind,
Alignment, Auras, Body, BuffEffect, CharacterState, Energy, Group, Hardcore, Health,
Inventory, Object, PickupItem, Player, Poise, PoiseChange, Pos, Presence, PresenceKind,
SkillSet, Stats, BASE_ABILITY_LIMIT,
},
consts::TELEPORTER_RADIUS,
event::{
AuraEvent, BonkEvent, BuffEvent, ChangeAbilityEvent, ChangeBodyEvent, ChangeStanceEvent,
ChatEvent, ComboChangeEvent, CreateItemDropEvent, CreateNpcEvent, CreateObjectEvent,
DeleteEvent, DestroyEvent, EmitExt, Emitter, EnergyChangeEvent, EntityAttackedHookEvent,
EventBus, ExplosionEvent, HealthChangeEvent, KnockbackEvent, LandOnGroundEvent,
MakeAdminEvent, ParryHookEvent, PoiseChangeEvent, RegrowHeadEvent, RemoveLightEmitterEvent,
RespawnEvent, SoundEvent, StartTeleportingEvent, TeleportToEvent, TeleportToPositionEvent,
TransformEvent, UpdateMapMarkerEvent,
},
event_emitters,
generation::{EntityConfig, EntityInfo},
link::Is,
lottery::distribute_many,
mounting::{Rider, VolumeRider},
outcome::{HealthChangeInfo, Outcome},
resources::{EntitiesDiedLastTick, ProgramTime, Secs, Time},
spiral::Spiral2d,
states::utils::StageSection,
terrain::{Block, BlockKind, TerrainGrid},
trade::{TradeResult, Trades},
uid::{IdMaps, Uid},
util::Dir,
vol::ReadVol,
CachedSpatialGrid, Damage, DamageKind, DamageSource, GroupTarget, RadiusEffect,
};
use common_net::{msg::ServerGeneral, sync::WorldSyncExt, synced_components::Heads};
use common_state::{AreasContainer, BlockChange, NoDurabilityArea};
use hashbrown::HashSet;
use rand::Rng;
#[cfg(feature = "worldgen")]
use specs::WriteExpect;
use specs::{
shred, DispatcherBuilder, Entities, Entity as EcsEntity, Entity, Join, LendJoin, Read,
ReadExpect, ReadStorage, SystemData, WorldExt, Write, WriteStorage,
};
#[cfg(feature = "worldgen")] use std::sync::Arc;
use std::{borrow::Cow, collections::HashMap, iter, time::Duration};
use tracing::{debug, warn};
use vek::{Vec2, Vec3};
#[cfg(feature = "worldgen")]
use world::{IndexOwned, World};
use super::{event_dispatch, event_sys_name, ServerEvent};
pub(super) fn register_event_systems(builder: &mut DispatcherBuilder) {
event_dispatch::<PoiseChangeEvent>(builder, &[]);
event_dispatch::<HealthChangeEvent>(builder, &[]);
event_dispatch::<KnockbackEvent>(builder, &[]);
event_dispatch::<DestroyEvent>(builder, &[&event_sys_name::<HealthChangeEvent>()]);
event_dispatch::<LandOnGroundEvent>(builder, &[]);
event_dispatch::<RespawnEvent>(builder, &[]);
event_dispatch::<ExplosionEvent>(builder, &[]);
event_dispatch::<BonkEvent>(builder, &[]);
event_dispatch::<AuraEvent>(builder, &[]);
event_dispatch::<BuffEvent>(builder, &[]);
event_dispatch::<EnergyChangeEvent>(builder, &[]);
event_dispatch::<ComboChangeEvent>(builder, &[]);
event_dispatch::<ParryHookEvent>(builder, &[]);
event_dispatch::<TeleportToEvent>(builder, &[]);
event_dispatch::<EntityAttackedHookEvent>(builder, &[]);
event_dispatch::<ChangeAbilityEvent>(builder, &[]);
event_dispatch::<UpdateMapMarkerEvent>(builder, &[]);
event_dispatch::<MakeAdminEvent>(builder, &[]);
event_dispatch::<ChangeStanceEvent>(builder, &[]);
event_dispatch::<ChangeBodyEvent>(builder, &[]);
event_dispatch::<RemoveLightEmitterEvent>(builder, &[]);
event_dispatch::<TeleportToPositionEvent>(builder, &[]);
event_dispatch::<StartTeleportingEvent>(builder, &[]);
event_dispatch::<RegrowHeadEvent>(builder, &[]);
}
event_emitters! {
struct ReadExplosionEvents[ExplosionEmitters] {
health_change: HealthChangeEvent,
energy_change: EnergyChangeEvent,
poise_change: PoiseChangeEvent,
sound: SoundEvent,
parry_hook: ParryHookEvent,
kockback: KnockbackEvent,
entity_attack_hoow: EntityAttackedHookEvent,
combo_change: ComboChangeEvent,
buff: BuffEvent,
bonk: BonkEvent,
}
struct ReadEntityAttackedHookEvents[EntityAttackedHookEmitters] {
buff: BuffEvent,
combo_change: ComboChangeEvent,
knockback: KnockbackEvent,
energy_change: EnergyChangeEvent,
}
}
pub fn handle_delete(server: &mut Server, DeleteEvent(entity): DeleteEvent) {
let _ = server
.state_mut()
.delete_entity_recorded(entity)
.map_err(|e| error!(?e, ?entity, "Failed to delete destroyed entity"));
}
#[derive(Hash, Eq, PartialEq)]
enum DamageContrib {
Solo(EcsEntity),
Group(Group),
NotFound,
}
impl ServerEvent for PoiseChangeEvent {
type SystemData<'a> = (
Entities<'a>,
ReadStorage<'a, CharacterState>,
WriteStorage<'a, Poise>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(entities, character_states, mut poises): Self::SystemData<'_>,
) {
for ev in events {
if let Some((character_state, mut poise)) = (&character_states, &mut poises)
.lend_join()
.get(ev.entity, &entities)
{
// Entity is invincible to poise change during stunned character state
if !matches!(character_state, CharacterState::Stunned(_)) {
poise.change(ev.change);
}
}
}
}
}
#[cfg(feature = "worldgen")]
pub fn entity_as_actor(
entity: Entity,
rtsim_entities: &ReadStorage<RtSimEntity>,
presences: &ReadStorage<Presence>,
) -> Option<Actor> {
if let Some(rtsim_entity) = rtsim_entities.get(entity).copied() {
Some(Actor::Npc(rtsim_entity.0))
} else if let Some(PresenceKind::Character(character)) = presences.get(entity).map(|p| p.kind) {
Some(Actor::Character(character))
} else {
None
}
}
#[derive(SystemData)]
pub struct HealthChangeEventData<'a> {
entities: Entities<'a>,
#[cfg(feature = "worldgen")]
rtsim: WriteExpect<'a, RtSim>,
outcomes: Read<'a, EventBus<Outcome>>,
destroy_events: Read<'a, EventBus<DestroyEvent>>,
time: Read<'a, Time>,
#[cfg(feature = "worldgen")]
id_maps: Read<'a, IdMaps>,
#[cfg(feature = "worldgen")]
world: ReadExpect<'a, Arc<World>>,
#[cfg(feature = "worldgen")]
index: ReadExpect<'a, IndexOwned>,
positions: ReadStorage<'a, Pos>,
uids: ReadStorage<'a, Uid>,
#[cfg(feature = "worldgen")]
presences: ReadStorage<'a, Presence>,
#[cfg(feature = "worldgen")]
rtsim_entities: ReadStorage<'a, RtSimEntity>,
agents: WriteStorage<'a, Agent>,
healths: WriteStorage<'a, Health>,
heads: WriteStorage<'a, Heads>,
}
impl ServerEvent for HealthChangeEvent {
type SystemData<'a> = HealthChangeEventData<'a>;
fn handle(events: impl ExactSizeIterator<Item = Self>, mut data: Self::SystemData<'_>) {
let mut outcomes_emitter = data.outcomes.emitter();
let mut destroy_emitter = data.destroy_events.emitter();
let mut rng = rand::thread_rng();
for ev in events {
if let Some((mut health, pos, uid, heads)) = (
&mut data.healths,
data.positions.maybe(),
data.uids.maybe(),
(&mut data.heads).maybe(),
)
.lend_join()
.get(ev.entity, &data.entities)
{
// If the change amount was not zero
let changed = health.change_by(ev.change);
if let Some(mut heads) = heads {
// We want some hp to be left for a headless body, so we divide by (max amount
// of heads + 2)
let hp_per_head = health.maximum() / (heads.capacity() as f32 + 2.0);
let target_heads = (health.current() / hp_per_head) as usize;
if heads.amount() > 0 && ev.change.amount < 0.0 && heads.amount() > target_heads
{
for _ in target_heads..heads.amount() {
if let Some(head) = heads.remove_one(&mut rng, *data.time) {
if let Some(uid) = uid {
outcomes_emitter.emit(Outcome::HeadLost { uid: *uid, head });
}
} else {
break;
}
}
}
}
#[cfg(feature = "worldgen")]
let entity_as_actor =
|entity| entity_as_actor(entity, &data.rtsim_entities, &data.presences);
#[cfg(feature = "worldgen")]
if let Some(actor) = entity_as_actor(ev.entity) {
let cause = ev
.change
.damage_by()
.map(|by| by.uid())
.and_then(|uid| data.id_maps.uid_entity(uid))
.and_then(entity_as_actor);
data.rtsim.hook_rtsim_actor_hp_change(
&data.world,
data.index.as_index_ref(),
actor,
cause,
health.fraction(),
);
}
if let (Some(pos), Some(uid)) = (pos, uid) {
if changed {
outcomes_emitter.emit(Outcome::HealthChange {
pos: pos.0,
info: HealthChangeInfo {
amount: ev.change.amount,
by: ev.change.by,
target: *uid,
cause: ev.change.cause,
precise: ev.change.precise,
instance: ev.change.instance,
},
});
}
}
if !health.is_dead && health.should_die() {
destroy_emitter.emit(DestroyEvent {
entity: ev.entity,
cause: ev.change,
});
}
}
// This if statement filters out anything under 5 damage, for DOT ticks
// TODO: Find a better way to separate direct damage from DOT here
let damage = -ev.change.amount;
if damage > 5.0 {
if let Some(agent) = data.agents.get_mut(ev.entity) {
agent.inbox.push_back(AgentEvent::Hurt);
}
}
}
}
}
impl ServerEvent for KnockbackEvent {
type SystemData<'a> = (
Entities<'a>,
ReadStorage<'a, Client>,
ReadStorage<'a, PhysicsState>,
ReadStorage<'a, comp::Mass>,
WriteStorage<'a, comp::Vel>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(entities, clients, physic_states, mass, mut velocities): Self::SystemData<'_>,
) {
for ev in events {
if let Some((physics, mass, vel, client)) = (
&physic_states,
mass.maybe(),
&mut velocities,
clients.maybe(),
)
.lend_join()
.get(ev.entity, &entities)
{
//Check if the entity is on a surface. If it is not, reduce knockback.
let mut impulse = ev.impulse
* if physics.on_surface().is_some() {
1.0
} else {
0.4
};
// we go easy on the little ones (because they fly so far)
impulse /= mass.map_or(0.0, |m| m.0).max(40.0);
vel.0 += impulse;
if let Some(client) = client {
client.send_fallible(ServerGeneral::Knockback(impulse));
}
}
}
}
}
fn handle_exp_gain(
exp_reward: f32,
inventory: &Inventory,
skill_set: &mut SkillSet,
uid: &Uid,
outcomes_emitter: &mut Emitter<Outcome>,
) {
use comp::inventory::{item::ItemKind, slot::EquipSlot};
// Create hash set of xp pools to consider splitting xp amongst
let mut xp_pools = HashSet::<SkillGroupKind>::new();
// Insert general pool since it is always accessible
xp_pools.insert(SkillGroupKind::General);
// Closure to add xp pool corresponding to weapon type equipped in a particular
// EquipSlot
let mut add_tool_from_slot = |equip_slot| {
let tool_kind = inventory
.equipped(equip_slot)
.and_then(|i| match &*i.kind() {
ItemKind::Tool(tool) if tool.kind.gains_combat_xp() => Some(tool.kind),
_ => None,
});
if let Some(weapon) = tool_kind {
// Only adds to xp pools if entity has that skill group available
if skill_set.skill_group_accessible(SkillGroupKind::Weapon(weapon)) {
xp_pools.insert(SkillGroupKind::Weapon(weapon));
}
}
};
// Add weapons to xp pools considered
add_tool_from_slot(EquipSlot::ActiveMainhand);
add_tool_from_slot(EquipSlot::ActiveOffhand);
add_tool_from_slot(EquipSlot::InactiveMainhand);
add_tool_from_slot(EquipSlot::InactiveOffhand);
let num_pools = xp_pools.len() as f32;
for pool in xp_pools.iter() {
if let Some(level_outcome) =
skill_set.add_experience(*pool, (exp_reward / num_pools).ceil() as u32)
{
outcomes_emitter.emit(Outcome::SkillPointGain {
uid: *uid,
skill_tree: *pool,
total_points: level_outcome,
});
}
}
outcomes_emitter.emit(Outcome::ExpChange {
uid: *uid,
exp: exp_reward as u32,
xp_pools,
});
}
#[derive(SystemData)]
pub struct DestroyEventData<'a> {
entities: Entities<'a>,
#[cfg(feature = "worldgen")]
rtsim: WriteExpect<'a, RtSim>,
id_maps: Read<'a, IdMaps>,
msm: ReadExpect<'a, MaterialStatManifest>,
ability_map: ReadExpect<'a, AbilityMap>,
time: Read<'a, Time>,
program_time: ReadExpect<'a, ProgramTime>,
#[cfg(feature = "worldgen")]
world: ReadExpect<'a, Arc<World>>,
#[cfg(feature = "worldgen")]
index: ReadExpect<'a, IndexOwned>,
areas_container: Read<'a, AreasContainer<NoDurabilityArea>>,
outcomes: Read<'a, EventBus<Outcome>>,
create_item_drop: Read<'a, EventBus<CreateItemDropEvent>>,
delete_event: Read<'a, EventBus<DeleteEvent>>,
transform_events: Read<'a, EventBus<TransformEvent>>,
chat_events: Read<'a, EventBus<ChatEvent>>,
entities_died_last_tick: Write<'a, EntitiesDiedLastTick>,
melees: WriteStorage<'a, comp::Melee>,
beams: WriteStorage<'a, comp::Beam>,
skill_sets: WriteStorage<'a, SkillSet>,
inventories: WriteStorage<'a, Inventory>,
item_drops: WriteStorage<'a, comp::ItemDrops>,
velocities: WriteStorage<'a, comp::Vel>,
force_updates: WriteStorage<'a, comp::ForceUpdate>,
energies: WriteStorage<'a, Energy>,
character_states: WriteStorage<'a, CharacterState>,
death_effects: WriteStorage<'a, DeathEffects>,
players: ReadStorage<'a, Player>,
clients: ReadStorage<'a, Client>,
uids: ReadStorage<'a, Uid>,
positions: ReadStorage<'a, Pos>,
healths: WriteStorage<'a, Health>,
bodies: ReadStorage<'a, Body>,
poises: ReadStorage<'a, Poise>,
groups: ReadStorage<'a, Group>,
alignments: ReadStorage<'a, Alignment>,
stats: ReadStorage<'a, Stats>,
agents: ReadStorage<'a, Agent>,
#[cfg(feature = "worldgen")]
rtsim_entities: ReadStorage<'a, RtSimEntity>,
#[cfg(feature = "worldgen")]
presences: ReadStorage<'a, Presence>,
buff_events: Read<'a, EventBus<BuffEvent>>,
masses: ReadStorage<'a, comp::Mass>,
}
/// Handle an entity dying. If it is a player, it will send a message to all
/// other players. If the entity that killed it had stats, then give it exp for
/// the kill. Experience given is equal to the level of the entity that was
/// killed times 10.
impl ServerEvent for DestroyEvent {
type SystemData<'a> = DestroyEventData<'a>;
fn handle(events: impl ExactSizeIterator<Item = Self>, mut data: Self::SystemData<'_>) {
let mut chat_emitter = data.chat_events.emitter();
let mut create_item_drop = data.create_item_drop.emitter();
let mut delete_emitter = data.delete_event.emitter();
let mut outcomes_emitter = data.outcomes.emitter();
let mut buff_emitter = data.buff_events.emitter();
let mut transform_emitter = data.transform_events.emitter();
data.entities_died_last_tick.0.clear();
for ev in events {
// TODO: Investigate duplicate `Destroy` events (but don't remove this).
// If the entity was already deleted, it can't be destroyed again.
if !data.entities.is_alive(ev.entity) {
continue;
}
let mut outcomes = data.outcomes.emitter();
if let Some(mut health) = data.healths.get_mut(ev.entity) {
if !health.is_dead {
health.is_dead = true;
if let Some(pos) = data.positions.get(ev.entity).copied() {
data.entities_died_last_tick.0.push((ev.entity, pos));
}
} else {
// Skip for entities that have already died
continue;
}
}
// Remove components that should not persist across death
data.melees.remove(ev.entity);
data.beams.remove(ev.entity);
let get_attacker_name = |cause_of_death: KillType, by: Uid| -> KillSource {
// Get attacker entity
if let Some(char_entity) = data.id_maps.uid_entity(by) {
// Check if attacker is another player or entity with stats (npc)
if data.players.contains(char_entity) {
KillSource::Player(by, cause_of_death)
} else if let Some(stats) = data.stats.get(char_entity) {
KillSource::NonPlayer(stats.name.clone(), cause_of_death)
} else {
KillSource::NonExistent(cause_of_death)
}
} else {
KillSource::NonExistent(cause_of_death)
}
};
// Push an outcome if entity is has a character state (entities that don't have
// one, we probably don't care about emitting death outcome)
if let Some((pos, _)) = (&data.positions, &data.character_states)
.lend_join()
.get(ev.entity, &data.entities)
{
outcomes_emitter.emit(Outcome::Death { pos: pos.0 });
}
let mut should_delete = true;
// Handle any effects on death
if let Some(killed_stats) = data.stats.get(ev.entity) {
let attacker_entity = ev.cause.by.and_then(|x| data.id_maps.uid_entity(x.uid()));
let killed_uid = data.uids.get(ev.entity);
let attacker_stats = attacker_entity.and_then(|e| data.stats.get(e));
let attacker_mass = attacker_entity.and_then(|e| data.masses.get(e));
let mut death_effects = data
.death_effects
.remove(ev.entity)
.map(|ef| ef.0.into_iter().map(Cow::Owned));
for effect in killed_stats
.effects_on_death
.iter()
.map(Cow::Borrowed)
.chain(death_effects.as_mut().map_or(
&mut core::iter::empty() as &mut dyn Iterator<Item = Cow<DeathEffect>>,
|death_effects| death_effects as &mut dyn Iterator<Item = Cow<DeathEffect>>,
))
{
match effect.as_ref() {
DeathEffect::AttackerBuff {
kind,
strength,
duration,
} => {
if let Some(attacker) = attacker_entity {
let dest_info = buff::DestInfo {
stats: attacker_stats,
mass: attacker_mass,
};
buff_emitter.emit(BuffEvent {
entity: attacker,
buff_change: buff::BuffChange::Add(buff::Buff::new(
*kind,
buff::BuffData::new(*strength, *duration),
vec![],
if let Some(uid) = killed_uid {
BuffSource::Character { by: *uid }
} else {
BuffSource::World
},
*data.time,
dest_info,
data.masses.get(ev.entity),
)),
});
}
},
DeathEffect::Transform {
entity_spec,
allow_players,
} => {
if data.clients.contains(ev.entity) && !allow_players {
continue;
}
let Some(killed_uid) = killed_uid.copied() else {
warn!(
"Could not handle transform death effect for entity without \
Uid"
);
continue;
};
transform_emitter.emit(TransformEvent {
target_entity: killed_uid,
entity_info: {
let Ok(entity_config) = EntityConfig::load(entity_spec)
.inspect_err(|error| {
error!(
?entity_spec,
?error,
"Could not load entity configuration for death \
effect"
)
})
else {
continue;
};
EntityInfo::at(
data.positions
.get(ev.entity)
.map(|pos| pos.0)
.unwrap_or_default(),
)
.with_entity_config(
entity_config.read().clone(),
Some(entity_spec),
&mut rand::thread_rng(),
None,
)
},
allow_players: *allow_players,
delete_on_failure: true,
});
should_delete = false;
},
}
}
}
// Chat message
// If it was a player that died
if let Some((uid, _player)) = (&data.uids, &data.players)
.lend_join()
.get(ev.entity, &data.entities)
{
let kill_source = match (ev.cause.cause, ev.cause.by.map(|x| x.uid())) {
(Some(DamageSource::Melee), Some(by)) => get_attacker_name(KillType::Melee, by),
(Some(DamageSource::Projectile), Some(by)) => {
get_attacker_name(KillType::Projectile, by)
},
(Some(DamageSource::Explosion), Some(by)) => {
get_attacker_name(KillType::Explosion, by)
},
(Some(DamageSource::Energy), Some(by)) => {
get_attacker_name(KillType::Energy, by)
},
(Some(DamageSource::Buff(buff_kind)), by) => {
if let Some(by) = by {
get_attacker_name(KillType::Buff(buff_kind), by)
} else {
KillSource::NonExistent(KillType::Buff(buff_kind))
}
},
(Some(DamageSource::Other), Some(by)) => get_attacker_name(KillType::Other, by),
(Some(DamageSource::Falling), _) => KillSource::FallDamage,
// HealthSource::Suicide => KillSource::Suicide,
_ => KillSource::Other,
};
chat_emitter.emit(ChatEvent(comp::UnresolvedChatMsg::death(kill_source, *uid)));
}
let mut exp_awards = Vec::<(Entity, f32, Option<Group>)>::new();
// Award EXP to damage contributors
//
// NOTE: Debug logging is disabled by default for this module - to enable it add
// veloren_server::events::entity_manipulation=debug to RUST_LOG
'xp: {
let Some((
entity_skill_set,
entity_health,
entity_energy,
entity_inventory,
entity_body,
entity_poise,
entity_pos,
)) = (
&data.skill_sets,
&data.healths,
&data.energies,
&data.inventories,
&data.bodies,
&data.poises,
&data.positions,
)
.lend_join()
.get(ev.entity, &data.entities)
else {
break 'xp;
};
// Calculate the total EXP award for the kill
let exp_reward = combat::combat_rating(
entity_inventory,
entity_health,
entity_energy,
entity_poise,
entity_skill_set,
*entity_body,
&data.msm,
) * 20.0;
let mut damage_contributors = HashMap::<DamageContrib, (u64, f32)>::new();
for (damage_contributor, damage) in entity_health.damage_contributions() {
match damage_contributor {
DamageContributor::Solo(uid) => {
if let Some(attacker) = data.id_maps.uid_entity(*uid) {
damage_contributors
.insert(DamageContrib::Solo(attacker), (*damage, 0.0));
} else {
// An entity who was not in a group contributed damage but is now
// either dead or offline. Add a
// placeholder to ensure that the contributor's
// exp is discarded, not distributed between
// the other contributors
damage_contributors.insert(DamageContrib::NotFound, (*damage, 0.0));
}
},
DamageContributor::Group {
entity_uid: _,
group,
} => {
// Damage made by entities who were in a group at the time of attack is
// attributed to their group rather than themselves. This allows for all
// members of a group to receive EXP, not just the damage dealers.
let entry = damage_contributors
.entry(DamageContrib::Group(*group))
.or_insert((0, 0.0));
entry.0 += damage;
},
}
}
// Calculate the percentage of total damage that each DamageContributor
// contributed
let total_damage: f64 = damage_contributors
.values()
.map(|(damage, _)| *damage as f64)
.sum();
damage_contributors
.iter_mut()
.for_each(|(_, (damage, percentage))| {
*percentage = (*damage as f64 / total_damage) as f32
});
let destroyed_group = data.groups.get(ev.entity);
let within_range = |attacker_pos: &Pos| {
// Maximum distance that an attacker must be from an entity at the time of its
// death to receive EXP for the kill
const MAX_EXP_DIST: f32 = 150.0;
entity_pos.0.distance_squared(attacker_pos.0) < MAX_EXP_DIST.powi(2)
};
let is_pvp_kill = |attacker: Entity| {
data.players.contains(ev.entity) && data.players.contains(attacker)
};
// Iterate through all contributors of damage for the killed entity, calculating
// how much EXP each contributor should be awarded based on their
// percentage of damage contribution
exp_awards = damage_contributors.iter().filter_map(|(damage_contributor, (_, damage_percent))| {
let contributor_exp = exp_reward * damage_percent;
match damage_contributor {
DamageContrib::Solo(attacker) => {
// No exp for self kills or PvP
if *attacker == ev.entity || is_pvp_kill(*attacker) { return None; }
// Only give EXP to the attacker if they are within EXP range of the killed entity
data.positions.get(*attacker).and_then(|attacker_pos| {
if within_range(attacker_pos) {
debug!("Awarding {} exp to individual {:?} who contributed {}% damage to the kill of {:?}", contributor_exp, attacker, *damage_percent * 100.0, ev.entity);
Some(iter::once((*attacker, contributor_exp, None)).collect())
} else {
None
}
})
},
DamageContrib::Group(group) => {
// Don't give EXP to members in the destroyed entity's group
if destroyed_group == Some(group) { return None; }
// Only give EXP to members of the group that are within EXP range of the killed entity and aren't a pet
let members_in_range = (
&data.entities,
&data.groups,
&data.positions,
data.alignments.maybe(),
&data.uids,
)
.join()
.filter_map(|(member_entity, member_group, member_pos, alignment, uid)| {
if *member_group == *group && within_range(member_pos) && !is_pvp_kill(member_entity) && !matches!(alignment, Some(Alignment::Owned(owner)) if owner != uid) {
Some(member_entity)
} else {
None
}
})
.collect::<Vec<_>>();
if members_in_range.is_empty() { return None; }
// Divide EXP reward by square root of number of people in group for group EXP scaling
let exp_per_member = contributor_exp / (members_in_range.len() as f32).sqrt();
debug!("Awarding {} exp per member of group ID {:?} with {} members which contributed {}% damage to the kill of {:?}", exp_per_member, group, members_in_range.len(), *damage_percent * 100.0, ev.entity);
Some(members_in_range.into_iter().map(|entity| (entity, exp_per_member, Some(*group))).collect::<Vec<(Entity, f32, Option<Group>)>>())
},
DamageContrib::NotFound => {
// Discard exp for dead/offline individual damage contributors
None
}
}
}).flatten().collect::<Vec<(Entity, f32, Option<Group>)>>();
exp_awards.iter().for_each(|(attacker, exp_reward, _)| {
// Process the calculated EXP rewards
if let Some((mut attacker_skill_set, attacker_uid, attacker_inventory)) =
(&mut data.skill_sets, &data.uids, &data.inventories)
.lend_join()
.get(*attacker, &data.entities)
{
handle_exp_gain(
*exp_reward,
attacker_inventory,
&mut attacker_skill_set,
attacker_uid,
&mut outcomes,
);
}
});
};
should_delete &= if data.clients.contains(ev.entity) {
if let Some(vel) = data.velocities.get_mut(ev.entity) {
vel.0 = Vec3::zero();
}
if let Some(force_update) = data.force_updates.get_mut(ev.entity) {
force_update.update();
}
if let Some(mut energy) = data.energies.get_mut(ev.entity) {
energy.refresh();
}
if let Some(mut character_state) = data.character_states.get_mut(ev.entity) {
*character_state = CharacterState::default();
}
false
} else {
if let Some((_agent, pos, alignment, vel)) = (
&data.agents,
&data.positions,
data.alignments.maybe(),
data.velocities.maybe(),
)
.lend_join()
.get(ev.entity, &data.entities)
{
// Only drop loot if entity has agency (not a player),
// and if it is not owned by another entity (not a pet)
if !matches!(alignment, Some(Alignment::Owned(_)))
&& let Some(items) = data
.item_drops
.remove(ev.entity)
.map(|comp::ItemDrops(item)| item)
{
// Remove entries where zero exp was awarded - this happens because some
// entities like Object bodies don't give EXP.
let mut item_receivers = HashMap::new();
for (entity, exp, group) in exp_awards {
if exp >= f32::EPSILON {
let loot_owner = if let Some(group) = group {
Some(LootOwnerKind::Group(group))
} else {
let uid = data.bodies.get(entity).and_then(|body| {
// Only humanoids are awarded loot ownership - if the winner
// was a non-humanoid NPC the loot will be free-for-all
if matches!(body, Body::Humanoid(_)) {
data.uids.get(entity).copied()
} else {
None
}
});
uid.map(LootOwnerKind::Player)
};
*item_receivers.entry(loot_owner).or_insert(0.0) += exp;
}
}
let mut item_offset_spiral =
Spiral2d::new().map(|offset| offset.as_::<f32>() * 0.5);
let mut rng = rand::thread_rng();
let mut spawn_item = |item, loot_owner| {
let offset = item_offset_spiral.next().unwrap_or_default();
create_item_drop.emit(CreateItemDropEvent {
pos: Pos(pos.0 + Vec3::unit_z() * 0.25 + offset),
vel: vel.copied().unwrap_or(comp::Vel(Vec3::zero())),
// TODO: Random
ori: comp::Ori::from(Dir::random_2d(&mut rng)),
item: PickupItem::new(item, *data.program_time),
loot_owner: if let Some(loot_owner) = loot_owner {
debug!(
"Assigned UID {loot_owner:?} as the winner for the loot \
drop"
);
Some(LootOwner::new(loot_owner, false))
} else {
debug!("No loot owner");
None
},
})
};
if item_receivers.is_empty() {
debug!("No item receivers");
for item in flatten_counted_items(&items, &data.ability_map, &data.msm)
{
spawn_item(item, None)
}
} else {
let mut rng = rand::thread_rng();
distribute_many(
item_receivers
.iter()
.map(|(loot_owner, weight)| (*weight, *loot_owner)),
&mut rng,
&items,
|(amount, _)| *amount,
|(_, item), loot_owner, count| {
for item in
item.stacked_duplicates(&data.ability_map, &data.msm, count)
{
spawn_item(item, loot_owner)
}
},
);
}
}
}
true
};
if !should_delete {
let resists_durability =
data.positions
.get(ev.entity)
.cloned()
.map_or(false, |our_pos| {
let our_pos = our_pos.0.map(|i| i as i32);
let is_in_area = data
.areas_container
.areas()
.iter()
.any(|(_, area)| area.contains_point(our_pos));
is_in_area
});
// Modify durability on all equipped items
if !resists_durability
&& let Some(mut inventory) = data.inventories.get_mut(ev.entity)
{
inventory.damage_items(&data.ability_map, &data.msm, *data.time);
}
}
#[cfg(feature = "worldgen")]
let entity_as_actor =
|entity| entity_as_actor(entity, &data.rtsim_entities, &data.presences);
#[cfg(feature = "worldgen")]
if let Some(actor) = entity_as_actor(ev.entity)
// Skip the death hook for rtsim entities if they aren't deleted, otherwise
// we'll end up with rtsim respawning an entity that wasn't actually
// removed, producing 2 entities having the same RtsimEntityId.
&& should_delete
{
data.rtsim.hook_rtsim_actor_death(
&data.world,
data.index.as_index_ref(),
actor,
data.positions.get(ev.entity).map(|p| p.0),
ev.cause
.by
.as_ref()
.and_then(
|(DamageContributor::Solo(entity_uid)
| DamageContributor::Group { entity_uid, .. })| {
data.id_maps.uid_entity(*entity_uid)
},
)
.and_then(entity_as_actor),
);
}
if should_delete {
delete_emitter.emit(DeleteEvent(ev.entity));
}
}
}
}
impl ServerEvent for LandOnGroundEvent {
type SystemData<'a> = (
Read<'a, Time>,
ReadExpect<'a, MaterialStatManifest>,
Read<'a, EventBus<HealthChangeEvent>>,
Read<'a, EventBus<PoiseChangeEvent>>,
ReadStorage<'a, PhysicsState>,
ReadStorage<'a, CharacterState>,
ReadStorage<'a, comp::Mass>,
ReadStorage<'a, Inventory>,
ReadStorage<'a, Stats>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(
time,
msm,
health_change_events,
poise_change_events,
physic_states,
character_states,
masses,
inventories,
stats,
): Self::SystemData<'_>,
) {
let mut health_change_emitter = health_change_events.emitter();
let mut poise_change_emitter = poise_change_events.emitter();
for ev in events {
// HACK: Certain ability movements currently take us above the fall damage
// threshold in the horizontal axis. This factor dampens velocity in the
// horizontal axis when applying fall damage.
let horizontal_damp = 0.5
+ ev.vel
.try_normalized()
.unwrap_or_default()
.dot(Vec3::unit_z())
.abs()
* 0.5;
let relative_vel = ev.vel.dot(-ev.surface_normal) * horizontal_damp;
// The second part of this if statement disables all fall damage when in the
// water. This was added as a *temporary* fix a bug that causes you to take
// fall damage while swimming downwards. FIXME: Fix the actual bug and
// remove the following relevant part of the if statement.
if relative_vel >= 30.0
&& physic_states
.get(ev.entity)
.map_or(true, |ps| ps.in_liquid().is_none())
{
let reduced_vel =
if let Some(CharacterState::DiveMelee(c)) = character_states.get(ev.entity) {
(relative_vel + c.static_data.vertical_speed).min(0.0)
} else {
relative_vel
};
let mass = masses.get(ev.entity).copied().unwrap_or_default();
let impact_energy = mass.0 * reduced_vel.powi(2) / 2.0;
let falldmg = impact_energy / 1000.0;
// Emit health change
let damage = Damage {
source: DamageSource::Falling,
kind: DamageKind::Crushing,
value: falldmg,
};
let damage_reduction = Damage::compute_damage_reduction(
Some(damage),
inventories.get(ev.entity),
stats.get(ev.entity),
&msm,
);
let change = damage.calculate_health_change(
damage_reduction,
0.0,
None,
None,
0.0,
1.0,
*time,
rand::random(),
);
health_change_emitter.emit(HealthChangeEvent {
entity: ev.entity,
change,
});
// Emit poise change
let poise_damage = -(mass.0 * reduced_vel.powi(2) / 1500.0);
let poise_change = Poise::apply_poise_reduction(
poise_damage,
inventories.get(ev.entity),
&msm,
character_states.get(ev.entity),
stats.get(ev.entity),
);
let poise_change = comp::PoiseChange {
amount: poise_change,
impulse: Vec3::unit_z(),
by: None,
cause: None,
time: *time,
};
poise_change_emitter.emit(PoiseChangeEvent {
entity: ev.entity,
change: poise_change,
});
}
}
}
}
impl ServerEvent for RespawnEvent {
type SystemData<'a> = (
Read<'a, SpawnPoint>,
WriteStorage<'a, Health>,
WriteStorage<'a, comp::Combo>,
WriteStorage<'a, Pos>,
WriteStorage<'a, comp::PhysicsState>,
WriteStorage<'a, comp::ForceUpdate>,
WriteStorage<'a, Heads>,
ReadStorage<'a, Client>,
ReadStorage<'a, Hardcore>,
ReadStorage<'a, comp::Waypoint>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(
spawn_point,
mut healths,
mut combos,
mut positions,
mut physic_states,
mut force_updates,
mut heads,
clients,
hardcore,
waypoints,
): Self::SystemData<'_>,
) {
for RespawnEvent(entity) in events {
// Hardcore characters cannot respawn
if !hardcore.contains(entity) && clients.contains(entity) {
let respawn_point = waypoints
.get(entity)
.map(|wp| wp.get_pos())
.unwrap_or(spawn_point.0);
healths.get_mut(entity).map(|mut health| health.revive());
combos.get_mut(entity).map(|mut combo| combo.reset());
positions.get_mut(entity).map(|pos| pos.0 = respawn_point);
heads.get_mut(entity).map(|mut heads| heads.reset());
physic_states
.get_mut(entity)
.map(|phys_state| phys_state.reset());
force_updates
.get_mut(entity)
.map(|force_update| force_update.update());
}
}
}
}
#[derive(SystemData)]
pub struct ExplosionData<'a> {
entities: Entities<'a>,
block_change: Write<'a, BlockChange>,
settings: Read<'a, Settings>,
time: Read<'a, Time>,
id_maps: Read<'a, IdMaps>,
spatial_grid: Read<'a, CachedSpatialGrid>,
terrain: ReadExpect<'a, TerrainGrid>,
msm: ReadExpect<'a, MaterialStatManifest>,
event_busses: ReadExplosionEvents<'a>,
outcomes: Read<'a, EventBus<Outcome>>,
groups: ReadStorage<'a, Group>,
auras: ReadStorage<'a, Auras>,
positions: ReadStorage<'a, Pos>,
players: ReadStorage<'a, Player>,
energies: ReadStorage<'a, Energy>,
combos: ReadStorage<'a, comp::Combo>,
inventories: ReadStorage<'a, Inventory>,
alignments: ReadStorage<'a, Alignment>,
entered_auras: ReadStorage<'a, EnteredAuras>,
buffs: ReadStorage<'a, comp::Buffs>,
stats: ReadStorage<'a, comp::Stats>,
healths: ReadStorage<'a, Health>,
bodies: ReadStorage<'a, Body>,
orientations: ReadStorage<'a, comp::Ori>,
character_states: ReadStorage<'a, CharacterState>,
uids: ReadStorage<'a, Uid>,
masses: ReadStorage<'a, comp::Mass>,
}
impl ServerEvent for ExplosionEvent {
type SystemData<'a> = ExplosionData<'a>;
fn handle(events: impl ExactSizeIterator<Item = Self>, mut data: Self::SystemData<'_>) {
let mut emitters = data.event_busses.get_emitters();
let mut outcome_emitter = data.outcomes.emitter();
// TODO: Faster RNG?
let mut rng = rand::thread_rng();
for ev in events {
let owner_entity = ev.owner.and_then(|uid| data.id_maps.uid_entity(uid));
let explosion_volume = 6.25 * ev.explosion.radius;
emitters.emit(SoundEvent {
sound: Sound::new(SoundKind::Explosion, ev.pos, explosion_volume, data.time.0),
});
let outcome_power = ev.explosion.radius;
outcome_emitter.emit(Outcome::Explosion {
pos: ev.pos,
power: outcome_power,
radius: ev.explosion.radius,
is_attack: ev
.explosion
.effects
.iter()
.any(|e| matches!(e, RadiusEffect::Attack(_))),
reagent: ev.explosion.reagent,
});
/// Used to get strength of explosion effects as they falloff over
/// distance
fn cylinder_sphere_strength(
sphere_pos: Vec3<f32>,
radius: f32,
min_falloff: f32,
cyl_pos: Vec3<f32>,
cyl_body: Body,
) -> f32 {
// 2d check
let horiz_dist = Vec2::<f32>::from(sphere_pos - cyl_pos).distance(Vec2::default())
- cyl_body.max_radius();
// z check
let half_body_height = cyl_body.height() / 2.0;
let vert_distance =
(sphere_pos.z - (cyl_pos.z + half_body_height)).abs() - half_body_height;
// Use whichever gives maximum distance as that closer to real value. Sets
// minimum to 0 as negative values would indicate inside entity.
let distance = horiz_dist.max(vert_distance).max(0.0);
if distance > radius {
// If further than exploion radius, no strength
0.0
} else {
// Falloff inversely proportional to radius
let fall_off = ((distance / radius).min(1.0) - 1.0).abs();
let min_falloff = min_falloff.clamp(0.0, 1.0);
min_falloff + fall_off * (1.0 - min_falloff)
}
}
// TODO: Process terrain destruction first so that entities don't get protected
// by terrain that gets destroyed?
'effects: for effect in ev.explosion.effects {
match effect {
RadiusEffect::TerrainDestruction(power, new_color) => {
const RAYS: usize = 500;
// Prevent block colour changes within the radius of a safe zone aura
if data
.spatial_grid
.0
.in_circle_aabr(ev.pos.xy(), SAFE_ZONE_RADIUS)
.filter_map(|entity| {
data.auras
.get(entity)
.and_then(|entity_auras| {
data.positions.get(entity).map(|pos| (entity_auras, pos))
})
.and_then(|(entity_auras, pos)| {
entity_auras
.auras
.iter()
.find(|(_, aura)| {
matches!(aura.aura_kind, aura::AuraKind::Buff {
kind: BuffKind::Invulnerability,
source: BuffSource::World,
..
})
})
.map(|(_, aura)| (*pos, aura.radius))
})
})
.any(|(aura_pos, aura_radius)| {
ev.pos.distance_squared(aura_pos.0) < aura_radius.powi(2)
})
{
continue 'effects;
}
// Color terrain
let mut touched_blocks = Vec::new();
let color_range = power * 2.7;
for _ in 0..RAYS {
let dir = Vec3::new(
rng.gen::<f32>() - 0.5,
rng.gen::<f32>() - 0.5,
rng.gen::<f32>() - 0.5,
)
.normalized();
let _ = data
.terrain
.ray(ev.pos, ev.pos + dir * color_range)
.until(|_| rng.gen::<f32>() < 0.05)
.for_each(|_: &Block, pos| touched_blocks.push(pos))
.cast();
}
for block_pos in touched_blocks {
if let Ok(block) = data.terrain.get(block_pos) {
if !matches!(block.kind(), BlockKind::Lava | BlockKind::GlowingRock)
&& (
// Check that owner is not player or explosion_burn_marks by
// players
// is enabled
owner_entity.map_or(true, |e| data.players.get(e).is_none())
|| data.settings.gameplay.explosion_burn_marks
)
{
let diff2 =
block_pos.map(|b| b as f32).distance_squared(ev.pos);
let fade = (1.0 - diff2 / color_range.powi(2)).max(0.0);
if let Some(mut color) = block.get_color() {
let r = color[0] as f32
+ (fade
* (color[0] as f32 * 0.5 - color[0] as f32
+ new_color[0]));
let g = color[1] as f32
+ (fade
* (color[1] as f32 * 0.3 - color[1] as f32
+ new_color[1]));
let b = color[2] as f32
+ (fade
* (color[2] as f32 * 0.3 - color[2] as f32
+ new_color[2]));
// Darken blocks, but not too much
color[0] = (r as u8).max(30);
color[1] = (g as u8).max(30);
color[2] = (b as u8).max(30);
data.block_change
.set(block_pos, Block::new(block.kind(), color));
}
}
if block.is_bonkable() {
emitters.emit(BonkEvent {
pos: block_pos.map(|e| e as f32 + 0.5),
owner: ev.owner,
target: None,
});
}
}
}
// Destroy terrain
for _ in 0..RAYS {
let dir = Vec3::new(
rng.gen::<f32>() - 0.5,
rng.gen::<f32>() - 0.5,
rng.gen::<f32>() - 0.15,
)
.normalized();
let mut ray_energy = power;
let from = ev.pos;
let to = ev.pos + dir * power;
let _ = data
.terrain
.ray(from, to)
.while_(|block: &Block| {
ray_energy -= block.explode_power().unwrap_or(0.0)
+ rng.gen::<f32>() * 0.1;
// Stop if:
// 1) Block is liquid
// 2) Consumed all energy
// 3) Can't explode block (for example we hit stone wall)
block.is_liquid()
|| block.explode_power().is_none()
|| ray_energy <= 0.0
})
.for_each(|block: &Block, pos| {
if block.explode_power().is_some() {
data.block_change.set(pos, block.into_vacant());
}
})
.cast();
}
},
RadiusEffect::Attack(attack) => {
for (
entity_b,
pos_b,
health_b,
(body_b_maybe, ori_b_maybe, char_state_b_maybe, uid_b),
) in (
&data.entities,
&data.positions,
&data.healths,
(
data.bodies.maybe(),
data.orientations.maybe(),
data.character_states.maybe(),
&data.uids,
),
)
.join()
.filter(|(_, _, h, _)| !h.is_dead)
{
let dist_sqrd = ev.pos.distance_squared(pos_b.0);
// Check if it is a hit
let strength = if let Some(body) = body_b_maybe {
cylinder_sphere_strength(
ev.pos,
ev.explosion.radius,
ev.explosion.min_falloff,
pos_b.0,
*body,
)
} else {
1.0 - dist_sqrd / ev.explosion.radius.powi(2)
};
// Cast a ray from the explosion to the entity to check visibility
if strength > 0.0
&& (data
.terrain
.ray(ev.pos, pos_b.0)
.until(Block::is_opaque)
.cast()
.0
+ 0.1)
.powi(2)
>= dist_sqrd
{
// See if entities are in the same group
let same_group = owner_entity
.and_then(|e| data.groups.get(e))
.map(|group_a| Some(group_a) == data.groups.get(entity_b))
.unwrap_or(Some(entity_b) == owner_entity);
let target_group = if same_group {
GroupTarget::InGroup
} else {
GroupTarget::OutOfGroup
};
let dir = Dir::new(
(pos_b.0 - ev.pos)
.try_normalized()
.unwrap_or_else(Vec3::unit_z),
);
let attacker_info =
owner_entity.zip(ev.owner).map(|(entity, uid)| {
combat::AttackerInfo {
entity,
uid,
group: data.groups.get(entity),
energy: data.energies.get(entity),
combo: data.combos.get(entity),
inventory: data.inventories.get(entity),
stats: data.stats.get(entity),
mass: data.masses.get(entity),
}
});
let target_info = combat::TargetInfo {
entity: entity_b,
uid: *uid_b,
inventory: data.inventories.get(entity_b),
stats: data.stats.get(entity_b),
health: Some(health_b),
pos: pos_b.0,
ori: ori_b_maybe,
char_state: char_state_b_maybe,
energy: data.energies.get(entity_b),
buffs: data.buffs.get(entity_b),
mass: data.masses.get(entity_b),
};
let target_dodging = char_state_b_maybe
.and_then(|cs| cs.roll_attack_immunities())
.map_or(false, |i| i.explosions);
let allow_friendly_fire =
owner_entity.is_some_and(|owner_entity| {
combat::allow_friendly_fire(
&data.entered_auras,
owner_entity,
entity_b,
)
});
// PvP check
let permit_pvp = combat::permit_pvp(
&data.alignments,
&data.players,
&data.entered_auras,
&data.id_maps,
owner_entity,
entity_b,
);
let attack_options = combat::AttackOptions {
target_dodging,
permit_pvp,
allow_friendly_fire,
target_group,
precision_mult: None,
};
attack.apply_attack(
attacker_info,
&target_info,
dir,
attack_options,
strength,
combat::AttackSource::Explosion,
*data.time,
&mut emitters,
|o| outcome_emitter.emit(o),
&mut rng,
0,
);
}
}
},
RadiusEffect::Entity(mut effect) => {
for (entity_b, pos_b, body_b_maybe) in
(&data.entities, &data.positions, data.bodies.maybe()).join()
{
let strength = if let Some(body) = body_b_maybe {
cylinder_sphere_strength(
ev.pos,
ev.explosion.radius,
ev.explosion.min_falloff,
pos_b.0,
*body,
)
} else {
let distance_squared = ev.pos.distance_squared(pos_b.0);
1.0 - distance_squared / ev.explosion.radius.powi(2)
};
// Player check only accounts for PvP/PvE flag (unless in a friendly
// fire aura), but bombs are intented to do
// friendly fire.
//
// What exactly is friendly fire is subject to discussion.
// As we probably want to minimize possibility of being dick
// even to your group members, the only exception is when
// you want to harm yourself.
//
// This can be changed later.
let permit_pvp = || {
combat::permit_pvp(
&data.alignments,
&data.players,
&data.entered_auras,
&data.id_maps,
owner_entity,
entity_b,
) || owner_entity.map_or(true, |entity_a| entity_a == entity_b)
};
if strength > 0.0 {
let is_alive =
data.healths.get(entity_b).map_or(true, |h| !h.is_dead);
if is_alive {
effect.modify_strength(strength);
if !effect.is_harm() || permit_pvp() {
emit_effect_events(
&mut emitters,
*data.time,
entity_b,
effect.clone(),
ev.owner.map(|owner| {
(
owner,
data.id_maps
.uid_entity(owner)
.and_then(|e| data.groups.get(e))
.copied(),
)
}),
data.inventories.get(entity_b),
&data.msm,
data.character_states.get(entity_b),
data.stats.get(entity_b),
data.masses.get(entity_b),
owner_entity.and_then(|e| data.masses.get(e)),
);
}
}
}
}
},
}
}
}
}
}
pub fn emit_effect_events(
emitters: &mut (impl EmitExt<HealthChangeEvent> + EmitExt<PoiseChangeEvent> + EmitExt<BuffEvent>),
time: Time,
entity: EcsEntity,
effect: common::effect::Effect,
source: Option<(Uid, Option<Group>)>,
inventory: Option<&Inventory>,
msm: &MaterialStatManifest,
char_state: Option<&CharacterState>,
stats: Option<&Stats>,
tgt_mass: Option<&comp::Mass>,
source_mass: Option<&comp::Mass>,
) {
let damage_contributor = source.map(|(uid, group)| DamageContributor::new(uid, group));
match effect {
common::effect::Effect::Health(change) => {
emitters.emit(HealthChangeEvent { entity, change })
},
common::effect::Effect::Poise(amount) => {
let amount = Poise::apply_poise_reduction(amount, inventory, msm, char_state, stats);
emitters.emit(PoiseChangeEvent {
entity,
change: comp::PoiseChange {
amount,
impulse: Vec3::zero(),
by: damage_contributor,
cause: None,
time,
},
})
},
common::effect::Effect::Damage(damage) => {
let change = damage.calculate_health_change(
combat::Damage::compute_damage_reduction(Some(damage), inventory, stats, msm),
0.0,
damage_contributor,
None,
0.0,
1.0,
time,
rand::random(),
);
emitters.emit(HealthChangeEvent { entity, change })
},
common::effect::Effect::Buff(buff) => {
let dest_info = buff::DestInfo {
stats,
mass: tgt_mass,
};
emitters.emit(BuffEvent {
entity,
buff_change: comp::BuffChange::Add(comp::Buff::new(
buff.kind,
buff.data,
buff.cat_ids,
comp::BuffSource::Item,
time,
dest_info,
source_mass,
)),
});
},
}
}
impl ServerEvent for BonkEvent {
type SystemData<'a> = (
Write<'a, BlockChange>,
ReadExpect<'a, TerrainGrid>,
ReadExpect<'a, ProgramTime>,
Read<'a, EventBus<CreateObjectEvent>>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(mut block_change, terrain, program_time, create_object_events): Self::SystemData<'_>,
) {
let mut create_object_emitter = create_object_events.emitter();
for ev in events {
if let Some(_target) = ev.target {
// TODO: bonk entities but do no damage?
} else {
use common::terrain::SpriteKind;
let pos = ev.pos.map(|e| e.floor() as i32);
if let Some(block) = terrain.get(pos).ok().copied().filter(|b| b.is_bonkable()) {
if block_change
.try_set(pos, block.with_sprite(SpriteKind::Empty))
.is_some()
{
if let Some(items) = comp::Item::try_reclaim_from_block(block) {
let msm = &MaterialStatManifest::load().read();
let ability_map = &AbilityMap::load().read();
for item in flatten_counted_items(&items, ability_map, msm) {
create_object_emitter.emit(CreateObjectEvent {
pos: Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)),
vel: comp::Vel::default(),
body: match block.get_sprite() {
// Create different containers depending on the original
// sprite
Some(SpriteKind::Apple) => comp::object::Body::Apple,
Some(SpriteKind::Beehive) => comp::object::Body::Hive,
Some(SpriteKind::Coconut) => comp::object::Body::Coconut,
Some(SpriteKind::Bomb) => comp::object::Body::Bomb,
_ => comp::object::Body::Pouch,
},
object: match block.get_sprite() {
Some(SpriteKind::Bomb) => {
Some(comp::Object::Bomb { owner: ev.owner })
},
_ => None,
},
item: Some(comp::PickupItem::new(item, *program_time)),
light_emitter: None,
stats: None,
});
}
}
}
}
}
}
}
}
impl ServerEvent for AuraEvent {
type SystemData<'a> = (WriteStorage<'a, Auras>, WriteStorage<'a, EnteredAuras>);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(mut auras, mut entered_auras): Self::SystemData<'_>,
) {
for ev in events {
use aura::AuraChange;
match ev.aura_change {
AuraChange::Add(new_aura) => {
if let Some(mut auras) = auras.get_mut(ev.entity) {
auras.insert(new_aura);
}
},
AuraChange::RemoveByKey(keys) => {
if let Some(mut auras) = auras.get_mut(ev.entity) {
for key in keys {
auras.remove(key);
}
}
},
AuraChange::EnterAura(uid, key, variant) => {
if let Some(mut entered_auras) = entered_auras.get_mut(ev.entity) {
entered_auras
.auras
.entry(variant)
.and_modify(|entered_auras| {
entered_auras.insert((uid, key));
})
.or_insert_with(|| <_ as Into<_>>::into([(uid, key)]));
}
},
AuraChange::ExitAura(uid, key, variant) => {
if let Some(mut entered_auras) = entered_auras.get_mut(ev.entity) {
if let Some(entered_auras_variant) = entered_auras.auras.get_mut(&variant) {
entered_auras_variant.remove(&(uid, key));
if entered_auras_variant.is_empty() {
entered_auras.auras.remove(&variant);
}
}
}
},
}
}
}
}
impl ServerEvent for BuffEvent {
type SystemData<'a> = (
Read<'a, Time>,
WriteStorage<'a, comp::Buffs>,
ReadStorage<'a, Body>,
ReadStorage<'a, Health>,
ReadStorage<'a, Stats>,
ReadStorage<'a, comp::Mass>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(time, mut buffs, bodies, healths, stats, masses): Self::SystemData<'_>,
) {
for ev in events {
if let Some(mut buffs) = buffs.get_mut(ev.entity) {
use buff::BuffChange;
match ev.buff_change {
BuffChange::Add(new_buff) => {
let immunity_by_buff = buffs
.buffs
.values_mut()
.flat_map(|b| b.kind.effects(&b.data))
.find(|b| match b {
BuffEffect::BuffImmunity(kind) => new_buff.kind == *kind,
_ => false,
});
if !bodies
.get(ev.entity)
.map_or(false, |body| body.immune_to(new_buff.kind))
&& immunity_by_buff.is_none()
&& healths.get(ev.entity).map_or(true, |h| !h.is_dead)
{
if let Some(strength) =
new_buff.kind.resilience_ccr_strength(new_buff.data)
{
let resilience_buff = buff::Buff::new(
BuffKind::Resilience,
buff::BuffData::new(
strength,
Some(
new_buff
.data
.duration
.map_or(Secs(30.0), |dur| dur * 5.0),
),
),
Vec::new(),
BuffSource::Buff,
*time,
buff::DestInfo {
stats: stats.get(ev.entity),
mass: masses.get(ev.entity),
},
// There is no source entity
None,
);
buffs.insert(resilience_buff, *time);
}
buffs.insert(new_buff, *time);
}
},
BuffChange::RemoveByKey(keys) => {
for key in keys {
buffs.remove(key);
}
},
BuffChange::RemoveByKind(kind) => {
buffs.remove_kind(kind);
},
BuffChange::RemoveFromController(kind) => {
if kind.is_buff() {
buffs.remove_kind(kind);
}
},
BuffChange::RemoveByCategory {
all_required,
any_required,
none_required,
} => {
let mut keys_to_remove = Vec::new();
for (key, buff) in buffs.buffs.iter() {
let mut required_met = true;
for required in &all_required {
if !buff.cat_ids.iter().any(|cat| cat == required) {
required_met = false;
break;
}
}
let mut any_met = any_required.is_empty();
for any in &any_required {
if buff.cat_ids.iter().any(|cat| cat == any) {
any_met = true;
break;
}
}
let mut none_met = true;
for none in &none_required {
if buff.cat_ids.iter().any(|cat| cat == none) {
none_met = false;
break;
}
}
if required_met && any_met && none_met {
keys_to_remove.push(key);
}
}
for key in keys_to_remove {
buffs.remove(key);
}
},
BuffChange::Refresh(kind) => {
buffs
.buffs
.values_mut()
.filter(|b| b.kind == kind)
.for_each(|buff| {
// Resets buff so that its remaining duration is equal to its
// original duration
buff.start_time = *time;
buff.end_time = buff.data.duration.map(|dur| Time(time.0 + dur.0));
})
},
}
}
}
}
}
impl ServerEvent for EnergyChangeEvent {
type SystemData<'a> = WriteStorage<'a, Energy>;
fn handle(events: impl ExactSizeIterator<Item = Self>, mut energies: Self::SystemData<'_>) {
for ev in events {
if let Some(mut energy) = energies.get_mut(ev.entity) {
energy.change_by(ev.change);
if ev.reset_rate {
energy.reset_regen_rate();
}
}
}
}
}
impl ServerEvent for ComboChangeEvent {
type SystemData<'a> = (
Read<'a, Time>,
Read<'a, EventBus<Outcome>>,
WriteStorage<'a, comp::Combo>,
ReadStorage<'a, Uid>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(time, outcomes, mut combos, uids): Self::SystemData<'_>,
) {
let mut outcome_emitter = outcomes.emitter();
for ev in events {
if let Some(mut combo) = combos.get_mut(ev.entity) {
combo.change_by(ev.change, time.0);
if let Some(uid) = uids.get(ev.entity) {
outcome_emitter.emit(Outcome::ComboChange {
uid: *uid,
combo: combo.counter(),
});
}
}
}
}
}
impl ServerEvent for ParryHookEvent {
type SystemData<'a> = (
Read<'a, Time>,
Read<'a, EventBus<EnergyChangeEvent>>,
Read<'a, EventBus<PoiseChangeEvent>>,
Read<'a, EventBus<BuffEvent>>,
WriteStorage<'a, CharacterState>,
ReadStorage<'a, Uid>,
ReadStorage<'a, Stats>,
ReadStorage<'a, comp::Mass>,
ReadStorage<'a, Inventory>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(
time,
energy_change_events,
poise_change_events,
buff_events,
mut character_states,
uids,
stats,
masses,
inventories,
): Self::SystemData<'_>,
) {
let mut energy_change_emitter = energy_change_events.emitter();
let mut poise_change_emitter = poise_change_events.emitter();
let mut buff_emitter = buff_events.emitter();
for ev in events {
if let Some(mut char_state) = character_states.get_mut(ev.defender) {
let return_to_wield = match &mut *char_state {
CharacterState::RiposteMelee(c) => {
c.stage_section = StageSection::Action;
c.timer = Duration::default();
c.whiffed = false;
false
},
CharacterState::BasicBlock(c) => {
// Refund half the energy of entering the block for a successful parry
energy_change_emitter.emit(EnergyChangeEvent {
entity: ev.defender,
change: c.static_data.energy_regen,
reset_rate: false,
});
c.is_parry = true;
false
},
_ => false,
};
if return_to_wield {
*char_state = CharacterState::Wielding(common::states::wielding::Data {
is_sneaking: false,
});
}
};
if let Some(attacker) = ev.attacker
&& matches!(ev.source, AttackSource::Melee)
{
// When attacker is parried, the debuff lasts 2 seconds, the attacker takes
// poise damage, get precision vulnerability and get slower recovery speed
let data = buff::BuffData::new(1.0, Some(Secs(2.0)));
let source = if let Some(uid) = uids.get(ev.defender) {
BuffSource::Character { by: *uid }
} else {
BuffSource::World
};
let dest_info = buff::DestInfo {
stats: stats.get(attacker),
mass: masses.get(attacker),
};
let buff = buff::Buff::new(
BuffKind::Parried,
data,
vec![buff::BuffCategory::Physical],
source,
*time,
dest_info,
masses.get(ev.defender),
);
buff_emitter.emit(BuffEvent {
entity: attacker,
buff_change: buff::BuffChange::Add(buff),
});
let attacker_poise_change = Poise::apply_poise_reduction(
ev.poise_multiplier.clamp(1.0, 2.0) * BASE_PARRIED_POISE_PUNISHMENT,
inventories.get(attacker),
&MaterialStatManifest::load().read(),
character_states.get(attacker),
stats.get(attacker),
);
poise_change_emitter.emit(PoiseChangeEvent {
entity: attacker,
change: PoiseChange {
amount: -attacker_poise_change,
impulse: Vec3::zero(),
by: uids
.get(ev.defender)
.map(|d| DamageContributor::new(*d, None)),
cause: Some(DamageSource::Melee),
time: *time,
},
});
}
}
}
}
impl ServerEvent for TeleportToEvent {
type SystemData<'a> = (
Read<'a, IdMaps>,
WriteStorage<'a, Pos>,
WriteStorage<'a, comp::ForceUpdate>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(id_maps, mut positions, mut force_updates): Self::SystemData<'_>,
) {
for ev in events {
let target_pos = id_maps
.uid_entity(ev.target)
.and_then(|e| positions.get(e))
.copied();
if let (Some(pos), Some(target_pos)) = (positions.get_mut(ev.entity), target_pos) {
if ev
.max_range
.map_or(true, |r| pos.0.distance_squared(target_pos.0) < r.powi(2))
{
*pos = target_pos;
force_updates
.get_mut(ev.entity)
.map(|force_update| force_update.update());
}
}
}
}
}
#[derive(SystemData)]
pub struct EntityAttackedHookData<'a> {
entities: Entities<'a>,
trades: Write<'a, Trades>,
id_maps: Read<'a, IdMaps>,
time: Read<'a, Time>,
event_busses: ReadEntityAttackedHookEvents<'a>,
outcomes: Read<'a, EventBus<Outcome>>,
character_states: WriteStorage<'a, CharacterState>,
poises: WriteStorage<'a, Poise>,
agents: WriteStorage<'a, Agent>,
positions: ReadStorage<'a, Pos>,
uids: ReadStorage<'a, Uid>,
clients: ReadStorage<'a, Client>,
stats: ReadStorage<'a, Stats>,
}
impl ServerEvent for EntityAttackedHookEvent {
type SystemData<'a> = EntityAttackedHookData<'a>;
/// Intended to handle things that should happen for any successful attack,
/// regardless of the damages and effects specific to that attack
fn handle(events: impl ExactSizeIterator<Item = Self>, mut data: Self::SystemData<'_>) {
let mut emitters = data.event_busses.get_emitters();
let mut outcomes = data.outcomes.emitter();
for ev in events {
if let Some(attacker) = ev.attacker {
emitters.emit(BuffEvent {
entity: attacker,
buff_change: buff::BuffChange::RemoveByCategory {
all_required: vec![buff::BuffCategory::RemoveOnAttack],
any_required: vec![],
none_required: vec![],
},
});
}
if let Some((mut char_state, mut poise, pos)) = (
&mut data.character_states,
&mut data.poises,
&data.positions,
)
.lend_join()
.get(ev.entity, &data.entities)
{
// Interrupt sprite interaction and item use if any attack is applied to entity
if matches!(
*char_state,
CharacterState::SpriteInteract(_) | CharacterState::UseItem(_)
) {
let poise_state = comp::poise::PoiseState::Interrupted;
let was_wielded = char_state.is_wield();
if let (Some((stunned_state, stunned_duration)), impulse_strength) =
poise_state.poise_effect(was_wielded)
{
// Reset poise if there is some stunned state to apply
poise.reset(*data.time, stunned_duration);
*char_state = stunned_state;
outcomes.emit(Outcome::PoiseChange {
pos: pos.0,
state: poise_state,
});
if let Some(impulse_strength) = impulse_strength {
emitters.emit(KnockbackEvent {
entity: ev.entity,
impulse: impulse_strength * *poise.knockback(),
});
}
}
}
}
// Remove potion/saturation buff if attacked
emitters.emit(BuffEvent {
entity: ev.entity,
buff_change: buff::BuffChange::RemoveByKind(BuffKind::Potion),
});
emitters.emit(BuffEvent {
entity: ev.entity,
buff_change: buff::BuffChange::RemoveByKind(BuffKind::Saturation),
});
// If entity was in an active trade, cancel it
if let Some(uid) = data.uids.get(ev.entity) {
if let Some(trade) = data.trades.entity_trades.get(uid).copied() {
data.trades
.decline_trade(trade, *uid)
.and_then(|uid| data.id_maps.uid_entity(uid))
.map(|entity_b| {
// Notify both parties that the trade ended
let mut notify_trade_party = |entity| {
// TODO: Can probably improve UX here for the user that sent the
// trade invite, since right now it
// may seems like their request was
// purposefully declined, rather than e.g. being interrupted.
if let Some(client) = data.clients.get(entity) {
client.send_fallible(ServerGeneral::FinishedTrade(
TradeResult::Declined,
));
}
if let Some(agent) = data.agents.get_mut(entity) {
agent.inbox.push_back(AgentEvent::FinishedTrade(
TradeResult::Declined,
));
}
};
notify_trade_party(ev.entity);
notify_trade_party(entity_b);
});
}
}
if let Some(stats) = data.stats.get(ev.entity) {
for effect in &stats.effects_on_damaged {
use combat::DamagedEffect;
match effect {
DamagedEffect::Combo(c) => {
emitters.emit(ComboChangeEvent {
entity: ev.entity,
change: *c,
});
},
DamagedEffect::Energy(e) => {
emitters.emit(EnergyChangeEvent {
entity: ev.entity,
change: *e,
reset_rate: false,
});
},
}
}
}
}
}
}
impl ServerEvent for ChangeAbilityEvent {
type SystemData<'a> = (
WriteStorage<'a, comp::ActiveAbilities>,
ReadStorage<'a, Inventory>,
ReadStorage<'a, SkillSet>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(mut active_abilities, inventories, skill_sets): Self::SystemData<'_>,
) {
for ev in events {
if let Some(mut active_abilities) = active_abilities.get_mut(ev.entity) {
active_abilities.change_ability(
ev.slot,
ev.auxiliary_key,
ev.new_ability,
inventories.get(ev.entity),
skill_sets.get(ev.entity),
);
}
}
}
}
impl ServerEvent for UpdateMapMarkerEvent {
type SystemData<'a> = (
Entities<'a>,
WriteStorage<'a, comp::MapMarker>,
ReadStorage<'a, Group>,
ReadStorage<'a, Uid>,
ReadStorage<'a, Client>,
ReadStorage<'a, Alignment>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(entities, mut map_markers, groups, uids, clients, alignments): Self::SystemData<'_>,
) {
for ev in events {
match ev.update {
comp::MapMarkerChange::Update(waypoint) => {
let _ = map_markers.insert(ev.entity, comp::MapMarker(waypoint));
},
comp::MapMarkerChange::Remove => {
map_markers.remove(ev.entity);
},
}
// Send updated waypoint to group members
if let Some((group_id, uid)) = (&groups, &uids).lend_join().get(ev.entity, &entities) {
for client in
comp::group::members(*group_id, &groups, &entities, &alignments, &uids)
.filter_map(|(e, _)| if e != ev.entity { clients.get(e) } else { None })
{
client.send_fallible(ServerGeneral::MapMarker(
comp::MapMarkerUpdate::GroupMember(*uid, ev.update),
));
}
}
}
}
}
impl ServerEvent for MakeAdminEvent {
type SystemData<'a> = (WriteStorage<'a, comp::Admin>, ReadStorage<'a, Player>);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(mut admins, players): Self::SystemData<'_>,
) {
for ev in events {
if players
.get(ev.entity)
.map_or(false, |player| player.uuid() == ev.uuid)
{
let _ = admins.insert(ev.entity, ev.admin);
}
}
}
}
impl ServerEvent for ChangeStanceEvent {
type SystemData<'a> = WriteStorage<'a, comp::Stance>;
fn handle(events: impl ExactSizeIterator<Item = Self>, mut stances: Self::SystemData<'_>) {
for ev in events {
if let Some(mut stance) = stances.get_mut(ev.entity) {
*stance = ev.stance;
}
}
}
}
impl ServerEvent for ChangeBodyEvent {
type SystemData<'a> = WriteStorage<'a, comp::Body>;
fn handle(events: impl ExactSizeIterator<Item = Self>, mut bodies: Self::SystemData<'_>) {
for ev in events {
if let Some(mut body) = bodies.get_mut(ev.entity) {
*body = ev.new_body;
}
}
}
}
impl ServerEvent for RemoveLightEmitterEvent {
type SystemData<'a> = WriteStorage<'a, comp::LightEmitter>;
fn handle(
events: impl ExactSizeIterator<Item = Self>,
mut light_emitters: Self::SystemData<'_>,
) {
for ev in events {
light_emitters.remove(ev.entity);
}
}
}
impl ServerEvent for TeleportToPositionEvent {
type SystemData<'a> = (
Read<'a, IdMaps>,
WriteStorage<'a, Is<VolumeRider>>,
WriteStorage<'a, Pos>,
WriteStorage<'a, comp::ForceUpdate>,
ReadStorage<'a, Is<Rider>>,
ReadStorage<'a, Presence>,
ReadStorage<'a, Client>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(
id_maps,
mut is_volume_riders,
mut positions,
mut force_updates,
is_riders,
presences,
clients,
): Self::SystemData<'_>,
) {
for ev in events {
if let Err(error) = crate::state_ext::position_mut(
ev.entity,
true,
|pos| pos.0 = ev.position,
&id_maps,
&mut is_volume_riders,
&mut positions,
&mut force_updates,
&is_riders,
&presences,
&clients,
) {
warn!(?error, "Failed to teleport entity");
}
}
}
}
impl ServerEvent for StartTeleportingEvent {
type SystemData<'a> = (
Read<'a, Time>,
WriteStorage<'a, comp::Teleporting>,
ReadStorage<'a, Pos>,
ReadStorage<'a, comp::Object>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(time, mut teleportings, positions, objects): Self::SystemData<'_>,
) {
for ev in events {
if let Some(end_time) = (!teleportings.contains(ev.entity))
.then(|| positions.get(ev.entity))
.flatten()
.zip(positions.get(ev.portal))
.filter(|(entity_pos, portal_pos)| {
entity_pos.0.distance_squared(portal_pos.0) <= TELEPORTER_RADIUS.powi(2)
})
.and_then(|(_, _)| {
Some(
time.0
+ objects.get(ev.portal).and_then(|object| {
if let Object::Portal { buildup_time, .. } = object {
Some(buildup_time.0)
} else {
None
}
})?,
)
})
{
let _ = teleportings.insert(ev.entity, comp::Teleporting {
portal: ev.portal,
end_time: Time(end_time),
});
}
}
}
}
pub fn handle_transform(
server: &mut Server,
TransformEvent {
target_entity,
entity_info,
allow_players,
delete_on_failure,
}: TransformEvent,
) {
let Some(entity) = server.state().ecs().entity_from_uid(target_entity) else {
return;
};
if let Err(error) = transform_entity(server, entity, entity_info, allow_players) {
if delete_on_failure
&& !server
.state()
.ecs()
.read_storage::<Client>()
.contains(entity)
{
_ = server.state.delete_entity_recorded(entity);
}
error!(?error, ?target_entity, "Failed transform entity");
}
}
#[derive(Debug)]
pub enum TransformEntityError {
EntityDead,
UnexpectedSpecialEntity,
LoadingCharacter,
EntityIsPlayer,
}
pub fn transform_entity(
server: &mut Server,
entity: Entity,
entity_info: EntityInfo,
allow_players: bool,
) -> Result<(), TransformEntityError> {
let is_player = server
.state()
.read_storage::<comp::Player>()
.contains(entity);
match SpawnEntityData::from_entity_info(entity_info) {
SpawnEntityData::Npc(NpcData {
inventory,
stats,
skill_set,
poise,
health,
body,
scale,
agent,
loot,
alignment: _,
pos: _,
pets,
death_effects,
}) => {
fn set_or_remove_component<C: specs::Component>(
server: &mut Server,
entity: EcsEntity,
component: Option<C>,
with: Option<fn(&mut C, Option<C>)>,
) -> Result<(), TransformEntityError> {
let mut storage = server.state.ecs_mut().write_storage::<C>();
if let Some(mut component) = component {
if let Some(with) = with {
let prev = storage.remove(entity);
with(&mut component, prev);
}
storage
.insert(entity, component)
.and(Ok(()))
.map_err(|_| TransformEntityError::EntityDead)
} else {
storage.remove(entity);
Ok(())
}
}
// Disable persistence
'persist: {
match server
.state
.ecs()
.read_storage::<Presence>()
.get(entity)
.map(|presence| presence.kind)
{
// Transforming while the character is being loaded or is spectating is invalid!
Some(PresenceKind::Spectator | PresenceKind::LoadingCharacter(_)) => {
return Err(TransformEntityError::LoadingCharacter);
},
Some(PresenceKind::Character(_)) if !allow_players => {
return Err(TransformEntityError::EntityIsPlayer);
},
Some(PresenceKind::Possessor | PresenceKind::Character(_)) => {},
None => break 'persist,
}
// Run persistence once before disabling it
//
// We must NOT early return between persist_entity() being called and
// persistence being set to Possessor
super::player::persist_entity(server.state_mut(), entity);
// We re-fetch presence here as mutable, because checking for a valid
// [`PresenceKind`] must be done BEFORE persist_entity but persist_entity needs
// exclusive mutable access to the server's state
let mut presences = server.state.ecs().write_storage::<Presence>();
let Some(presence) = presences.get_mut(entity) else {
// Checked above
unreachable!("We already know this entity has a Presence");
};
if let PresenceKind::Character(id) = presence.kind {
server.state.ecs().write_resource::<IdMaps>().remove_entity(
Some(entity),
None,
Some(id),
None,
);
presence.kind = PresenceKind::Possessor;
}
}
// Should do basically what StateExt::create_npc does
set_or_remove_component(server, entity, Some(inventory), None)?;
set_or_remove_component(server, entity, Some(stats), None)?;
set_or_remove_component(server, entity, Some(skill_set), None)?;
set_or_remove_component(server, entity, Some(poise), None)?;
set_or_remove_component(server, entity, health, None)?;
set_or_remove_component(server, entity, Some(comp::Energy::new(body)), None)?;
set_or_remove_component(server, entity, Some(body), None)?;
set_or_remove_component(server, entity, Some(body.mass()), None)?;
set_or_remove_component(server, entity, Some(body.density()), None)?;
set_or_remove_component(server, entity, Some(body.collider()), None)?;
set_or_remove_component(server, entity, Some(scale), None)?;
set_or_remove_component(server, entity, death_effects, None)?;
// Reset active abilities
set_or_remove_component(
server,
entity,
Some(if body.is_humanoid() {
comp::ActiveAbilities::default_limited(BASE_ABILITY_LIMIT)
} else {
comp::ActiveAbilities::default()
}),
None,
)?;
set_or_remove_component(server, entity, body.heads().map(Heads::new), None)?;
// Don't add Agent or ItemDrops to players
if !is_player {
set_or_remove_component(
server,
entity,
agent,
Some(|new_agent, old_agent| {
if let Some(old_agent) = old_agent {
new_agent.target = old_agent.target;
new_agent.awareness = old_agent.awareness;
}
}),
)?;
set_or_remove_component(
server,
entity,
loot.to_items().map(comp::ItemDrops),
None,
)?;
}
// Spawn pets
let position = server.state.read_component_copied::<comp::Pos>(entity);
if let Some(pos) = position {
for (pet, offset) in pets
.into_iter()
.map(|(pet, offset)| (pet.to_npc_builder().0, offset))
{
let pet_entity = handle_create_npc(server, CreateNpcEvent {
pos: comp::Pos(pos.0 + offset),
ori: comp::Ori::from_unnormalized_vec(offset).unwrap_or_default(),
npc: pet,
rider: None,
});
tame_pet(server.state.ecs(), pet_entity, entity);
}
}
},
SpawnEntityData::Special(_, _) => {
return Err(TransformEntityError::UnexpectedSpecialEntity);
},
}
Ok(())
}
impl ServerEvent for RegrowHeadEvent {
type SystemData<'a> = (
Read<'a, EventBus<HealthChangeEvent>>,
Read<'a, Time>,
WriteStorage<'a, Heads>,
ReadStorage<'a, Health>,
);
fn handle(
events: impl ExactSizeIterator<Item = Self>,
(health_change_events, time, mut heads, healths): Self::SystemData<'_>,
) {
let mut health_change_emitter = health_change_events.emitter();
for ev in events {
if let Some(mut heads) = heads.get_mut(ev.entity)
&& heads.regrow_oldest()
&& let Some(health) = healths.get(ev.entity)
{
let amount = 1.0 / (heads.capacity() as f32) * health.maximum();
health_change_emitter.emit(HealthChangeEvent {
entity: ev.entity,
change: comp::HealthChange {
amount,
by: None,
cause: Some(DamageSource::Other),
time: *time,
precise: false,
instance: rand::random(),
},
})
}
}
}
}