this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

FEATURE: Byte Battle (#2602)

http://www.sizecoding.org/wiki/Byte_Battle

authored by

Alice and committed by
GitHub
b1d765ea 5d60c024

+281 -6
+57 -1
src/studio/editors/code.c
··· 109 109 history_add(code->history); 110 110 } 111 111 112 + tic_color getCodeColor(Code* code) 113 + { 114 + Bytebattle* bb = getBytebattle(code->studio); 115 + 116 + if(bb) 117 + { 118 + s32 size = strlen(code->src) - bb->limit.lower; 119 + 120 + if(size <= 0) return tic_color_light_green; 121 + 122 + s32 delta = (bb->limit.current - bb->limit.lower) / 2; 123 + 124 + if(size <= delta) return tic_color_yellow; 125 + if(size <= delta*2) return tic_color_orange; 126 + return tic_color_red; 127 + } 128 + 129 + return tic_color_white; 130 + } 131 + 112 132 static void drawStatus(Code* code) 113 133 { 114 134 enum {Height = TIC_FONT_HEIGHT + 1, StatusY = TIC80_HEIGHT - TIC_FONT_HEIGHT}; 115 135 116 - tic_api_rect(code->tic, 0, TIC80_HEIGHT - Height, TIC80_WIDTH, Height, code->status.color); 136 + Bytebattle* bb = getBytebattle(code->studio); 137 + 138 + if(bb) 139 + { 140 + tic_api_rect(code->tic, 0, TIC80_HEIGHT - Height, TIC80_WIDTH, Height, getCodeColor(code)); 141 + if(!bb->battle.hidetime) 142 + { 143 + sprintf(code->status.size, "%i/%i", (u32)strlen(code->src), bb->limit.current); 144 + 145 + char buf[sizeof "00:00"]; 146 + s32 sec = bb->battle.left / 1000; 147 + sprintf(buf, "%02i:%02i", sec / 60, sec % 60); 148 + 149 + tic_api_print(code->tic, buf, (TIC80_WIDTH - sizeof "00:00" * TIC_FONT_WIDTH) / 2, 150 + StatusY, getConfig(code->studio)->theme.code.BG, true, 1, false); 151 + } 152 + } 153 + else 154 + { 155 + tic_api_rect(code->tic, 0, TIC80_HEIGHT - Height, TIC80_WIDTH, Height, code->status.color); 156 + } 157 + 117 158 tic_api_print(code->tic, code->status.line, 0, StatusY, getConfig(code->studio)->theme.code.BG, true, 1, false); 118 159 tic_api_print(code->tic, code->status.size, TIC80_WIDTH - (s32)strlen(code->status.size) * TIC_FONT_WIDTH, 119 160 StatusY, getConfig(code->studio)->theme.code.BG, true, 1, false); ··· 370 411 371 412 pointer++; 372 413 } 414 + } 415 + 416 + void codeGetPos(Code* code, s32* x, s32* y) 417 + { 418 + getCursorPosition(code, x, y); 419 + } 420 + 421 + static void setCursorPosition(Code* code, s32 x, s32 y); 422 + static void parseSyntaxColor(Code*); 423 + 424 + void codeSetPos(Code* code, s32 x, s32 y) 425 + { 426 + setCursorPosition(code, x, y); 427 + parseSyntaxColor(code); 428 + code->cursor.delay = 0; 373 429 } 374 430 375 431 static s32 getLinesCount(Code* code)
+2
src/studio/editors/code.h
··· 140 140 141 141 void initCode(Code*, Studio* studio); 142 142 void freeCode(Code*); 143 + void codeGetPos(Code*, s32* x, s32* y); 144 + void codeSetPos(Code*, s32 x, s32 y); 143 145 144 146 void trimWhitespace(Code*);
+5 -3
src/studio/screens/console.c
··· 4332 4332 } 4333 4333 else printBack(console, "\n loading cart..."); 4334 4334 } 4335 - 4336 - if (getStudioMode(console->studio) != TIC_CONSOLE_MODE) return; 4337 - 4335 + 4338 4336 tic_api_cls(tic, TIC_COLOR_BG); 4339 4337 drawConsoleText(console); 4340 4338 ··· 4358 4356 { 4359 4357 if(console->cursor.delay) 4360 4358 console->cursor.delay--; 4359 + 4360 + console->tickCounter++; 4361 + 4362 + if (getStudioMode(console->studio) != TIC_CONSOLE_MODE) return; 4361 4363 4362 4364 drawCursor(console); 4363 4365
+176 -2
src/studio/studio.c
··· 221 221 tic_fs* fs; 222 222 s32 samplerate; 223 223 tic_font systemFont; 224 + 225 + Bytebattle bytebattle; 224 226 }; 225 227 226 228 #if defined(BUILD_EDITORS) ··· 1705 1707 } 1706 1708 #endif 1707 1709 1710 + static u32 getTime() 1711 + { 1712 + return tic_sys_counter_get() * 1000 / tic_sys_freq_get(); 1713 + } 1714 + 1715 + static void hideBattleTime(Studio* studio) 1716 + { 1717 + studio->bytebattle.battle.hidetime = !studio->bytebattle.battle.hidetime; 1718 + } 1719 + 1720 + static void startBattle(Studio* studio) 1721 + { 1722 + studio->bytebattle.battle.started = getTime(); 1723 + } 1724 + 1708 1725 #if defined(TIC80_PRO) 1709 1726 1710 1727 static void switchBank(Studio* studio, s32 bank) ··· 1850 1867 } 1851 1868 else if(keyWasPressedOnce(studio, tic_key_f8)) takeScreenshot(studio); 1852 1869 else if(keyWasPressedOnce(studio, tic_key_f9)) startVideoRecord(studio); 1870 + else if(keyWasPressedOnce(studio, tic_key_f10)) hideBattleTime(studio); 1871 + else if(keyWasPressedOnce(studio, tic_key_f12)) startBattle(studio); 1853 1872 else if(studio->mode == TIC_RUN_MODE && keyWasPressedOnce(studio, tic_key_f7)) 1854 1873 setCoverImage(studio); 1855 1874 ··· 2180 2199 tic->ram->input.mouse.scrollx *= -1; 2181 2200 } 2182 2201 2202 + #if defined(BUILD_EDITORS) 2203 + static void doCodeExport(Studio* studio) 2204 + { 2205 + char pos[sizeof studio->bytebattle.last.postag]; 2206 + { 2207 + s32 x = 0, y = 0; 2208 + 2209 + if(studio->mode != TIC_RUN_MODE) 2210 + { 2211 + codeGetPos(studio->code, &x, &y); 2212 + x++; y++; 2213 + } 2214 + 2215 + sprintf(pos, "-- pos: %i,%i\n", x, y); 2216 + } 2217 + 2218 + if(strcmp(studio->bytebattle.last.postag, pos) || strcmp(studio->bytebattle.last.code.data, studio->code->src)) 2219 + { 2220 + FILE* file = fopen(studio->bytebattle.exp, "wb"); 2221 + 2222 + if(file) 2223 + { 2224 + strcpy(studio->bytebattle.last.postag, pos); 2225 + strcpy(studio->bytebattle.last.code.data, studio->code->src); 2226 + 2227 + fwrite(pos, 1, strlen(pos), file); 2228 + fwrite(studio->code->src, 1, strlen(studio->code->src), file); 2229 + fclose(file); 2230 + } 2231 + } 2232 + } 2233 + 2234 + static void doCodeImport(Studio* studio) 2235 + { 2236 + FILE* file = fopen(studio->bytebattle.imp, "rb"); 2237 + 2238 + if(file) 2239 + { 2240 + static tic_code code; 2241 + code.data[fread(code.data, 1, sizeof(tic_code), file)] = '\0'; 2242 + 2243 + char* end = strchr(code.data, '\n'); 2244 + 2245 + if(end) 2246 + { 2247 + static const char PosTag[] = "-- pos: "; 2248 + enum{TagSize = sizeof PosTag - 1}; 2249 + 2250 + if(memcmp(code.data, PosTag, TagSize) == 0) 2251 + { 2252 + char* start = code.data + TagSize; 2253 + char* sep = strchr(start, ','); 2254 + 2255 + if(sep) 2256 + { 2257 + *sep = *end = '\0'; 2258 + s32 x = atoi(start); 2259 + s32 y = atoi(sep + 1); 2260 + 2261 + if(x == 0 && y == 0) 2262 + { 2263 + if(studio->mode != TIC_RUN_MODE) 2264 + runGame(studio); 2265 + } 2266 + else 2267 + { 2268 + s32 offset = end - code.data + 1; 2269 + memcpy(studio->code->src, code.data + offset, sizeof(tic_code) - offset); 2270 + codeSetPos(studio->code, x - 1, y - 1); 2271 + 2272 + if(studio->mode == TIC_RUN_MODE) 2273 + setStudioMode(studio, TIC_CODE_MODE); 2274 + } 2275 + } 2276 + } 2277 + } 2278 + 2279 + fclose(file); 2280 + } 2281 + } 2282 + #endif 2283 + 2183 2284 static void blitCursor(Studio* studio) 2184 2285 { 2185 2286 tic_mem* tic = studio->tic; ··· 2291 2392 2292 2393 #if defined(BUILD_EDITORS) 2293 2394 tic_net_end(studio->net); 2395 + 2396 + { 2397 + Bytebattle* bb = &(studio->bytebattle); 2398 + if(bb->battle.started) 2399 + { 2400 + u32 passed = getTime() - bb->battle.started; 2401 + bb->battle.left = bb->battle.time - passed; 2402 + 2403 + if(bb->battle.left > 0) 2404 + { 2405 + s32 delta = bb->battle.time / (bb->limit.upper - bb->limit.lower); 2406 + bb->limit.current = bb->limit.upper - passed / delta; 2407 + } 2408 + else 2409 + { 2410 + bb->battle.left = 0; 2411 + bb->limit.current = bb->limit.lower; 2412 + } 2413 + } 2414 + 2415 + if(bb->delay) 2416 + if(bb->ticks++ < bb->delay) 2417 + return; 2418 + 2419 + if(bb->exp) 2420 + doCodeExport(studio); 2421 + else if(bb->imp) 2422 + doCodeImport(studio); 2423 + 2424 + bb->ticks = 0; 2425 + } 2294 2426 #endif 2295 2427 } 2296 2428 ··· 2394 2526 #endif 2395 2527 2396 2528 free(studio->fs); 2529 + 2530 + if(studio->bytebattle.exp) free(studio->bytebattle.exp); 2531 + if(studio->bytebattle.imp) free(studio->bytebattle.imp); 2532 + 2397 2533 free(studio); 2398 2534 } 2399 2535 2536 + Bytebattle* getBytebattle(Studio* studio) 2537 + { 2538 + return studio->bytebattle.exp || studio->bytebattle.imp ? &(studio->bytebattle) : NULL; 2539 + } 2540 + 2400 2541 static StartArgs parseArgs(s32 argc, char **argv) 2401 2542 { 2402 2543 static const char *const usage[] = ··· 2405 2546 NULL, 2406 2547 }; 2407 2548 2408 - StartArgs args = {.volume = -1}; 2549 + StartArgs args = {0}; 2550 + args.volume = -1; 2551 + args.lowerlimit = 256; 2552 + args.upperlimit = 512; 2409 2553 2410 2554 struct argparse_option options[] = 2411 2555 { ··· 2413 2557 #define CMD_PARAMS_DEF(name, ctype, type, post, help) OPT_##type('\0', #name, &args.name, help), 2414 2558 CMD_PARAMS_LIST(CMD_PARAMS_DEF) 2415 2559 #undef CMD_PARAMS_DEF 2560 + OPT_GROUP("Byte battle options:\n"), 2561 + OPT_STRING('\0', "codeexport", &args.codeexport, "export code to filename"), 2562 + OPT_STRING('\0', "codeimport", &args.codeimport, "import code from filename"), 2563 + OPT_INTEGER('\0', "delay", &args.delay, "codeexport / codeimport update interval in ticks"), 2564 + OPT_INTEGER('\0', "lowerlimit", &args.lowerlimit, "lower limit for code size (256 by default)"), 2565 + OPT_INTEGER('\0', "upperlimit", &args.upperlimit, "upper limit for code size (512 by default)"), 2566 + OPT_INTEGER('\0', "battletime", &args.battletime, "battletime in minutes"), 2416 2567 OPT_END(), 2417 2568 }; 2418 2569 ··· 2532 2683 2533 2684 .samplerate = samplerate, 2534 2685 .net = tic_net_create(TIC_WEBSITE), 2686 + 2687 + .bytebattle = {0}, 2535 2688 #endif 2536 2689 .tic = tic_core_create(samplerate, format), 2537 2690 }; ··· 2631 2784 studio->config->data.soft |= args.soft; 2632 2785 studio->config->data.cli |= args.cli; 2633 2786 2787 + if(args.codeexport) 2788 + studio->bytebattle.exp = strdup(args.codeexport); 2789 + else if(args.codeimport) 2790 + studio->bytebattle.imp = strdup(args.codeimport); 2791 + 2792 + studio->bytebattle.delay = args.delay; 2793 + studio->bytebattle.limit.lower = args.lowerlimit; 2794 + 2795 + studio->bytebattle.limit.current 2796 + = studio->bytebattle.limit.upper 2797 + = args.upperlimit; 2798 + 2799 + studio->bytebattle.battle.left 2800 + = studio->bytebattle.battle.time 2801 + = args.battletime * 60 * 1000; 2802 + 2634 2803 studioConfigChanged(studio); 2635 2804 2636 2805 if(args.cli) 2637 2806 args.skip = true; 2638 2807 2808 + #if defined(BUILD_EDITORS) 2639 2809 if(args.skip) 2640 - setStudioMode(studio, TIC_CONSOLE_MODE); 2810 + { 2811 + studio->console->tick(studio->console); 2812 + gotoCode(studio); 2813 + } 2814 + #endif 2641 2815 2642 2816 return studio; 2643 2817 }
+41
src/studio/studio.h
··· 95 95 #define CMD_PARAMS_DEF(name, ctype, type, post, help) ctype name; 96 96 CMD_PARAMS_LIST(CMD_PARAMS_DEF) 97 97 #undef CMD_PARAMS_DEF 98 + 99 + const char *codeexport; 100 + const char *codeimport; 101 + s32 delay; 102 + s32 lowerlimit; 103 + s32 upperlimit; 104 + s32 battletime; 98 105 } StartArgs; 99 106 100 107 typedef enum ··· 279 286 void tiles2ram(tic_ram* ram, const tic_tiles* src); 280 287 void fadePalette(tic_palette* pal, s32 value); 281 288 bool project_ext(const char* name); 289 + 290 + typedef struct 291 + { 292 + char* exp; 293 + char* imp; 294 + 295 + struct 296 + { 297 + tic_code code; 298 + char postag[32]; 299 + } last; 300 + 301 + s32 delay; 302 + s32 ticks; 303 + 304 + struct 305 + { 306 + s32 lower; 307 + s32 upper; 308 + s32 current; 309 + } limit; 310 + 311 + struct 312 + { 313 + s32 started; 314 + s32 time; 315 + s32 left; 316 + 317 + bool hidetime; 318 + } battle; 319 + 320 + } Bytebattle; 321 + 322 + Bytebattle* getBytebattle(Studio* studio);