modkit examples

Run Settings
LanguageLua
Language Version
Run Command
repairers_proto = { attribs = function (c, p, s) return { family_weights = { -- custom families corvette = 1, frigate = 1.5, resource = 4, -- ...etc... } }; end }; -- makes the repairer repair the target function repairers_proto:repair(target) SobGroup_RepairSobGroup(self.own_group, target.own_group); end -- our own priority algorithm (based on weights): -- (distance_weight * missing_health) + family_weight + lost_since_last_weight function repairers_proto:targetPriority(target) local distance_weight = 1 - max(0, self:distanceTo(target) / 10000); -- range, 0 = 1 --> 10000 = 0 local missing_health_weight = (1 - target:HP()); local family_weight = self.family_weights[target:family("custom")]; local lost_since_last_weight = 0; if (target.past_self) then lost_since_last_weight = -1 * abs(target:HP() - target.past_self.HP); end return (distance_weight * missing_health_weight) + family_weight + lost_since_last_weight; end function repairers_proto:update() -- get all damaged ships belonging to us local damaged_ships = modkit.table.filter( GLOBAL_REGISTRY.all(), function (ship) return ship:HP() < 1 and ship.player == %self.player; end ); -- if any found, sort them by our prio algorithm, repair the top result (heaviest weighted target) if (modkit.table.length(damaged_ships) > 0) then sort( damaged_ships, function (ship_a, ship_b) return %self:targetPriority(ship_a) < %self:targetPriority(ship_b); end ); self:repair(damaged_ships[1]); -- repair the top result end end
gravwell_proto = { attribs = { effect_range = 3000, stun_duration = 1, -- ideally the same as the tick rate, stun_effect = "PowerOff" } }; -- customcommand update is called 'go' instead of 'do' (since do is a lua keyword) function gravwell_proto:go() local ships_to_stun = modkit.table.filter( GLOBAL_REGISTRY.all(), function (ship) local in_range = %self:distanceTo(ship) < %self.effect_range; local is_stunnable_type = ship:isFighter() == 0 or ship:isCorvette(); local not_salvette = ship:isSalvager(); return in_range and is_stunnable_type and not_salvette; end ); for _, ship in ships_to_stun do ship:stunned(1, self.stun_duration); ship:playFx(self.stun_effect); end end
Editor Settings
Theme
Key bindings
Full width
Lines