Public feed #599102

Miscellany

public lua v1 · immutable
Raw Edit
·published 2007-07-02 00:12 UTC
rendered paste body
----------------------------------------------------------------------------------------------------------------------------------------------------------------function widget:GetInfo()  return {    name      = "Voices",    desc      = "Unit replies and notifications",    author    = "quantum",    date      = "1/7/2007",    license   = "GNU GPL, v2 or later",    layer     = -10,    enabled   = true  --  loaded by default?  }end--TODO:--unit retreating--can't reach destination--chat    'TextCommand'--captured--lose--player exit--resources/units transferred--upgrades--comkill?--playerdied--selectsensor--move plane--FIXME:--com sounds at start------------------------------------------------------------------------------------------------------------------------------------------------------------------Speed-upslocal GetSelectedUnits = Spring.GetSelectedUnitslocal GetUnitDefID     = Spring.GetUnitDefIDlocal GetGameSeconds   = Spring.GetGameSecondslocal GetUnitTeam      = Spring.GetUnitTeamlocal GetTeamResources = Spring.GetTeamResourceslocal GetUnitAllyTeam  = Spring.GetUnitAllyTeamlocal GetGameSpeed     = Spring.GetGameSpeedlocal GetUnitHealth    = Spring.GetUnitHealthlocal insert           = table.insert----------------------------------------------------------------------------------------------------------------------------------------------------------------local SOUND_DIRNAME    = LUAUI_DIRNAME .. 'Sounds/'local VFSMODE          = VFS.ZIP_FIRSTlocal CMD_RETREAT      = 32200local mySidelocal myTeamIDlocal myAllyTeamIDlocal saidPausedlocal saidStartslocal counter          = 0local cooldownList     = {}local selectBuffer     = {}local cooldown         = {}local radarList        = {}local moveState        = {}local retreatState     = {}local fireState        = {}local energyList = {"corsolar","armsolar","coradvsol","armadvsol","corfus",                    "armfus", "aafus", "cafus", "cortide", "armtide", "corwin",                    "armwin"} --FIXME                    ----------------------------------------------------------------------------------------------------------------------------------------------------------------function widget:Initialize()  myTeamID = Spring.GetMyTeamID()  myAllyTeamID = Spring.GetMyAllyTeamID()  local _, _, _, _, _, side = Spring.GetTeamInfo(myTeamID)  mySide = side  --mySide = 'arm'     --DELETEME  Play("initialized")  --for i, v in pairs(CMD) do  --  --Spring.Echo("CMD "..i.." "..v)  --endendfunction widget:UnitCreated(unitID, unitDefID, unitTeam)  if (UnitDefs[unitDefID].canMove) then    moveState[unitID] = 1    retreatState[unitID] = 0    fireState[unitID] = 2  end  if (unitTeam == myTeamID) then    if (IsStructure(unitID)) then      Play("constructions")    else      Play("productions")    end  endendfunction widget:UnitFinished(unitID, unitDefID, unitTeam)  if (unitTeam == myTeamID) then    if (IsStructure(unitID)) then      if ((UnitDefs[UnitDedID]) and          (UnitDefs[UnitDedID].canAttack == 1)) then        Play("constructiontc")  --FIXME      else        Play("constructionc")      end    else      Play("productionc")    end  endendfunction widget:UnitGiven(unitID, unitDefID, unitTeam, oldTeam) --not called  Spring.SendMessage("unitGiven")  if (oldTeam == myTeamID) then    CoolPlay("unitsgiven", 1)  endendfunction widget:UnitDamaged(unitID, unitTeam)  local unitTeam = GetUnitTeam(unitID)  if (unitTeam == myTeamID) then    local unitDefID = GetUnitDefID(unitID)    if (IsStructure(unitID)) then      CoolPlay("sdamaged", 10)    elseif (UnitDefs[unitDefID].isTransport) then      CoolPlay("tdamaged", 10)    else      CoolPlay("udamaged", 10)    end    if ((UnitDefs[unitDefID].name == 'armcom') or        (UnitDefs[unitDefID].name == 'corcom')) then      health, maxHealth = GetUnitHealth(unitID)      if health/maxHealth < 0.2 then        CoolPlay("comdying", 1)      end    end  endendfunction widget:UnitDestroyed(unitID, unitDefID, unitTeam)  moveState[unitID] = nil  retreatState[unitID] = nil  fireState[unitID] = nil  allyTeam = GetUnitAllyTeam(unitID)  if (allyTeam ~= myAllyTeamID) then    CoolPlay("destroyedu", 1)    for _, energyName in ipairs(energyList) do      if (energyName == UnitDefs[unitDefID].name) then        CoolPlay("edestroyed", 0)      end    end    if ((UnitDefs[UnitDefID].name == "corcom") or        (UnitDefs[UnitDefID].name == "armcom")) then      Play("comkill")    end  endendfunction widget:Update(dt)  local selectedUnits = GetSelectedUnits()  local notEqual = false  for i, unitID in ipairs(selectedUnits) do    if (selectBuffer[i] ~= selectedUnits[i]) then      notEqual = true    end  end  if (notEqual) then    CheckSelected()  end  selectBuffer = selectedUnits  counter = counter+dt  if (counter > 1) then    counter = 0    if ((GetGameSeconds() > 0) and        not saidStarts) then      Play("gamestarts")      saidStarts = true    end    local e = GetTeamResources(myTeamID, "energy")    if (e and (e < 10)) then      CoolPlay("energylow", 15)    end    local _, _, isPaused = GetGameSpeed()    if (isPaused and (not saidPause)) then      Play("paused")      saidPause = true    end    if ((not isPaused) and saidPause) then      Play("unpaused")      saidPause = nil    end  endendfunction widget:CommandNotify(commandID, params ,options)  PlaySound("left.wav")  --Spring.Echo("CMD "..(CMD[commandID] or "not found"))  local unitID = GetSelectedUnits()[1]  local unitDefID = GetUnitDefID(unitID)  if (commandID == CMD_RETREAT) then    if (retreatState[unitID] == 0) then      retreatState[unitID] = 1      Play("retreath")    elseif (retreatState[unitID] == 1) then      retreatState[unitID] = 2      Play("retreatm")    else --if retreatState[unitID] == 2 then      retreatState[unitID] = 0    end  end  if (commandID == CMD.FIRE_STATE) then    if (fireState[unitID] == 0) then      fireState[unitID] = 1    elseif (fireState[unitID] == 1) then      fireState[unitID] = 2    else      fireState[unitID] = 0      Play("firehold")    end  end  if (commandID == CMD.MOVE_STATE) then    if (moveState[unitID] == 0) then      moveState[unitID] = 1    elseif (moveState[unitID] == 1) then      moveState[unitID] = 2      Play("moveroam")    else      moveState[unitID] = 0      Play("movehold")    end  end    if (commandID == CMD.GUARD)   then    Play("guard"  )  end  if (commandID == CMD.PATROL) then    Play("patrol" )  end  if (commandID == CMD.RECLAIM) then    Play("reclaim")  end    if ((commandID == CMD.ATTACK) or     (commandID == CMD.FIGHT) or     (commandID == CMD.AREA_ATTACK) or     (commandID == CMD.DGUN)) then    if (UnitDefs[unitDefID].canFly) then      Play("attackp")    else      Play("attack")    end  end  if (commandID == CMD.MOVE) then    if (IsStructure(unitID)) then      Play("movef")    else      Play("move")    end  end  return falseendfunction widget:UnitEnteredRadar(unitID, unitTeam)  Play("detected")  radarList[unitID] = trueendfunction widget:UnitLeftRadar(unitID, unitTeam)  if radarList[unitID] then    radarList[unitID] = nil  endendfunction widget:UnitEnteredLos(unitID, unitDefID, unitTeam)  if (UnitDefs[unitDefID].isTransport) then    Play("detectedt")  elseif (not radarList[unitID]) then    Play("detected")  endend----------------------------------------------------------------------------------------------------------------------------------------------------------------function PlaySound(filename, ...)  Spring.PlaySoundFile(SOUND_DIRNAME .. filename, unpack(arg))endfunction Play(category)  fileList = VFS.DirList(SOUND_DIRNAME, category.."_"..mySide.."_*.wav", VFSMODE)  x = #fileList  if (x > 0) then    local y = rand(x)    local fileName = category.."_"..mySide.."_"..y..".wav"    --Spring.Echo("Playing "..fileName)    PlaySound(fileName)  else    --Spring.Echo("no sound found")  endendfunction rand(x)  return math.floor(math.random(1,x))endfunction CheckSelected()  local unitTable = GetSelectedUnits()  local isTurret  = false  local isFus     = false  local isSam     = false  local isCom     = false  for _, unitID in ipairs(unitTable) do    local unitDefID = GetUnitDefID(unitID)    if ((not UnitDefs[unitDefID].canMove) and        (UnitDefs[unitDefID].canAttack == 1)) then  --FIXME: canAttack broken?      isTurret =true    end    if ((UnitDefs[unitDefID].name == "corfus") or        (UnitDefs[unitDefID].name == "armfus") or        (UnitDefs[unitDefID].name == "cafus") or        (UnitDefs[unitDefID].name == "aafus")) then --FIXME: sea & cloak      isFus = true    end    if ((UnitDefs[unitDefID].name == "corrl") or        (UnitDefs[unitDefID].name == "armrl")) then --FIXME: more AA      isSam = true    end    if ((UnitDefs[unitDefID].name == "armcom") or        (UnitDefs[unitDefID].name == "corcom")) then      isCom = true    end      end  if (isTurret) then    Play("turret")  elseif (isFus) then    Play("selectfus")  elseif (isSam) then    Play("selectsam")  elseif (isCom) then    Play("selectcom")  elseif (#unitTable > 1) then    Play("selectgroup")  else    Play("select")  endendfunction IsStructure(unitID)  local unitDefID = GetUnitDefID(unitID)  if (UnitDefs[unitDefID].isBuilding or      UnitDefs[unitDefID].isFactory) then    return true  endendfunction CoolPlay(category, cooldownTime)  cooldownTime = cooldownTime or 0  local t = GetGameSeconds()  if ((not cooldown[category]) or      (t - cooldown[category] > cooldownTime)) then    Play(category)    cooldown[category] = t  endend----------------------------------------------------------------------------------------------------------------------------------------------------------------