3 #include <boost/thread.hpp>
4 #include <boost/foreach.hpp>
5 #include <ail/string.hpp>
6 #include <ail/time.hpp>
9 #include "d2_functions.hpp"
10 #include "reveal_map.hpp"
11 #include "utility.hpp"
14 //#define MAPHACK_TEST
23 console_command commands
[] =
25 console_command("help", "", "Prints this help", 0, &print_help
),
26 console_command("?", "", "See 'help'", 0, &print_help
),
27 console_command("quit", "", "Terminates the program", 0, &quit_program
),
28 console_command("exit", "", "See 'quit'", 0, &quit_program
),
29 console_command("life", "", "Print your character's life", 0, &print_life
),
30 console_command("reveal", "", "Reveal the map of the act your character is currently in", 0, &reveal_act_command
),
31 //console_command("test", "", "Perform maphack test", 0, &maphack_test),
32 console_command("name", "", "Retrieve your character's name", 0, &get_character_name_command
),
33 console_command("player", "", "Get player pointer", 0, &get_player_pointer
),
34 console_command("move", "<x> <y>", "Move to the specified coordinates.", 2, &move
),
35 console_command("pid", "", "Print the process ID", 0, &print_pid
),
36 console_command("test", "", "Run test function", 0, &run_test
),
37 console_command("print", " <text>", "Print chat text", -1, &print_text
),
41 console_command::console_command(std::string
const & command
, std::string
const & argument_description
, std::string
const & description
, long argument_count
, command_handler handler
):
43 argument_description(argument_description
),
44 description(description
),
45 argument_count(argument_count
),
50 bool console_command::match(std::string
const & match_command
, string_vector
const & arguments
) const
52 return command
== match_command
&& (argument_count
== -1 || argument_count
== arguments
.size());
55 void print_life(string_vector
const & arguments
)
61 if(get_life(current_life
, maximum_life
))
62 std::cout
<< "Life: " << current_life
<< "/" << maximum_life
<< std::endl
;
64 std::cout
<< "Your character is not in a game" << std::endl
;
67 void get_player_pointer(string_vector
const & arguments
)
69 unit
* player_pointer
= d2_get_player_unit();
70 std::cout
<< ail::hex_string_32(reinterpret_cast<ulong
>(player_pointer
)) << std::endl
;
71 if(player_pointer
&& player_pointer
->path_data_pointer
)
73 path_data
& data
= *(player_pointer
->path_data_pointer
);
74 std::cout
<< "Location: " << data
.position_x
<< ", " << data
.position_y
<< std::endl
;
78 void maphack_test(string_vector
const & arguments
)
82 std::cout
<< "Running the maphack test" << std::endl
;
86 std::cout
<< "Defined the offsets" << std::endl
;
90 std::cout
<< "The game is not ready" << std::endl
;
94 thread_controller controller
;
95 if(!controller
.suspend())
97 std::cout
<< "Failed to suspend all threads, resuming them" << std::endl
;
102 std::cout
<< "Revealing the act" << std::endl
;
105 std::cout
<< "Done revealing the map" << std::endl
;
107 std::cout
<< "Failed to reveal the act" << std::endl
;
112 void move(string_vector
const & arguments
)
118 if(!ail::string_to_number(arguments
[0], x
) || !ail::string_to_number(arguments
[1], y
))
120 std::cout
<< "Invalid coordinates specified" << std::endl
;
126 std::cout
<< "Moving to (" << x
<< ", " << y
<< ")" << std::endl
;
131 void reveal_act_command(string_vector
const & arguments
)
133 std::cout
<< "Revealing the map" << std::endl
;
135 bool const use_suspension
= false;
137 thread_controller controller
;
139 if(use_suspension
&& !controller
.suspend())
141 std::cout
<< "Failed to suspend all threads, resuming them" << std::endl
;
146 ullong start
= ail::milliseconds();
148 bool success
= reveal_act();
150 ullong duration
= ail::milliseconds() - start
;
152 if(use_suspension
&& !controller
.resume())
156 std::cout
<< "Done revealing the map after " << duration
<< " ms" << std::endl
;
158 std::cout
<< "Failed to reveal the act" << std::endl
;
161 void get_character_name_command(string_vector
const & arguments
)
163 std::cout
<< "Attempting to retrieve your character's name" << std::endl
;
164 unit
* unit_pointer
= d2_get_player_unit();
165 if(unit_pointer
== 0)
167 std::cout
<< "You do not appear to be in a game" << std::endl
;
172 if(get_name_by_id(unit_pointer
->id
, name
))
173 std::cout
<< "Your name is " << name
<< "." << std::endl
;
175 std::cout
<< "Failed to retrieve your name." << std::endl
;
178 void print_help(string_vector
const & arguments
)
180 std::cout
<< "Available commands:" << std::endl
;
181 BOOST_FOREACH(console_command
const & current_command
, commands
)
183 std::cout
<< std::endl
<< "Command: " << current_command
.command
<< " " << current_command
.argument_description
<< std::endl
;
184 std::cout
<< "Description: " << current_command
.description
<< std::endl
;
188 void quit_program(string_vector
const & arguments
)
190 std::cout
<< "Terminating..." << std::endl
;
194 void print_pid(string_vector
const & arguments
)
196 std::cout
<< ail::hex_string_32(GetCurrentProcessId()) << std::endl
;
199 void run_test(string_vector
const & arguments
)
201 unit
* unit_pointer
= d2_get_player_unit();
202 if(unit_pointer
== 0)
204 std::cout
<< "Not in a game" << std::endl
;
208 std::vector
<unit
> minions
;
209 if(!get_minions(unit_pointer
->id
, minions
))
211 std::cout
<< "Failed to retrieve minions" << std::endl
;
213 std::cout
<< "Counted " << minions
.size() << " minion(s)" << std::endl
;
216 void print_text(string_vector
const & arguments
)
220 BOOST_FOREACH(std::string
const & argument
, arguments
)
228 std::cout
<< "Printing \"" << text
<< "\"" << std::endl
;
229 print_chat_text(text
);
232 void console_prompt()
238 std::getline(std::cin
, line
);
241 string_vector tokens
= ail::tokenise(line
, " ");
245 std::string
const & command
= tokens
[0];
247 bool success
= false;
249 string_vector arguments
= tokens
;
250 arguments
.erase(arguments
.begin());
252 BOOST_FOREACH(console_command
const & current_command
, commands
)
254 if(current_command
.match(command
, arguments
))
256 current_command
.handler(arguments
);
257 if(command
!= "help")
263 if(!success
&& !python::perform_command_callback(line
))
264 std::cout
<< "Unknown command: \"" << command
<< "\"" << std::endl
;
271 new boost::thread(&console_prompt
);