Skip to content

Instantly share code, notes, and snippets.

@joningold
Last active November 19, 2021 05:29
Show Gist options
  • Save joningold/359edd9ebe446051bb99211af024ad7d to your computer and use it in GitHub Desktop.
Save joningold/359edd9ebe446051bb99211af024ad7d to your computer and use it in GitHub Desktop.
Ink: here's a system that allows you to easily pair elements from two ink lists together - say, characteristics with people; weapons with materials; etc.
/*------------------------------------------------------------
RAWINT Keys
------------------------------------------------------------*/
LIST RawInts = RAWINT0 = 0, RAWINT1, RAWINT2, RAWINT3, RAWINT4, RAWINT5, RAWINT6, RAWINT7, RAWINT8, RAWINT9, RAWINT10, RAWINT11, RAWINT12, RAWINT13, RAWINT14, RAWINT15, RAWINT16, RAWINT17, RAWINT18, RAWINT19, RAWINT20, RAWINT21, RAWINT22, RAWINT23, RAWINT24, RAWINT25, RAWINT26, RAWINT27, RAWINT28, RAWINT29, RAWINT30, RAWINT31, RAWINT32, RAWINT33, RAWINT34, RAWINT35, RAWINT36, RAWINT37, RAWINT38, RAWINT39, RAWINT40, RAWINT41, RAWINT42, RAWINT43, RAWINT44, RAWINT45, RAWINT46, RAWINT47, RAWINT48, RAWINT49, RAWINT50, RAWINT51, RAWINT52, RAWINT53, RAWINT54, RAWINT55, RAWINT56, RAWINT57, RAWINT58, RAWINT59, RAWINT60, RAWINT61, RAWINT62, RAWINT63, RAWINT64, RAWINT65, RAWINT66, RAWINT67, RAWINT68, RAWINT69, RAWINT70, RAWINT71, RAWINT72, RAWINT73, RAWINT74, RAWINT75, RAWINT76, RAWINT77, RAWINT78, RAWINT79, RAWINT80, RAWINT81, RAWINT82, RAWINT83, RAWINT84, RAWINT85, RAWINT86, RAWINT87, RAWINT88, RAWINT89, RAWINT90, RAWINT91, RAWINT92, RAWINT93, RAWINT94, RAWINT95, RAWINT96, RAWINT97, RAWINT98, RAWINT99, RAWINT100, RAWINT101, RAWINT102, RAWINT103, RAWINT104, RAWINT105, RAWINT106, RAWINT107, RAWINT108, RAWINT109, RAWINT110, RAWINT111, RAWINT112, RAWINT113, RAWINT114, RAWINT115, RAWINT116, RAWINT117, RAWINT118, RAWINT119, RAWINT120, RAWINT121, RAWINT122, RAWINT123, RAWINT124, RAWINT125, RAWINT126, RAWINT127, RAWINT128, RAWINT129, RAWINT130, RAWINT131, RAWINT132, RAWINT133, RAWINT134, RAWINT135, RAWINT136, RAWINT137, RAWINT138, RAWINT139, RAWINT140, RAWINT141, RAWINT142, RAWINT143, RAWINT144, RAWINT145, RAWINT146, RAWINT147, RAWINT148, RAWINT149, RAWINT150, RAWINT151, RAWINT152, RAWINT153, RAWINT154, RAWINT155, RAWINT156, RAWINT157, RAWINT158, RAWINT159, RAWINT160, RAWINT161, RAWINT162, RAWINT163, RAWINT164, RAWINT165, RAWINT166, RAWINT167, RAWINT168, RAWINT169, RAWINT170, RAWINT171, RAWINT172, RAWINT173, RAWINT174, RAWINT175, RAWINT176, RAWINT177, RAWINT178, RAWINT179, RAWINT180, RAWINT181, RAWINT182, RAWINT183, RAWINT184, RAWINT185, RAWINT186, RAWINT187, RAWINT188, RAWINT189, RAWINT190, RAWINT191, RAWINT192, RAWINT193, RAWINT194, RAWINT195, RAWINT196, RAWINT197, RAWINT198, RAWINT199, RAWINT200, RAWINT201, RAWINT202, RAWINT203, RAWINT204, RAWINT205, RAWINT206, RAWINT207, RAWINT208, RAWINT209, RAWINT210, RAWINT211, RAWINT212, RAWINT213, RAWINT214, RAWINT215, RAWINT216, RAWINT217, RAWINT218, RAWINT219, RAWINT220, RAWINT221, RAWINT222, RAWINT223, RAWINT224, RAWINT225, RAWINT226, RAWINT227, RAWINT228, RAWINT229, RAWINT230, RAWINT231, RAWINT232, RAWINT233, RAWINT234, RAWINT235, RAWINT236, RAWINT237, RAWINT238, RAWINT239, RAWINT240, RAWINT241, RAWINT242, RAWINT243, RAWINT244, RAWINT245, RAWINT246, RAWINT247, RAWINT248, RAWINT249, RAWINT250, RAWINT251, RAWINT252, RAWINT253, RAWINT254, RAWINT255, RAWINT256, RAWINT257, RAWINT258, RAWINT259, RAWINT260, RAWINT261, RAWINT262, RAWINT263, RAWINT264, RAWINT265, RAWINT266, RAWINT267, RAWINT268, RAWINT269, RAWINT270, RAWINT271, RAWINT272, RAWINT273, RAWINT274, RAWINT275, RAWINT276, RAWINT277, RAWINT278, RAWINT279, RAWINT280, RAWINT281, RAWINT282, RAWINT283, RAWINT284, RAWINT285, RAWINT286, RAWINT287, RAWINT288, RAWINT289, RAWINT290, RAWINT291, RAWINT292, RAWINT293, RAWINT294, RAWINT295, RAWINT296, RAWINT297, RAWINT298, RAWINT299, RAWINT300, RAWINT301, RAWINT302, RAWINT303, RAWINT304, RAWINT305, RAWINT306, RAWINT307, RAWINT308, RAWINT309, RAWINT310, RAWINT311, RAWINT312, RAWINT313, RAWINT314, RAWINT315, RAWINT316, RAWINT317, RAWINT318, RAWINT319, RAWINT320, RAWINT321, RAWINT322, RAWINT323, RAWINT324, RAWINT325, RAWINT326, RAWINT327, RAWINT328, RAWINT329, RAWINT330, RAWINT331, RAWINT332, RAWINT333, RAWINT334, RAWINT335, RAWINT336, RAWINT337, RAWINT338, RAWINT339, RAWINT340, RAWINT341, RAWINT342, RAWINT343, RAWINT344, RAWINT345, RAWINT346, RAWINT347, RAWINT348, RAWINT349, RAWINT350, RAWINT351, RAWINT352, RAWINT353, RAWINT354, RAWINT355, RAWINT356, RAWINT357, RAWINT358, RAWINT359, RAWINT360, RAWINT361, RAWINT362, RAWINT363, RAWINT364, RAWINT365, RAWINT366, RAWINT367, RAWINT368, RAWINT369, RAWINT370, RAWINT371, RAWINT372, RAWINT373, RAWINT374, RAWINT375, RAWINT376, RAWINT377, RAWINT378, RAWINT379
INCLUDE list_pairkeys.ink
// The pop function is massively useful. Remove it if you're already got it in your project!
=== function pop(ref list)
~ temp el = LIST_MIN(list)
~ list -= el
~ return el
/*------------------------------------------------------
Story-facing Functions
------------------------------------------------------*/
=== function storePairs(listAList, listBList, ref pairStoreVariable)
{ listBList:
~ temp listB = pop(listBList)
~ _storePairsAgainstB(listAList, listB, pairStoreVariable)
~ storePairs(listAList, listBList, pairStoreVariable)
}
=== function getPairsOfTypeAForListB(typeForA, listB, pairStoreVariable)
// Gets all the elements paired with a particular item in the store, as a particular list type
// e.g. getPairsOfTypeAForListB(Adjectives, ProfPlum, CharacteristicsForPeople)
~ return _getPairsOfTypeAForListBFrom(typeForA, listB, pairStoreVariable, ())
=== function getPairsForListAOfTypeB(listA, typeForB, pairStoreVariable)
// Gets all the elements paired with a particular item in the store, as a particular list type
~ return _getPairsOfTypeBForListAFrom(listA, typeForB, pairStoreVariable, ())
/*------------------------------------------------------
Store - Internal Functions
------------------------------------------------------*/
=== function _storePairsAgainstB(listAList, listB, ref pairStoreVariable)
// Pairs a set of list items with a particular item.
// e.g. storePairs((Tall, Dark, Strange), MrBlack, CharacteristicsForPeople)
{ listAList:
~ temp listA = pop(listAList)
~ pairStoreVariable += _pairElement(listA, listB)
~ storePairs(listAList, listB, pairStoreVariable)
}
=== function _pairElement(listA, listB)
~ temp idx = ____idx(listA, listB)
~ temp pairEl = RawInts(idx)
{ not pairEl:
\*** ERROR: Trying to create a RawInt value of {idx}, but the current maximum is {LIST_VALUE(LIST_MAX(LIST_ALL(RawInts)))}. Please define more keys. ***
}
~ return pairEl
/*------------------------------------------------------
Access - Internal Functions
------------------------------------------------------*/
=== function _getPairsOfTypeAForListBFrom(sourceList, listB, pairStoreToReview, result)
~ return __getPairsOfTypeForListFrom(false, listB, LIST_ALL(sourceList),pairStoreToReview, result)
=== function _getPairsOfTypeBForListAFrom(listA, sourceList,pairStoreToReview, result)
~ return __getPairsOfTypeForListFrom(true, listA, LIST_ALL(sourceList) ,pairStoreToReview, result)
=== function __getPairsOfTypeForListFrom(listElementIsListA, listElement, sourceList,pairStoreToReview, result)
{ pairStoreToReview:
~ temp idxVal = LIST_VALUE(pop(pairStoreToReview))
~ temp resultIdx = ()
{ listElementIsListA:
{ ___idxHasA(idxVal, listElement, LIST_MIN(sourceList)):
~ resultIdx = ___getBGivenBType(idxVal, LIST_MIN(sourceList))
}
- else:
{ ___idxHasB(idxVal, listElement):
~ resultIdx = ___getAGivenBType(idxVal, listElement)
}
}
{ resultIdx:
// ink's ListName(intVal) method only works for explicit ListNames, so if you want to turn an int into a list_item generically you have to do it by looping through
~ temp listItemFound = ___getListValueWithValue(resultIdx, sourceList)
~ result += listItemFound
}
~ return __getPairsOfTypeForListFrom(listElementIsListA, listElement, sourceList, pairStoreToReview, result)
}
~ return result
=== function ___getListValueWithValue(resultIdx, sourceList)
{ sourceList:
~ temp el = pop(sourceList)
{ LIST_VALUE(el) == resultIdx:
// Found it!
~ return el
}
// Keep looking
~ return ___getListValueWithValue(resultIdx, sourceList)
}
// Give up (should never happen!)
~ return ()
/*------------------------------------------------------
Mathematical Functions
------------------------------------------------------*/
// At its heart, the system works by turning a pair of list items into a unique integer (and then in turn, turning that into a list item)
// The following functions do the work here.
=== function ____baseFromList(listB)
~ return (1 + LIST_VALUE(LIST_MAX(LIST_ALL(listB))))
=== function ____idx(listA, listB)
// return unique ID for the *ordered* pair
~ temp ret = ____baseFromList(listB) * (LIST_VALUE(listA) - 1) + LIST_VALUE(listB) - 1
~ return ret
=== function ___idxHasA(idxVal, listA, bElement)
~ return ___getAGivenBType(idxVal, bElement) == LIST_VALUE(listA)
=== function ___idxHasB(idxVal, listB)
~ return ___getBGivenBType(idxVal, listB) == LIST_VALUE(listB)
=== function ___getAGivenBType(idxVal, bElement)
~ temp base = ____baseFromList(bElement)
~ temp ret = ((idxVal - idxVal mod base) / base) + 1
~ return ret
=== function ___getBGivenBType(idxVal, bElement)
~ temp base = ____baseFromList(bElement)
~ temp ret = (idxVal mod base) + 1
~ return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment