-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgame.h
510 lines (398 loc) · 9.43 KB
/
game.h
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#pragma once
#include "physics.h"
#include "render.h"
#include "sprite.h"
#include "world.h"
#include "terrain.h"
#include "inventory.h"
#include "network.h"
enum GAME_KEYB
{
KEYB_DOWN,
KEYB_UP,
KEYB_CHAR,
KEYB_PRESS, // non-char terminal input with modifiers
};
enum GAME_MOUSE
{
MOUSE_MOVE,
MOUSE_LEFT_BUT_DOWN,
MOUSE_LEFT_BUT_UP,
MOUSE_RIGHT_BUT_DOWN,
MOUSE_RIGHT_BUT_UP,
MOUSE_MIDDLE_BUT_DOWN,
MOUSE_MIDDLE_BUT_UP,
MOUSE_WHEEL_DOWN,
MOUSE_WHEEL_UP
};
enum GAME_TOUCH
{
TOUCH_MOVE,
TOUCH_BEGIN,
TOUCH_END,
TOUCH_CANCEL
};
void Buzz();
void ConvertToCP437(char* cp437, const char* utf8, int maxlen=-1);
extern char player_name[];
extern char player_name_cp437[];
struct ACTION { enum
{
NONE = 0, // IDLE/MOVE
ATTACK,
/*HIT,*/
FALL,
DEAD,
STAND,
SIZE
};};
struct WEAPON { enum
{
NONE = 0,
REGULAR_SWORD,
REGULAR_CROSSBOW,
SIZE
};};
struct SHIELD { enum
{
NONE = 0,
REGULAR_SHIELD,
SIZE
};};
struct HELMET { enum
{
NONE = 0,
REGULAR_HELMET,
SIZE
};};
struct ARMOR { enum
{
NONE = 0,
REGULAR_ARMOR,
SIZE
};};
struct MOUNT { enum
{
NONE = 0,
WOLF,
BEE,
SIZE
};};
struct SpriteReq
{
enum KIND
{
HUMAN = 0,
WOLF = 1,
BEE = 2,
};
KIND kind;
// only human
int mount;
int action;
int armor;
int helmet;
int shield;
int weapon;
};
struct ItemOwner
{
// NPCs carrying items should inherit from it
static const int max_items = 5;
int items;
struct
{
Item* item;
int story_id;
bool in_use;
} has[max_items];
};
struct Character
{
// recolor?
Sprite* sprite;
int anim;
int frame;
float pos[3];
float dir;
float impulse[2];
uint64_t action_stamp;
bool hit_tested;
int HP, MAX_HP;
bool SetActionNone(uint64_t stamp);
bool SetActionAttack(uint64_t stamp);
bool SetActionFall(uint64_t stamp);
bool SetActionStand(uint64_t stamp);
bool SetActionDead(uint64_t stamp);
Character* prev;
Character* next;
SpriteReq req; // kind of character is inside (human / wolf)!!!
int leak; // blood / guts
int leak_steps;
Inst* inst; // only server players
int clr;
int stuck;
int around;
float unstuck[2][3]; // [0]unstuck, [1]candid
void* data; // npc physics
void* gen; // enemygen for reviving
Character* master;
Character* target; // can be 0, master or any enemy
Character* shoot_by;
uint64_t shoot_by_stamp;
int followers;
bool jump; // helper if got stuck
bool enemy; // buddy otherwise!
};
struct TalkBox;
Sprite* GetSprite(const SpriteReq* req, int clr = 0);
struct Human : Character
{
char name[32*4];
char name_cp437[32];
int level;
int max_xp; // to next level = C1 * pow(C2,C3*level)
int cur_xp; // 0..max_xp-1
int pr; // 0-transparent <0-evil >0-good ...
int max_hp;
int cur_hp;
int max_mp;
int cur_mp;
int max_speed;
int cur_speed;
int max_power;
int cur_power;
// 0-6
int prot_hit;
int prot_fire;
int nutr_vits;
int nutr_mins;
int nutr_prots;
int nutr_fats;
int nutr_carbs;
int nutr_water;
// -------------
bool SetWeapon(int w);
bool SetShield(int s);
bool SetHelmet(int h);
bool SetArmor(int a);
bool SetMount(int m);
void Say(const char* str, int len, uint64_t stamp);
TalkBox* talk_box;
struct Talk
{
uint64_t stamp;
TalkBox* box;
float pos[3];
};
int talks;
Talk talk[3];
Character* shoot_target;
uint64_t shoot_stamp;
float shoot_from[3];
float shoot_to[3];
bool shooting;
};
struct NPC_Creature : Character, ItemOwner {};
struct NPC_Human : Human, ItemOwner {};
struct Server
{
bool Proc(const uint8_t* ptr, int size); // called directly by JS (implemented in game.cpp)
void Proc(); // does nothing on JS, native apps calls above func for all queued commands
bool Send(const uint8_t* data, int size); // implemented by game_app/game_web
void Log(const char* str);
int max_clients;
Human* others; // [max_clients]
Human* head;
Human* tail;
uint64_t stamp;
uint64_t last_lag;
int lag_ms;
bool lag_wait;
// pose->pad with hold new/del/upd flags
};
extern Server* server; // global!
struct Game
{
// terrain & world are global
// World* world;
// Terrain* terrain;
uint64_t stamp;
bool main_menu;
static const int fps_window_size = 100;
int fps_window_pos;
uint64_t fps_window[fps_window_size];
int font_size[2];
int render_size[2];
bool mute;
bool perspective;
bool blood;
//bool player_hit; // helper for detecting clicks on the player sprite
bool show_keyb; // activated together with talk_box by clicking on character
int keyb_hide; // show / hide animator (vertical position)
bool show_gamepad;
bool show_inventory;
int scene_shift;
bool show_buts; // true only if no popup is visible
int bars_pos; // used to hide buts (0..7)
// time relaxated KEY_UP/DOWN emulation by KEY_PRESSes
uint64_t PressStamp;
int PressKey;
int TalkBox_blink;
int KeybAutoRepCap;
char KeybAutoRepChar;
uint64_t KeybAuroRepDelayStamp;
int keyb_pos[2];
int keyb_mul;
uint8_t keyb_key[32]; // simulated key presses by touch/mouse
int water;
float light[4];
float prev_yaw;
float yaw_vel;
bool prev_grounded; // moved from static Render::prev_grounded
Renderer* renderer;
Physics* physics;
Item** items_inrange;
int items_count;
int items_xarr[10];
int items_ylo;
int items_yhi;
Human player;
Inventory inventory;
bool DropItem(int index);
bool PickItem(Item* item);
bool CheckDrop(int c/*contact index*/, int xy[2]=0, AnsiCell* ptr=0, int w=0, int h=0);
int CheckPick(const int pos[2]);
struct TalkMem
{
char buf[256];
int len;
};
TalkMem talk_mem[4];
Inst* player_inst;
void CancelItemContacts();
void ExecuteItem(int my_item);
void StartContact(int id, int x, int y, int b);
void MoveContact(int id, int x, int y);
void EndContact(int id, int x, int y);
int GetContact(int id);
struct ConsumeAnim
{
int pos[2];
Sprite* sprite;
uint64_t stamp;
};
int consume_anims;
ConsumeAnim consume_anim[16];
// accumulated input state
struct Input
{
int last_hit_char;
uint8_t key[32]; // keyb state
// pad state
int pad_item; // item index to pick + 1
bool pad_connected;
int pad_autorep; // button+1
uint64_t pad_stamp;
uint32_t pad_button;
int16_t pad_axis[32];
// we split touch input to multiple separate mice with left button only
struct Contact
{
enum
{
NONE,
KEYBCAP,
PLAYER,
TORQUE, // can be abs (right mouse but) or (timer touch on margin)
FORCE,
ITEM_LIST_CLICK,
ITEM_LIST_DRAG,
ITEM_GRID_CLICK,
ITEM_GRID_DRAG,
ITEM_GRID_SCROLL,
};
int action;
int drag; // which button initiated drag and is still down since then OR ZERO if there is no contact!!!
int pos[2]; // mouse pos
int drag_from[2]; // where drag has started
Item* item;
int my_item;
int keyb_cap; // if touch starts at some cap
bool player_hit; // if touch started at player/talkbox
int margin; // -1: if touch started at left margin, +1 : if touch started at right margin, 0 otherwise
float start_yaw; // absolute by mouse
int scroll;
};
Contact contact[4]; // 0:mouse, 1:primary_touch 2:secondary_touch ( 3:unused -> GAMEPAD/KEYB )
uint8_t but; // real mouse buttons currently down
int wheel; // relative mouse wheel (only from real mouse)
int size[2]; // window size (in pixels)
bool jump;
float api_move[3]; // x,y,alpha
// x,y is screen space (rotate it by yaw to set it in world space)
// alpha=0 : fully additive,
// alpha=1 : replace
bool shoot;
int shoot_xy[2];
bool shot; // screenshot!
bool IsKeyDown(int k)
{
return (key[k >> 3] & (1 << (k & 7))) != 0;
}
void ClearKey(int k)
{
key[k >> 3] &= ~(1 << (k & 7));
}
};
Input input;
// just accumulates input
void OnKeyb(GAME_KEYB keyb, int key);
void OnMouse(GAME_MOUSE mouse, int x, int y);
void OnTouch(GAME_TOUCH touch, int id, int x, int y);
void OnFocus(bool set);
void OnSize(int w, int h, int fw, int fh);
void OnMessage(const uint8_t* msg, int len);
void OnPadMount(bool connect);
void OnPadButton(int b, bool down);
void OnPadAxis(int a, int16_t pos);
// update physics with accumulated input then render state to output
void Render(uint64_t _stamp, AnsiCell* ptr, int width, int height);
void ScreenToCell(int p[2]) const;
void MenuKeyb(GAME_KEYB keyb, int key);
void MenuMouse(GAME_MOUSE mouse, int x, int y);
void MenuTouch(GAME_TOUCH touch, int id, int x, int y);
void MenuPadMount(bool connected);
void MenuPadButton(int b, bool down);
void MenuPadAxis(int a, int16_t pos);
void OpenMenu(int method);
void CloseMenu();
void ToggleMenu(int method);
void PaintMenu(AnsiCell* ptr, int width, int height);
int HitMenu(int hx, int hy);
// menu context
int menu_stack[4]; // menu_stack[menu_depth] contains current item (hilight)
int menu_depth; // -1 when closed, 0 just after OpenMenu
// menu mouse / touch state
int menu_down; // 0: released, 1:mouse_captured, 2:touch_captured
int menu_down_x;
int menu_down_y;
// when mouse/touch is taking over, store current hilight here
// so we can revert hilight when pad/keyb is back
int menu_temp;
};
Game* CreateGame();
void DeleteGame(Game* g);
void WriteConf(Game* g);
void ReadConf(Game* g);
void InitGame(Game* g, int water, float pos[3], float yaw, float dir, float lt[4], uint64_t stamp);
void FreeGame(Game* g);
void LoadSprites();
void FreeSprites();
void PaintTerrain(float* xy, float r, int matid);
void BloodLeak(Character* c, int steps);
void GamePadMount(const char* name, int axes, int buttons, const uint8_t map[]);
void GamePadUnmount();
void GamePadButton(int b, int16_t pos);
void GamePadAxis(int a, int16_t pos);
extern uint64_t (*MakeStamp)();