/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2008 Dan Everton (safetydan) * Copyright (C) 2009 Maurus Cuelenaere * * 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 2 * of the License, or (at your option) any later version. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #define lrocklib_c #define LUA_LIB #include "lua.h" #include "lauxlib.h" #include "rocklib.h" /* * http://www.lua.org/manual/5.1/manual.html#lua_CFunction * * In order to communicate properly with Lua, a C function must use the following protocol, * which defines the way parameters and results are passed: a C function receives its arguments * from Lua in its stack in direct order (the first argument is pushed first). To return values to Lua, * a C function just pushes them onto the stack, in direct order (the first result is pushed first), * and returns the number of results. Any other value in the stack below the results will be properly * discarded by Lua. Like a Lua function, a C function called by Lua can also return many results. */ /* * ----------------------------- * * Rockbox Lua image wrapper * * ----------------------------- */ #define ROCKLUA_IMAGE "rb.image" struct rocklua_image { int width; int height; fb_data *data; fb_data dummy[1][1]; }; static void rli_wrap(lua_State *L, fb_data *src, int width, int height) { struct rocklua_image *a = (struct rocklua_image *)lua_newuserdata(L, sizeof(struct rocklua_image)); luaL_getmetatable(L, ROCKLUA_IMAGE); lua_setmetatable(L, -2); a->width = width; a->height = height; a->data = src; } static fb_data* rli_alloc(lua_State *L, int width, int height) { size_t nbytes = sizeof(struct rocklua_image) + ((width*height) - 1) * sizeof(fb_data); struct rocklua_image *a = (struct rocklua_image *)lua_newuserdata(L, nbytes); luaL_getmetatable(L, ROCKLUA_IMAGE); lua_setmetatable(L, -2); a->width = width; a->height = height; a->data = &a->dummy[0][0]; return a->data; } static int rli_new(lua_State *L) { int width = luaL_checkint(L, 1); int height = luaL_checkint(L, 2); rli_alloc(L, width, height); return 1; } static struct rocklua_image* rli_checktype(lua_State *L, int arg) { void *ud = luaL_checkudata(L, arg, ROCKLUA_IMAGE); luaL_argcheck(L, ud != NULL, arg, "'" ROCKLUA_IMAGE "' expected"); return (struct rocklua_image*) ud; } static int rli_width(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); lua_pushnumber(L, a->width); return 1; } static int rli_height(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); lua_pushnumber(L, a->height); return 1; } static fb_data* rli_element(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); luaL_argcheck(L, 1 <= x && x <= a->width, 2, "index out of range"); luaL_argcheck(L, 1 <= y && y <= a->height, 3, "index out of range"); /* return element address */ return &a->data[a->width * (y - 1) + (x - 1)]; } static int rli_set(lua_State *L) { fb_data newvalue = (fb_data) luaL_checknumber(L, 4); *rli_element(L) = newvalue; return 0; } static int rli_get(lua_State *L) { lua_pushnumber(L, *rli_element(L)); return 1; } static int rli_tostring(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); lua_pushfstring(L, ROCKLUA_IMAGE ": %dx%d", a->width, a->height); return 1; } static const struct luaL_reg rli_lib [] = { {"__tostring", rli_tostring}, {"set", rli_set}, {"get", rli_get}, {"width", rli_width}, {"height", rli_height}, {NULL, NULL} }; static inline void rli_init(lua_State *L) { luaL_newmetatable(L, ROCKLUA_IMAGE); lua_pushstring(L, "__index"); lua_pushvalue(L, -2); /* pushes the metatable */ lua_settable(L, -3); /* metatable.__index = metatable */ luaL_register(L, NULL, rli_lib); } /* * ----------------------------- * * Rockbox wrappers start here! * * ----------------------------- */ #define RB_WRAP(M) static int rock_##M(lua_State *L) /* Helper function for opt_viewport */ static void check_tablevalue(lua_State *L, const char* key, int tablepos, void* res, bool unsigned_val) { lua_getfield(L, tablepos, key); /* Find table[key] */ if(!lua_isnoneornil(L, -1)) { if(unsigned_val) *(unsigned*)res = luaL_checkint(L, -1); else *(int*)res = luaL_checkint(L, -1); } lua_pop(L, 1); /* Pop the value off the stack */ } static struct viewport* opt_viewport(lua_State *L, int narg, struct viewport* alt) { if(lua_isnoneornil(L, narg)) return alt; int tablepos = lua_gettop(L); struct viewport *vp; lua_getfield(L, tablepos, "vp"); /* get table['vp'] */ if(lua_isnoneornil(L, -1)) { lua_pop(L, 1); /* Pop nil off stack */ vp = (struct viewport*) lua_newuserdata(L, sizeof(struct viewport)); /* Allocate memory and push it as udata on the stack */ memset(vp, 0, sizeof(struct viewport)); /* Init viewport values to 0 */ lua_setfield(L, tablepos, "vp"); /* table['vp'] = vp (pops value off the stack) */ } else { vp = (struct viewport*) lua_touserdata(L, -1); /* Reuse viewport struct */ lua_pop(L, 1); /* We don't need the value on stack */ } luaL_checktype(L, narg, LUA_TTABLE); check_tablevalue(L, "x", tablepos, &vp->x, false); check_tablevalue(L, "y", tablepos, &vp->y, false); check_tablevalue(L, "width", tablepos, &vp->width, false); check_tablevalue(L, "height", tablepos, &vp->height, false); #ifdef HAVE_LCD_BITMAP check_tablevalue(L, "font", tablepos, &vp->font, false); check_tablevalue(L, "drawmode", tablepos, &vp->drawmode, false); #endif #if LCD_DEPTH > 1 check_tablevalue(L, "fg_pattern", tablepos, &vp->fg_pattern, true); check_tablevalue(L, "bg_pattern", tablepos, &vp->bg_pattern, true); #ifdef HAVE_LCD_COLOR check_tablevalue(L, "lss_pattern", tablepos, &vp->lss_pattern, true); check_tablevalue(L, "lse_pattern", tablepos, &vp->lse_pattern, true); check_tablevalue(L, "lst_pattern", tablepos, &vp->lst_pattern, true); #endif #endif return vp; } RB_WRAP(set_viewport) { struct viewport *vp = opt_viewport(L, 1, NULL); int screen = luaL_optint(L, 2, SCREEN_MAIN); rb->screens[screen]->set_viewport(vp); return 0; } RB_WRAP(clear_viewport) { int screen = luaL_optint(L, 1, SCREEN_MAIN); rb->screens[screen]->clear_viewport(); return 0; } #ifdef HAVE_LCD_BITMAP RB_WRAP(lcd_framebuffer) { rli_wrap(L, rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT); return 1; } RB_WRAP(lcd_mono_bitmap_part) { struct rocklua_image *src = rli_checktype(L, 1); int src_x = luaL_checkint(L, 2); int src_y = luaL_checkint(L, 3); int stride = luaL_checkint(L, 4); int x = luaL_checkint(L, 5); int y = luaL_checkint(L, 6); int width = luaL_checkint(L, 7); int height = luaL_checkint(L, 8); int screen = luaL_optint(L, 9, SCREEN_MAIN); rb->screens[screen]->mono_bitmap_part((const unsigned char *)src->data, src_x, src_y, stride, x, y, width, height); return 0; } RB_WRAP(lcd_mono_bitmap) { struct rocklua_image *src = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int width = luaL_checkint(L, 4); int height = luaL_checkint(L, 5); int screen = luaL_optint(L, 6, SCREEN_MAIN); rb->screens[screen]->mono_bitmap((const unsigned char *)src->data, x, y, width, height); return 0; } #if LCD_DEPTH > 1 RB_WRAP(lcd_bitmap_part) { struct rocklua_image *src = rli_checktype(L, 1); int src_x = luaL_checkint(L, 2); int src_y = luaL_checkint(L, 3); int stride = luaL_checkint(L, 4); int x = luaL_checkint(L, 5); int y = luaL_checkint(L, 6); int width = luaL_checkint(L, 7); int height = luaL_checkint(L, 8); int screen = luaL_optint(L, 9, SCREEN_MAIN); rb->screens[screen]->bitmap_part(src->data, src_x, src_y, stride, x, y, width, height); return 0; } RB_WRAP(lcd_bitmap) { struct rocklua_image *src = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int width = luaL_checkint(L, 4); int height = luaL_checkint(L, 5); int screen = luaL_optint(L, 6, SCREEN_MAIN); rb->screens[screen]->bitmap(src->data, x, y, width, height); return 0; } RB_WRAP(lcd_get_backdrop) { fb_data* backdrop = rb->lcd_get_backdrop(); if(backdrop == NULL) return 0; else { rli_wrap(L, backdrop, LCD_WIDTH, LCD_HEIGHT); return 1; } } #endif /* LCD_DEPTH > 1 */ #if LCD_DEPTH == 16 RB_WRAP(lcd_bitmap_transparent_part) { struct rocklua_image *src = rli_checktype(L, 1); int src_x = luaL_checkint(L, 2); int src_y = luaL_checkint(L, 3); int stride = luaL_checkint(L, 4); int x = luaL_checkint(L, 5); int y = luaL_checkint(L, 6); int width = luaL_checkint(L, 7); int height = luaL_checkint(L, 8); int screen = luaL_optint(L, 9, SCREEN_MAIN); rb->screens[screen]->transparent_bitmap_part(src->data, src_x, src_y, stride, x, y, width, height); return 0; } RB_WRAP(lcd_bitmap_transparent) { struct rocklua_image *src = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int width = luaL_checkint(L, 4); int height = luaL_checkint(L, 5); int screen = luaL_optint(L, 6, SCREEN_MAIN); rb->screens[screen]->transparent_bitmap(src->data, x, y, width, height); return 0; } #endif /* LCD_DEPTH == 16 */ #endif /* defined(LCD_BITMAP) */ RB_WRAP(current_tick) { lua_pushinteger(L, *rb->current_tick); return 1; } #ifdef HAVE_TOUCHSCREEN RB_WRAP(action_get_touchscreen_press) { short x, y; int result = rb->action_get_touchscreen_press(&x, &y); lua_pushinteger(L, result); lua_pushinteger(L, x); lua_pushinteger(L, y); return 3; } #endif RB_WRAP(kbd_input) { luaL_Buffer b; luaL_buffinit(L, &b); char *buffer = luaL_prepbuffer(&b); buffer[0] = '\0'; rb->kbd_input(buffer, LUAL_BUFFERSIZE); luaL_addsize(&b, strlen(buffer)); luaL_pushresult(&b); return 1; } #ifdef HAVE_TOUCHSCREEN RB_WRAP(touchscreen_set_mode) { enum touchscreen_mode mode = luaL_checkint(L, 1); rb->touchscreen_set_mode(mode); return 0; } #endif RB_WRAP(font_getstringsize) { const unsigned char* str = luaL_checkstring(L, 1); int fontnumber = luaL_checkint(L, 2); int w, h; int result = rb->font_getstringsize(str, &w, &h, fontnumber); lua_pushinteger(L, result); lua_pushinteger(L, w); lua_pushinteger(L, h); return 3; } #ifdef HAVE_LCD_COLOR RB_WRAP(lcd_rgbpack) { int r = luaL_checkint(L, 1); int g = luaL_checkint(L, 2); int b = luaL_checkint(L, 3); int result = LCD_RGBPACK(r, g, b); lua_pushinteger(L, result); return 1; } RB_WRAP(lcd_rgbunpack) { int rgb = luaL_checkint(L, 1); lua_pushinteger(L, RGB_UNPACK_RED(rgb)); lua_pushinteger(L, RGB_UNPACK_GREEN(rgb)); lua_pushinteger(L, RGB_UNPACK_BLUE(rgb)); return 3; } #endif RB_WRAP(read_bmp_file) { struct bitmap bm; const char* filename = luaL_checkstring(L, 1); bool dither = luaL_optboolean(L, 2, true); bool transparent = luaL_optboolean(L, 3, false); int format = FORMAT_NATIVE; if(dither) format |= FORMAT_DITHER; if(transparent) format |= FORMAT_TRANSPARENT; int result = rb->read_bmp_file(filename, &bm, 0, format | FORMAT_RETURN_SIZE, NULL); if(result > 0) { bm.data = (unsigned char*) rli_alloc(L, bm.width, bm.height); rb->read_bmp_file(filename, &bm, result, format, NULL); return 1; } return 0; } RB_WRAP(current_path) { const char *current_path = get_current_path(L, 1); if(current_path != NULL) { lua_pushstring(L, current_path); return 1; } else return 0; } static void fill_text_message(lua_State *L, struct text_message * message, int pos) { int i; luaL_checktype(L, pos, LUA_TTABLE); int n = luaL_getn(L, pos); const char **lines = (const char**) dlmalloc(n * sizeof(const char*)); for(i=1; i<=n; i++) { lua_rawgeti(L, pos, i); lines[i-1] = luaL_checkstring(L, -1); lua_pop(L, 1); } message->message_lines = lines; message->nb_lines = n; } RB_WRAP(gui_syncyesno_run) { struct text_message main_message, yes_message, no_message; struct text_message *yes = NULL, *no = NULL; fill_text_message(L, &main_message, 1); if(!lua_isnoneornil(L, 2)) fill_text_message(L, (yes = &yes_message), 2); if(!lua_isnoneornil(L, 3)) fill_text_message(L, (no = &no_message), 3); enum yesno_res result = rb->gui_syncyesno_run(&main_message, yes, no); dlfree(main_message.message_lines); if(yes) dlfree(yes_message.message_lines); if(no) dlfree(no_message.message_lines); lua_pushinteger(L, result); return 1; } #define R(NAME) {#NAME, rock_##NAME} static const luaL_Reg rocklib[] = { /* Graphics */ #ifdef HAVE_LCD_BITMAP R(lcd_framebuffer), R(lcd_mono_bitmap_part), R(lcd_mono_bitmap), #if LCD_DEPTH > 1 R(lcd_get_backdrop), R(lcd_bitmap_part), R(lcd_bitmap), #endif #if LCD_DEPTH == 16 R(lcd_bitmap_transparent_part), R(lcd_bitmap_transparent), #endif #endif #ifdef HAVE_LCD_COLOR R(lcd_rgbpack), R(lcd_rgbunpack), #endif /* Kernel */ R(current_tick), /* Buttons */ #ifdef HAVE_TOUCHSCREEN R(action_get_touchscreen_press), R(touchscreen_set_mode), #endif R(kbd_input), R(font_getstringsize), R(read_bmp_file), R(set_viewport), R(clear_viewport), R(current_path), R(gui_syncyesno_run), {"new_image", rli_new}, {NULL, NULL} }; #undef R extern const luaL_Reg rocklib_aux[]; #define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x); /* ** Open Rockbox library */ LUALIB_API int luaopen_rock(lua_State *L) { luaL_register(L, LUA_ROCKLIBNAME, rocklib); luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); RB_CONSTANT(HZ); RB_CONSTANT(LCD_WIDTH); RB_CONSTANT(LCD_HEIGHT); RB_CONSTANT(FONT_SYSFIXED); RB_CONSTANT(FONT_UI); #ifdef HAVE_TOUCHSCREEN RB_CONSTANT(TOUCHSCREEN_POINT); RB_CONSTANT(TOUCHSCREEN_BUTTON); #endif RB_CONSTANT(SCREEN_MAIN); #ifdef HAVE_REMOTE_LCD RB_CONSTANT(SCREEN_REMOTE); #endif rli_init(L); return 1; } id='n371' href='#n371'>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 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2000 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
*
* 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 2
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* DESCRIPTION: none
* The original Doom description was none, basically because this file
* has everything. This ties up the game logic, linking the menu and
* input code to the underlying game by creating & respawning players,
* building game tics, calling the underlying thing logic.
*
*-----------------------------------------------------------------------------
*/
#include "doomdef.h"
#include "doomstat.h"
#include "z_zone.h"
#include "f_finale.h"
#include "m_argv.h"
#include "m_misc.h"
#include "m_menu.h"
#include "m_random.h"
#include "i_system.h"
#include "p_map.h"
#include "p_setup.h"
#include "p_saveg.h"
#include "p_tick.h"
#include "d_main.h"
#include "wi_stuff.h"
#include "hu_stuff.h"
#include "st_stuff.h"
#include "am_map.h"
// Needs access to LFB.
#include "v_video.h"
#include "w_wad.h"
#include "r_main.h"
#include "s_sound.h"
// Data.
#include "dstrings.h"
#include "sounds.h"
// SKY handling - still the wrong place.
#include "r_data.h"
#include "r_sky.h"
#include "p_inter.h"
#include "g_game.h"
#include "rockmacros.h"
#define SAVEGAMESIZE 0x20000
#define SAVESTRINGSIZE 24
static size_t savegamesize = SAVEGAMESIZE; // killough
static boolean netdemo;
static const byte *demobuffer; /* cph - only used for playback */
static int demofd; /* cph - record straight to file */
static const byte *demo_p;
static short consistancy[MAXPLAYERS][BACKUPTICS];
gameaction_t gameaction;
gamestate_t gamestate;
skill_t gameskill;
boolean respawnmonsters;
int gameepisode;
int gamemap;
boolean paused;
// CPhipps - moved *_loadgame vars here
static boolean forced_loadgame = false;
static boolean command_loadgame = false;
boolean usergame; // ok to save / end game
boolean timingdemo; // if true, exit with report on completion
boolean fastdemo; // if true, run at full speed -- killough
boolean nodrawers; // for comparative timing purposes
boolean noblit; // for comparative timing purposes
int starttime; // for comparative timing purposes
boolean deathmatch; // only if started as net death
boolean netgame; // only true if packets are broadcast
boolean playeringame[MAXPLAYERS];
player_t players[MAXPLAYERS];
int consoleplayer; // player taking events and displaying
int displayplayer; // view being displayed
int gametic;
int levelstarttic; // gametic at level start
extern int basetic; /* killough 9/29/98: for demo sync */
int totalkills, totallive, totalitems, totalsecret; // for intermission
boolean demorecording;
boolean demoplayback;
boolean singledemo; // quit after playing a demo from cmdline
wbstartstruct_t wminfo; // parms for world map / intermission
boolean haswolflevels = false;// jff 4/18/98 wolf levels present
static byte *savebuffer; // CPhipps - static
int autorun = false; // always running? // phares
int totalleveltimes; // CPhipps - total time for all completed levels
int longtics;
//
// controls (have defaults)
//
int key_right;
int key_left;
int key_up;
int key_down;
int key_menu_right; // phares 3/7/98
int key_menu_left; // |
int key_menu_up; // V
int key_menu_down;
int key_menu_backspace; // ^
int key_menu_escape; // |
int key_menu_enter; // phares 3/7/98
int key_strafeleft;
int key_straferight;
int key_fire;
int key_use;
int key_strafe;
int key_speed;
int key_escape = KEY_ESCAPE; // phares 4/13/98
int key_weapon;
int key_savegame; // phares
int key_loadgame; // |
int key_autorun; // V
int key_reverse;
int key_zoomin;
int key_zoomout;
int key_reverse;
int key_chat;
int key_backspace;
int key_enter;
int key_map_right;
int key_map_left;
int key_map_up;
int key_map_down;
int key_map_zoomin;
int key_map_zoomout;
int key_map;
int key_map_gobig;
int key_map_follow;
int key_map_mark;
int key_map_clear;
int key_map_grid;
int key_map_overlay; // cph - map overlay
int key_map_rotate; // cph - map rotation
int key_help = KEY_F1; // phares 4/13/98
int key_soundvolume;
int key_hud;
int key_quicksave;
int key_endgame;
int key_messages;
int key_quickload;
int key_quit;
int key_gamma;
int key_spy;
int key_pause;
int key_setup;
int destination_keys[MAXPLAYERS];
int key_weapontoggle;
int key_weapon1;
int key_weapon2;
int key_weapon3;
int key_weapon4;
int key_weapon5;
int key_weapon6;
int key_weapon7; // ^
int key_weapon8; // |
int key_weapon9; // phares
int key_screenshot; // killough 2/22/98: screenshot key
int mousebfire;
int mousebstrafe;
int mousebforward;
int joybfire;
int joybstrafe;
int joybuse;
int joybspeed;
#define MAXPLMOVE (forwardmove[1])
#define TURBOTHRESHOLD 0x32
#define SLOWTURNTICS 6
#define QUICKREVERSE (short)32768 // 180 degree reverse // phares
#define NUMKEYS 512
fixed_t forwardmove[2] = {0x19, 0x32};
fixed_t sidemove[2] = {0x18, 0x28};
fixed_t angleturn[3] = {640, 1280, 320}; // + slow turn
// CPhipps - made lots of key/button state vars static
static boolean gamekeydown[NUMKEYS];
static int turnheld; // for accelerative turning
static boolean mousearray[4];
static boolean *mousebuttons = &mousearray[1]; // allow [-1]
// mouse values are used once
static int mousex;
static int mousey;
static unsigned int dclicktime;
static unsigned int dclickstate;
static unsigned int dclicks;
static unsigned int dclicktime2;
static unsigned int dclickstate2;
static unsigned int dclicks2;
// joystick values are repeated
static int joyxmove;
static int joyymove;
static boolean joyarray[5];
static boolean *joybuttons = &joyarray[1]; // allow [-1]
// Game events info
static buttoncode_t special_event; // Event triggered by local player, to send
static byte savegameslot; // Slot to load if gameaction == ga_loadgame
char savedescription[SAVEDESCLEN]; // Description to save in savegame if gameaction == ga_savegame
//jff 3/24/98 declare startskill external, define defaultskill here
extern skill_t startskill; //note 0-based
int defaultskill; //note 1-based
// killough 2/8/98: make corpse queue variable in size
int bodyqueslot, bodyquesize; // killough 2/8/98
mobj_t **bodyque = 0; // phares 8/10/98
void* statcopy; // for statistics driver
static void G_DoSaveGame (boolean menu);
static const byte* G_ReadDemoHeader(const byte* demo_p);
//
// G_BuildTiccmd
// Builds a ticcmd from all of the available inputs
// or reads it from the demo buffer.
// If recording a demo, write it out
//
static inline signed char fudgef(signed char b)
{
static int c;
if (!b || !demo_compatibility || longtics) return b;
if (++c & 0x1f) return b;
b |= 1; if (b>2) b-=2;
return b;
}
static inline signed short fudgea(signed short b)
{
if (!b || !demo_compatibility || !longtics) return b;
b |= 1; if (b>2) b-=2;
return b;
}
void G_BuildTiccmd(ticcmd_t* cmd)
{
boolean strafe;
boolean bstrafe;
int speed;
int tspeed;
int forward;
int side;
int newweapon=0; // phares
/* cphipps - remove needless I_BaseTiccmd call, just set the ticcmd to zero */
memset(cmd,0,sizeof*cmd);
cmd->consistancy = consistancy[consoleplayer][maketic%BACKUPTICS];
strafe = gamekeydown[key_strafe] || mousebuttons[mousebstrafe]
|| joybuttons[joybstrafe];
speed = autorun || gamekeydown[key_speed] || joybuttons[joybspeed]; // phares
forward = side = 0;
// use two stage accelerative turning
// on the keyboard and joystick
if (joyxmove < 0 || joyxmove > 0 ||
gamekeydown[key_right] || gamekeydown[key_left])
turnheld += ticdup;
else
turnheld = 0;
if (turnheld < SLOWTURNTICS)
tspeed = 2; // slow turn
else
tspeed = speed;
// turn 180 degrees in one keystroke? // phares
// |
if (gamekeydown[key_reverse]) // V
{
cmd->angleturn += QUICKREVERSE; // ^
gamekeydown[key_reverse] = false; // |
} // phares
// let movement keys cancel each other out
if (strafe)
{
if (gamekeydown[key_right])
side += sidemove[speed];
if (gamekeydown[key_left])
side -= sidemove[speed];
if (joyxmove > 0)
side += sidemove[speed];
if (joyxmove < 0)
side -= sidemove[speed];
}
else
{
if (gamekeydown[key_right])
cmd->angleturn -= angleturn[tspeed];
if (gamekeydown[key_left])
cmd->angleturn += angleturn[tspeed];
if (joyxmove > 0)
cmd->angleturn -= angleturn[tspeed];
if (joyxmove < 0)
cmd->angleturn += angleturn[tspeed];
}
if (gamekeydown[key_up])
forward += forwardmove[speed];
if (gamekeydown[key_down])
forward -= forwardmove[speed];
if (joyymove < 0)
forward += forwardmove[speed];
if (joyymove > 0)
forward -= forwardmove[speed];
if (gamekeydown[key_straferight])
side += sidemove[speed];
if (gamekeydown[key_strafeleft])
side -= sidemove[speed];
// buttons
cmd->chatchar = HU_dequeueChatChar();
if (gamekeydown[key_fire] || mousebuttons[mousebfire] ||
joybuttons[joybfire])
cmd->buttons |= BT_ATTACK;
if (gamekeydown[key_use] || joybuttons[joybuse])
{
cmd->buttons |= BT_USE;
// clear double clicks if hit use button
dclicks = 0;
}
// Toggle between the top 2 favorite weapons. // phares
// If not currently aiming one of these, switch to // phares
// the favorite. Only switch if you possess the weapon. // phares
// killough 3/22/98:
//
// Perform automatic weapons switch here rather than in p_pspr.c,
// except in demo_compatibility mode.
//
// killough 3/26/98, 4/2/98: fix autoswitch when no weapons are left
if ((!demo_compatibility && players[consoleplayer].attackdown && // killough
!P_CheckAmmo(&players[consoleplayer])) || gamekeydown[key_weapontoggle])
newweapon = P_SwitchWeapon(&players[consoleplayer]); // phares
else
{ // phares 02/26/98: Added gamemode checks
if(gamekeydown[key_weapon])
{
volatile unsigned int wpcheck; // I don't know why this is needed, but it is
for(wpcheck=0; wpcheck<9; wpcheck++)
if(players[consoleplayer].weaponowned[wpcheck] && wpcheck>players[consoleplayer].readyweapon )
{
newweapon=wpcheck;
break;
}
if(players[consoleplayer].weaponowned[wp_chainsaw]&&newweapon==0)
newweapon=1;
}
else
{
newweapon =
gamekeydown[key_weapon1] ? wp_fist : // killough 5/2/98: reformatted
gamekeydown[key_weapon2] ? wp_pistol :
gamekeydown[key_weapon3] ? wp_shotgun :
gamekeydown[key_weapon4] ? wp_chaingun :
gamekeydown[key_weapon5] ? wp_missile :
gamekeydown[key_weapon6] && gamemode != shareware ? wp_plasma :
gamekeydown[key_weapon7] && gamemode != shareware ? wp_bfg :
gamekeydown[key_weapon8] ? wp_chainsaw :
gamekeydown[key_weapon9] && gamemode == commercial ? wp_supershotgun :
wp_nochange;
}
// killough 3/22/98: For network and demo consistency with the
// new weapons preferences, we must do the weapons switches here
// instead of in p_user.c. But for old demos we must do it in
// p_user.c according to the old rules. Therefore demo_compatibility
// determines where the weapons switch is made.
// killough 2/8/98:
// Allow user to switch to fist even if they have chainsaw.
// Switch to fist or chainsaw based on preferences.
// Switch to shotgun or SSG based on preferences.
if (!demo_compatibility)
{
const player_t *player = &players[consoleplayer];
// only select chainsaw from '1' if it's owned, it's
// not already in use, and the player prefers it or
// the fist is already in use, or the player does not
// have the berserker strength.
if (newweapon==wp_fist && player->weaponowned[wp_chainsaw] &&
player->readyweapon!=wp_chainsaw &&
(player->readyweapon==wp_fist ||
!player->powers[pw_strength] ||
P_WeaponPreferred(wp_chainsaw, wp_fist)))
newweapon = wp_chainsaw;
// Select SSG from '3' only if it's owned and the player
// does not have a shotgun, or if the shotgun is already
// in use, or if the SSG is not already in use and the
// player prefers it.
if(!gamekeydown[key_weapon])
if (newweapon == wp_shotgun && gamemode == commercial &&
player->weaponowned[wp_supershotgun] &&
(!player->weaponowned[wp_shotgun] ||
player->readyweapon == wp_shotgun ||
(player->readyweapon != wp_supershotgun &&
P_WeaponPreferred(wp_supershotgun, wp_shotgun))))
newweapon = wp_supershotgun;
}
// killough 2/8/98, 3/22/98 -- end of weapon selection changes
}
if(newweapon >wp_nochange) // something is messed up with the weapon switching code above allowing it to give values greater
{ // then wp_nochange which really screws the game up
newweapon=0;
}
if (newweapon != wp_nochange)
{
cmd->buttons |= BT_CHANGE;
cmd->buttons |= newweapon<<BT_WEAPONSHIFT;
}
// mouse
if (mousebuttons[mousebforward])
forward += forwardmove[speed];
// forward double click
if (mousebuttons[mousebforward] != dclickstate && dclicktime > 1 )
{
dclickstate = mousebuttons[mousebforward];
if (dclickstate)
dclicks++;
if (dclicks == 2)
{
cmd->buttons |= BT_USE;
dclicks = 0;
}
else
dclicktime = 0;
}
else
if ((dclicktime += ticdup) > 20)
{
dclicks = 0;
dclickstate = 0;
}
// strafe double click
bstrafe = mousebuttons[mousebstrafe] || joybuttons[joybstrafe];
if (bstrafe != dclickstate2 && dclicktime2 > 1 )
{
dclickstate2 = bstrafe;
if (dclickstate2)
dclicks2++;
if (dclicks2 == 2)
{
cmd->buttons |= BT_USE;
dclicks2 = 0;
}
else
dclicktime2 = 0;
}
else
if ((dclicktime2 += ticdup) > 20)
{
dclicks2 = 0;
dclickstate2 = 0;
}
forward += mousey;
if (strafe)
side += mousex / 4; /* mead Don't want to strafe as fast as turns.*/
else
cmd->angleturn -= mousex; /* mead now have enough dynamic range 2-10-00 */
mousex = mousey = 0;
if (forward > MAXPLMOVE)
forward = MAXPLMOVE;
else if (forward < -MAXPLMOVE)
forward = -MAXPLMOVE;
if (side > MAXPLMOVE)
side = MAXPLMOVE;
else if (side < -MAXPLMOVE)
side = -MAXPLMOVE;
cmd->forwardmove += fudgef(forward);
cmd->sidemove += side;
cmd->angleturn = fudgea(cmd->angleturn);
// CPhipps - special events (game new/load/save/pause)
if (special_event & BT_SPECIAL) {
cmd->buttons = special_event;
special_event = 0;
}
}
//
// G_RestartLevel
//
void G_RestartLevel(void)
{
special_event = BT_SPECIAL | (BTS_RESTARTLEVEL & BT_SPECIALMASK);
}
#include "z_bmalloc.h"
//
// G_DoLoadLevel
//
extern gamestate_t wipegamestate;
static void G_DoLoadLevel (void)
{
int i;
// Set the sky map.
// First thing, we have a dummy sky texture name,
// a flat. The data is in the WAD only because
// we look for an actual index, instead of simply
// setting one.
skyflatnum = R_FlatNumForName ( SKYFLATNAME );
// DOOM determines the sky texture to be used
// depending on the current episode, and the game version.
if (gamemode == commercial)
// || gamemode == pack_tnt //jff 3/27/98 sorry guys pack_tnt,pack_plut
// || gamemode == pack_plut) //aren't gamemodes, this was matching retail
{
skytexture = R_TextureNumForName ("SKY3");
if (gamemap < 12)
skytexture = R_TextureNumForName ("SKY1");
else
if (gamemap < 21)
skytexture = R_TextureNumForName ("SKY2");
}
else //jff 3/27/98 and lets not forget about DOOM and Ultimate DOOM huh?
switch (gameepisode)
{
case 1:
skytexture = R_TextureNumForName ("SKY1");
break;
case 2:
skytexture = R_TextureNumForName ("SKY2");
break;
case 3:
skytexture = R_TextureNumForName ("SKY3");
break;
case 4: // Special Edition sky
skytexture = R_TextureNumForName ("SKY4");
break;
}//jff 3/27/98 end sky setting fix
levelstarttic = gametic; // for time calculation
if (!demo_compatibility && !mbf_features) // killough 9/29/98
basetic = gametic;
if (wipegamestate == GS_LEVEL)
wipegamestate = -1; // force a wipe
gamestate = GS_LEVEL;
for (i=0 ; i<MAXPLAYERS ; i++)
{
if (playeringame[i] && players[i].playerstate == PST_DEAD)
players[i].playerstate = PST_REBORN;
memset (players[i].frags,0,sizeof(players[i].frags));
}
// initialize the msecnode_t freelist. phares 3/25/98
// any nodes in the freelist are gone by now, cleared
// by Z_FreeTags() when the previous level ended or player
// died.
{
DECLARE_BLOCK_MEMORY_ALLOC_ZONE(secnodezone);
NULL_BLOCK_MEMORY_ALLOC_ZONE(secnodezone);
//extern msecnode_t *headsecnode; // phares 3/25/98
//headsecnode = NULL;
}
P_SetupLevel (gameepisode, gamemap, 0, gameskill);
displayplayer = consoleplayer; // view the guy you are playing
gameaction = ga_nothing;
Z_CheckHeap ();
// clear cmd building stuff
memset (gamekeydown, 0, sizeof(gamekeydown));
joyxmove = joyymove = 0;
mousex = mousey = 0;
special_event = 0; paused = false;
memset (mousebuttons, 0, sizeof(mousebuttons));
memset (joybuttons, 0, sizeof(joybuttons));
// killough 5/13/98: in case netdemo has consoleplayer other than green
ST_Start();
HU_Start();
// killough: make -timedemo work on multilevel demos
// Move to end of function to minimize noise -- killough 2/22/98:
if (timingdemo)
{
static int first=1;
if (first)
{
starttime = I_GetTime ();
first=0;
}
}
}
//
// G_Responder
// Get info needed to make ticcmd_ts for the players.
//
boolean G_Responder (event_t* ev)
{
// allow spy mode changes even during the demo
// killough 2/22/98: even during DM demo
//
// killough 11/98: don't autorepeat spy mode switch
#if 0
if (ev->data1 == key_spy && netgame && (demoplayback || !deathmatch) &&
gamestate == GS_LEVEL)
{
if (ev->type == ev_keyup)
gamekeydown[key_spy] = false;
if (ev->type == ev_keydown && !gamekeydown[key_spy])
{
gamekeydown[key_spy] = true;
do // spy mode
if (++displayplayer >= MAXPLAYERS)
displayplayer = 0;
while (!playeringame[displayplayer] && displayplayer!=consoleplayer);
ST_Start(); // killough 3/7/98: switch status bar views too
HU_Start();
S_UpdateSounds(players[displayplayer].mo);
}
return true;
}
#endif
// any other key pops up menu if in demos
//
// killough 8/2/98: enable automap in -timedemo demos
//
// killough 9/29/98: make any key pop up menu regardless of
// which kind of demo, and allow other events during playback
if (gameaction == ga_nothing && (demoplayback || gamestate == GS_DEMOSCREEN))
{
// killough 9/29/98: allow user to pause demos during playback
if (ev->type == ev_keydown && ev->data1 == key_pause)
{
if (paused ^= 2)
S_PauseSound();
else
S_ResumeSound();
return true;
}
// killough 10/98:
// Don't pop up menu, if paused in middle
// of demo playback, or if automap active.
// Don't suck up keys, which may be cheats
return gamestate == GS_DEMOSCREEN &&
!(paused & 2) && !(automapmode & am_active) &&
((ev->type == ev_keydown) ||
(ev->type == ev_mouse && ev->data1) ||
(ev->type == ev_joystick && ev->data1)) ?
M_StartControlPanel(), true : false;
}
if (gamestate == GS_FINALE && F_Responder(ev))
return true; // finale ate the event
switch (ev->type)
{
case ev_keydown:
if (ev->data1 == key_pause) // phares
{
special_event = BT_SPECIAL | (BTS_PAUSE & BT_SPECIALMASK);
return true;
}
if (ev->data1 <NUMKEYS)
gamekeydown[ev->data1] = true;
return true; // eat key down events
case ev_keyup:
if (ev->data1 <NUMKEYS)
gamekeydown[ev->data1] = false;
return false; // always let key up events filter down
case ev_mouse:
mousebuttons[0] = ev->data1 & 1;
mousebuttons[1] = ev->data1 & 2;
mousebuttons[2] = ev->data1 & 4;
mousex = ev->data2*(mouseSensitivity+5)/10;
mousey = ev->data3*(mouseSensitivity+5)/10;
return true; // eat events
case ev_joystick:
joybuttons[0] = ev->data1 & 1;
joybuttons[1] = ev->data1 & 2;
joybuttons[2] = ev->data1 & 4;
joybuttons[3] = ev->data1 & 8;
joyxmove = ev->data2;
joyymove = ev->data3;
return true; // eat events
default:
break;
}
return false;
}
//
// G_Ticker
// Make ticcmd_ts for the players.
//
extern int mapcolor_me;
void G_Ticker (void)
{
int i;
static gamestate_t prevgamestate;
P_MapStart();
// do player reborns if needed
for (i=0 ; i<MAXPLAYERS ; i++)
if (playeringame[i] && players[i].playerstate == PST_REBORN)
G_DoReborn (i);
P_MapEnd();
// do things to change the game state
while (gameaction != ga_nothing)
{
switch (gameaction)
{
case ga_loadlevel:
// force players to be initialized on level reload
for (i=0 ; i<MAXPLAYERS ; i++)
players[i].playerstate = PST_REBORN;
G_DoLoadLevel ();
break;
case ga_newgame:
G_DoNewGame ();
break;
case ga_loadgame:
G_DoLoadGame ();
break;
case ga_savegame:
G_DoSaveGame (false);
break;
case ga_playdemo:
G_DoPlayDemo ();
break;
case ga_completed:
G_DoCompleted ();
break;
case ga_victory:
F_StartFinale ();
break;
case ga_worlddone:
G_DoWorldDone ();
break;
case ga_nothing:
break;
}
}
if (paused & 2 || (!demoplayback && menuactive && !netgame))
basetic++; // For revenant tracers and RNG -- we must maintain sync
else
{
// get commands, check consistancy, and build new consistancy check
int buf = (gametic/ticdup)%BACKUPTICS;