nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
world.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_WORLD_H_
2#define NANOMMO_SERVER_WORLD_H_
3
4#include <chrono>
5#include <cstddef>
6#include <cstdint>
7#include <deque>
8#include <functional>
9#include <map>
10#include <memory>
11#include <optional>
12#include <random>
13#include <set>
14#include <string>
15#include <string_view>
16#include <utility>
17#include <vector>
18
19#include "absl/status/status.h"
20#include "absl/status/statusor.h"
21#include "content/content.h"
22#include "protocol/messages.h"
23#include "protocol/nanommo.pb.h"
24#include "server/duels.h"
25#include "server/history.h"
26#include "server/inventory.h"
27#include "server/trades.h"
28#include "server/walking.h"
29#include "server/wars.h"
30#include "terrain/lands.h"
31#include "terrain/paths.h"
32#include "terrain/terrain.h"
33
34namespace nanommo {
35
37
38/// What a player's worn items add up to, worked out as what they wear
39/// changes, so that a fight doesn't add it up for each blow.
40struct Stats {
41 int damage = 0;
42 int health = 0;
43 int armour = 0;
44 /// The weapon in the player's main hand, from the content, or nullptr.
45 const items::Weapon* weapon = nullptr;
46};
47
48struct Player {
49 std::string name;
50 /// How they chose to look, which everyone's shown, or none for a character
51 /// from before there were looks to choose, whose id picks theirs.
52 std::optional<protocol::Looks> looks;
53 /// The guild they're in, by its id, or 0 for none, which the server tells
54 /// the world, as it's in MySQL.
55 uint32_t guild = 0;
56 /// The faction they've sworn to, by its id in the factions file, or 0 for
57 /// none. It's for good.
58 uint32_t faction = 0;
59 float x = 0.0f;
60 float z = 0.0f;
61 /// In radians, around the vertical. Zero faces along z, towards the camera.
62 float yaw = 0.0f;
64 uint32_t experience = 0;
65 int health = 0;
66 /// Which of the world's lands, by its place in kLands.
67 int land = 0;
68 /// What the player carries and wears.
71 /// The skills the player knows, by their ids in the skills file.
72 std::set<uint32_t> skills;
73 /// The quests the player's on, by their ids in the quests file, with how
74 /// many they've killed for each.
75 std::map<uint32_t, uint32_t> quests;
76 /// The quests they've turned in, which they can't take again.
77 std::set<uint32_t> quests_done;
78 /// Each change to their quests takes this to the next, as a bag's revision
79 /// does, so the server can tell when to send them.
80 uint32_t quests_revision = 0;
81 /// The monster the player picked, by its id, which their swings and skills
82 /// go for, or 0 for none.
83 uint32_t target = 0;
84 /// The player they picked, by their id, in place of a monster: their
85 /// opponent in a duel, or an enemy at war, as nobody else can be. Players
86 /// and monsters have ids of their own, so it goes beside the monster.
87 uint32_t target_player = 0;
88 /// How far in the past their client draws everyone else, which their blows
89 /// on another player are checked at.
90 std::chrono::nanoseconds behind{0};
91 /// The skill the player's using, by its id in the skills file, and how far
92 /// into it they are.
93 uint32_t skill = 0;
94 std::chrono::nanoseconds cast{0};
95 /// What the skill does, as it was when it started, with the weapon in hand
96 /// then, so that changing the weapon doesn't change how long it takes. And
97 /// that weapon, as a weapon skill's blows land only while it's in hand.
98 skills::Effect effect;
99 const items::Weapon* cast_weapon = nullptr;
100 /// Where they stood as the skill started, which its strides go on from.
101 float cast_x = 0.0f;
102 float cast_z = 0.0f;
103 /// How many skills they've started, so that one straight after itself
104 /// starts again on the clients.
105 uint32_t casts = 0;
106 /// The swing that Space goes on into next, by its id, for as long as there's
107 /// left to use it, or 0 for the first.
108 uint32_t chain = 0;
109 std::chrono::nanoseconds chain_left{0};
110 /// How long until each skill they've used is ready again, by its id.
111 std::map<uint32_t, std::chrono::nanoseconds> cooldowns;
112 /// How long until they can use each item again, by its id in the items
113 /// file.
114 std::map<uint32_t, std::chrono::nanoseconds> item_cooldowns;
115 /// While they're dead, how long until they can come back.
116 std::chrono::nanoseconds respawn{0};
117 /// Where the player walks by themselves, as they asked to: each place in
118 /// turn, straight, until they get there, or move, fight or die.
119 std::deque<Place> way;
120 /// Whether the world moved them itself this tick, along their way, up to a
121 /// monster or with a skill, or they're dead, so their keys didn't.
122 bool steered = false;
123 float input_x = 0.0f;
124 float input_z = 0.0f;
125 bool input_run = false;
126 bool input_attack = false;
127 /// A skill the player asked for, which waits until they're free.
128 uint32_t input_skill = 0;
129 /// What the player's making, by its recipe's id in the recipes file, or 0
130 /// for nothing, and how long they've been at it.
131 uint32_t crafting = 0;
132 std::chrono::nanoseconds crafted{0};
133};
134
135/// How long a blow's push takes, fast at first and slowing, as a shove does.
136inline constexpr std::chrono::milliseconds kPushTime{200};
137
138/// What pulls a monster that a blow's thrown up back down, in metres a second
139/// each second: more than the Earth's, so that it doesn't float.
140inline constexpr float kGravity = 14.0f;
141
142/// How fast a blow without a lift of its own sends up a monster that's already
143/// in the air, in metres a second: enough to keep it there a moment longer.
144inline constexpr float kPop = 2.5f;
145
146/// However many players take turns at it, a monster's thrown up for this long
147/// at most, before it falls whatever hits it, and then it stands its ground
148/// for a while, so that it gets to fight back.
149inline constexpr std::chrono::milliseconds kMostInAir{2000};
150inline constexpr std::chrono::milliseconds kSteady{2000};
151
152struct Monster {
153 /// By its id in the monsters file.
154 uint32_t kind = 0;
155 float x = 0.0f;
156 float z = 0.0f;
157 float yaw = 0.0f;
159 int level = 1;
160 int health = 0;
161 int max_health = 0;
162 uint32_t experience = 0;
163 int damage = 0;
164 std::chrono::nanoseconds swing_time{0};
165 std::chrono::nanoseconds hit_time{0};
166 /// In metres, from its kind's fight.
167 float reach = 0.0f;
168 float sight = 0.0f;
169 float leash = 0.0f;
170 float wander = 0.0f;
171 /// Its kind's skills, which it uses in place of a swing, each as soon as
172 /// it's ready and the player it's fighting is within its range, the first
173 /// first.
174 std::vector<monsters::Skill> skills;
175 /// A boss's phases, and the one it's in, by its place among them. It uses
176 /// that phase's skills alone.
177 std::vector<monsters::Phase> phases;
178 std::size_t phase = 0;
179 /// For an add, the boss that called it up, by its id. It doesn't come back
180 /// to life, and it goes once its boss is back in its first phase, at home
181 /// or alive again.
182 uint32_t called_by = 0;
183 float home_x = 0.0f;
184 float home_z = 0.0f;
185 float target_x = 0.0f;
186 float target_z = 0.0f;
187 std::chrono::nanoseconds rest{0};
188 uint32_t prey = 0;
189 /// Its threat table: how much each player who's hurt it has, by their id.
190 /// It goes for whoever's hurt it most, seen or not, while they're within its
191 /// leash.
192 std::map<uint32_t, int> threat;
193 /// Whether a boss is on its way home, once nobody it was fighting is left,
194 /// to start again.
195 bool evading = false;
196 std::chrono::nanoseconds swing{0};
197 /// While its motion is a skill's: the skill, how far into it it is, and
198 /// where it stood as it started. The shape stays where it was aimed, the
199 /// way the monster faced, so that players can step out of it.
200 monsters::Skill casting;
201 std::chrono::nanoseconds cast{0};
202 float cast_x = 0.0f;
203 float cast_z = 0.0f;
204 /// How long until each skill it's used is ready again, by its id.
205 std::map<uint32_t, std::chrono::nanoseconds> cooldowns;
206 /// How long it lies dead before it comes back to life, from its kind, and
207 /// while it's dead, how long until it does.
208 std::chrono::nanoseconds respawn_time{0};
209 std::chrono::nanoseconds respawn{0};
210 /// A blow's push, which plays out over a few ticks: where it started, how
211 /// far it goes, in metres, and how long it's been going. While it does, the
212 /// monster thinks of nothing else, though a swing it started goes on.
213 float pushed_from_x = 0.0f;
214 float pushed_from_z = 0.0f;
215 float push_x = 0.0f;
216 float push_z = 0.0f;
217 std::chrono::nanoseconds pushed = kPushTime;
218 /// While a blow has it in the air: how high it is, in metres, and how fast
219 /// it's going up, falling, as it slows, for less than nothing. It does
220 /// nothing else until it lands.
221 float height = 0.0f;
222 float rise = 0.0f;
223 /// How long it's been in the air since it was first thrown up, and, once
224 /// it's down, how long until it can be thrown again.
225 std::chrono::nanoseconds in_air{0};
226 std::chrono::nanoseconds steady{0};
227
228 /// A monster with phases is a boss.
229 bool boss() const { return !phases.empty(); }
230};
231
232/// Someone who stands in one place, such as a guard. NPCs don't move or fight,
233/// so they have none of a monster's health or of what it thinks with.
234struct Npc {
235 std::string name;
236 /// By its id in the NPCs file.
237 uint32_t kind = 0;
238 float x = 0.0f;
239 float z = 0.0f;
240 float yaw = 0.0f;
241 /// Whether players can talk to it.
242 bool talks = false;
243 /// The skills it teaches, by their ids in the skills file.
244 std::set<uint32_t> teaches;
245};
246
247/// A line the world has for one of its players, such as what a monster
248/// they killed dropped, for the server to send them: as a sentence, for their
249/// client to say in its player's language, and in English.
250struct Notice {
251 uint32_t player = 0;
252 std::string text;
253 protocol::Message message;
254 /// CHANNEL_LAND for what everyone in the land hears, which the clients
255 /// show apart.
256 protocol::Channel channel = protocol::CHANNEL_WORLD;
257};
258
259/// An NPC, by its kind, for a sentence, with its English name.
260Part Of(const Npc& npc);
261
262/// Something a player did, or that happened to them, for the game's
263/// analytics and for the players near them to see: what, who and where, and
264/// what it was about, by its ids in the content.
265struct Event {
281 std::string player;
282 /// The player's id in the world, which the clients know them by.
283 uint32_t player_id = 0;
284 /// By its place in kLands.
285 int land = 0;
286 /// The copy of the land it happened in, which Copies fills in.
287 uint32_t copy = 0;
288 float x = 0.0f;
289 float z = 0.0f;
290 /// The monster killed, or that killed or hit the player.
291 uint32_t monster = 0;
292 /// The monster the player hit, by its id in the world.
293 uint32_t monster_id = 0;
294 /// An item dropped, given for a quest, used or made, and how many.
295 uint32_t item = 0;
296 uint32_t count = 0;
297 uint32_t quest = 0;
298 /// The level a player's reached.
299 uint32_t level = 0;
300 /// The health a hit took, or that a potion or a skill gave back.
301 uint32_t amount = 0;
302};
303
304/// The most health the player can have: their level's, and what they wear
305/// adds.
306int MaxHealthOf(const Player& player);
307
308/// What the player holds in their main hand, by its id in the items file, or
309/// zero.
310uint32_t MainHandOf(const Player& player);
311
312/// What else the player wears, in each slot from the off hand to the feet that
313/// has something in it, by its ids in the items file.
314std::vector<uint32_t> WornOf(const Player& player);
315
316class World {
317 public:
318 /// As walking.h has them, which the monsters go at too.
319 static constexpr float kWalkSpeed = nanommo::kWalkSpeed;
320 static constexpr float kRunSpeed = nanommo::kRunSpeed;
321
322 /// A player who swings or uses a skill at the monster they picked, while
323 /// it's out of reach, runs up to this share of the reach first, so that a
324 /// monster that steps back is still within it.
325 static constexpr float kCloseIn = 0.8f;
326
327 /// How long the dead wait before they can come back. A monster comes back
328 /// once it's over, and a player when they ask to.
329 static constexpr std::chrono::seconds kRespawnTime{10};
330
331 /// How far from a boss its adds come, in metres.
332 static constexpr float kAddsAround = 3.0f;
333
334 /// How much of a boss's health a player has to have taken, in percent, for
335 /// its experience and a roll of its loot when it dies.
336 static constexpr int kEnoughHelp = 5;
337
338 /// How long before a boss comes back to life its land hears that it will.
339 static constexpr std::chrono::seconds kRisesSoon{30};
340
341 static constexpr float kTalkReach = nanommo::kTalkReach;
342
343 /// Two players trade within talking reach, as close as they'd stand to talk.
344 static constexpr float kTradeReach = kTalkReach;
345
346 /// Players fight with the content's weapons and skills, so in a world
347 /// without content, nobody can. Content doesn't change as the game runs, so
348 /// the world's copies of its lands share one.
349 /// The world's copies of a land share its paths, as they're the same land;
350 /// without them, the world makes its own.
351 explicit World(
352 uint32_t seed = std::mt19937::default_seed, Terrain terrain = Terrain(),
353 std::shared_ptr<const Content> content = std::make_shared<Content>(),
354 std::shared_ptr<Paths> paths = nullptr);
355
356 void Join(uint32_t id, Player player);
357
358 void Leave(uint32_t id);
359
360 void SetInput(uint32_t id, float x, float z, bool run, bool attack = false);
361
362 /// The monster, by its id, that the player's swings and skills go for from
363 /// now on, or 0 for none. Or a player they may hurt, by their id, in its
364 /// place: anyone else is no target.
365 void Pick(uint32_t id, uint32_t monster, uint32_t player = 0);
366
367 /// The player's client draws everyone else this long ago, so their blows
368 /// on another player are checked against where that player was then.
369 /// Inputs believes Inputs::kMostRewind of what a client says, at most.
370 void SetBehind(uint32_t id, std::chrono::nanoseconds behind);
371
372 /// A dead player comes back in the middle of their land, at full health,
373 /// once their wait is over.
374 void Respawn(uint32_t id);
375
376 /// The player walks there by themselves, around what's in the way, or as
377 /// near as the way goes, if they can stand there.
378 void WalkTo(uint32_t id, Place to);
379
380 /// The player uses the skill as soon as they're free, if they know it and
381 /// it's ready.
382 void UseSkill(uint32_t id, uint32_t skill);
383
384 /// The NPC, a trainer in this land, teaches the player the skill, or the
385 /// error says why not, in words for the player.
386 absl::Status Learn(uint32_t id, uint32_t npc, uint32_t skill);
387
388 /// The player takes the quest from the NPC, who gives it, in this land, or
389 /// the error says why not, in words for the player.
390 absl::Status TakeQuest(uint32_t id, uint32_t npc, uint32_t quest);
391
392 /// The player turns the quest in to the NPC, who gave it, once they've
393 /// killed as many as it asks for, for its experience, and its prizes in
394 /// their bag, or the error says why not, in words for the player.
395 absl::Status TurnIn(uint32_t id, uint32_t npc, uint32_t quest);
396
397 /// The player swears to the faction whose herald the NPC is, in this land,
398 /// for good, and hears so. Or the error says why not, in words for the
399 /// player: one who's sworn to a faction stays with it.
400 absl::Status Enlist(uint32_t id, uint32_t npc);
401
402 /// Gives up a quest the player's on, wherever they are, with what they've
403 /// killed for it, so that its giver gives it again. A NotFound error says
404 /// they're not on it.
405 absl::Status GiveUp(uint32_t id, uint32_t quest);
406
407 /// Moves the player's items around, as Inventory::Move does, wearing them
408 /// and taking them off, or the error says why not, in words for the player.
409 /// Nothing goes to or from a place they offer in a trade.
410 absl::Status MoveItem(uint32_t id, const protocol::Place& from,
411 const protocol::Place& to, uint32_t count = 0);
412
413 /// Throws away all of what's at the place, unless they offer it in a trade.
414 absl::Status DestroyItem(uint32_t id, const protocol::Place& place);
415
416 /// The player starts making the recipe, wherever they are, which takes its
417 /// seconds, if they're of its level and their bag has all it takes, outside
418 /// what they offer in a trade, and room for what it makes. Or the error says
419 /// why not, in words for the player. Moving, a swing or a skill, a blow, a
420 /// trade or a duel stops it, and they lose nothing.
421 absl::Status Craft(uint32_t id, uint32_t recipe);
422
423 /// Stops what the player's making, if anything, and they lose nothing.
424 void StopCrafting(uint32_t id);
425
426 /// The player uses one of what's in the slot of their bag, such as drinks a
427 /// potion, unless they offer it in a trade, or the error says why not, in
428 /// words for the player.
429 absl::Status UseItem(uint32_t id, uint32_t slot);
430
431 /// The player asks the one of that name here, whatever its case, within
432 /// kTradeReach, to trade, or says yes to them asking first, which opens the
433 /// trade. Or the error says why not, in words for the player.
434 absl::Status AskToTrade(uint32_t id, std::string_view name);
435
436 /// What the player offers in their trade, as Trades::Offer has it.
437 absl::Status OfferToTrade(uint32_t id, const protocol::TradeOffer& offer);
438
439 /// Whether the player's ready to trade, at the revision of the trade they
440 /// saw. Once both are, within kTradeReach, they trade, there and then, and
441 /// it returns who with, or 0 until then. Or the error says why not.
442 absl::StatusOr<uint32_t> ReadyToTrade(uint32_t id, bool ready,
443 uint32_t revision);
444
445 /// Calls the player's trade off, for both of them.
446 void CancelTrade(uint32_t id);
447
448 /// The player's trade, as their client gets it: who with, by name, what
449 /// each side offers, and its revision, or nobody, once it's over.
450 protocol::Trade TradeOf(uint32_t id) const;
451
452 /// The players whose trade has opened, changed or ended since the last
453 /// time, which it hands over.
454 std::set<uint32_t> TakeTradeNews();
455
456 /// The player asks the one of that name here, whatever its case, within
457 /// Duels::kReach, to duel, or says yes to them asking first. Or the error
458 /// says why not, in words for the player.
459 absl::Status AskToDuel(uint32_t id, std::string_view name);
460
461 /// The player says yes or no to the one who asked them to duel. A yes puts
462 /// the flag down where the one who asked stands, and starts the countdown.
463 absl::Status AnswerDuel(uint32_t id, bool yes);
464
465 /// The player gives their duel up, and the other side wins it, or before
466 /// it's started, calls it off.
467#if defined(Yield)
468// winbase.h's, from 16-bit Windows, which does nothing.
469#undef Yield
470#endif
471 absl::Status Yield(uint32_t id);
472
473 /// Ends the player's duel, however far it's got, with nobody winning, as
474 /// when the world hands its players over. Returns who it was with, or 0.
475 uint32_t CallOffDuel(uint32_t id);
476
477 /// The player's duel, as their client gets it: who with, how far it's got,
478 /// and its flag, or nobody, once it's over.
479 protocol::Duel DuelOf(uint32_t id) const;
480
481 /// The players whose duel has been asked for, counted down, started or
482 /// ended since the last time, which it hands over.
483 std::set<uint32_t> TakeDuelNews();
484
485 /// The player's guild, by its id, or 0 for none.
486 void SetGuild(uint32_t id, uint32_t guild);
487
488 /// The wars between the world's guilds, in place of those before. Members
489 /// of two guilds at war can hurt each other anywhere.
490 void SetWars(const std::vector<War>& wars);
491
492 /// Whether the player may hurt the other: their opponent in a duel that's
493 /// started, or a member of the guild at war with theirs.
494 bool Hostile(uint32_t id, uint32_t other) const;
495
496 /// The players killed by an enemy at war since the last time, which it
497 /// hands over.
498 std::vector<WarKill> TakeWarKills();
499
500 uint32_t Spawn(Monster monster);
501
502 /// NPCs have ids of their own, apart from the monsters'.
503 uint32_t Spawn(Npc npc);
504
505 void Step(std::chrono::nanoseconds time);
506
507 /// The lines for players since the last time, which it hands over.
508 std::vector<Notice> TakeNotices();
509
510 /// What's happened since the last time, which it hands over.
511 std::vector<Event> TakeEvents();
512
513 const std::map<uint32_t, Player>& players() const { return players_; }
514 const std::map<uint32_t, Monster>& monsters() const { return monsters_; }
515 const std::map<uint32_t, Npc>& npcs() const { return npcs_; }
516 const Terrain& terrain() const { return terrain_; }
517
518 private:
519 /// After what the player wears has changed: works out their stats again.
520 void Dress(Player& player) const;
521 void Act(uint32_t id, Player& player, std::chrono::nanoseconds time);
522 void Move(Player& player, std::chrono::nanoseconds time);
523 /// Moves the player along the direction, at most a step of the speed long,
524 /// less for a shorter one.
525 void Go(Player& player, float x, float z, bool run,
526 std::chrono::nanoseconds time);
527 /// Runs the player towards what they picked, to within the reach.
528 void Approach(Player& player, float reach, std::chrono::nanoseconds time);
529 /// Runs the player along their way, a step at a time.
530 void Follow(Player& player, std::chrono::nanoseconds time);
531 /// Where what the player picked is, while it's alive and within reach: the
532 /// monster, or the player they may hurt, where they saw them. Or nothing.
533 std::optional<Place> Target(const Player& player, float reach) const;
534 /// Where the player's client drew the other player, as far behind as it
535 /// draws everyone, from the history: or where they are, for one that's not
536 /// behind.
537 Place Seen(const Player& player, uint32_t other) const;
538 void Aim(Player& player, float reach);
539 /// What the skill does when this player uses it: a swing's reach, damage
540 /// and timing are the weapon's in their hand.
541 skills::Effect EffectOf(const Player& player,
542 const skills::Skill& skill) const;
543 /// The skill, if the player can use it now: they know it and it's ready. A
544 /// swing needs a weapon, and a skill that hurts one monster needs a living
545 /// one picked, so that its cooldown isn't spent on the air, while a swing
546 /// goes at the air.
547 const skills::Skill* Usable(const Player& player, uint32_t id) const;
548 void Start(Player& player, const skills::Skill& skill);
549 /// The skill's blow, which hurts and pushes what it reaches. A skill that
550 /// lands more than once shares its damage out between its blows, and heals
551 /// with the first.
552 void Land(uint32_t id, Player& player, const skills::Effect& effect,
553 const skills::Blow& blow, int index, int blows);
554 /// Takes the player as far along the skill's strides as it's got to, from
555 /// where they started it.
556 void Stride(Player& player, const skills::Effect& effect);
557 /// Starts a push away from the player, by metres, which Slide plays out.
558 void Push(const Player& player, Monster& monster, float metres);
559 /// Moves the monster on along its push, and says whether one's going on.
560 bool Slide(Monster& monster, std::chrono::nanoseconds time);
561 /// Throws the monster up, to the height in metres, or, already in the air,
562 /// down for less than nothing, and a little further up without one.
563 static void Throw(Monster& monster, float lift);
564 /// Moves the monster up or down as it flies, and says whether it's still in
565 /// the air.
566 static bool Fly(Monster& monster, std::chrono::nanoseconds time);
567 void Hurt(uint32_t id, Player& player, uint32_t monster_id, Monster& monster,
568 int damage);
569 /// A blow on the player from another who may hurt them, by their id. From
570 /// an enemy at war, it can kill them, as a monster's can. From their
571 /// opponent in a duel, one that would leave them no health leaves them one,
572 /// and they've lost.
573 void Hurt(uint32_t from, uint32_t id, Player& player, int damage);
574 /// The war between the two players' guilds, or nullptr.
575 const War* WarBetween(const Player& player, const Player& other) const;
576 /// Gives the player back as much of the health as they're missing.
577 void Heal(uint32_t id, Player& player, int health);
578 /// The player's part in the monster's death: its experience, what it drops
579 /// for them, and a kill for their quests.
580 void Reward(uint32_t id, Player& player, const Monster& monster);
581 /// What the monster the player's just killed drops, into their bag, and its
582 /// gold.
583 void Loot(uint32_t id, Player& player, const Monster& monster);
584 /// Puts count of the item into the bag, each with rolls of its own, and
585 /// returns how many fitted.
586 uint32_t Give(Inventory& bag, uint32_t item, uint32_t count);
587 /// The player's bag with the recipe made: all it takes taken from the bag,
588 /// outside what they offer in a trade, and what it makes given. Or why not,
589 /// in words for the player.
590 absl::StatusOr<Inventory> Made(uint32_t id, const Player& player,
591 const recipes::Recipe& recipe);
592 /// What the player's making goes on, and once it's taken its recipe's
593 /// seconds, it's made.
594 void Make(uint32_t id, Player& player, std::chrono::nanoseconds time);
595 /// Stops what the player's making, if anything, and says so.
596 void Interrupt(uint32_t id, Player& player);
597 /// The monster the player's just killed, for each of their quests that
598 /// asks for its kind, until they've killed as many as it asks.
599 void Count(uint32_t id, Player& player, const Monster& monster);
600 void Act(Monster& monster, std::chrono::nanoseconds time);
601 /// The adds of the boss's phase, by their places among its phases, round
602 /// it, unless it's died since.
603 void CallAdds(uint32_t id, std::size_t phase);
604 /// A line everyone in the land hears about the boss, with its name in it,
605 /// such as "The Bone Colossus has risen.", unless the content hasn't got its
606 /// kind.
607 void Announce(const Monster& boss,
608 const std::function<Sentence(Part boss)>& news);
609 /// A line for the player, which the server sends them.
610 void Tell(uint32_t player, Sentence said,
611 protocol::Channel channel = protocol::CHANNEL_WORLD);
612 void Hit(Monster& monster);
613 /// A monster's blow on the player, from a swing or a skill: what they wear
614 /// takes some of it, down to one, and with no health left, they die.
615 void Hit(uint32_t id, Player& player, const Monster& monster, int blow);
616 /// The monster's skill, once its shape has shown for long enough: on each
617 /// living player in the shape where it was aimed, where they stand now.
618 void LandSkill(const Monster& monster);
619 /// An event of the kind, for the player, by their id, where they are.
620 Event& Record(Event::Kind kind, uint32_t id, const Player& player);
621 /// After everyone's acted: the duels' asks and countdowns run down, and a
622 /// duellist who's dead, or too far from the flag, loses.
623 void Referee(std::chrono::nanoseconds time);
624 /// The player's lost their duel, which is over, and both hear who won.
625 void Lose(uint32_t id);
626
627 Terrain terrain_;
628 std::shared_ptr<const Content> content_;
629 std::shared_ptr<Paths> paths_;
630 std::map<uint32_t, Player> players_;
631 std::map<uint32_t, Monster> monsters_;
632 uint32_t next_monster_ = 1;
633 std::map<uint32_t, Npc> npcs_;
634 uint32_t next_npc_ = 1;
635 std::mt19937 random_;
636 std::vector<Notice> notices_;
637 std::vector<Event> events_;
638 /// The bosses that have gone into a phase this step, by their ids, and the
639 /// phase, whose adds come after the players' blows.
640 std::vector<std::pair<uint32_t, std::size_t>> calls_;
641 /// Between the players here, as both have to be.
642 Trades trades_;
643 Duels duels_;
644 /// The wars between the world's guilds, each by both its sides, as a guild
645 /// is in one at a time.
646 std::map<uint32_t, War> wars_;
647 std::vector<WarKill> war_kills_;
648 /// How long the world's run, a step at a time, which its history goes by.
649 std::chrono::nanoseconds clock_{0};
650 /// Where each player here has been, after each step, for the last second.
651 History history_;
652};
653
654} // namespace nanommo
655
656#endif // NANOMMO_SERVER_WORLD_H_
const std::map< uint32_t, Player > & players() const
Definition world.h:513
void Step(std::chrono::nanoseconds time)
Definition world.cc:940
const std::map< uint32_t, Monster > & monsters() const
Definition world.h:514
const Terrain & terrain() const
Definition world.h:516
protocol::Duel DuelOf(uint32_t id) const
Definition world.cc:578
World(uint32_t seed=std::mt19937::default_seed, Terrain terrain=Terrain(), std::shared_ptr< const Content > content=std::make_shared< Content >(), std::shared_ptr< Paths > paths=nullptr)
Definition world.cc:206
std::vector< Notice > TakeNotices()
The lines for players since the last time, which it hands over.
Definition world.cc:1691
void SetGuild(uint32_t id, uint32_t guild)
The player's guild, by its id, or 0 for none.
Definition world.cc:609
void Join(uint32_t id, Player player)
Definition world.cc:215
std::vector< Event > TakeEvents()
What's happened since the last time, which it hands over.
Definition world.cc:1700
protocol::Trade TradeOf(uint32_t id) const
Definition world.cc:480
absl::Status Craft(uint32_t id, uint32_t recipe)
Definition world.cc:351
absl::StatusOr< uint32_t > ReadyToTrade(uint32_t id, bool ready, uint32_t revision)
Definition world.cc:441
absl::Status AnswerDuel(uint32_t id, bool yes)
Definition world.cc:534
static constexpr float kAddsAround
How far from a boss its adds come, in metres.
Definition world.h:332
std::set< uint32_t > TakeTradeNews()
Definition world.cc:491
void UseSkill(uint32_t id, uint32_t skill)
Definition world.cc:767
void Pick(uint32_t id, uint32_t monster, uint32_t player=0)
Definition world.cc:720
static constexpr std::chrono::seconds kRespawnTime
Definition world.h:329
void StopCrafting(uint32_t id)
Stops what the player's making, if anything, and they lose nothing.
Definition world.cc:383
std::vector< WarKill > TakeWarKills()
Definition world.cc:643
absl::Status Learn(uint32_t id, uint32_t npc, uint32_t skill)
Definition world.cc:774
static constexpr int kEnoughHelp
Definition world.h:336
absl::Status TakeQuest(uint32_t id, uint32_t npc, uint32_t quest)
Definition world.cc:804
static constexpr std::chrono::seconds kRisesSoon
How long before a boss comes back to life its land hears that it will.
Definition world.h:339
void WalkTo(uint32_t id, Place to)
Definition world.cc:755
absl::Status AskToTrade(uint32_t id, std::string_view name)
Definition world.cc:390
void Respawn(uint32_t id)
Definition world.cc:739
uint32_t Spawn(Monster monster)
Definition world.cc:685
void Leave(uint32_t id)
Definition world.cc:251
absl::Status OfferToTrade(uint32_t id, const protocol::TradeOffer &offer)
What the player offers in their trade, as Trades::Offer has it.
Definition world.cc:431
absl::Status MoveItem(uint32_t id, const protocol::Place &from, const protocol::Place &to, uint32_t count=0)
Definition world.cc:264
static constexpr float kCloseIn
Definition world.h:325
void SetInput(uint32_t id, float x, float z, bool run, bool attack=false)
Definition world.cc:702
absl::Status TurnIn(uint32_t id, uint32_t npc, uint32_t quest)
Definition world.cc:875
static constexpr float kTalkReach
Definition world.h:341
absl::Status Enlist(uint32_t id, uint32_t npc)
Definition world.cc:835
uint32_t CallOffDuel(uint32_t id)
Definition world.cc:576
absl::Status DestroyItem(uint32_t id, const protocol::Place &place)
Throws away all of what's at the place, unless they offer it in a trade.
Definition world.cc:283
void SetBehind(uint32_t id, std::chrono::nanoseconds behind)
Definition world.cc:732
absl::Status AskToDuel(uint32_t id, std::string_view name)
Definition world.cc:493
void SetWars(const std::vector< War > &wars)
Definition world.cc:616
absl::Status Yield(uint32_t id)
Definition world.cc:562
static constexpr float kTradeReach
Two players trade within talking reach, as close as they'd stand to talk.
Definition world.h:344
std::set< uint32_t > TakeDuelNews()
Definition world.cc:607
void CancelTrade(uint32_t id)
Calls the player's trade off, for both of them.
Definition world.cc:474
static constexpr float kWalkSpeed
As walking.h has them, which the monsters go at too.
Definition world.h:319
absl::Status UseItem(uint32_t id, uint32_t slot)
Definition world.cc:300
bool Hostile(uint32_t id, uint32_t other) const
Definition world.cc:624
const std::map< uint32_t, Npc > & npcs() const
Definition world.h:515
static constexpr float kRunSpeed
Definition world.h:320
absl::Status GiveUp(uint32_t id, uint32_t quest)
Definition world.cc:864
Definition chat.cc:9
constexpr float kWalkSpeed
In tiles a second. A tile is a metre, and walking is the walk clip's speed.
Definition walking.h:9
Motion
Definition world.h:36
void Dress(Model &model, const std::vector< figures::Part > &parts, float metre)
Definition figures.cc:253
constexpr std::chrono::milliseconds kPushTime
How long a blow's push takes, fast at first and slowing, as a shove does.
Definition world.h:136
constexpr float kRunSpeed
Definition walking.h:10
constexpr float kTalkReach
Definition npcs.h:17
std::vector< uint32_t > WornOf(const Player &player)
Definition world.cc:194
int MaxHealthOf(const Player &player)
Definition world.cc:148
Part Of(const items::Item &item, uint32_t count)
Definition messages.cc:139
uint32_t MainHandOf(const Player &player)
Definition world.cc:189
constexpr std::chrono::milliseconds kMostInAir
Definition world.h:149
constexpr float kPop
Definition world.h:144
constexpr float kGravity
Definition world.h:140
constexpr std::chrono::milliseconds kSteady
Definition world.h:150
int land
By its place in kLands.
Definition world.h:285
uint32_t monster
The monster killed, or that killed or hit the player.
Definition world.h:291
uint32_t monster_id
The monster the player hit, by its id in the world.
Definition world.h:293
uint32_t copy
The copy of the land it happened in, which Copies fills in.
Definition world.h:287
uint32_t level
The level a player's reached.
Definition world.h:299
uint32_t amount
The health a hit took, or that a potion or a skill gave back.
Definition world.h:301
uint32_t item
An item dropped, given for a quest, used or made, and how many.
Definition world.h:295
uint32_t quest
Definition world.h:297
std::string player
Definition world.h:281
uint32_t player_id
The player's id in the world, which the clients know them by.
Definition world.h:283
uint32_t count
Definition world.h:296
uint32_t prey
Definition world.h:188
std::chrono::nanoseconds steady
Definition world.h:226
std::chrono::nanoseconds rest
Definition world.h:187
std::chrono::nanoseconds pushed
Definition world.h:217
std::map< uint32_t, std::chrono::nanoseconds > cooldowns
How long until each skill it's used is ready again, by its id.
Definition world.h:205
std::vector< monsters::Skill > skills
Definition world.h:174
std::chrono::nanoseconds swing
Definition world.h:196
float pushed_from_z
Definition world.h:214
std::chrono::nanoseconds cast
Definition world.h:201
float pushed_from_x
Definition world.h:213
float reach
In metres, from its kind's fight.
Definition world.h:167
std::chrono::nanoseconds respawn
Definition world.h:209
std::chrono::nanoseconds hit_time
Definition world.h:165
std::size_t phase
Definition world.h:178
std::chrono::nanoseconds swing_time
Definition world.h:164
Motion motion
Definition world.h:158
bool boss() const
A monster with phases is a boss.
Definition world.h:229
uint32_t called_by
Definition world.h:182
std::chrono::nanoseconds in_air
Definition world.h:225
uint32_t kind
By its id in the monsters file.
Definition world.h:154
float target_z
Definition world.h:186
float target_x
Definition world.h:185
std::map< uint32_t, int > threat
Definition world.h:192
uint32_t experience
Definition world.h:162
monsters::Skill casting
Definition world.h:200
std::vector< monsters::Phase > phases
Definition world.h:177
std::chrono::nanoseconds respawn_time
Definition world.h:208
std::string text
Definition world.h:252
protocol::Message message
Definition world.h:253
protocol::Channel channel
Definition world.h:256
uint32_t player
Definition world.h:251
float yaw
Definition world.h:240
bool talks
Whether players can talk to it.
Definition world.h:242
std::string name
Definition world.h:235
uint32_t kind
By its id in the NPCs file.
Definition world.h:237
float z
Definition world.h:239
std::set< uint32_t > teaches
The skills it teaches, by their ids in the skills file.
Definition world.h:244
float x
Definition world.h:238
In metres from the middle of a land.
Definition lands.h:13
uint32_t experience
Definition world.h:64
std::chrono::nanoseconds respawn
While they're dead, how long until they can come back.
Definition world.h:116
const items::Weapon * cast_weapon
Definition world.h:99
uint32_t guild
Definition world.h:55
std::string name
Definition world.h:49
bool input_run
Definition world.h:125
std::set< uint32_t > skills
The skills the player knows, by their ids in the skills file.
Definition world.h:72
std::chrono::nanoseconds crafted
Definition world.h:132
uint32_t target_player
Definition world.h:87
uint32_t target
Definition world.h:83
int land
Which of the world's lands, by its place in kLands.
Definition world.h:67
uint32_t input_skill
A skill the player asked for, which waits until they're free.
Definition world.h:128
float cast_x
Where they stood as the skill started, which its strides go on from.
Definition world.h:101
uint32_t casts
Definition world.h:105
std::set< uint32_t > quests_done
The quests they've turned in, which they can't take again.
Definition world.h:77
float input_z
Definition world.h:124
uint32_t crafting
Definition world.h:131
std::optional< protocol::Looks > looks
Definition world.h:52
std::chrono::nanoseconds cast
Definition world.h:94
std::deque< Place > way
Definition world.h:119
float yaw
In radians, around the vertical. Zero faces along z, towards the camera.
Definition world.h:62
uint32_t quests_revision
Definition world.h:80
std::chrono::nanoseconds chain_left
Definition world.h:109
float cast_z
Definition world.h:102
std::map< uint32_t, std::chrono::nanoseconds > item_cooldowns
Definition world.h:114
float input_x
Definition world.h:123
uint32_t skill
Definition world.h:93
uint32_t chain
Definition world.h:108
skills::Effect effect
Definition world.h:98
bool input_attack
Definition world.h:126
std::map< uint32_t, uint32_t > quests
Definition world.h:75
Motion motion
Definition world.h:63
std::map< uint32_t, std::chrono::nanoseconds > cooldowns
How long until each skill they've used is ready again, by its id.
Definition world.h:111
uint32_t faction
Definition world.h:58
Inventory inventory
What the player carries and wears.
Definition world.h:69
Stats stats
Definition world.h:70
std::chrono::nanoseconds behind
Definition world.h:90
const items::Weapon * weapon
The weapon in the player's main hand, from the content, or nullptr.
Definition world.h:45