Skip to content

Commit

Permalink
Merge pull request #1360 from Courseplay/DisplayesMissingInfoTexts
Browse files Browse the repository at this point in the history
Only shows cp info texts as blinking message and added out of money e…
  • Loading branch information
Tensuko committed Apr 11, 2022
2 parents a46c23a + dbadd9b commit 5050afc
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 94 deletions.
24 changes: 16 additions & 8 deletions config/InfoTexts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
name: The name for the info text to call it in the lua scripts.
text: The button text shown.
optional:
hasFinished: Called when the driver has finished.
event: Event called, when the info text is activated.
aiMessageClass: string reference to a giants ai message.
The priority is from top to bottom, which makes the first the highest priority in the hud.
TODO: AIMessageErrorBlockedByObject
-->

<InfoTexts prefix="CP_infoTexts_">
<InfoText name="IS_STUCK" text="isStuck"/>
<InfoText name="TRAFFIC" text="isInTraffic"/>
<InfoText name="IS_STUCK" text="isStuck"/>
<InfoText name="TRAFFIC" text="isInTraffic"/>

<InfoText name="FUEL_IS_LOW" text="shouldRefuel"/>
<InfoText name="FUEL_IS_EMPTY" text="needsFuel"/>
<InfoText name="IS_COMPLETELY_BROKEN" text="needsRepair"/>
<InfoText name="NEEDS_FILLING" text="needsFilling"/>
<InfoText name="NEEDS_UNLOADING" text="needsUnloading"/>
<InfoText name="WORK_FINISHED" text="workFinished"/>
<InfoText name="OUT_OF_MONEY" text="outOfMoney" class="AIMessageErrorOutOfMoney"/>
<InfoText name="FUEL_IS_EMPTY" text="needsFuel" hasFinished="true" event="onCpFuelEmpty" class="AIMessageErrorOutOfFuel"/>
<InfoText name="IS_COMPLETELY_BROKEN" text="needsRepair" hasFinished="true" event="onCpBroken" class="AIMessageErrorVehicleBroken"/>
<InfoText name="NEEDS_FILLING" text="needsFilling" hasFinished="true" event="onCpEmpty" class="AIMessageErrorOutOfFill"/>
<InfoText name="NEEDS_UNLOADING" text="needsUnloading" hasFinished="true" event="onCpFull" class="AIMessageErrorIsFull"/>
<InfoText name="FUEL_IS_LOW" text="shouldRefuel"/>
<InfoText name="WORK_FINISHED" text="workFinished" hasFinished="true" event="onCpFinished" class="AIMessageSuccessFinishedJob"/>
</InfoTexts>
100 changes: 84 additions & 16 deletions scripts/ai/InfoTextsManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,53 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]


---@class CpInfoTextElement
CpInfoTextElement = CpObject()
function CpInfoTextElement:init(name,text,id)

CpInfoTextElement.aiMessageNameToClass = {
AIMessageErrorOutOfFill = AIMessageErrorOutOfFill,
AIMessageErrorIsFull = AIMessageErrorIsFull,
AIMessageSuccessFinishedJob = AIMessageSuccessFinishedJob,
AIMessageErrorOutOfFuel = AIMessageErrorOutOfFuel,
AIMessageErrorVehicleBroken = AIMessageErrorVehicleBroken,
AIMessageErrorOutOfMoney = AIMessageErrorOutOfMoney,
AIMessageErrorBlockedByObject = AIMessageErrorBlockedByObject
}

--- Info text
---@param name string name called by in the lua code.
---@param text string displayed text
---@param id number unique id for mp
---@param hasFinished boolean is true, when the driver finished.
---@param event string event called when the info text was activated.
---@param aiMessageClass string reference to a giants ai message.
function CpInfoTextElement:init(name, text, id, hasFinished, event, aiMessageClass)
self.name = name
self.text = text
self.id = id
self.hasFinished = hasFinished
self.event = event
if aiMessageClass then
self.aiMessageClass = CpInfoTextElement.aiMessageNameToClass[aiMessageClass]
end
end

function CpInfoTextElement:__tostring()
return string.format("name: %s, text: %s",self.name,self.text)
return string.format("name: %s, text: %s, hasFinished: %s, event: %s, hasClass: %s",
self.name, self.text, tostring(self.hasFinished), tostring(self.event), tostring(self.aiMessageClass))
end

--- Checks if the given message is assigned to this info text.
function CpInfoTextElement:isAssignedToAIMessage(message)
return self.aiMessageClass and message:isa(self.aiMessageClass)
end

function CpInfoTextElement:getData()
return self.hasFinished, self.event
end

function CpInfoTextElement:getText()
return self.text
end

---Loads the possible info texts and combines the active ones from every vehicle here.
Expand All @@ -45,33 +82,42 @@ end

function InfoTextManager:registerXmlSchema()
self.xmlSchema = XMLSchema.new("infoTexts")
self.xmlSchema:register(XMLValueType.STRING,self.baseXmlKey.."#prefix","Info text prefix.")
self.xmlSchema:register(XMLValueType.STRING,self.xmlKey.."(?)#name","Info text name for the lua code.")
self.xmlSchema:register(XMLValueType.STRING,self.xmlKey.."(?)#text","Info text displayed.")
self.xmlSchema:register(XMLValueType.STRING, self.baseXmlKey.."#prefix", "Info text prefix.")
self.xmlSchema:register(XMLValueType.STRING, self.xmlKey.."(?)#name", "Info text name for the lua code.")
self.xmlSchema:register(XMLValueType.STRING, self.xmlKey.."(?)#text", "Info text displayed.")
self.xmlSchema:register(XMLValueType.BOOL, self.xmlKey.."(?)#hasFinished", "Is folding of implements allowed?")
self.xmlSchema:register(XMLValueType.STRING, self.xmlKey.."(?)#event", "Event to call with the message.")
self.xmlSchema:register(XMLValueType.STRING, self.xmlKey.."(?)#class", "AI message class.")
end

--- Load the info text xml File.
function InfoTextManager:loadFromXml()
self.xmlFileName = Utils.getFilename('config/InfoTexts.xml', Courseplay.BASE_DIRECTORY)

local xmlFile = XMLFile.loadIfExists("InfoTextsXmlFile",self.xmlFileName,self.xmlSchema)
local xmlFile = XMLFile.loadIfExists("InfoTextsXmlFile", self.xmlFileName, self.xmlSchema)
if xmlFile then
local name, text, id
local prefix = xmlFile:getValue(self.baseXmlKey.."#prefix","")
local name, text, id, aiMessageClass, event, hasFinished
local prefix = xmlFile:getValue(self.baseXmlKey.."#prefix", "")
xmlFile:iterate(self.xmlKey, function (ix, key)
name = xmlFile:getValue(key .. "#name")
text = xmlFile:getValue(key .. "#text")
text = g_i18n:getText(prefix..text)
id = bitShiftLeft(1, ix-1)
InfoTextManager[name] = CpInfoTextElement(name, text, id)
self.infoTextsById[id] = InfoTextManager[name]
table.insert(self.infoTexts,InfoTextManager[name])

hasFinished = xmlFile:getValue(key .. "#hasFinished", false)
event = xmlFile:getValue(key .. "#event")
aiMessageClass = xmlFile:getValue(key .. "#class")

InfoTextManager[name] = CpInfoTextElement(name, text, id,
hasFinished, event, aiMessageClass)
self.infoTextsById[id] = InfoTextManager[name]
table.insert(self.infoTexts, InfoTextManager[name])
end)
xmlFile:delete()
end
end

function InfoTextManager:registerVehicle(v,id)
function InfoTextManager:registerVehicle(v, id)
self.vehicles[id] = v
end

Expand All @@ -95,8 +141,8 @@ function InfoTextManager:getActiveInfoTexts()
local infos = {}
local i = self.numActiveTexts
local validInfoText
for _,infoText in ipairs(self.infoTexts) do
for _,vehicle in pairs(self.vehicles) do
for _, infoText in ipairs(self.infoTexts) do
for _, vehicle in pairs(self.vehicles) do
if g_currentMission.accessHandler:canPlayerAccess(vehicle) then
--- dev functionally for testing.
if self.numActiveTexts > 0 then
Expand All @@ -114,13 +160,35 @@ function InfoTextManager:getActiveInfoTexts()
text = infoText.text,
vehicle = vehicle
}
table.insert(infos,info)
table.insert(infos, info)
end
end
end
end
return infos
end

function InfoTextManager:getInfoTextByAIMessage(message)
for i, infoText in pairs(self.infoTexts) do
if infoText:isAssignedToAIMessage(message)then
return infoText
end
end
end

--- Gets the info text and the additional data by the ai message.
---@param message AIMessage
---@return CpInfoTextElement Info text
---@return boolean Has the driver finished?
---@return string Event to call with the info text.
function InfoTextManager:getInfoTextDataByAIMessage(message)
local infoText = self:getInfoTextByAIMessage(message)
if infoText then
local hasFinished, event = infoText:getData()
return infoText, hasFinished, event
end
end

--- Dev test function, to simulate the info texts.
InfoTextManager.numActiveTexts = -1
function InfoTextManager:changeNumActiveTexts()
Expand Down
14 changes: 5 additions & 9 deletions scripts/ai/jobs/CpAIJob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,12 @@ function CpAIJob:showNotification(aiMessage)
CpAIJob:superClass().showNotification(self, aiMessage)
return
end
local hasFinished, releaseMessage, event = CpAIWorker.getMessageData(aiMessage)
local releaseMessage, hasFinished, event = g_infoTextManager:getInfoTextDataByAIMessage(aiMessage)
local vehicle = self:getVehicle()
if vehicle:getIsEntered() then
--- Makes sure the message is shown, when a player is in the vehicle.
g_currentMission:showBlinkingWarning(string.format(aiMessage:getMessage(), self:getHelperName(), aiMessage:getMessageArguments()), 5000)
elseif releaseMessage == nil then
--- Makes sure message that are not handled by the info texts are shown.
CpAIJob:superClass().showNotification(self, aiMessage)
end

--- Makes sure the message is shown, when a player is in the vehicle.
if releaseMessage and vehicle:getIsEntered() then
g_currentMission:showBlinkingWarning(releaseMessage:getText(), 5000)
end
end

--- Automatically repairs the vehicle, depending on the auto repair setting.
Expand Down
48 changes: 1 addition & 47 deletions scripts/specializations/CpAIWorker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,6 @@ CpAIWorker.NAME = ".cpAIWorker"
CpAIWorker.SPEC_NAME = CpAIWorker.MOD_NAME .. CpAIWorker.NAME
CpAIWorker.KEY = "."..CpAIWorker.MOD_NAME..CpAIWorker.NAME .. "."

--- TODO: this table can probably be moved into the InfoTexts.xml :)
CpAIWorker.messages = {
{
class = AIMessageErrorOutOfFill,
hasFinished = true,
releaseMessage = g_infoTextManager.NEEDS_FILLING,
event = "onCpEmpty"
},
{
class = AIMessageErrorIsFull,
hasFinished = true,
releaseMessage = g_infoTextManager.NEEDS_UNLOADING,
event = "onCpFull"
},
{
class = AIMessageSuccessFinishedJob,
hasFinished = true,
releaseMessage = g_infoTextManager.WORK_FINISHED,
event = "onCpFinished"
},
{
class = AIMessageErrorOutOfFuel,
hasFinished = true,
releaseMessage = g_infoTextManager.FUEL_IS_EMPTY,
event = "onCpFuelEmpty"
},
{
class = AIMessageErrorVehicleBroken,
hasFinished = true,
releaseMessage = g_infoTextManager.IS_COMPLETELY_BROKEN,
event = "onCpBroken"
},
}

function CpAIWorker.initSpecialization()
local schema = Vehicle.xmlSchemaSavegame
end
Expand Down Expand Up @@ -139,18 +105,6 @@ function CpAIWorker:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSel
CpAIWorker.updateActionEvents(self)
end

function CpAIWorker.getMessageData(message)
local hasFinished, releaseMessage, event
for _, data in pairs(CpAIWorker.messages) do
if message:isa(data.class) then
hasFinished = data.hasFinished
releaseMessage = data.releaseMessage
event = data.event
end
end
return hasFinished, releaseMessage, event
end


--- Used to enable/disable release of the helper
--- and handles post release functionality with for example auto drive.
Expand All @@ -162,7 +116,7 @@ function CpAIWorker:stopCurrentAIJob(superFunc, message, ...)
CpUtil.infoVehicle(self, "no stop message was given.")
return superFunc(self, message, ...)
end
local hasFinished, releaseMessage, event = CpAIWorker.getMessageData(message)
local releaseMessage, hasFinished, event = g_infoTextManager:getInfoTextDataByAIMessage(message)

CpUtil.debugVehicle(CpDebug.DBG_FIELDWORK, self, "finished: %s, event: %s",
tostring(hasFinished), tostring(event))
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_br.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@
<text name="CP_infoTexts_needsFilling" text="Precisa ser recarregado"/>
<text name="CP_infoTexts_needsUnloading" text="Precisa ser descarregado"/>
<text name="CP_infoTexts_workFinished" text="Trabalho feito"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Deseja salvar a rota gravado como campo customizado %s?"/>
<text name="CP_customFieldManager_confirm_delete" text="Deseja excluir o campo customizado %s?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_cs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@
<text name="CP_infoTexts_needsFilling" text="需要重新填充"/>
<text name="CP_infoTexts_needsUnloading" text="需要卸载"/>
<text name="CP_infoTexts_workFinished" text="完成工作"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="是否要将录制的任务保存为自定义田地 %s?"/>
<text name="CP_customFieldManager_confirm_delete" text="要删除自定义田地吗 %s?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_ct.xml
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@
<text name="CP_infoTexts_needsFilling" text="Needs to be refilled"/>
<text name="CP_infoTexts_needsUnloading" text="Needs to be unloaded"/>
<text name="CP_infoTexts_workFinished" text="Finished work"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Do you want to save the recorded course as custom field %s?"/>
<text name="CP_customFieldManager_confirm_delete" text="Do you want to delete the custom field %s?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_cz.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@
<text name="CP_infoTexts_needsFilling" text="Je třeba doplnit"/>
<text name="CP_infoTexts_needsUnloading" text="Je třeba vyložit"/>
<text name="CP_infoTexts_workFinished" text="Dokončil práci"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Chcete uložit zaznamenaný kurz jako vlastní pole %s?"/>
<text name="CP_customFieldManager_confirm_delete" text="Chcete odstranit vlastní pole %s?"/>
Expand Down
1 change: 1 addition & 0 deletions translations/translation_da.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
<text name="CP_infoTexts_needsFilling" text="Needs to be refilled"/>
<text name="CP_infoTexts_needsUnloading" text="Needs to be unloaded"/>
<text name="CP_infoTexts_workFinished" text="Finished work"/>
<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Ønsker du at gemme den optagnede rute som en brugerdefineret mark %s?"/>
Expand Down
1 change: 1 addition & 0 deletions translations/translation_de.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
<text name="CP_infoTexts_needsFilling" text="Muss aufgefüllt werden"/>
<text name="CP_infoTexts_needsUnloading" text="Muss entladen werden"/>
<text name="CP_infoTexts_workFinished" text="Arbeit beendet"/>
<text name="CP_infoTexts_outOfMoney" text="Kein Geld mehr"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Möchtest du diesen benutzerdefinierten Randkurs als %s speichern?"/>
Expand Down
1 change: 1 addition & 0 deletions translations/translation_en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@
<text name="CP_infoTexts_needsFilling" text="Needs to be refilled"/>
<text name="CP_infoTexts_needsUnloading" text="Needs to be unloaded"/>
<text name="CP_infoTexts_workFinished" text="Finished work"/>
<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Do you want to save the recorded course as custom field %s?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_es.xml
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@
<text name="CP_infoTexts_needsFilling" text="Necesita ser rellenado"/>
<text name="CP_infoTexts_needsUnloading" text="Necesita ser descargado"/>
<text name="CP_infoTexts_workFinished" text="Trabajo finalizado"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="¿Quieres guardar la ruta como campo personalizado %s?"/>
<text name="CP_customFieldManager_confirm_delete" text="¿Quieres eliminar el campo personalizado %s?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_fr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@
<text name="CP_infoTexts_needsFilling" text="doit être réapprovisionné"/>
<text name="CP_infoTexts_needsUnloading" text="doit être déchargé"/>
<text name="CP_infoTexts_workFinished" text="a terminé son travail"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Souhaitez-vous sauvegarder la course enregistrée comme contour de champ personnalisé %s?"/>
<text name="CP_customFieldManager_confirm_delete" text="Souhaitez-vous supprimer le champ personnalisé %s?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_hu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@
<text name="CP_infoTexts_needsFilling" text="fel kell tölteni"/>
<text name="CP_infoTexts_needsUnloading" text="üríteni kell"/>
<text name="CP_infoTexts_workFinished" text="készen van"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="El akarod menteni az útvonalat szántóföldként %s néven?"/>
<text name="CP_customFieldManager_confirm_delete" text="Törlöd a %s szántóföldet?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_it.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@
<text name="CP_infoTexts_needsFilling" text="Ha bisogno di essere caricato"/>
<text name="CP_infoTexts_needsUnloading" text="Ha bisogno di essere scaricato"/>
<text name="CP_infoTexts_workFinished" text="Ha finito il lavoro"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="Vuoi salvare il percorso registrato come campo personalizzato %s?"/>
<text name="CP_customFieldManager_confirm_delete" text="Vuoi eliminare il campo personalizzato %s?"/>
Expand Down
3 changes: 2 additions & 1 deletion translations/translation_jp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@
<text name="CP_infoTexts_needsFilling" text="Needs to be refilled"/>
<text name="CP_infoTexts_needsUnloading" text="Needs to be unloaded"/>
<text name="CP_infoTexts_workFinished" text="Finished work"/>

<text name="CP_infoTexts_outOfMoney" text="Ouf of money"/>

<!--Custom Field-->
<text name="CP_customFieldManager_confirm_save" text="カスタムコース「%s」を保存しますか?"/>
<text name="CP_customFieldManager_confirm_delete" text="カスタムフィールド「%s」を消去しますか?"/>
Expand Down
Loading

0 comments on commit 5050afc

Please sign in to comment.