nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
main.cc
Go to the documentation of this file.
1// The server's red-team client: a player without a window who breaks the
2// client's rules, one way at a time, and prints what it gets for it. It's how
3// we check what the server already stops, and what it doesn't yet. Run it
4// beside the servers, with an account of its own, which it makes the first
5// time:
6//
7// nanommo_cheat <name> <password> <cheat> [<partner>]
8//
9// The cheats, each with what it's after:
10//
11// walk: one running input a tick, as the client sends, to compare with.
12// speed: three running inputs a tick, to run faster than running.
13// teleport: a direction a thousand times the client's, and then one that
14// isn't a number, east and then west, to go further than running would.
15// reach: a monster about 20 metres away, picked and swung at, to hit it
16// from where it stands.
17// fight: the nearest monster, and the first skill each time it's ready,
18// to compare with.
19// rapid: the same, but the skill ten times a tick, to use it more often.
20// swing: the nearest monster, with the weapon alone, to compare with.
21// cancel: the same, but the weapon put away after each hit, and back two
22// ticks later, to end the swing early and start the next one sooner.
23//
24// The walks go east and back west, three seconds each way, so that it stays
25// near where it started. The trades are with the partner named after the
26// cheat, who runs take:
27//
28// take: asks the partner to trade, offers nothing, and is ready once they
29// offer something, to compare with.
30// swap: offers its sword from the bag, and once the partner's ready, moves
31// it to another slot and is ready too, to give the sword and keep it.
32// run: offers its sword, and once the partner's ready, is ready too and
33// leaves at once, to keep the sword when it comes back.
34// twice: comes into the world's second instance too, and trades its sword
35// on the first, which it then leaves. On the second, where it still has
36// the sword, it puts the sword in the bag, so that instance saves its bag
37// over the trade, to keep the sword when it comes back. If the second
38// turns it away, it tries again each second, for twenty seconds, and is
39// ready once it's in, or has given up.
40//
41// The duels are with the partner too, who says yes in their client, or runs
42// duel, which asks back:
43//
44// duel: asks the partner to duel, and once it's on, picks them and swings,
45// each input saying it drew them as a client does, 100 milliseconds
46// before the newest world, to compare with.
47// stale: the same, but saying it drew them a second before, to hit where
48// they were rather than where they are.
49//
50// The war is with the partner too, a friend in the other guild: each founds
51// a guild of its own, and asks the other's to war until they're at war.
52//
53// feed: stands still until it's killed, comes back as soon as it can, and
54// walks back to the partner, to be killed again.
55// farm: picks the partner and swings each tick, and the server runs it up
56// to them, to run up its guild's score. It prints each kill, and the
57// war's score each time it moves.
58#include <chrono>
59#include <cmath>
60#include <cstdint>
61#include <iomanip>
62#include <iostream>
63#include <limits>
64#include <memory>
65#include <optional>
66#include <string>
67#include <thread>
68#include <utility>
69#include <vector>
70
71#include "absl/status/statusor.h"
72#include "absl/strings/str_cat.h"
73#include "asio/ssl/context.hpp"
74#include "client/connection.h"
76#include "protocol/nanommo.pb.h"
77#include "protocol/settings.h"
78#include "protocol/tls.h"
79#include "protocol/version.h"
80#include "server/history.h"
81#include "tools/cheat/measure.h"
82
83namespace {
84
85constexpr uint16_t kLoginPort = 7778;
86constexpr uint16_t kWorldPort = 7777;
87// The world's second instance, which takes its players over in a deploy.
88constexpr uint16_t kNextPort = 7877;
89// How long twice tries the second instance for: twice a lease's ten seconds.
90constexpr auto kKnocking = std::chrono::seconds(20);
91constexpr char kWorld[] = "Ember";
92// The server's tick, on which the client sends its inputs.
93constexpr auto kTick = std::chrono::milliseconds(50);
94constexpr auto kEachWay = std::chrono::seconds(3);
95// How long before the newest world a client draws everyone else, which duel
96// says it drew them at, and how long before it stale says.
97constexpr auto kDrawn = std::chrono::milliseconds(100);
98constexpr auto kStale = std::chrono::milliseconds(1000);
99// The guilds of farm and feed, at war with each other.
100constexpr char kFarmers[] = "Farmers";
101constexpr char kFeeders[] = "Feeders";
102
103absl::StatusOr<nanommo::protocol::LoginReply> Ask(
104 asio::ssl::context& tls, const nanommo::protocol::LoginRequest& request) {
105 absl::StatusOr<nanommo::protocol::LoginReply> reply =
106 nanommo::LogIn("localhost", kLoginPort, tls, request);
107 if (reply.ok() && !reply->ok()) {
108 return absl::PermissionDeniedError(reply->error());
109 }
110 return reply;
111}
112
113// As the bots do: the account the first time, and a character in kWorld
114// named after it. Returns the token for the world.
115absl::StatusOr<std::string> Enter(asio::ssl::context& tls,
116 const std::string& name,
117 const std::string& password) {
118 nanommo::protocol::LoginRequest request;
119 request.set_username(name);
120 request.set_password(password);
121 request.set_version(nanommo::kVersion);
122 absl::StatusOr<nanommo::protocol::LoginReply> reply = Ask(tls, request);
123 if (absl::IsPermissionDenied(reply.status())) {
124 request.set_new_account(true);
125 reply = Ask(tls, request);
126 }
127 if (!reply.ok()) {
128 return reply.status();
129 }
130 const auto mine = [](const nanommo::protocol::LoginReply& listed) {
131 for (const nanommo::protocol::CharacterListing& character :
132 listed.characters()) {
133 if (character.world() == kWorld) {
134 return character.id();
135 }
136 }
137 return uint32_t{0};
138 };
139 nanommo::protocol::LoginRequest next;
140 next.set_session(reply->session());
141 uint32_t character = mine(*reply);
142 if (character == 0) {
143 nanommo::protocol::LoginRequest create = next;
144 create.set_create_character(true);
145 create.set_world(kWorld);
146 create.set_name(name);
147 reply = Ask(tls, create);
148 if (!reply.ok()) {
149 return reply.status();
150 }
151 character = mine(*reply);
152 }
153 next.set_enter(true);
154 next.set_character(character);
155 reply = Ask(tls, next);
156 if (!reply.ok()) {
157 return reply.status();
158 }
159 return reply->token();
160}
161
162// The player the world has with the id, if it has them.
163const nanommo::protocol::Player* Player(const nanommo::protocol::World& world,
164 uint32_t id) {
165 for (const nanommo::protocol::Player& player : world.players()) {
166 if (player.id() == id) {
167 return &player;
168 }
169 }
170 return nullptr;
171}
172
173// Where the world has us, once it has.
174const nanommo::protocol::Player* Us(const nanommo::protocol::World& world) {
175 return Player(world, world.you());
176}
177
178// The player the world has with the name, once it has their name.
179const nanommo::protocol::Player* Named(const nanommo::protocol::World& world,
180 const std::string& name) {
181 for (const nanommo::protocol::Player& player : world.players()) {
182 if (player.name() == name) {
183 return &player;
184 }
185 }
186 return nullptr;
187}
188
189// The monster the world has with the id, if it has it.
190const nanommo::protocol::Monster* Monster(const nanommo::protocol::World& world,
191 uint32_t id) {
192 for (const nanommo::protocol::Monster& monster : world.monsters()) {
193 if (monster.id() == id) {
194 return &monster;
195 }
196 }
197 return nullptr;
198}
199
200// The live monster nearest to being that far from us, or 0 for none.
201uint32_t Pick(const nanommo::protocol::World& world,
202 const nanommo::protocol::Player& us, float away) {
203 uint32_t picked = 0;
204 float best = std::numeric_limits<float>::max();
205 for (const nanommo::protocol::Monster& monster : world.monsters()) {
206 const float off =
207 std::abs(std::hypot(monster.x() - us.x(), monster.z() - us.z()) - away);
208 if (monster.health() > 0 && off < best) {
209 best = off;
210 picked = monster.id();
211 }
212 }
213 return picked;
214}
215
216enum class Cheat {
217 kWalk,
218 kSpeed,
219 kTeleport,
220 kReach,
221 kFight,
222 kRapid,
223 kSwing,
224 kCancel,
225 kTake,
226 kSwap,
227 kRun,
228 kTwice,
229 kDuel,
230 kStale,
231 kFeed,
232 kFarm
233};
234
235// An input that moves what's in one place to another.
236nanommo::protocol::Input Move(const nanommo::protocol::Place& from,
237 const nanommo::protocol::Place& to) {
238 nanommo::protocol::Input input;
239 nanommo::protocol::ItemMove* move = input.mutable_item()->mutable_move();
240 *move->mutable_from() = from;
241 *move->mutable_to() = to;
242 return input;
243}
244
245// The first slot of the bag with nothing in it.
246uint32_t FreeSlot(const nanommo::protocol::Inventory& inventory) {
247 uint32_t slot = 0;
248 while (inventory.bag().contains(slot)) {
249 ++slot;
250 }
251 return slot;
252}
253
254// An input that does something about a trade.
255nanommo::protocol::Input Trading(const nanommo::protocol::TradeAction& action) {
256 nanommo::protocol::Input input;
257 *input.mutable_trade() = action;
258 return input;
259}
260
261// How many of the item the player has, in the bag and worn, said in words.
262std::string Count(const nanommo::protocol::Inventory& inventory,
263 uint32_t item) {
264 int count = 0;
265 for (const auto& [slot, stack] : inventory.bag()) {
266 count += stack.item() == item ? 1 : 0;
267 }
268 for (const auto& [slot, stack] : inventory.worn()) {
269 count += stack.item() == item ? 1 : 0;
270 }
271 return absl::StrCat(count, count == 1 ? " sword" : " swords");
272}
273
274// The bag's slot that holds the item, if one does.
275std::optional<uint32_t> SlotOf(const nanommo::protocol::Inventory& inventory,
276 uint32_t item) {
277 for (const auto& [slot, stack] : inventory.bag()) {
278 if (stack.item() == item) {
279 return slot;
280 }
281 }
282 return std::nullopt;
283}
284
285// Comes back into the world's first instance after a second, and prints how
286// many swords the player has then. Returns what main does.
287int Back(asio::ssl::context& tls, const std::string& name,
288 const std::string& password, uint32_t sword) {
289 std::this_thread::sleep_for(std::chrono::seconds(1));
290 absl::StatusOr<std::string> token = Enter(tls, name, password);
291 if (!token.ok()) {
292 std::cerr << "cheat: " << name << " couldn't come back: " << token.status()
293 << "\n";
294 return 1;
295 }
296 absl::StatusOr<std::unique_ptr<nanommo::Connection>> back =
297 nanommo::Connection::Create("localhost", kWorldPort, tls, *token, false);
298 if (!back.ok()) {
299 std::cerr << "cheat: " << back.status() << "\n";
300 return 1;
301 }
302 while ((*back)->connected() && (*back)->inventory().bag().empty()) {
303 (*back)->Poll();
304 std::this_thread::sleep_for(std::chrono::milliseconds(5));
305 }
306 std::cout << "cheat: " << name << " is back, and has "
307 << Count((*back)->inventory(), sword) << std::endl;
308 return 0;
309}
310
311// The player in the world's second instance as well, once it has sent them
312// their bag, or nothing if it turned them away.
313std::unique_ptr<nanommo::Connection> Second(asio::ssl::context& tls,
314 const std::string& name,
315 const std::string& password) {
316 absl::StatusOr<std::string> token = Enter(tls, name, password);
317 if (!token.ok()) {
318 return nullptr;
319 }
320 absl::StatusOr<std::unique_ptr<nanommo::Connection>> second =
321 nanommo::Connection::Create("localhost", kNextPort, tls, *token, false);
322 if (!second.ok()) {
323 return nullptr;
324 }
325 const auto start = std::chrono::steady_clock::now();
326 while ((*second)->connected() && (*second)->inventory().worn().empty() &&
327 std::chrono::steady_clock::now() - start < std::chrono::seconds(3)) {
328 (*second)->Poll();
329 std::this_thread::sleep_for(std::chrono::milliseconds(5));
330 }
331 if (!(*second)->connected() || (*second)->inventory().worn().empty()) {
332 return nullptr;
333 }
334 return *std::move(second);
335}
336
337// The trades, with the partner, who runs take. They print what they do, and
338// how many swords they have once the trade's over, or for run, once it's
339// back. Returns what main does.
340int Trade(asio::ssl::context& tls, const std::string& name,
341 const std::string& password, Cheat cheat, const std::string& partner,
342 std::unique_ptr<nanommo::Connection> connection) {
343 nanommo::protocol::Place hand;
344 hand.set_worn(nanommo::items::SLOT_MAIN_HAND);
345 // The kit's sword, by its item, once the bag has come.
346 uint32_t sword = 0;
347 std::chrono::steady_clock::time_point asked;
348 bool open = false;
349 bool stowed = false;
350 std::optional<uint32_t> offered;
351 // When swap moved the sword, if it has.
352 std::optional<std::chrono::steady_clock::time_point> moved;
353 bool ready = false;
354 // twice is in the second instance too, with a token of its own, once that
355 // instance has sent it its bag. One that turns it away, as the first has
356 // its lease, takes it once the lease runs out, as it does if the first
357 // stalls, so it tries again each second until then.
358 std::unique_ptr<nanommo::Connection> next;
359 bool knocking = cheat == Cheat::kTwice;
360 const auto started = std::chrono::steady_clock::now();
361 std::chrono::steady_clock::time_point knocked;
362 while (connection->connected()) {
363 connection->Poll();
364 if (knocking &&
365 std::chrono::steady_clock::now() - knocked >= std::chrono::seconds(1)) {
366 knocked = std::chrono::steady_clock::now();
367 next = Second(tls, name, password);
368 if (next != nullptr) {
369 knocking = false;
370 std::cout << "cheat: " << name << " is in Ember a second time, on "
371 << kNextPort << std::endl;
372 } else if (knocked - started >= kKnocking) {
373 knocking = false;
374 std::cout << "cheat: the second instance turned " << name
375 << " away, as it's playing on the first" << std::endl;
376 }
377 }
378 if (next != nullptr) {
379 next->Poll();
380 }
381 std::this_thread::sleep_for(std::chrono::milliseconds(5));
382 const nanommo::protocol::Inventory& bag = connection->inventory();
383 const auto in_hand = static_cast<uint32_t>(hand.worn());
384 if (sword == 0) {
385 if (bag.worn().contains(in_hand)) {
386 sword = bag.worn().at(in_hand).item();
387 std::cout << "cheat: " << name << " has " << Count(bag, sword)
388 << std::endl;
389 }
390 continue;
391 }
392 const nanommo::protocol::Trade& trade = connection->trade();
393 if (trade.partner() != partner) {
394 if (open) {
395 // The bag that comes with the trade's end can come a tick after it.
396 const auto end = std::chrono::steady_clock::now();
397 while (std::chrono::steady_clock::now() - end <
398 std::chrono::milliseconds(500)) {
399 connection->Poll();
400 std::this_thread::sleep_for(std::chrono::milliseconds(5));
401 }
402 std::cout << "cheat: the trade's over, and " << name << " has "
403 << Count(connection->inventory(), sword) << std::endl;
404 if (next == nullptr) {
405 return 0;
406 }
407 // The first instance saves the bag without the sword as we leave it.
408 // The second still has the sword in hand, and a move to the bag is a
409 // change, which it saves on its next tick.
410 connection.reset();
411 nanommo::protocol::Place free;
412 free.set_bag(FreeSlot(next->inventory()));
413 next->Send(Move(hand, free));
414 const auto put = std::chrono::steady_clock::now();
415 while (std::chrono::steady_clock::now() - put <
416 std::chrono::milliseconds(500)) {
417 next->Poll();
418 std::this_thread::sleep_for(std::chrono::milliseconds(5));
419 }
420 std::cout << "cheat: on " << kNextPort << ", " << name
421 << " still has its sword, and puts it in the bag"
422 << std::endl;
423 next.reset();
424 return Back(tls, name, password, sword);
425 }
426 // The partner may not be in yet, so we ask again each second.
427 const auto now = std::chrono::steady_clock::now();
428 if (now - asked >= std::chrono::seconds(1)) {
429 nanommo::protocol::TradeAction ask;
430 ask.set_ask(partner);
431 connection->Send(Trading(ask));
432 asked = now;
433 }
434 continue;
435 }
436 open = true;
437 if (cheat == Cheat::kTake) {
438 if (!ready && trade.theirs().bag_size() > 0) {
439 nanommo::protocol::TradeAction yes;
440 yes.set_ready(true);
441 yes.set_revision(trade.revision());
442 connection->Send(Trading(yes));
443 ready = true;
444 std::cout << "cheat: " << partner << " offers a sword, so " << name
445 << " is ready" << std::endl;
446 }
447 continue;
448 }
449 // swap, run and twice offer the sword from the bag, so it goes there
450 // first.
451 if (!offered) {
452 if (!stowed) {
453 nanommo::protocol::Place free;
454 free.set_bag(FreeSlot(bag));
455 connection->Send(Move(hand, free));
456 stowed = true;
457 } else if (const std::optional<uint32_t> slot = SlotOf(bag, sword)) {
458 nanommo::protocol::TradeAction offer;
459 offer.mutable_offer()->add_bag(*slot);
460 connection->Send(Trading(offer));
461 offered = slot;
462 std::cout << "cheat: " << name << " offers its sword, from slot "
463 << *slot << std::endl;
464 }
465 continue;
466 }
467 // twice is ready once it's in the second instance, or has given up.
468 if (ready || !trade.theirs().ready() || knocking) {
469 continue;
470 }
471 nanommo::protocol::TradeAction yes;
472 yes.set_ready(true);
473 yes.set_revision(trade.revision());
474 if (cheat == Cheat::kSwap) {
475 // The sword leaves the slot it was offered from, and then we're ready.
476 if (!moved) {
477 nanommo::protocol::Place from;
478 from.set_bag(*offered);
479 nanommo::protocol::Place to;
480 to.set_bag(FreeSlot(bag));
481 connection->Send(Move(from, to));
482 moved = std::chrono::steady_clock::now();
483 std::cout << "cheat: " << partner << " is ready, so " << name
484 << " moves the sword to slot " << to.bag() << std::endl;
485 } else if (!bag.bag().contains(*offered)) {
486 connection->Send(Trading(yes));
487 ready = true;
488 std::cout << "cheat: " << name << " is ready too" << std::endl;
489 } else if (std::chrono::steady_clock::now() - *moved >
490 std::chrono::seconds(1)) {
491 // The server didn't move it, so the trade goes ahead as offered.
492 connection->Send(Trading(yes));
493 ready = true;
494 std::cout << "cheat: the sword's still in slot " << *offered << ", so "
495 << name << " is ready with it there" << std::endl;
496 }
497 continue;
498 }
499 connection->Send(Trading(yes));
500 if (cheat == Cheat::kTwice) {
501 ready = true;
502 std::cout << "cheat: " << partner << " is ready, so " << name
503 << " is ready too" << std::endl;
504 continue;
505 }
506 // run: ready, and gone before the server's next tick saves the bags.
507 connection.reset();
508 std::cout << "cheat: " << partner << " is ready, so " << name
509 << " is ready too, and leaves at once" << std::endl;
510 return Back(tls, name, password, sword);
511 }
512 std::cout << "cheat: the server closed the connection" << std::endl;
513 return 0;
514}
515
516// The duels, with the partner. Once one's on, it picks them, and swings at
517// them each tick, each input saying when it drew everyone, on the server's
518// clock: kDrawn before the newest world, as a client does, or for stale,
519// kStale. It prints each blow that lands, and each second what it says, and
520// where the partner was then, by its own history of where they've been, as
521// the server keeps one. Returns what main does.
522int Duel(Cheat cheat, const std::string& partner,
523 std::unique_ptr<nanommo::Connection> connection) {
524 const std::string named = cheat == Cheat::kStale ? "stale" : "duel";
525 const auto says = cheat == Cheat::kStale ? kStale : kDrawn;
526 nanommo::History history;
527 uint32_t sequence = 0;
528 int landed = 0;
529 bool fought = false;
530 std::chrono::steady_clock::time_point asked;
531 std::chrono::steady_clock::time_point tick;
532 std::chrono::steady_clock::time_point second;
533 std::cout << std::fixed << std::setprecision(1);
534 while (connection->connected()) {
535 connection->Poll();
536 std::this_thread::sleep_for(std::chrono::milliseconds(5));
537 const auto now = std::chrono::steady_clock::now();
538 const nanommo::protocol::World& world = connection->world();
539 const nanommo::protocol::Duel& duel = connection->duel();
540 const std::vector<nanommo::protocol::Event> events =
541 connection->TakeEvents();
542 if (duel.state() != nanommo::protocol::DUEL_FIGHTING) {
543 if (fought) {
544 std::cout << "cheat: the duel's over, and " << landed
545 << (landed == 1 ? " blow" : " blows") << " landed"
546 << std::endl;
547 return 0;
548 }
549 // The partner may not be in yet, so we ask each second, and asking
550 // back a partner who asked first is a yes.
551 const bool waiting = duel.state() == nanommo::protocol::DUEL_ASKING ||
552 duel.state() == nanommo::protocol::DUEL_COUNTDOWN;
553 if (!waiting && now - asked >= std::chrono::seconds(1)) {
554 nanommo::protocol::Input ask;
555 ask.mutable_duel()->set_ask(partner);
556 connection->Send(ask);
557 asked = now;
558 }
559 tick = now;
560 continue;
561 }
562 fought = true;
563 const uint32_t them = duel.opponent_id();
564 const nanommo::protocol::Player* us = Us(world);
565 const nanommo::protocol::Player* other = Player(world, them);
566 if (us == nullptr || other == nullptr) {
567 continue;
568 }
569 const auto sent = std::chrono::microseconds(world.sent());
570 history.Record(them, sent, {.x = other->x(), .z = other->z()});
571 // How far from us they are now, or were, as long ago as that.
572 const auto away = [&](std::chrono::milliseconds ago) {
573 const nanommo::Place then = *history.At(them, sent - ago);
574 return std::hypot(then.x - us->x(), then.z - us->z());
575 };
576 for (const nanommo::protocol::Event& event : events) {
577 if (event.kind() == nanommo::protocol::EVENT_HURT &&
578 event.player() == them) {
579 ++landed;
580 std::cout << "cheat: a blow landed on " << partner << ", for "
581 << event.amount() << ", " << away({}) << " m away"
582 << std::endl;
583 }
584 }
585 if (now >= tick) {
586 tick += kTick;
587 nanommo::protocol::Input input;
588 input.set_target_player(them);
589 input.set_attack(true);
590 input.set_sequence(++sequence);
591 input.set_seen(connection->whole());
592 input.set_drawn((sent - says).count());
593 connection->Send(input);
594 }
595 if (now - second >= std::chrono::seconds(1)) {
596 second = now;
597 std::cout << "cheat: " << named << " says it drew " << partner << " "
598 << says.count() << " ms ago, " << away(says)
599 << " m away then, and " << away({}) << " m away now"
600 << std::endl;
601 }
602 }
603 std::cout << "cheat: the server closed the connection" << std::endl;
604 return 0;
605}
606
607// The war, with the partner, in the other guild. Each founds a guild of its
608// own, and asks the partner's to war each second until they're at war, as
609// the second to ask says yes. Then feed stands still, comes back once it's
610// killed, as soon as it can, and walks back to the partner, and farm picks
611// the partner and swings each tick, printing each kill, and the war's score
612// as it moves, a few seconds behind. Returns what main does.
613int War(Cheat cheat, const std::string& partner,
614 std::unique_ptr<nanommo::Connection> connection) {
615 const bool farming = cheat == Cheat::kFarm;
616 const std::string ours = farming ? kFarmers : kFeeders;
617 const std::string theirs = farming ? kFeeders : kFarmers;
618 uint32_t sequence = 0;
619 int kills = 0;
620 bool dead = false;
621 uint32_t score = 0;
622 std::chrono::steady_clock::time_point asked;
623 std::chrono::steady_clock::time_point tick;
624 std::chrono::steady_clock::time_point started;
625 std::cout << std::fixed << std::setprecision(1);
626 while (connection->connected()) {
627 connection->Poll();
628 std::this_thread::sleep_for(std::chrono::milliseconds(5));
629 const auto now = std::chrono::steady_clock::now();
630 const nanommo::protocol::Guild& guild = connection->guild();
631 if (guild.war().enemy() != theirs) {
632 if (now - asked >= std::chrono::seconds(1)) {
633 nanommo::protocol::Input ask;
634 if (guild.name().empty()) {
635 ask.mutable_guild()->set_create(ours);
636 } else {
637 ask.mutable_guild()->set_war(theirs);
638 }
639 connection->Send(ask);
640 asked = now;
641 }
642 started = now;
643 continue;
644 }
645 const nanommo::protocol::World& world = connection->world();
646 const nanommo::protocol::Player* us = Us(world);
647 const nanommo::protocol::Player* them = Named(world, partner);
648 if (us == nullptr || them == nullptr) {
649 continue;
650 }
651 const double minutes =
652 std::chrono::duration<double>(now - started).count() / 60;
653 if (!farming) {
654 const bool died = us->motion() == nanommo::protocol::MOTION_DEAD;
655 if (died && world.respawn() == 0 && now >= tick) {
656 nanommo::protocol::Input respawn;
657 respawn.set_respawn(true);
658 connection->Send(respawn);
659 tick = now + std::chrono::seconds(1);
660 } else if (!died && dead) {
661 nanommo::protocol::Input back;
662 back.mutable_walk_to()->set_x(them->x());
663 back.mutable_walk_to()->set_z(them->z());
664 connection->Send(back);
665 std::cout << "cheat: " << partner << " killed " << us->name()
666 << ", who walks back" << std::endl;
667 }
668 dead = died;
669 continue;
670 }
671 if (now >= tick) {
672 tick = now + kTick;
673 nanommo::protocol::Input input;
674 input.set_target_player(them->id());
675 input.set_attack(true);
676 input.set_sequence(++sequence);
677 input.set_seen(connection->whole());
678 connection->Send(input);
679 }
680 const bool killed = them->motion() == nanommo::protocol::MOTION_DEAD;
681 if (killed && !dead) {
682 ++kills;
683 std::cout << "cheat: farm killed " << partner << ", " << kills
684 << (kills == 1 ? " time" : " times") << " in " << minutes
685 << " minutes" << std::endl;
686 }
687 dead = killed;
688 if (guild.war().kills() != score) {
689 score = guild.war().kills();
690 std::cout << "cheat: after " << minutes << " minutes, the war's score is "
691 << score << " to " << guild.war().enemy_kills() << ", "
692 << score / minutes << " a minute" << std::endl;
693 }
694 }
695 std::cout << "cheat: the server closed the connection" << std::endl;
696 return 0;
697}
698
699} // namespace
700
701int main(int argc, char** argv) {
702 const std::string usage =
703 "usage: nanommo_cheat <name> <password> "
704 "<walk|speed|teleport|reach|fight|rapid|swing|cancel>\n"
705 " nanommo_cheat <name> <password> "
706 "<take|swap|run|twice|duel|stale|feed|farm> <partner>\n";
707 if (argc != 4 && argc != 5) {
708 std::cerr << usage;
709 return 2;
710 }
711 const std::string name = argv[1];
712 const std::string password = argv[2];
713 const std::string named = argv[3];
714 Cheat cheat = Cheat::kWalk;
715 if (named == "walk") {
716 cheat = Cheat::kWalk;
717 } else if (named == "speed") {
718 cheat = Cheat::kSpeed;
719 } else if (named == "teleport") {
720 cheat = Cheat::kTeleport;
721 } else if (named == "reach") {
722 cheat = Cheat::kReach;
723 } else if (named == "fight") {
724 cheat = Cheat::kFight;
725 } else if (named == "rapid") {
726 cheat = Cheat::kRapid;
727 } else if (named == "swing") {
728 cheat = Cheat::kSwing;
729 } else if (named == "cancel") {
730 cheat = Cheat::kCancel;
731 } else if (named == "take") {
732 cheat = Cheat::kTake;
733 } else if (named == "swap") {
734 cheat = Cheat::kSwap;
735 } else if (named == "run") {
736 cheat = Cheat::kRun;
737 } else if (named == "twice") {
738 cheat = Cheat::kTwice;
739 } else if (named == "duel") {
740 cheat = Cheat::kDuel;
741 } else if (named == "stale") {
742 cheat = Cheat::kStale;
743 } else if (named == "feed") {
744 cheat = Cheat::kFeed;
745 } else if (named == "farm") {
746 cheat = Cheat::kFarm;
747 } else {
748 std::cerr << usage;
749 return 2;
750 }
751 const bool trades = cheat == Cheat::kTake || cheat == Cheat::kSwap ||
752 cheat == Cheat::kRun || cheat == Cheat::kTwice;
753 const bool duels = cheat == Cheat::kDuel || cheat == Cheat::kStale;
754 const bool wars = cheat == Cheat::kFeed || cheat == Cheat::kFarm;
755 // A trade, a duel or a war needs a partner, and nothing else does.
756 if ((trades || duels || wars) != (argc == 5)) {
757 std::cerr << usage;
758 return 2;
759 }
760 const bool fights = cheat == Cheat::kReach || cheat == Cheat::kFight ||
761 cheat == Cheat::kRapid || cheat == Cheat::kSwing ||
762 cheat == Cheat::kCancel;
763
764 absl::StatusOr<asio::ssl::context> tls =
765 nanommo::ClientTls(nanommo::FileSetting("NANOMMO_TLS_CA", "tls/ca.crt"));
766 if (!tls.ok()) {
767 std::cerr << "cheat: " << tls.status() << "\n";
768 return 1;
769 }
770 // The servers may still be starting.
771 absl::StatusOr<std::string> token;
772 for (int tries = 0; tries < 60; ++tries) {
773 token = Enter(*tls, name, password);
774 if (token.ok()) {
775 break;
776 }
777 std::this_thread::sleep_for(std::chrono::milliseconds(500));
778 }
779 if (!token.ok()) {
780 std::cerr << "cheat: " << name << " couldn't enter: " << token.status()
781 << "\n";
782 return 1;
783 }
784 absl::StatusOr<std::unique_ptr<nanommo::Connection>> connection =
785 nanommo::Connection::Create("localhost", kWorldPort, *tls, *token, false);
786 if (!connection.ok()) {
787 std::cerr << "cheat: " << connection.status() << "\n";
788 return 1;
789 }
790 std::cout << "cheat: " << name << " is in " << kWorld << ", trying " << named
791 << std::endl;
792 if (trades) {
793 return Trade(*tls, name, password, cheat, argv[4], *std::move(connection));
794 }
795 if (duels) {
796 return Duel(cheat, argv[4], *std::move(connection));
797 }
798 if (wars) {
799 return War(cheat, argv[4], *std::move(connection));
800 }
801
803 nanommo::Hits hits;
804 uint32_t sequence = 0;
805 uint32_t target = 0;
806 // Where cancel put its weapon, and when it puts it back.
807 std::optional<nanommo::protocol::Place> away;
808 std::chrono::steady_clock::time_point back;
809 int sent = 0;
810 int64_t last = 0;
811 const auto start = std::chrono::steady_clock::now();
812 auto tick = start;
813 auto second = start;
814 while ((*connection)->connected()) {
815 (*connection)->Poll();
816 const auto now = std::chrono::steady_clock::now();
817 const double seconds = std::chrono::duration<double>(now - start).count();
818 const nanommo::protocol::World& world = (*connection)->world();
819 const nanommo::protocol::Player* us = Us(world);
820 if (world.sent() != last && us != nullptr) {
821 last = world.sent();
822 speed.Add(static_cast<double>(world.sent()) / 1e6, us->x(), us->z());
823 }
824 for (const nanommo::protocol::Event& event : (*connection)->TakeEvents()) {
825 if (event.kind() == nanommo::protocol::EVENT_HIT &&
826 event.player() == world.you()) {
827 hits.Add(seconds, event.amount());
828 // Once the blow has landed, the weapon goes into the bag, and back two
829 // ticks later, so that the server sees a tick without it.
830 const nanommo::protocol::Inventory& bag = (*connection)->inventory();
831 const auto hand = static_cast<uint32_t>(nanommo::items::SLOT_MAIN_HAND);
832 if (cheat == Cheat::kCancel && !away && bag.worn().contains(hand)) {
833 nanommo::protocol::Place worn;
834 worn.set_worn(nanommo::items::SLOT_MAIN_HAND);
835 away.emplace();
836 away->set_bag(FreeSlot(bag));
837 (*connection)->Send(Move(worn, *away));
838 back = std::chrono::steady_clock::now() + 2 * kTick;
839 ++sent;
840 }
841 }
842 }
843 if (away && std::chrono::steady_clock::now() >= back) {
844 nanommo::protocol::Place worn;
845 worn.set_worn(nanommo::items::SLOT_MAIN_HAND);
846 (*connection)->Send(Move(*away, worn));
847 away.reset();
848 ++sent;
849 }
850 // A fight picks its monster once it sees us, and again when it's gone.
851 if (fights && us != nullptr) {
852 const nanommo::protocol::Monster* picked = Monster(world, target);
853 if (picked == nullptr || picked->health() == 0) {
854 target = Pick(world, *us, cheat == Cheat::kReach ? 20.0f : 0.0f);
855 }
856 }
857 const auto ways = (now - start) / kEachWay;
858 const bool east = ways % 2 == 0;
859 // Teleport sends each way as 1000 for three seconds, and then as not a
860 // number for three, which leaves the last keys as they were.
861 const bool number = ways % 2 == 0;
862 const float far_x = (ways / 2) % 2 == 0 ? 1000.0f : -1000.0f;
863 if (now >= tick) {
864 tick += kTick;
865 // The keys, numbered, once a tick, or three times for speed.
866 const int keys = cheat == Cheat::kSpeed ? 3 : 1;
867 for (int i = 0; i < keys; ++i) {
868 nanommo::protocol::Input input;
869 if (cheat == Cheat::kTeleport) {
870 input.set_x(number ? far_x : std::numeric_limits<float>::quiet_NaN());
871 input.set_run(true);
872 } else if (fights) {
873 input.set_target(target);
874 input.set_attack(target != 0);
875 } else {
876 input.set_x(east ? 1.0f : -1.0f);
877 input.set_run(true);
878 }
879 input.set_sequence(++sequence);
880 input.set_seen((*connection)->whole());
881 (*connection)->Send(input);
882 ++sent;
883 }
884 // A skill goes on its own, unnumbered, as the client sends one: once
885 // it's ready for a fight, and ten times a tick for rapid.
886 const bool skills = cheat == Cheat::kFight || cheat == Cheat::kRapid;
887 if (skills && target != 0 && world.skills_size() > 0) {
888 const uint32_t skill = world.skills(0);
889 const bool ready = !world.cooldowns().contains(skill);
890 const int uses = cheat == Cheat::kRapid ? 10
891 : cheat == Cheat::kFight && ready ? 1
892 : 0;
893 for (int i = 0; i < uses; ++i) {
894 nanommo::protocol::Input use;
895 use.set_skill(skill);
896 (*connection)->Send(use);
897 ++sent;
898 }
899 }
900 }
901 if (now - second >= std::chrono::seconds(1)) {
902 second = now;
903 std::cout << "cheat: " << named << " sent " << sent
904 << " inputs a second, and ";
905 if (cheat == Cheat::kTeleport) {
906 std::cout << "with x "
907 << (number ? (far_x > 0 ? "1000" : "-1000") : "not a number")
908 << " ran ";
909 }
910 std::cout << std::fixed << std::setprecision(1);
911 if (!fights) {
912 std::cout << (cheat == Cheat::kTeleport ? "" : "ran ")
913 << speed.MetresASecond() << " m/s";
914 } else {
915 const nanommo::protocol::Monster* picked = Monster(world, target);
916 if (picked != nullptr && us != nullptr) {
917 std::cout << "is "
918 << std::hypot(picked->x() - us->x(), picked->z() - us->z())
919 << " m from its monster, ";
920 }
921 std::cout << "hits " << hits.PerSecond(seconds) << " times a second, "
922 << hits.DamagePerSecond(seconds) << " damage a second";
923 }
924 std::cout << std::endl;
925 sent = 0;
926 }
927 std::this_thread::sleep_for(std::chrono::milliseconds(5));
928 }
929 std::cout << "cheat: the server closed the connection" << std::endl;
930 return 0;
931}
static absl::StatusOr< std::unique_ptr< Connection > > Create(const std::string &host, uint16_t port, asio::ssl::context &tls, const std::string &token, bool udp)
Connects to the server over TLS, or returns why it couldn't.
Definition connection.cc:19
std::optional< Place > At(uint32_t id, std::chrono::nanoseconds time) const
Definition history.cc:20
void Record(uint32_t id, std::chrono::nanoseconds time, Place place)
Definition history.cc:12
float DamagePerSecond(double now) const
Definition measure.cc:55
void Add(double at, uint32_t amount)
A hit that did the amount, at the time, in seconds on our clock.
Definition measure.cc:40
float PerSecond(double now) const
Over the last five seconds to now, as a swing takes about one.
Definition measure.cc:47
float MetresASecond() const
Definition measure.cc:27
void Add(double sent, float x, float z)
Definition measure.cc:17
int main()
Definition login_main.cc:28
absl::StatusOr< protocol::LoginReply > LogIn(const std::string &host, uint16_t port, asio::ssl::context &tls, const protocol::LoginRequest &request, std::chrono::milliseconds deadline)
std::string FileSetting(const char *name, const std::string &path)
Definition settings.cc:22
absl::StatusOr< asio::ssl::context > ClientTls(const std::string &authority)
Definition tls.cc:41
In metres from the middle of a land.
Definition lands.h:13
float z
Definition lands.h:15
float x
Definition lands.h:14