Define Properties() owner = 10004 selected = 0 sprite = 0 -- name name = "Dude" rank = "Rookie" -- stats strength = 10 perception = 10 endurance = 10 charisma = 10 intelligence = 10 agility = 10 luck = 10 -- skills medicine = 0 -- Stat: Intelligence mechanical = 0 -- Stat: Intelligence electronics = 0 -- Stat: Intelligence -- diplomacy barter = 0 -- Stat: Charisma spin = 0 -- Stat: Charisma leadership = 0 -- Stat: Charisma -- weapons skills melee = 0 -- Stat: Strength smallArms = 0 -- Stat: Agility heavyWeapons = 0 -- Stat: Endurance thrownWeapons = 0 -- Stat: Agility demolitions = 0 -- Stat: Perception, Agility -- derived stats -- action points maxActionPoints = 100 curActionPoints = 100 -- health curHealth = 100 maxHealth = 100 -- energy curEnergy = 100 maxEnergy = 100 -- carry weight curWeight = 0 maxWeight = 100 -- Strength-based -- movementCost baseMoveCost = 2 reservePoints = 20 state = "idle" -- inventory -- general inventory is player's container -- all player objects go in the container, just store ids of contained objects -- in the equipment slots: l_hand, r_hand, belt_1, belt_2, belt_3, belt_4 -- UI references apRect = 0 healthRect = 0 energyRect = 0 -- selection UI object_select_label = 0 end Trigger attach() end Trigger deploy(user) AddEffect(self, "hover", "glow", {red=0, green=255, blue=75, strength=100, blurX=2, blurY=2}) -- display selection arrow on unit if (self.object_select_label ~= 0) then UiDelete(self.object_select_label) self.object_select_label = 0 end self.object_select_label = UiImage(0, "selectionArrow", -4, -40, 8, 16, '0:736') UiTintPartial(self.object_select_label, user.team_color.red, user.team_color.green, user.team_color.blue, "grey") UiAttachUserObject(user, self, self.object_select_label) end Trigger pickup() if (self.object_select_label ~= 0) then UiDelete(self.object_select_label) self.object_select_label = 0 end end Trigger hold() self.state = "hold" end Trigger useInventoryItem(user, location, slot) -- user has requested that the unit activate an object from inventory AlertToUser(user, 1500, "Use %s %d", location, slot) -- sendto object in inventory item request to show actionMenu end Trigger dropObjectInHand(dropx, dropy) Debug("Drop") Debug("%d %d", dropx, dropy) SendTo(self, "dropObject", 0, GetObjectById(self.inHand), dropx, dropy, 0, 0) end Trigger updateAP() local user = GetObjectById(self.owner) if self.state == "moving" then --Debug("Moving tick: %d", self.actionPoints) -- Todo: add tile-based modifier for movement cost -- Todo: display AP on-screen if self.curActionPoints - self.baseMoveCost <= self.reservePoints then --Debug("Stopping %d < %d", self.actionPoints, self.reservePoints) CancelPathfinding(self) CancelPathfinding(user) self.state = "idle" else self.curActionPoints = self.curActionPoints - self.baseMoveCost -- 100% width = 32 SendTo(user, "ui_redrawAPRect", 0, self) --UiSize(user.apRect, math.floor((self.actionPoints/(self.baseActionPoints+5))*32), 2) --Debug("Continue") SendTo(self, "updateAP", 100) end end end Trigger updateHealth(val) -- apply val to current health self.curHealth = self.curHealth + val -- cap health to 0..maxHealth range if self.curHealth > self.maxHealth then self.curHealth = self.maxHealth elseif self.curHealth < 0 then self.curHealth = 0 self.state = "dead" end -- adjust rectangle size UiSize(self.healthRect, math.floor((self.curHealth/self.maxHealth)*32), 2) end Trigger updateEnergy(val) -- apply val to current energy self.curEnergy = self.curEnergy + val -- cap energy to 0..maxEnergy range if self.curEnergy > self.maxEnergy then self.curEnergy = self.maxEnergy elseif self.curEnergy < 0 then self.curEnergy = 0 self.state = "stunned" end -- adjust rectangle size UiSize(self.energyRect, math.floor((self.curEnergy/self.maxEnergy)*32), 2) end Trigger resetUnit() self.state = "idle" -- reset unit's action points resetAP(self) end Trigger selectUnit(user) -- handle unit selection here self.selected = 1 -- display selection arrow on unit if (self.object_select_label ~= 0) then UiDelete(self.object_select_label) self.object_select_label = 0 end self.object_select_label = UiImage(0, "selectionArrow", -4, -40, 8, 16, '0:737') UiTintPartial(self.object_select_label, user.team_color.red, user.team_color.green, user.team_color.blue, "grey") UiAttachUserObject(user, self, self.object_select_label) end Trigger unselectUnit(user) -- handle deselection here self.selected = 0 -- close inventory if open SendTo(self, "dismissInventory", 0) -- remove selection arrow if (self.object_select_label ~= 0) then UiDelete(self.object_select_label) self.object_select_label = 0 end self.object_select_label = UiImage(0, "selectionArrow", -4, -40, 8, 16, '0:736') UiTintPartial(self.object_select_label, user.team_color.red, user.team_color.green, user.team_color.blue, "grey") UiAttachUserObject(user, self, self.object_select_label) end Trigger requestMove(x, y) -- Debug("Path to %f, %f", x, y) self.speed = 2 if (self.curActionPoints - self.baseMoveCost) > self.reservePoints then --Debug("AP remaining: %d", self.curActionPoints) PathToLocation(self, round(x), round(y)) else AlertToUser(GetObjectById(self.owner), 1000, "No action points!") CancelPathfinding(GetObjectById(self.owner)) end end -- path_end -- System Trigger: sent to user when path is complete -- Args: -- x, y - position at end of path Trigger path_end(x,y) --Debug("arrived at [%d, %d]", x, y) self.state = "idle" end Trigger path_not_found(x,y) self.speed=0 self.state = "idle" --Debug("cannot pathfind to [%d, %d]", x, y) end Trigger path_begin(x,y) --Debug("pathfinding to [%d, %d]", x, y) self.state = "moving" SendTo(self, "updateAP", 100) end -- FUNCTIONS function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end function resetAP(self) local rollOver = 0 if self.curActionPoints > 5 then rollOver = 5 else rollOver = self.curActionPoints end self.curActionPoints = self.maxActionPoints + rollOver if (self.selected == 1) then SendTo(GetObjectById(self.owner), "ui_redrawAPRect", 0, self) end -- UiSize(self.apRect, (self.actionPoints/(self.baseActionPoints+5))*32, 2) end