-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbots.c
178 lines (124 loc) · 4.27 KB
/
bots.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "bot_wasm.h"
#include "bots.h"
#include "file_ext.h"
#include "game_mode.h"
#include "include/wasm3.h"
#include "include/wasm3_ext.h"
#include "minimal_state_info.h"
#include "player.h"
#include "player_ability.h"
#include "player_def.h"
uint8_t* get_bot_wasm_memory(IM3Runtime rt) {
return m3_GetMemory(rt, NULL, 0);
}
MinimalTeamInfo* get_bot_wasm_team_ptr(IM3Runtime rt) {
uint64_t teams_ptr;
IM3Function teams_ptr_fn;
m3_FindFunction(&teams_ptr_fn, rt, "teams_ptr");
m3_CallV(teams_ptr_fn);
m3_GetResultsV(teams_ptr_fn, &teams_ptr);
return (MinimalTeamInfo*)&get_bot_wasm_memory(rt)[teams_ptr];
}
uint8_t* get_bot_wasm_num_teams_ptr(IM3Runtime rt) {
uint64_t num_teams_ptr;
IM3Function num_teams_ptr_fn;
m3_FindFunction(&num_teams_ptr_fn, rt, "num_teams_ptr");
m3_CallV(num_teams_ptr_fn);
m3_GetResultsV(num_teams_ptr_fn, &num_teams_ptr);
return &get_bot_wasm_memory(rt)[num_teams_ptr];
}
uint64_t* get_bot_player_id_ptr(IM3Runtime rt) {
uint64_t player_id_ptr;
IM3Function player_id_ptr_fn;
m3_FindFunction(&player_id_ptr_fn, rt, "my_player_id_ptr");
m3_CallV(player_id_ptr_fn);
m3_GetResultsV(player_id_ptr_fn, &player_id_ptr);
return (uint64_t*)&get_bot_wasm_memory(rt)[player_id_ptr];
}
BotInfo* get_bot_info(IM3Runtime rt) {
uint64_t bot_info_ptr;
IM3Function bot_info_ptr_fn;
m3_FindFunction(&bot_info_ptr_fn, rt, "bot_info_ptr");
m3_CallV(bot_info_ptr_fn);
m3_GetResultsV(bot_info_ptr_fn, &bot_info_ptr);
return (BotInfo*)&get_bot_wasm_memory(rt)[bot_info_ptr];
}
Bot* new_bot(const char* bot_file, Ability* ability, Weapon* weapon) {
FILE* wasm_file = fopen(bot_file, "rb");
if (wasm_file == NULL) {
fprintf(stderr, "Couldn't open bot file: %s\b", bot_file);
exit(-1);
}
size_t wasm_file_size = get_file_size(wasm_file);
uint8_t* wasm_file_bytes = malloc(wasm_file_size);
size_t remaining_bytes_to_read = wasm_file_size;
while (remaining_bytes_to_read > 0) {
size_t bytes_read = fread(wasm_file_bytes + (wasm_file_size-remaining_bytes_to_read), 1, wasm_file_size, wasm_file);
if (bytes_read == 0) {
break;
} else {
remaining_bytes_to_read -= bytes_read;
}
}
if (remaining_bytes_to_read > 0) {
fprintf(stderr, "Failed to read entire wasm file\n");
exit(-1);
}
fclose(wasm_file);
IM3Environment env = m3_NewEnvironment();
IM3Runtime rt = m3_NewRuntime(env, 1000000, NULL); // 1 MB
IM3Module module = m3_NewModule(env);
m3_ParseModule(env, &module, wasm_file_bytes, wasm_file_size);
m3_LoadModule(rt, module);
BotInfo* bot_info = get_bot_info(rt);
*ability = bot_info->ability;
*weapon = bot_info->weapon;
Bot* bot = malloc(sizeof(Bot));
bot->rt = rt;
return bot;
}
void run_bots(Player* players, uint8_t num_players, const Map* map) {
for (uint8_t i = 0; i < num_players; i += 1) {
Player* player = &players[i];
if (player->bot_data == NULL) {
continue;
}
IM3Function bot_actions_fn;
if (m3_FindFunction(&bot_actions_fn, player->bot_data->rt, "bot_actions") != m3Err_none) {
fprintf(stderr, "Failed to find bot_actions fn\n");
exit(-1);
}
if (m3_CallV(bot_actions_fn) != m3Err_none) {
M3ErrorInfo err;
m3_GetErrorInfo(player->bot_data->rt, &err);
fprintf(stderr, "Failed to call movement bot_actions_fn: %s\n", err.message);
exit(-1);
}
uint64_t bot_actions_ptr;
m3_GetResultsV(bot_actions_fn, &bot_actions_ptr);
BotActions* bot_actions = (BotActions*)&get_bot_wasm_memory(player->bot_data->rt)[bot_actions_ptr];
if (bot_actions->using_ability) {
use_ability(player, map);
}
player->shooting = bot_actions->shooting;
player->direction = bot_actions->angle;
move_player(player, bot_actions->movement_info, map);
}
}
void sync_teams_bots(Player* players, uint8_t num_players, const GameModeData* game_mode_data) {
for (uint8_t i = 0; i < num_players; i += 1) {
Player* player = &players[i];
if (player->bot_data == NULL) {
continue;
}
Team* team_ptr = get_bot_wasm_team_ptr(player->bot_data->rt);
uint8_t* num_teams = get_bot_wasm_num_teams_ptr(player->bot_data->rt);
*num_teams = game_mode_data->num_teams;
memcpy(team_ptr, game_mode_data_teams, sizeof(Team) * game_mode_data->num_teams);
uint64_t* player_id_ptr = get_bot_player_id_ptr(player->bot_data->rt);
*player_id_ptr = player->id;
}
}