All files / src/lib/compiler compileBootstrap.ts

47.37% Statements 9/19
33.33% Branches 4/12
28.57% Functions 2/7
47.06% Lines 8/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          1x         1x   1x 1x                               1x                         1x             1x                                       1x                                                                                
import type { EngineFieldSchema } from "store/features/engine/engineState";
import type {
  ActorDirection,
  EngineFieldValue,
} from "shared/lib/entities/entitiesTypes";
import {
  avatarFontSymbol,
  PrecompiledFontData,
  PrecompiledScene,
} from "./generateGBVMData";
import { dirEnum } from "./helpers";
import { PrecompiledAvatarData } from "./compileAvatars";
import { gbvmSetConstForCType } from "shared/lib/engineFields/engineFieldToCType";
import { pxToSubpx, tileToSubpx } from "shared/lib/helpers/subpixels";
 
interface InitialState {
  startX: number;
  startY: number;
  startDirection: ActorDirection;
  startScene: PrecompiledScene;
  startMoveSpeed: number;
  startAnimSpeed: number;
  fonts: PrecompiledFontData[];
  avatarFonts: PrecompiledAvatarData[][];
  engineFields: EngineFieldSchema[];
  engineFieldValues: EngineFieldValue[];
  usedSceneTypeIds: string[];
}
 
export const compileScriptEngineInit = ({
  startX,
  startY,
  startDirection,
  startScene,
  startMoveSpeed,
  startAnimSpeed,
  fonts,
  avatarFonts,
  engineFields,
  engineFieldValues,
  usedSceneTypeIds,
}: InitialState) => {
  const usedEngineFields = engineFields.filter(
    (engineField: EngineFieldSchema) =>
      engineField.cType !== "define" &&
      (!engineField.sceneType ||
        usedSceneTypeIds.includes(engineField.sceneType))
  );
 
  return `.include "vm.i"
.include "macro.i"
.include "data/game_globals.i"
 
; define constants in rom bank 0
.area _CODE
 
_start_scene_x:: 
        .dw ${tileToSubpx(startX || 0)}
_start_scene_y:: 
        .dw ${tileToSubpx(startY || 0)} 
_start_scene_dir:: 
        .db .${dirEnum(startDirection)}
_start_scene::
        IMPORT_FAR_PTR_DATA _${startScene.symbol}
_start_player_move_speed:: 
        .db ${pxToSubpx(startMoveSpeed)}
_start_player_anim_tick:: 
        .db ${startAnimSpeed}
_ui_fonts:: 
${fonts.map((font) => `        IMPORT_FAR_PTR_DATA _${font.symbol}`).join("\n")}
${avatarFonts
  .map(
    (_, avatarFontIndex) =>
      `        IMPORT_FAR_PTR_DATA _${avatarFontSymbol(avatarFontIndex)}`
  )
  .join("\n")}
 
; define engine init VM routine which will be packed into some bank
.area _CODE_255
 
___bank_script_engine_init = 255
.globl ___bank_script_engine_init
 
${usedEngineFields
  .map((engineField) => {
    return `.globl _${engineField.key}`;
  })
  .join("\n")}
 
_script_engine_init::
${usedEngineFields
  .map((engineField) => {
    Iif (engineField.cType === "define") {
      return "";
    }
    const engineValue = engineFieldValues.find((v) => v.id === engineField.key);
    const value =
      engineValue && engineValue.value !== undefined
        ? engineValue.value
        : engineField.defaultValue;
    const gbvmSetConstInstruction = gbvmSetConstForCType(engineField.cType);
    return `        ${gbvmSetConstInstruction}      _${engineField.key}, ${value}`;
  })
  .join("\n")}
 
        ; return from init routine
        VM_RET_FAR
`;
};