Skip to content

Commit

Permalink
-Completed Book 29
Browse files Browse the repository at this point in the history
-More updates for strict mode
-Various fixes
-Updated printable action chart with only fixed data feature
  • Loading branch information
lonevvolf committed Sep 29, 2024
1 parent 74a9af4 commit 500b186
Show file tree
Hide file tree
Showing 29 changed files with 395 additions and 222 deletions.
4 changes: 2 additions & 2 deletions doc/README-mechanics.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Execute once only. New Order: PERMANENTLY damage the player's Kai Weapon
```xml
<damageKaiWeapon damage="-2" />
```
Permanently applies -2CS to the player's Kai Weapon
Permanently applies the specified CS penalty to the player's Kai Weapon

### setSkills
Game setup: The player selects the initial Endurance and Combat Skill
Expand Down Expand Up @@ -223,7 +223,7 @@ decrease a counter, use the "pick" rule. The "objectId" can be one or more objec

If you set more than one object id, the first one owned by the player will be dropped

The property "backpackItemSlots" and specialItemSlots is used to drop objects on some given Backpack / Special Items positions. They
The property "backpackItemSlots", "weaponSlots" and "specialItemSlots" is used to drop objects on some given Backpack / Weapon / Special Items positions. They
can contain the index positions (1-based), or "last" for the last item

If the optional property "restorePoint" is specified, the dropped item will can be restored with the rule "restoreInventoryState", with
Expand Down
43 changes: 26 additions & 17 deletions src/ts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,40 @@ export function declareCommonHelpers(declareJqueryPlugins: boolean = true) {
/****************** ARRAY ******************/

if (typeof Array.prototype.removeValue !== "function") {
Array.prototype.removeValue = function(value) {
const index = $.inArray(value, this);
if ( index >= 0 ) {
this.splice(index, 1);
return true;
} else {
return false;
Object.defineProperty(Array.prototype, 'removeValue', {
enumerable: false,
value: function(value) {
const index = $.inArray(value, this);
if ( index >= 0 ) {
this.splice(index, 1);
return true;
} else {
return false;
}
}
};
});
}

if (!Array.prototype.clone) {
Array.prototype.clone = function() {
return this.slice(0);
};
Object.defineProperty(Array.prototype, 'clone', {
enumerable: false,
value: function() {
return this.slice(0);
}
});
}

if (!Array.prototype.deepClone) {
Array.prototype.deepClone = function() {
const copy = [];
for (const element of this) {
copy.push( element.clone ? element.clone() : element );
Object.defineProperty(Array.prototype, 'deepClone', {
enumerable: false,
value: function() {
const copy = [];
for (const element of this) {
copy.push( element.clone ? element.clone() : element );
}
return copy;
}
return copy;
};
});
}

/****************** WINDOW ******************/
Expand Down
2 changes: 1 addition & 1 deletion src/ts/controller/mechanics/combatMechanics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export class CombatMechanics {
// Check if player can use Power Strike
const hasPowerStrike = state.actionChart.hasNewOrderDiscipline(NewOrderDiscipline.MagiMagic);
if (state.book.getBookSeries().id !== BookSeriesId.NewOrder ||
!hasPowerStrike || state.actionChart.getDisciplines().length < 13 || combat.mentalOnly) {
!hasPowerStrike || state.actionChart.getDisciplines().length < 13 || combat.mentalOnly || combat.noObjectBonuses) {
// Hide Power Strike check
$combatUI.find(".powerstrikecheck").hide();
return;
Expand Down
17 changes: 14 additions & 3 deletions src/ts/controller/mechanics/mechanicsEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export const mechanicsEngine = {
* @param fromUI True if the event was fired from the UI
* @param o Only applies if fromUI is true. The object picked / dropped
*/
fireInventoryEvents(fromUI: boolean = false, o: Item = null) {
fireInventoryEvents(fromUI: boolean = false, o: Item|null = null) {

// Render object tables
mechanicsEngine.showAvailableObjects();
Expand Down Expand Up @@ -1395,11 +1395,22 @@ export const mechanicsEngine = {
}

// Drop backpack item slots by its index (1-based index)
droppedObjects = droppedObjects.concat(
mechanicsEngine.dropActionChartSlots($rule, "weaponSlots", state.actionChart.weapons));

// Hack to drop all arrows when slot contains a bow (this only happens in book 29)
for (const item of droppedObjects) {
if (item.getItem().id === "bow" || item.getItem().weaponType === "bow") {
actionChartController.increaseArrows(-state.actionChart.arrows);
}
}

droppedObjects = droppedObjects.concat(
mechanicsEngine.dropActionChartSlots($rule, "backpackItemSlots", state.actionChart.backpackItems));
droppedObjects = droppedObjects.concat(
mechanicsEngine.dropActionChartSlots($rule, "specialItemSlots", state.actionChart.specialItems));


// Store dropped objects as an inventory state
const restorePointId: string = $rule.attr("restorePoint");
if (restorePointId) {
Expand Down Expand Up @@ -2168,7 +2179,7 @@ export const mechanicsEngine = {
},

/**
* Drop backpack / special items slots by its index (1-based index)
* Drop backpack / special / weapon items slots by its index (1-based index)
* @param $rule The "drop" rule
* @param property The rule property with the slots to drop
* @param objectsArray The Action Chart array (the Special Items or BackBackItems)
Expand Down Expand Up @@ -2225,7 +2236,7 @@ export const mechanicsEngine = {
if (msgId) {
$messageUI.attr("id", msgId);
}
$messageUI.find("b").text(msg);
$messageUI.find("b").html(msg);
gameView.appendToSection($messageUI);
},

Expand Down
8 changes: 4 additions & 4 deletions src/ts/controller/mechanics/numberPickerMechanics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export const numberPickerMechanics = {
if ($(rule).attr("money") === "true") {
$ui.find("#mechanics-mpAmount").attr("data-ismoneypicker", "true");
let currency = $(rule).attr("currency");
if (!currency) {
currency = CurrencyName.CROWN;

if( currency ) {
$ui.find("#mechanics-mpAmount").attr("data-moneypickercurrency", currency);
}
$ui.find("#mechanics-mpAmount").attr("data-moneypickercurrency", currency);
}

// Check if it has an action button
Expand Down Expand Up @@ -107,7 +107,7 @@ export const numberPickerMechanics = {
isValid(): boolean {
const $picker = $("#mechanics-mpAmount");

// If the money picker has been disabled, dont check it
// If the money picker has been disabled, don't check it
if (!$picker.isEnabled()) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ts/model/actionChartItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class ActionChartItem {
/**
* Item id
*/
public id: string;
public id: string|null;

/**
* Number of allowed item uses
Expand Down Expand Up @@ -37,7 +37,7 @@ export class ActionChartItem {
* @param id Item identifier
* @param usageCount Number of allowed item uses. If < 0 or not passed, the default numberuses will be assigned from the Item
*/
constructor(id: string = null, usageCount: number = -1) {
constructor(id: string|null = null, usageCount: number = -1) {
this.id = id;
if (usageCount >= 0) {
this.usageCount = usageCount;
Expand Down
6 changes: 3 additions & 3 deletions src/ts/model/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ export class Book {
* @param sectionId The destination section
* @return Section ids that can go to the given section
*/
public getOriginSections(sectionId: string): (string|undefined)[] {
public getOriginSections(sectionId: string): (string|undefined|null)[] {
const sourceSectionIds = <(string|undefined)[]>[];
$(this.bookXml)
$(this.bookXml as XMLDocument)
.find('section[class="numbered"]' )
.has( 'data > choice[idref="' + sectionId + '"]')
.each( (index: Number, section: Element) => {
Expand Down Expand Up @@ -443,7 +443,7 @@ export class Book {
* @return Array with the 100 numbers of the random table
*/
public getRandomTable(): number[] {
const $randomCells = $(this.bookXml)
const $randomCells = $(this.bookXml as XMLDocument)
.find("section[id=random] > data > illustration > instance[class=text], section[id=random] > data > table[class=random-number-table]")
.find("td");
const numbers = <number[]>[];
Expand Down
2 changes: 1 addition & 1 deletion src/ts/model/bookDownloadState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class BookDownloadState {
public downloaded = false;

/** Book zip size, in MB, to show on UI */
public size: string;
public size: string|null = null;

/**
* Constructor
Expand Down
36 changes: 25 additions & 11 deletions src/ts/model/bookSectionStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { SectionState, Section, Combat, CombatTurn } from "..";
export class BookSectionStates {

/** The current section id */
public currentSection: string = null;
public currentSection: string|null = null;

/**
* Visited section states. The key is the section id (string), and the value
* is a SectionState instance
*/
public sectionStates: { [ sectionId: string ]: SectionState } = {};
public sectionStates: { [ sectionId: string ]: SectionState|null } = {};

/**
* Hunting discipline enabled?
Expand All @@ -26,10 +26,10 @@ export class BookSectionStates {
* Enumerated properties are NOT the only ones, there can be others
*/
public otherStates = {
book6sect26TargetPoints: <number>null,
book6sect284: <number[][]>null,
book6sect26TargetPoints: <number|null>null,
book6sect284: <number[][]|null>null,
book6sect340: [-1 , -1 , -1],
book9sect91: <string>null,
book9sect91: <string|null>null,
};

/**
Expand All @@ -43,18 +43,23 @@ export class BookSectionStates {
* will be returned.
* @return The section state
*/
public getSectionState( sectionId: string = null ): SectionState {
public getSectionState( sectionId: string|null = null ): SectionState {

if ( !sectionId ) {
sectionId = this.currentSection;
}

let sectionState = this.sectionStates[ sectionId ];
if ( !sectionState ) {
sectionState = new SectionState();
this.sectionStates[ sectionId ] = sectionState;
if ( sectionId ) {
let sectionState = this.sectionStates[ sectionId ];
if ( !sectionState ) {
sectionState = new SectionState();
this.sectionStates[ sectionId ] = sectionState;
}

return sectionState;
}
return sectionState;

return new SectionState();
}

/**
Expand Down Expand Up @@ -114,12 +119,18 @@ export class BookSectionStates {
$.each( sectionState.combats , ( index , combat ) => {
const rightCombat = $.extend( new Combat( "" , 0 , 0 ) , combat );
// Fix for spelling error in old saves
// @ts-ignore
if (rightCombat["dammageMultiplier"] !== undefined) {
// @ts-ignore
rightCombat.damageMultiplier = rightCombat["dammageMultiplier"];
// @ts-ignore
delete(rightCombat["dammageMultiplier"]);
}
// @ts-ignore
if (rightCombat["permanentDammage"] !== undefined) {
// @ts-ignore
rightCombat.permanentDamage = rightCombat["permanentDammage"];
// @ts-ignore
delete(rightCombat["permanentDammage"]);
}
combats.push( rightCombat );
Expand All @@ -128,8 +139,11 @@ export class BookSectionStates {
const turns = <CombatTurn[]>[];
$.each( rightCombat.turns , ( turnIndex , turn ) => {
// Fix for spelling error in old saves
// @ts-ignore
if (turn["dammageMultiplier"] !== undefined) {
// @ts-ignore
turn.damageMultiplier = turn["dammageMultiplier"];
// @ts-ignore
delete(turn["dammageMultiplier"]);
}
turns.push( $.extend( new CombatTurn(null, 0, false, false) , turn ) );
Expand Down
2 changes: 1 addition & 1 deletion src/ts/model/combatTurn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class CombatTurn {
* @param elude True if the player is eluding the combat
* @param helshezagUsed Helshezag used on this turn?
*/
public constructor( combat: Combat , randomValue: number , elude: boolean , helshezagUsed: boolean , ansengsKirusamiLoss?: number ) {
public constructor( combat: Combat|null , randomValue: number , elude: boolean , helshezagUsed: boolean , ansengsKirusamiLoss?: number ) {

if ( !combat ) {
// Default constructor (called on BookSectionStates.prototype.fromStateObject)
Expand Down
4 changes: 2 additions & 2 deletions src/ts/model/mechanics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ export class Mechanics {
* Returns an jquery object with the section mechanics XML. null if there are no mechanics
*/
public getSection(sectionId: string|null|undefined): JQuery<HTMLElement>|null {
const $section = $(this.mechanicsXml)
const $section = $(this.mechanicsXml as XMLDocument)
.find("mechanics > sections > section[id=" + sectionId + "]");
return $section.length === 0 ? null : $section;
}

/**
* Returns a JS object with the object properties. null if it was not found
*/
public getObject(objectId: string|undefined): Item|null {
public getObject(objectId: string|null|undefined): Item|null {

if (!objectId) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/ts/model/sectionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Combat, Mechanics, Item, state, ActionChart, ActionChartItem, mechanics
*/
export interface SectionItem {
/** The object id */
id: string;
id: string|undefined|null;

/** The object price. If its zero or null, the object is free */
price: number;
Expand Down Expand Up @@ -140,7 +140,7 @@ export class SectionState {
}

const i = state.mechanics.getObject( sectionItem.id );
if ( !type || i.type === type ) {
if ( !type || i?.type === type ) {
items.push(i);
}
}
Expand All @@ -163,7 +163,7 @@ export class SectionState {
public getWeaponObjects(): (Item|null)[] {
const weapons: (Item|null)[] = [];
for ( const i of this.getSectionObjects() ) {
if ( i.isWeapon() ) {
if ( i?.isWeapon() ) {
weapons.push( i );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ts/tests/__tests__/currency.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("currency", () => {

test("Drop all coins", testDropAllCoins);
test("Drop partial crowns", testDropPartialCrowns);
test("Text Belt Pouch capacity", testMaxCapacity);
test("Test Belt Pouch capacity", testMaxCapacity);
});

async function testDropAllCoins() {
Expand Down
4 changes: 3 additions & 1 deletion src/ts/views/mapView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const mapView = {
const map = state.mechanics.getObject( Item.MAP );
// On book 11, map is on section 233
const mapSection = new Section( state.book , "sect233" , state.mechanics );
mapView.setContent( map.name , mapSection.getFirstIllustrationHtml() );
if (map) {
mapView.setContent( map.name , mapSection.getFirstIllustrationHtml() );
}
},

/**
Expand Down
Loading

0 comments on commit 500b186

Please sign in to comment.