Pokebot
Pokemon FireRed bot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Script.hh
Go to the documentation of this file.
1 #ifndef __SCRIPT_HH__
2 #define __SCRIPT_HH__
3 
4 #include <sstream>
5 #include <iterator>
6 #include <iostream>
7 #include <string>
8 #include <cstdio>
9 #include <stdarg.h>
10 #include <stdint.h>
11 #include <vector>
12 #include <list>
13 #include <queue>
14 #include <map>
15 
16 #include "Action/AAction.hh"
17 #include "PokemonUtils.hh"
18 #include "../vbam/gba/Globals.h"
19 #include "Data.hh"
20 
21 class Script
22 {
23 private:
24  struct Range
25  {
26  Range(uint32_t x = 0, uint32_t y = 0)
27  : start(x), end(y) {}
28  uint32_t start;
29  uint32_t end;
30  };
31 
32 public:
33  typedef std::vector<uint32_t> Args;
34  typedef std::vector<std::string> TypeList;
35  typedef uint32_t(Script::*ParamReader)();
36 
37  struct Instruction
38  {
39  uint32_t offset;
40  uint8_t *bytecode;
41  uint8_t length;
42  uint32_t next;
43  uint8_t cmd;
44  std::string str;
45  Args args;
46  TypeList types;
47  uint8_t toVisit; // bit field storing what parts of the branch was visited
48 
49  Instruction(uint32_t p_off, uint8_t *p_mem)
50  : offset(p_off), bytecode(p_mem + p_off), length(1), cmd(*bytecode), str(""),
51  toVisit((cmd == 0x06 || cmd == 0x07) * 3)
52  {}
53  bool notVisited(bool result)
54  { return (toVisit & (1 << result)); }
55  void visit(bool result)
56  { toVisit &= ~(1 << result); }
57  void print()
58  {
59  printf("%#08x: ", offset);
60  for (int i = 0; i < 10; i++)
61  printf(i < length ? "%02x " : " ", bytecode[i]);
62  printf("%s\n", str.c_str());
63  }
64  void setLength(uint8_t len)
65  {
66  length = len;
67  next = (cmd == 0x02 || cmd == 0x03 ? 0 : offset + length);
68  }
69  };
70 
71  struct Command
72  {
73  Command(std::initializer_list<std::string> il)
74  : format(il.size() ? *(il.begin()) : "UNKNOWN"),
75  args(il.size() > 1 ? *(il.begin() + 1) : ""),
76  hook(NULL)
77  {}
78  Command(const std::string &fmt, const std::string &params = "", void (Script::*func)(Instruction *) = NULL)
79  : format(fmt), args(params), hook(func)
80  {}
81 
82  std::string format;
83  std::string args;
84  void (Script::*hook)(Instruction *);
85  };
86 
87 public:
88  Script();
89  ~Script();
90 
91 public:
92  Script &load(uint32_t ptr);
93  Script &loadStd(uint8_t n);
94  void print();
95  static Script &getStd(uint8_t n) { return (_std[n * (n < 10)]); }
96  static void initStd();
97  static Script *getPerson(uint8_t bank, uint8_t map, uint8_t id);
98  static Script *getPerson(uint8_t id);
99  static Script *getSign(uint8_t bank, uint8_t map, uint8_t id);
100  static Script *getSign(uint8_t id);
101  static Script *getScript(uint8_t bank, uint8_t map, uint8_t id);
102  static Script *getScript(uint8_t id);
103 
104 public:
105  std::map<int, Instruction *> &getInstructions() { return (_instructions); }
106  uint32_t getStartOffset() { return (_offset); }
107 
108 private:
109  void _subPrint(uint32_t ptr);
110  void _reset();
111  bool _setupNextAddr();
112  void _getInstruction(Command &cmd);
113 
114 private:
115  uint32_t _readByte();
116  uint32_t _readWord();
117  uint32_t _readDword();
118 
119 private:
120  void _loadpointer(Instruction *instr);
121  void _bufferstring(Instruction *instr);
122  void _preparemsg(Instruction *instr);
123  void _checkattack(Instruction *instr);
124  void _if(Instruction *instr);
125  void _branch(Instruction *instr);
126 
127 private:
128  Data &_data;
129  uint32_t _offset;
130  uint32_t _start;
131  uint8_t *_ptr;
132  uint32_t _pc;
133  uint32_t _oldpc;
134  std::vector<Range> _ranges;
135  std::queue<uint32_t> _addrs;
136  std::map<int, Instruction *> _instructions;
137 
138  static Command _cmds[0xD6];
139  static std::map<std::string, ParamReader> _readers;
140  static Script _std[10];
141 };
142 
143 #endif
Instruction(uint32_t p_off, uint8_t *p_mem)
Definition: Script.hh:49
uint8_t * bytecode
Definition: Script.hh:40
Args args
Definition: Script.hh:45
std::string args
Definition: Script.hh:83
bool notVisited(bool result)
Definition: Script.hh:53
std::vector< uint32_t > Args
Definition: Script.hh:33
Definition: Script.hh:37
static Script * getPerson(uint8_t bank, uint8_t map, uint8_t id)
Definition: Script.cpp:178
std::vector< std::string > TypeList
Definition: Script.hh:34
std::string format
Definition: Script.hh:82
uint32_t next
Definition: Script.hh:42
Definition: Script.hh:21
TypeList types
Definition: Script.hh:46
static void initStd()
Definition: Script.cpp:127
Script & load(uint32_t ptr)
Definition: Script.cpp:93
std::string str
Definition: Script.hh:44
uint8_t length
Definition: Script.hh:41
Command(const std::string &fmt, const std::string &params="", void(Script::*func)(Instruction *)=NULL)
Definition: Script.hh:78
uint32_t offset
Definition: Script.hh:39
Command(std::initializer_list< std::string > il)
Definition: Script.hh:73
uint8_t toVisit
Definition: Script.hh:47
std::map< int, Instruction * > & getInstructions()
Definition: Script.hh:105
Definition: Script.hh:71
static Script * getSign(uint8_t bank, uint8_t map, uint8_t id)
Definition: Script.cpp:199
void setLength(uint8_t len)
Definition: Script.hh:64
uint32_t(Script::* ParamReader)()
Definition: Script.hh:35
static Script * getScript(uint8_t bank, uint8_t map, uint8_t id)
Definition: Script.cpp:219
~Script()
Definition: Script.cpp:10
void print()
Definition: Script.cpp:156
uint8_t cmd
Definition: Script.hh:43
Definition: Data.hh:27
static Script & getStd(uint8_t n)
Definition: Script.hh:95
Range(float a=0, float b=0)
Definition: Data.hh:21
Script()
Definition: Script.cpp:5
uint32_t getStartOffset()
Definition: Script.hh:106
void visit(bool result)
Definition: Script.hh:55
void print()
Definition: Script.hh:57
void(Script::* hook)(Instruction *)
Definition: Script.hh:84
Script & loadStd(uint8_t n)
Definition: Script.cpp:121