All files / src/lib/compiler compileBootstrap.ts

37.93% Statements 11/29
17.24% Branches 5/29
22.22% Functions 2/9
35.71% Lines 10/28

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 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                1x         1x   1x                               1x                 1x                         1x             1x                                                     1x                             1x                                       1x                                                      
import type {
  EngineFieldCType,
  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 { 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[];
}
 
const roundValue = (
  value: string | number | boolean | undefined,
): string | number | boolean | undefined => {
  Iif (typeof value === "number") {
    return Math.floor(value);
  }
  return value;
};
 
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.key?.length > 0 &&
      (!engineField.sceneType ||
        usedSceneTypeIds.includes(engineField.sceneType)),
  );
  const engineFieldInitialValues = usedEngineFields.reduce(
    (memo, engineField) => {
      Iif (engineField.cType === "define" || engineField.runtimeOnly) {
        return memo;
      }
      const engineValue = engineFieldValues.find(
        (v) => v.id === engineField.key,
      );
      const value = roundValue(
        engineValue && engineValue.value !== undefined
          ? engineValue.value
          : engineField.defaultValue,
      );
      memo.push({
        cType: engineField.cType,
        key: engineField.key,
        value,
      });
      return memo;
    },
    [] as Array<{
      cType: EngineFieldCType;
      key: string;
      value: string | number | boolean | undefined;
    }>,
  );
  const engineFieldInitRPN =
    engineFieldInitialValues.length === 0
      ? ""
      : "VM_RPN\n" +
        engineFieldInitialValues
          .map(({ cType, key, value }) => {
            if (cType === "WORD" || cType === "UWORD") {
              return `            .R_INT16 ${value}\n            .R_REF_MEM_SET .MEM_I16, _${key}\n`;
            } else Iif (cType === "BYTE" || cType === "UBYTE") {
              return `            .R_INT8 ${value}\n            .R_REF_MEM_SET .MEM_I8, _${key}\n`;
            }
            return "";
          })
          .join("") +
        "            .R_STOP";
 
  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::
        ${engineFieldInitRPN}
 
        ; return from init routine
        VM_RET_FAR
`;
};