ManaPlus
crazymoves.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "being/crazymoves.h"
23 
24 #include "configuration.h"
25 #include "settings.h"
26 
27 #include "being/localplayer.h"
28 
30 
32 
34 
35 #include "net/packetlimiter.h"
36 #include "net/pethandler.h"
37 #include "net/playerhandler.h"
38 
39 #include "resources/db/emotedb.h"
40 
41 #include "resources/map/map.h"
42 
43 #include "utils/timer.h"
44 
45 #include "debug.h"
46 
48 
50  mMoveProgram(),
51  mDisableCrazyMove(false)
52 {
53 }
54 
56 {
57  const bool oldDisableCrazyMove = mDisableCrazyMove;
58  mDisableCrazyMove = true;
59  switch (settings.crazyMoveType)
60  {
61  case 1:
62  crazyMove1();
63  break;
64  case 2:
65  crazyMove2();
66  break;
67  case 3:
68  crazyMove3();
69  break;
70  case 4:
71  crazyMove4();
72  break;
73  case 5:
74  crazyMove5();
75  break;
76  case 6:
77  crazyMove6();
78  break;
79  case 7:
80  crazyMove7();
81  break;
82  case 8:
83  crazyMove8();
84  break;
85  case 9:
86  crazyMove9();
87  break;
88  case 10:
89  crazyMoveA();
90  break;
91  default:
92  break;
93  }
94  mDisableCrazyMove = oldDisableCrazyMove;
95 }
96 
98 {
99  if ((localPlayer == nullptr) ||
100  (playerHandler == nullptr) ||
102  {
103  return;
104  }
105 
106 // if (!PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
107 // return;
108 
109  switch (localPlayer->getDirection())
110  {
111  case BeingDirection::UP:
115  break;
120  break;
125  break;
130  break;
131  default:
132  break;
133  }
134 }
135 
137 {
138  if ((localPlayer == nullptr) ||
139  (playerHandler == nullptr) ||
141  {
142  return;
143  }
144 
145 // if (!PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
146 // return;
147 
148  switch (localPlayer->getDirection())
149  {
150  case BeingDirection::UP:
156  break;
157 
164  break;
165 
172  break;
173 
180  break;
181 
182  default:
183  break;
184  }
185 }
186 
188 {
189  if ((localPlayer == nullptr) ||
190  (playerHandler == nullptr) ||
192  {
193  return;
194  }
195 
196  switch (settings.crazyMoveState)
197  {
198  case 0:
199  localPlayer->move(1, 1);
201  break;
202  case 1:
203  localPlayer->move(1, -1);
205  break;
206  case 2:
207  localPlayer->move(-1, -1);
209  break;
210  case 3:
211  localPlayer->move(-1, 1);
213  break;
214  default:
215  break;
216  }
217 
218 // if (!PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
219 // return;
220 
223 }
224 
226 {
227  if ((localPlayer == nullptr) ||
229  {
230  return;
231  }
232 
233  switch (settings.crazyMoveState)
234  {
235  case 0:
236  localPlayer->move(7, 0);
238  break;
239  case 1:
240  localPlayer->move(-7, 0);
242  break;
243  default:
244  break;
245  }
246 }
247 
249 {
250  if ((localPlayer == nullptr) ||
252  {
253  return;
254  }
255 
256  switch (settings.crazyMoveState)
257  {
258  case 0:
259  localPlayer->move(0, 7);
261  break;
262  case 1:
263  localPlayer->move(0, -7);
265  break;
266  default:
267  break;
268  }
269 }
270 
272 {
273  if ((localPlayer == nullptr) ||
275  {
276  return;
277  }
278 
279  switch (settings.crazyMoveState)
280  {
281  case 0:
282  localPlayer->move(3, 0);
284  break;
285  case 1:
286  localPlayer->move(2, -2);
288  break;
289  case 2:
290  localPlayer->move(0, -3);
292  break;
293  case 3:
294  localPlayer->move(-2, -2);
296  break;
297  case 4:
298  localPlayer->move(-3, 0);
300  break;
301  case 5:
302  localPlayer->move(-2, 2);
304  break;
305  case 6:
306  localPlayer->move(0, 3);
308  break;
309  case 7:
310  localPlayer->move(2, 2);
312  break;
313  default:
314  break;
315  }
316 }
317 
319 {
320  if ((localPlayer == nullptr) ||
322  {
323  return;
324  }
325 
326  switch (settings.crazyMoveState)
327  {
328  case 0:
329  localPlayer->move(1, 1);
331  break;
332  case 1:
333  localPlayer->move(-1, 1);
335  break;
336  case 2:
337  localPlayer->move(-1, -1);
339  break;
340  case 3:
341  localPlayer->move(1, -1);
343  break;
344  default:
345  break;
346  }
347 }
348 
350 {
351  if (localPlayer == nullptr ||
353  {
354  return;
355  }
356  const Map *const map = localPlayer->getMap();
357  if (map == nullptr)
358  return;
359  const int x = localPlayer->getTileX();
360  const int y = localPlayer->getTileY();
361  int idx = 0;
362  const int dist = 1;
363 
364 // look
365 // up, ri,do,le
366  static const int movesX[][4] =
367  {
368  {-1, 0, 1, 0}, // move left
369  { 0, 1, 0, -1}, // move up
370  { 1, 0, -1, 0}, // move right
371  { 0, -1, 0, 1} // move down
372  };
373 
374 // look
375 // up, ri,do,le
376  static const int movesY[][4] =
377  {
378  { 0, -1, 0, 1}, // move left
379  {-1, 0, 1, 0}, // move up
380  { 0, 1, 0, -1}, // move right
381  { 1, 0, -1, 0} // move down
382  };
383 
384  switch (localPlayer->getDirection())
385  {
386  case BeingDirection::UP:
387  idx = 0;
388  break;
389 
391  idx = 1;
392  break;
393 
395  idx = 2;
396  break;
397 
399  idx = 3;
400  break;
401 
402  default:
403  break;
404  }
405 
406  int mult = 1;
407  const unsigned char blockWalkMask = localPlayer->getBlockWalkMask();
408  if (map->getWalk(x + movesX[idx][0],
409  y + movesY[idx][0], blockWalkMask))
410  {
411  while (map->getWalk(x + movesX[idx][0] * mult,
412  y + movesY[idx][0] * mult,
413  blockWalkMask) && mult <= dist)
414  {
415  mult ++;
416  }
417  localPlayer->move(movesX[idx][0] * (mult - 1),
418  movesY[idx][0] * (mult - 1));
419  }
420  else if (map->getWalk(x + movesX[idx][1],
421  y + movesY[idx][1], blockWalkMask))
422  {
423  while (map->getWalk(x + movesX[idx][1] * mult,
424  y + movesY[idx][1] * mult,
425  blockWalkMask) && mult <= dist)
426  {
427  mult ++;
428  }
429  localPlayer->move(movesX[idx][1] * (mult - 1),
430  movesY[idx][1] * (mult - 1));
431  }
432  else if (map->getWalk(x + movesX[idx][2],
433  y + movesY[idx][2], blockWalkMask))
434  {
435  while (map->getWalk(x + movesX[idx][2] * mult,
436  y + movesY[idx][2] * mult,
437  blockWalkMask) && mult <= dist)
438  {
439  mult ++;
440  }
441  localPlayer->move(movesX[idx][2] * (mult - 1),
442  movesY[idx][2] * (mult - 1));
443  }
444  else if (map->getWalk(x + movesX[idx][3],
445  y + movesY[idx][3], blockWalkMask))
446  {
447  while (map->getWalk(x + movesX[idx][3] * mult,
448  y + movesY[idx][3] * mult,
449  blockWalkMask) && mult <= dist)
450  {
451  mult ++;
452  }
453  localPlayer->move(movesX[idx][3] * (mult - 1),
454  movesY[idx][3] * (mult - 1));
455  }
456 }
457 
459 {
460  int dx = 0;
461  int dy = 0;
462 
463  if (localPlayer == nullptr ||
465  {
466  return;
467  }
468 
469  switch (settings.crazyMoveState)
470  {
471  case 0:
472  switch (localPlayer->getDirection())
473  {
474  case BeingDirection::UP : dy = -1; break;
475  case BeingDirection::DOWN : dy = 1; break;
476  case BeingDirection::LEFT : dx = -1; break;
477  case BeingDirection::RIGHT: dx = 1; break;
478  default: break;
479  }
480  localPlayer->move(dx, dy);
482  break;
483  case 1:
485  if (!localPlayer->allowAction())
486  return;
487  if (playerHandler != nullptr)
489  break;
490  case 2:
492  break;
493  case 3:
495  break;
496  default:
497  break;
498  }
499 }
500 
502 {
503  if (localPlayer == nullptr)
504  return;
505 
507  if (settings.crazyMoveState < mMoveProgram.length())
508  {
509  int dx = 0;
510  int dy = 0;
511 
512  signed char param = mMoveProgram[settings.crazyMoveState++];
513  if (param == '?')
514  {
515  const char cmd[] = {'l', 'r', 'u', 'd', 'L', 'R', 'U', 'D'};
516  srand(tick_time);
517  param = cmd[rand() % 8];
518  }
519  switch (param)
520  {
521  case 'd':
522  localPlayer->move(0, 1);
523  break;
524  case 'u':
525  localPlayer->move(0, -1);
526  break;
527  case 'l':
528  localPlayer->move(-1, 0);
529  break;
530  case 'r':
531  localPlayer->move(1, 0);
532  break;
533  case 'D':
534  localPlayer->move(1, 1);
535  break;
536  case 'U':
537  localPlayer->move(-1, -1);
538  break;
539  case 'L':
540  localPlayer->move(-1, 1);
541  break;
542  case 'R':
543  localPlayer->move(1, -1);
544  break;
545  case 'f':
546  {
547  const uint8_t dir = localPlayer->getDirection();
548  if ((dir & BeingDirection::UP) != 0)
549  dy = -1;
550  else if ((dir & BeingDirection::DOWN) != 0)
551  dy = 1;
552  if ((dir & BeingDirection::LEFT) != 0)
553  dx = -1;
554  else if ((dir & BeingDirection::RIGHT) != 0)
555  dx = 1;
556  localPlayer->move(dx, dy);
557  break;
558  }
559  case 'b':
560  {
561  const uint8_t dir = localPlayer->getDirection();
562  if ((dir & BeingDirection::UP) != 0)
563  dy = 1;
564  else if ((dir & BeingDirection::DOWN) != 0)
565  dy = -1;
566  if ((dir & BeingDirection::LEFT) != 0)
567  dx = 1;
568  else if ((dir & BeingDirection::RIGHT) != 0)
569  dx = -1;
570  localPlayer->move(dx, dy);
571  break;
572  }
573  default:
574  break;
575  }
576  }
577 }
578 
580 {
582 
583  if (settings.crazyMoveState < mMoveProgram.length())
584  {
585  signed char param = mMoveProgram[settings.crazyMoveState++];
586  if (param == '?')
587  {
588  const char cmd[] = {'l', 'r', 'u', 'd'};
589  srand(tick_time);
590  param = cmd[rand() % 4];
591  }
592  switch (param)
593  {
594  case 'd':
595 // if (PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
596  {
600  }
601  break;
602  case 'u':
603 // if (PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
604  {
608  }
609  break;
610  case 'l':
611 // if (PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
612  {
616  }
617  break;
618  case 'r':
619 // if (PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
620  {
624  }
625  break;
626  case 'L':
627 // if (PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
628  {
629  uint8_t dir = 0;
630  switch (localPlayer->getDirection())
631  {
632  case BeingDirection::UP:
633  dir = BeingDirection::LEFT;
634  break;
636  dir = BeingDirection::RIGHT;
637  break;
639  dir = BeingDirection::DOWN;
640  break;
642  dir = BeingDirection::UP;
643  break;
644  default:
645  break;
646  }
649  }
650  break;
651  case 'R':
652 // if (PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
653  {
654  uint8_t dir = 0;
655  switch (localPlayer->getDirection())
656  {
657  case BeingDirection::UP:
658  dir = BeingDirection::RIGHT;
659  break;
661  dir = BeingDirection::LEFT;
662  break;
664  dir = BeingDirection::UP;
665  break;
667  dir = BeingDirection::DOWN;
668  break;
669  default:
670  break;
671  }
674  }
675  break;
676  case 'b':
677 // if (PacketLimiter::limitPackets(PacketType::PACKET_DIRECTION))
678  {
679  uint8_t dir = 0;
680  switch (localPlayer->getDirection())
681  {
682  case BeingDirection::UP:
683  dir = BeingDirection::DOWN;
684  break;
686  dir = BeingDirection::UP;
687  break;
689  dir = BeingDirection::RIGHT;
690  break;
692  dir = BeingDirection::LEFT;
693  break;
694  default:
695  break;
696  }
699  }
700  break;
701  case '0':
703  break;
704  case 'a':
706  break;
707  default:
708  break;
709  }
710  }
711 }
712 
714 {
716  if (localPlayer->toggleSit())
718 }
719 
721 {
723  if (settings.crazyMoveState < mMoveProgram.length())
724  {
725  // wear next outfit
727  {
730  }
731  // wear previous outfit
732  else if (mMoveProgram[settings.crazyMoveState] == 'p')
733  {
736  }
737  }
738 }
739 
741 {
743  const signed char emo = mMoveProgram[settings.crazyMoveState];
744  unsigned char emoteId = 0;
745  if (emo == '?')
746  {
747  srand(tick_time);
748  emoteId = CAST_U8(
749  1 + (rand() % EmoteDB::size()));
750  }
751  else
752  {
753  if (emo >= '0' && emo <= '9')
754  emoteId = CAST_U8(emo - '0' + 1);
755  else if (emo >= 'a' && emo <= 'z')
756  emoteId = CAST_U8(emo - 'a' + 11);
757  else if (emo >= 'A' && emo <= 'Z')
758  emoteId = CAST_U8(emo - 'A' + 37);
759  }
760  if (mMoveProgram[settings.crazyMoveState - 1] == 'e')
761  LocalPlayer::emote(emoteId);
763  petHandler->emote(emoteId);
764 
766 }
767 
769 {
770  mMoveProgram = config.getStringValue("crazyMoveProgram");
771 
773  return;
774 
775  if (mMoveProgram.empty())
776  return;
777 
778  if (settings.crazyMoveState >= mMoveProgram.length())
780 
781  // move command
783  {
784  crazyMoveAm();
785  }
786  // direction command
787  else if (mMoveProgram[settings.crazyMoveState] == 'd')
788  {
789  crazyMoveAd();
790  }
791  // sit command
792  else if (mMoveProgram[settings.crazyMoveState] == 's')
793  {
794  crazyMoveAs();
795  }
796  // wear outfits
797  else if (mMoveProgram[settings.crazyMoveState] == 'o')
798  {
799  crazyMoveAo();
800  }
801  // pause
802  else if (mMoveProgram[settings.crazyMoveState] == 'w')
803  {
805  }
806  // pick up
807  else if (mMoveProgram[settings.crazyMoveState] == 'p')
808  {
811  }
812  // emote
813  else if (mMoveProgram[settings.crazyMoveState] == 'e'
815  {
816  crazyMoveAe();
817  }
818  else
819  {
821  }
822 
823  if (settings.crazyMoveState >= mMoveProgram.length())
825 }
#define CAST_U8
Definition: cast.h:27
const Map * getMap() const
Definition: actor.h:130
virtual void setDirection(const uint8_t direction)
Definition: being.cpp:1690
int getTileX() const
Definition: being.h:168
uint8_t getDirection() const
Definition: being.h:494
int getTileY() const
Definition: being.h:174
BeingActionT getCurrentAction() const
Definition: being.h:482
std::string getStringValue(const std::string &key) const
static void crazyMove9()
Definition: crazymoves.cpp:458
static void crazyMove7()
Definition: crazymoves.cpp:318
void crazyMoveAd() const
Definition: crazymoves.cpp:579
void crazyMove()
Definition: crazymoves.cpp:55
void crazyMoveA()
Definition: crazymoves.cpp:768
static void crazyMove1()
Definition: crazymoves.cpp:97
static void crazyMove5()
Definition: crazymoves.cpp:248
void crazyMoveAe() const
Definition: crazymoves.cpp:740
static void crazyMove6()
Definition: crazymoves.cpp:271
static void crazyMove8()
Definition: crazymoves.cpp:349
std::string mMoveProgram
Definition: crazymoves.h:55
void crazyMoveAm() const
Definition: crazymoves.cpp:501
bool mDisableCrazyMove
Definition: crazymoves.h:58
static void crazyMove4()
Definition: crazymoves.cpp:225
static void crazyMove3()
Definition: crazymoves.cpp:187
static void crazyMove2()
Definition: crazymoves.cpp:136
void crazyMoveAo() const
Definition: crazymoves.cpp:720
static void crazyMoveAs()
Definition: crazymoves.cpp:713
void dropFirst() const
void dropItems(const int cnt)
void setWalkingDir(const unsigned char dir)
bool pickUpItems(int pickUpType)
bool toggleSit() const
void move(const int dX, const int dY)
bool allowAction()
static bool emote(const uint8_t emotion)
unsigned char getBlockWalkMask() const A_CONST
Definition: map.h:75
bool getWalk(const int x, const int y, const unsigned char blockWalkMask) const
Definition: map.cpp:785
virtual void emote(const uint8_t emoteId)=0
virtual void setDirection(const unsigned char direction) const =0
virtual void changeAction(const BeingActionT &action) const =0
void wearNextOutfit(const bool all)
void wearPreviousOutfit(const bool all)
unsigned int crazyMoveState
Definition: settings.h:145
unsigned int crazyMoveType
Definition: settings.h:134
Configuration config
CrazyMoves * crazyMoves
Definition: crazymoves.cpp:47
DropShortcut * dropShortcut
LocalPlayer * localPlayer
volatile int tick_time
Definition: timer.cpp:53
int size()
Definition: emotedb.cpp:306
bool limitPackets(const PacketTypeT type)
static const int blockWalkMask
Net::PetHandler * petHandler
Definition: net.cpp:95
Net::PlayerHandler * playerHandler
Definition: net.cpp:96
OutfitWindow * outfitWindow
Settings settings
Definition: settings.cpp:32