[go: up one dir, main page]

Menu

[904618]: / src / bot_vm.c  Maximize  Restore  History

Download this file

380 lines (321 with data), 8.7 kB

  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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 2013-2013 GhostlyDeath <ghostlydeath@remood.org>
// <ghostlydeath@gmail.com>
// ----------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// ----------------------------------------------------------------------------
// DESCRIPTION: Virtual Machine Helpers for bots
/***************
*** INCLUDES ***
***************/
#include "doomtype.h"
#include "bot.h"
#include "sn.h"
#include "d_netcmd.h"
#include "bot_priv.h"
#include "console.h"
#include "g_state.h"
#include "z_zone.h"
#include "w_wad.h"
#include "m_argv.h"
#include "d_player.h"
#include "bot_lib.h"
#include "r_defs.h"
#include "r_state.h"
#include "p_local.h"
#include "p_maputl.h"
#include "p_setup.h"
#include "m_random.h"
/****************
*** FUNCTIONS ***
****************/
/* BOT_VMFDataStructs() -- Data Structure Handling */
static uint32_t BOT_VMFDataStructs(MIPS_VM_t* const a_VM, MIPS_Map_t* const a_Map, const uint_fast32_t a_BaseAddr)
{
uint32_t Index;
uint32_t ABase, SBase;
union
{
player_t* pl;
mobj_t* mo;
sector_t* Sect;
mapthing_t* MT;
node_t* Node;
subsector_t* Sub;
line_t* Line;
side_t* Side;
seg_t* Seg;
msecnode_t* MSec;
ffloor_t* FF;
vertex_t* V;
} p;
/* Players */
if (a_BaseAddr >= MPLAYERBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MPLAYERBASEADDR);
Index = ABase / MPLAYERSIZE;
SBase = ABase % MPLAYERSIZE;
if (Index < MAXPLAYERS)
p.pl = &players[Index];
else
return 0;
}
/* Mobjs */
else if (a_BaseAddr >= MOBJECTBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MOBJECTBASEADDR);
Index = ABase / MOBJECTSIZE;
SBase = ABase % MOBJECTSIZE;
return 0;
}
/* Sectors */
else if (a_BaseAddr >= MSECTORBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MSECTORBASEADDR);
Index = ABase / MSECTORSIZE;
SBase = ABase % MSECTORSIZE;
if (Index < numsectors)
p.Sect = &sectors[Index];
else
return 0;
}
/* Map Things */
else if (a_BaseAddr >= MTHINGBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MTHINGBASEADDR);
Index = ABase / MTHINGSIZE;
SBase = ABase % MTHINGSIZE;
if (Index < nummapthings)
p.MT = &mapthings[Index];
else
return 0;
}
/* Nodes */
else if (a_BaseAddr >= MNODEBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MNODEBASEADDR);
Index = ABase / MNODESIZE;
SBase = ABase % MNODESIZE;
if (Index < numnodes)
p.Node = &nodes[Index];
else
return 0;
}
/* Sub Sector */
else if (a_BaseAddr >= MSUBSBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MSUBSBASEADDR);
Index = ABase / MSUBSSIZE;
SBase = ABase % MSUBSSIZE;
if (Index < numsubsectors)
p.Sub = &subsectors[Index];
else
return 0;
}
/* LineDef */
else if (a_BaseAddr >= MLINEBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MLINEBASEADDR);
Index = ABase / MLINESIZE;
SBase = ABase % MLINESIZE;
if (Index < numlines)
p.Line = &lines[Index];
else
return 0;
}
/* SideDef */
else if (a_BaseAddr >= MSIDEBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MSIDEBASEADDR);
Index = ABase / MSIDESIZE;
SBase = ABase % MSIDESIZE;
if (Index < numsides)
p.Side = &sides[Index];
else
return 0;
}
/* Segs */
else if (a_BaseAddr >= MSEGBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MSEGBASEADDR);
Index = ABase / MSEGSIZE;
SBase = ABase % MSEGSIZE;
if (Index < numsegs)
p.Seg = &segs[Index];
else
return 0;
}
/* Sector Nodes */
else if (a_BaseAddr >= MSECNODEBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MSECNODEBASEADDR);
Index = ABase / MSECNODESIZE;
SBase = ABase % MSECNODESIZE;
if (Index < g_NumMSecNodes)
p.MSec = g_MSecNodes[Index];
else
return 0;
// Illegal SecNode?
if (!p.MSec)
return 0;
}
/* Fake Floors */
else if (a_BaseAddr >= MFFBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MFFBASEADDR);
Index = ABase / MFFSIZE;
SBase = ABase % MFFSIZE;
if (Index < g_NumPFakeFloors)
p.FF = g_PFakeFloors[Index];
else
return 0;
// Illegal Floor?
if (!p.FF)
return 0;
}
/* Vertexes */
else if (a_BaseAddr >= MVERTEXBASEADDR)
{
// Obtain Index
ABase = (a_BaseAddr - MVERTEXBASEADDR);
Index = ABase / MVERTEXSIZE;
SBase = ABase % MVERTEXSIZE;
if (Index < numvertexes)
p.V = &vertexes[Index];
else
return 0;
// Based on struct addresss
switch (SBase)
{
case 0: return p.V->x;
case 4: return p.V->y;
}
}
/* Unknown */
return 0;
}
/* BOT_SysCall() -- Handles system calls */
static bool_t BOT_SysCall(MIPS_VM_t* const a_VM, void* const a_Data)
{
#define BUFSIZE 256
char Buf[BUFSIZE];
int i;
BOT_t* Bot = a_Data;
uint32_t ko = a_VM->CPU.r[MIPS_K0];
uint32_t kl = a_VM->CPU.r[MIPS_K1];
fixed_t fa, fb, fc, fd;
/* Based on syscall performed */
switch (ko)
{
// Sleep(len)
case BOTSYSCALL_BSLEEP:
Bot->SleepCount = MIPS_ReadMemX(a_VM, kl, 4);
return false; // Break execution for a bit
// FMul(a, b) -> c
case BOTSYSCALL_FMUL:
a_VM->CPU.r[MIPS_K1] = FixedMul(MIPS_ReadMemX(a_VM, kl, 4), MIPS_ReadMemX(a_VM, kl + 4, 4));
break;
// DistTo(a, b) -> c
case BOTSYSCALL_DISTTO:
fa = Bot->BInfo.x;
fb = Bot->BInfo.y;
fc = MIPS_ReadMemX(a_VM, kl, 4);
fd = MIPS_ReadMemX(a_VM, kl + 4, 4);
a_VM->CPU.r[MIPS_K1] = P_AproxDistance(fc - fa, fd - fb);
break;
// SightTo(a, b) -> true/false
case BOTSYSCALL_SIGHTTO:
fa = Bot->BInfo.x;
fb = Bot->BInfo.y;
fc = MIPS_ReadMemX(a_VM, kl, 4);
fd = MIPS_ReadMemX(a_VM, kl + 4, 4);
a_VM->CPU.r[MIPS_K1] = P_CheckSightLine(fa, fb, fc, fd);
break;
// Chat Message (A bit slow)
case BOTSYSCALL_CHAT:
// Read chat buffer
memset(Buf, 0, sizeof(Buf));
for (i = 0; i < BUFSIZE - 1; i++)
{
Buf[i] = MIPS_ReadMemX(a_VM, kl + i, 1);
// No more characters
if (!Buf[i])
break;
// Do not permit below non-ASCII characters
if (Buf[i] < ' ' || Buf[i] > 0x7F)
Buf[i] = '?';
}
// Send chat to SN system
if (Buf[0])
SN_SendChat(Bot->Port, false, Buf);
break;
// Returns a random number
case BOTSYSCALL_RANDLTZGT:
fa = M_Random() & 3;
a_VM->CPU.r[MIPS_K1] = !!(fa & 1);
if (fa & 2)
a_VM->CPU.r[MIPS_K1] = (~a_VM->CPU.r[MIPS_K1]) + 1;
// Unknown SysCall
default:
break;
}
#define BOTSYSCALL_DISTTO UINT32_C(0x00000003)
#define BOTSYSCALL_SIGHTTO UINT32_C(0x00000004)
/* Always continue */
return true;
#undef BUFSIZE
}
/* BOT_RegisterVMJunk() -- Registers virtual machine hanlders */
void BOT_RegisterVMJunk(BOT_t* const a_Bot)
{
MIPS_VM_t* VM;
/* Check */
if (!a_Bot)
return;
/* Get virtual machine pointer */
VM = &a_Bot->VM;
/* Handle System Calls */
VM->DataP = a_Bot;
VM->SysCall = BOT_SysCall;
/* Map Data Structure Handler */
MIPS_VMAddMapFunc(VM, BOT_VMFDataStructs, NULL, MDSBASEADDR, MDSENDADDR - MDSBASEADDR, MIPS_MFR);
/* Map Static R (possibly W) structures */
// Information on the bot
MIPS_VMAddMap(VM, &a_Bot->BInfo, BBINFOBASEADDR, sizeof(a_Bot->BInfo), MIPS_MFR | MIPS_MFW);
// Real Time
MIPS_VMAddMap(VM, &g_BotRT, BBREALBASEADDR, sizeof(g_BotRT), MIPS_MFR);
}