Last active
June 28, 2017 03:24
-
-
Save jwulf/1bc37ccd2b53dab04bdb50f16c1b63b4 to your computer and use it in GitHub Desktop.
Magikcraft API: mct1 files, by jwulf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| const PotionEffect = magik.type("potion.PotionEffect"); | |
| const PotionEffectType = magik.type("potion.PotionEffectType"); | |
| const Color = magik.type("Color"); | |
| module.exports = function effects(type, opts) { | |
| if (!opts) { | |
| opts = {}; | |
| } | |
| const duration = opts.duration || 500; | |
| const amplifier = opts.amplifier || 1; | |
| const color = opts.color || "GREEN"; | |
| const c = Color[color]; | |
| const l = PotionEffectType[type]; | |
| const effect = new PotionEffect(l, duration, amplifier, true, true, c); | |
| magik.getSender().addPotionEffect(effect); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| const EventPriority = magik.type("event.EventPriority"); | |
| const PlayerItemConsumeEvent = magik.type("event.player.PlayerItemConsumeEvent"); | |
| const PlayerQuitEvent = magik.type("event.player.PlayerQuitEvent"); | |
| const EventCallback = Java.type("io.magikcraft.EventCallback"); | |
| module.exports = function events() { | |
| magik.getPlugin().registerEvent( | |
| PlayerItemConsumeEvent.class, | |
| EventPriority.MONITOR, | |
| true, | |
| new EventCallback({ | |
| callback: function (event) { | |
| var username = event.player.playerListName; | |
| magik.dixit(event.ItemStack) | |
| } | |
| })); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| const effects = require('./effects'); | |
| const player = magik.getSender(); | |
| const setBGL = require('./setBGL'); | |
| const say = (msg) => magik.dixit(msg, player.getName()); | |
| module.exports = function gameloop() { | |
| const mct1 = magik.global('mct1'); | |
| // say(mct1); | |
| const state = mct1.state; | |
| // say(state.bgl); | |
| let bgl = state.bgl; | |
| let newBGL; | |
| const insulinOnBoard = state.insulinOnBoard; | |
| const carbsOnBoard = state.carbsOnBoard; | |
| const insulinAbsorptionRate = insulinOnBoard / 80 + 0.0025; | |
| // say(insulinAbsorptionRate.toString()); | |
| if (insulinAbsorptionRate > 0) { | |
| } | |
| // do Insulin Absorption | |
| if (insulinOnBoard > 0) { | |
| const newInsulin = insulinOnBoard - insulinAbsorptionRate; | |
| const food = player.getFoodLevel(); | |
| player.setFoodLevel(food - 1); | |
| mct1.setInsulin(Math.max(newInsulin, 0)); | |
| // do BGL Absorption | |
| if (bgl > 0) { | |
| const bglAbsorbed = insulinAbsorptionRate * 0.8; | |
| newBGL = bgl - bglAbsorbed; | |
| newBGL = Math.max(newBGL, 0); | |
| newBGL = Math.min(newBGL, 0.99); | |
| if (newBGL == 0) { | |
| say("Aaaaaarggggh!"); | |
| } | |
| } | |
| } | |
| // Do BGL increase | |
| if (state.insulinOnBoard == 0) { | |
| newBGL += 0.7; | |
| } | |
| setBGL(bgl); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // filename: index | |
| const mct1_version = '1.2'; | |
| const magik = magikcraft.io; | |
| const say = (msg) => { | |
| magik.dixit(msg, magik.getSender().getName()); | |
| } | |
| const Promise = require('promise'); | |
| const setupBars = require('./setup_bars'); | |
| const setupState = require('./setup_state'); | |
| const gameLoop = require('./gameloop'); | |
| say(`MCT version ${mct1_version}`); | |
| const setBGL = require('./setBGL'); | |
| const setInsulin = require('./setInsulin'); | |
| module.exports = function index() { | |
| return new Promise((resolve, reject) => { | |
| const mct1 = magik.global('mct1'); | |
| if (mct1.initialised) { | |
| say('Resolving promise...'); | |
| return resolve(mct1); | |
| } | |
| const cancelGameLoop = () => { | |
| if (mct1.loop) { | |
| magik.clearInterval(mct1.loop); | |
| } | |
| }; | |
| say('Promisified MCT1 module loading...'); | |
| mct1.version = mct1_version; | |
| setupBars().then( | |
| (bars) => { | |
| mct1.bars = bars; | |
| mct1.setBGL = setBGL; | |
| mct1.setInsulin = setInsulin; | |
| setupState(); | |
| mct1.initialised = true; | |
| mct1.controller = { | |
| start: () => { | |
| say('Starting...'); | |
| cancelGameLoop(); | |
| say('Initiating Game Loop'); | |
| mct1.loop = magik.setInterval(gameLoop, 1000); | |
| }, | |
| stop: () => { | |
| cancelGameLoop(); | |
| }, | |
| reset: () => { | |
| setBGL(0.4); | |
| mct1.state.insulinOnBoard = 0.2; | |
| } | |
| } | |
| say('Resolving promise') | |
| resolve(mct1); | |
| }); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This is the Insulin class | |
| * Create a new instance of this class for basal and fast-acting insulins. | |
| * | |
| * param {number} onsetDelay - the number of seconds before the insulin effect kicks instance. | |
| * param {number} duration - the number of seconds of the total effective duration of this insulin. | |
| * param {number} peak - the number of seconds to the peak action of the insulin. Set to 0 for a flat (consistent) effect. Is modelled as a bell curve centered on the peak. | |
| * param {number} power - the effect of the insulin. This is the peak power for insulins wiht a peak response. | |
| */ | |
| const magik = magikcraft.io; | |
| const secondsPerTick = 1; | |
| function Insulin(onsetDelay = 0, duration, power, peak = 0) { | |
| this.onsetDelay = onsetDelay; | |
| this.duration = duration; | |
| this.peak = peak; | |
| this.power = power; | |
| } | |
| Insulin.prototype.calculateInsulinEffect = function(elapsedTime) { | |
| if (this.peak === 0) { | |
| return this.power; | |
| } | |
| const a = Math.atan(this.power / (this.duration * 0.5)); | |
| const getE = () => { | |
| if (elapsedTime <= this.duration / 2) { | |
| return a * elapsedTime; | |
| } else { | |
| return a * this.duration - elapsedTime; | |
| } | |
| }; | |
| const e = getE(); | |
| const effect = e * a; | |
| } | |
| Insulin.prototype.take = function(amount) { | |
| let elapsedTime = this.onsetDelay; | |
| const magik = magikcraft.io | |
| const mct1 = magik.global('mct1'); | |
| magik.setTimeout(() => { | |
| // the insulin starts to act | |
| let _loop = magik.setInterval( | |
| () => { | |
| if (elapsedTime >= this.duration - this.onsetDelay) { | |
| // insulin effect exhausted | |
| magik.clearInterval(_loop); | |
| return; | |
| } | |
| // == Do Insulin effect == | |
| // calculate insulin power | |
| const effect = this.calculateInsulinEffect(elapsedTime); | |
| mct1.mutateBGL(effect); | |
| elapsedTime += secondsPerTick; | |
| }, | |
| secondsPerTick | |
| ); | |
| }, this.onsetDelay); | |
| } | |
| module.exports = Insulin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| const mct1 = magik.global('mct1'); | |
| const say = magik.dixit; | |
| const effects = require('./effects'); | |
| module.exports = function mutateBGL(bgl = 0) { | |
| const previous = mct1.state.bgl; | |
| mct1.state.bgl += bgl; | |
| const current = mct1.state.bgl; | |
| const rate = previous - current; | |
| mct1.bars.bgl.setProgress(bgl); | |
| if (bgl > 0.2 && bgl < 0.4) { | |
| mct1.bars.bgl.setColor(magik.Bars.Color.GREEN); | |
| } else { | |
| mct1.bars.bgl.setColor(magik.Bars.Color.RED); | |
| if (bgl < 0.3 && rate > 0.05) { | |
| say('WARNING: BGL fall alert'); | |
| } | |
| if (bgl < 0.2) { | |
| effects('CONFUSION'); | |
| } | |
| if (bgl > 0.8) { | |
| effects('BLINDNESS'); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| const mct1 = magik.global('mct1'); | |
| const say = magik.dixit; | |
| const effects = require('./effects'); | |
| module.exports = function setBGL(bgl, insulinAbsorptionRate = 0) { | |
| mct1.state.bgl = bgl; | |
| say(bgl); | |
| mct1.bars.bgl.setProgress(bgl); | |
| if (bgl > 0.2 && bgl < 0.4) { | |
| mct1.bars.bgl.setColor(magik.Bars.Color.GREEN); | |
| } else { | |
| mct1.bars.bgl.setColor(magik.Bars.Color.RED); | |
| if (bgl < 0.3 && insulinAbsorptionRate > 0.05) { | |
| say('WARNING: BGL Fall alert'); | |
| } | |
| if (bgl < 0.2) { | |
| effects('CONFUSION'); | |
| } | |
| if (bgl > 0.8) { | |
| effects('BLINDNESS'); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| module.exports = function setInsulin(insulin) { | |
| const mct1 = magik.global('mct1'); | |
| mct1.bars.insulin.setProgress(insulin); | |
| mct1.state.insulinOnBoard = insulin; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| const mct1 = magik.global('mct1'); | |
| module.exports = function setup_bars() { | |
| const Bars = magik.Bars; | |
| const TextComponent = magik.TextComponent; | |
| const Promise = require('jwulf'); | |
| if (mct1.initialised) { | |
| return; | |
| } | |
| return new Promise((resolve, reject) => { | |
| const insulin = Bars.addBar(magik.getSender(), | |
| new TextComponent("Insulin"), | |
| Bars.Color.BLUE, | |
| Bars.Style.NOTCHED_20, | |
| 0.0, // Progress (0.0 - 1.0) | |
| ); | |
| const bgl = Bars.addBar(magik.getSender(), | |
| new TextComponent("BGL"), | |
| Bars.Color.RED, | |
| Bars.Style.NOTCHED_20, | |
| 0.0 // Progress (0.0 - 1.0) | |
| ); | |
| resolve({ insulin, bgl }); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const magik = magikcraft.io; | |
| module.exports = function setup_state() { | |
| const mct1 = magik.global('mct1'); | |
| mct1.setBGL(0.4); | |
| mct1.setInsulin(0.2); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment