Skip to content

Commit

Permalink
Clamp vehicle colors, better textdraw rendering and logic (to some ex…
Browse files Browse the repository at this point in the history
…tent), hide vehicle names when entering vehicles
  • Loading branch information
colistro123 committed Jul 1, 2020
1 parent 12f9de4 commit f5d01d2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 48 deletions.
104 changes: 69 additions & 35 deletions amx/client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -849,16 +849,16 @@ function hideTextDraw(textdraw)
end

function hudGetVerticalScale()
return 0.002232143
return 1.0 / 448.0
end

function hudGetHorizontalScale()
return 0.0015625
return 1.0 / 640.0
end

function initTextDraw(textdraw)
textdraw.id = textdraw.id or (#g_TextDraws + 1)
g_TextDraws[textdraw.id] = textdraw
textdraw.clientTDId = textdraw.clientTDId or (#g_TextDraws + 1)
g_TextDraws[textdraw.clientTDId] = textdraw

-- GTA replaces underscores with spaces
textdraw.text = textdraw.text:gsub("_", " ")
Expand Down Expand Up @@ -937,14 +937,13 @@ function initTextDraw(textdraw)
end

local textWidth = dxGetTextWidth(line:gsub('#%x%x%x%x%x%x', ''), scale, font)
textdraw.width = math.max(textdraw.width, textWidth)

if textdraw.align == 1 then
-- left
TDXPos = textdraw.x
TDXPos = textdraw.x-- - textWidth
elseif textdraw.align == 2 or not textdraw.align then
-- center
--outputConsole(string.format("Got centered text %d %d %s", TDXPos, TDYPos, textdraw.text))
TDXPos = 640/2 - textWidth/2
TDXPos = textdraw.x - textWidth / 2
elseif textdraw.align == 3 then
-- right
TDXPos = textdraw.x - textWidth
Expand Down Expand Up @@ -974,53 +973,55 @@ function renderTextDraws()
for id,textdraw in pairs(g_TextDraws) do
if textdraw.visible and textdraw.parts and not (textdraw.text:match('^%s*$')) then-- and not textdraw.usebox) then
local font = textDrawFonts[textdraw.font and textdraw.font >= 0 and textdraw.font <= #textDrawFonts and textdraw.font or 0]
if textdraw.upscalex == nil then
textdraw.upscalex = 1.0
end
if textdraw.upscaley == nil then
textdraw.upscaley = 1.0
end

textdraw.upscalex = textdraw.upscalex or 1.0
textdraw.upscaley = textdraw.upscaley or 1.0

textdraw.lheight = textdraw.lheight or 0.5
textdraw.lwidth = textdraw.lwidth or 0.5

local letterHeight = (textdraw.lheight * textdraw.upscaley or 0.25)
local letterWidth = (textdraw.lwidth * textdraw.upscalex or 0.5)

local vertHudScale = hudGetVerticalScale()
local horHudScale = hudGetHorizontalScale()

local scaley = SCREEN_SCALE_Y(screenHeight * vertHudScale * letterHeight * 0.175) --This should replicate what the game does
local scalex = SCREEN_SCALE_X(screenWidth * horHudScale * letterWidth * 0.35)
local scalex = screenWidth * horHudScale * letterWidth;
local scaley = screenHeight * vertHudScale * letterHeight * 0.5;

local sourceY = screenHeight - ((DEFAULT_SCREEN_HEIGHT - textdraw.y) * (screenHeight * vertHudScale))
local sourceX = screenWidth - ((DEFAULT_SCREEN_WIDTH - textdraw.x) * (screenWidth * horHudScale))

font = font.font
--Process box alignments
if textdraw.usebox then
if textdraw.usebox ~= nil and textdraw.usebox ~= 0 then
--outputConsole('textdraw uses box: ' .. textdraw.text)
local boxcolor = textdraw.boxcolor or tocolor(0, 0, 0, 120*(textdraw.alpha or 1))
local x, y, w, h
w = textdraw.width
if textdraw.align == 1 then --left
x = textdraw.x
x = sourceX
if textdraw.boxsize then
w = textdraw.boxsize[1]-- - x
else
w = textdraw.width
end
elseif textdraw.align == 2 then --centered
x = textdraw.x
x = sourceX-- / 2
if textdraw.boxsize then
w = textdraw.boxsize[1]
else
w = textdraw.width
end
elseif textdraw.align == 3 then --right
x = textdraw.x - w
x = sourceX - w
if textdraw.boxsize then
w = textdraw.x - textdraw.boxsize[1]
w = sourceX - textdraw.boxsize[1]
else
w = textdraw.width
end
end
y = textdraw.y
y = sourceY

--Calculates box height
if textdraw.boxsize and textdraw.text:match('^%s*$') then
Expand All @@ -1029,7 +1030,7 @@ function renderTextDraws()
h = textdraw.absheight
end

dxDrawRectangle(sourceX, sourceY, w * getAspectRatio(), h * getAspectRatio(), boxcolor)
dxDrawRectangle(x, y, w * getAspectRatio(), h * getAspectRatio(), boxcolor)
--outputConsole(string.format("Drawing textdraw box: sourceX: %f, sourceY: %f %s", sourceX, sourceY, textdraw.text))
end

Expand Down Expand Up @@ -1060,7 +1061,8 @@ function destroyTextDraw(textdraw)
return
end
hideTextDraw(textdraw)
table.removevalue(g_TextDraws, textdraw)
g_TextDraws[textdraw.clientTDId] = nil
--table.removevalue(g_TextDraws, textdraw)
end

local gameText = {}
Expand All @@ -1081,6 +1083,12 @@ function GameTextForPlayer(text, time, style)

destroyAllGameTextsWithStyle(style) --So same styles don't overlap

--[[
alignments
1 = left
2 = center
3 = right
]]
gameText[gIndex] = { text = text, font = 2 }
if style == 1 then
gameText[gIndex].x = 0.9 * 640
Expand Down Expand Up @@ -1176,7 +1184,7 @@ function Create3DTextLabel(id, textlabel)
textlabel.id = id
textlabel.enabled = false
g_TextLabels[id] = textlabel
outputConsole('Created text label with id ' .. textlabel.id)
--outputConsole('Created text label with id ' .. textlabel.id)
end

function Delete3DTextLabel(id)
Expand All @@ -1192,9 +1200,9 @@ function Attach3DTextLabel(textlabel)
end

function TextDrawCreate(id, textdraw)
textdraw.id = id
textdraw.clientTDId = id
textdraw.visible = false
--outputConsole('Got TextDrawCreate, textdraw.visible is ' .. textdraw.visible)
--outputConsole('Got TextDrawCreate, textdraw id ' .. id)

g_TextDraws[id] = textdraw
if textdraw.x then
Expand All @@ -1207,26 +1215,50 @@ function TextDrawCreate(id, textdraw)
initTextDraw(textdraw)
end

function findTextDrawHandleByID(id)
for _,textdraw in pairs(g_TextDraws) do
if textdraw.clientTDId == id then
return textdraw.clientTDId
end
end
return -1
end

function TextDrawDestroy(id)
destroyTextDraw(g_TextDraws[id])
local clientTDIdx = findTextDrawHandleByID(id)
if clientTDIdx ~= -1 then
destroyTextDraw(g_TextDraws[clientTDIdx])
end
end

function TextDrawHideForPlayer(id)
hideTextDraw(g_TextDraws[id])
local clientTDIdx = findTextDrawHandleByID(id)
if clientTDIdx ~= -1 then
hideTextDraw(g_TextDraws[clientTDIdx])
end
end

function TextDrawPropertyChanged(id, prop, newval, skipInit)
if g_TextDraws == nil then
outputConsole('Error: g_TextDraws is nil')
outputConsole('[TextDrawPropertyChanged] Error: g_TextDraws is nil')
return
end

local clientTDIdx = findTextDrawHandleByID(id)
if clientTDIdx == -1 then
outputConsole('[TextDrawPropertyChanged] Error: g_TextDraws couldn\'t find handle for id: ' .. id)
return
end

if g_TextDraws[id] == nil then
outputConsole('Error: g_TextDraws is nil at index: ' .. id)
if g_TextDraws[clientTDIdx] == nil then
outputConsole('[TextDrawPropertyChanged] Error: g_TextDraws is nil at index: ' .. clientTDIdx)
return
end

local textdraw = g_TextDraws[id]
local textdraw = g_TextDraws[clientTDIdx]

--outputConsole('[TextDrawPropertyChanged]: Received new property ' .. prop .. ' - ' .. newval .. ' for ' .. textdraw.text)

textdraw[prop] = newval
if prop == 'boxsize' then
textdraw.boxsize[1] = textdraw.boxsize[1]
Expand All @@ -1242,8 +1274,10 @@ end
function TextDrawShowForPlayer(id)
--outputConsole(string.format("TextDrawShowForPlayer trying to show textdraw with id %d", id))
--outputConsole(string.format("TextDrawShowForPlayer trying to show textdraw with text %s", g_TextDraws[id].text))

showTextDraw(g_TextDraws[id])
local clientTDIdx = findTextDrawHandleByID(id)
if clientTDIdx ~= -1 then
showTextDraw(g_TextDraws[clientTDIdx])
end
end

function displayFadingMessage(text, r, g, b, fadeInTime, stayTime, fadeOutTime)
Expand Down
1 change: 1 addition & 0 deletions amx/server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function gameModeInit(player)
local r, g, b = math.random(50, 255), math.random(50, 255), math.random(50, 255)
ShowPlayerMarker(false, player, g_ShowPlayerMarkers)
setPlayerHudComponentVisible(player, 'area_name', g_ShowZoneNames)
setPlayerHudComponentVisible(player, 'vehicle_name', false) --Samp doesn't show vehicle names when entering vehicles
SetPlayerColor(false, player, r, g, b)
setElementData(player, 'Score', 0)
toggleAllControls(player, false, true, false)
Expand Down
6 changes: 4 additions & 2 deletions amx/server/natives/a_players.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function CreatePlayerTextDraw(amx, player, x, y, text)
local serverTDId = #g_PlayerTextDraws[player]+1
local clientTDId = #g_TextDraws + serverTDId

local textdraw = { x = x, y = y, lwidth=0.5, lheight = 0.5, shadow = { visible=0, align=1, text=text, font=1, lwidth=0.5, lheight = 0.5} }
local textdraw = { x = x, y = y, lwidth=0.5, lheight = 0.5, shadow = { visible=0, align=1, text=text, font=1, lwidth=0.5, lheight = 0.5 } }
textdraw.clientTDId = clientTDId
textdraw.serverTDId = serverTDId
textdraw.visible = 0
Expand All @@ -372,7 +372,7 @@ function CreatePlayerTextDraw(amx, player, x, y, text)
end
if different then
--table.dump(v, 1, nil) --Dump the data
--outputDebugString(string.format('A property changed for %s string: %s visibility is %d', textdraw.serverTDId, textdraw.text, textdraw.visible))
--outputDebugString(string.format('A property changed for %d string: %s', textdraw.clientTDId, textdraw.text))
clientCall(player, 'TextDrawPropertyChanged', textdraw.clientTDId, k, v)
t.shadow[k] = v
end
Expand All @@ -389,6 +389,7 @@ function PlayerTextDrawDestroy(amx, player, textdrawID)
if not IsPlayerTextDrawValid(player, textdrawID) then
return false
end
outputDebugString('Sending textdraw id s->' .. g_PlayerTextDraws[player][textdrawID].serverTDId .. ' c->' .. g_PlayerTextDraws[player][textdrawID].clientTDId .. ' for destruction')
clientCall(player, 'TextDrawDestroy', g_PlayerTextDraws[player][textdrawID].clientTDId)
g_PlayerTextDraws[player][textdrawID] = nil
end
Expand Down Expand Up @@ -428,6 +429,7 @@ end

function PlayerTextDrawUseBox(amx, player, textdrawID, usebox)
if not IsPlayerTextDrawValid(player, textdrawID) then
outputDebugString('textdraw is invalid, not setting usebox ' .. textdrawID)
return false
end
g_PlayerTextDraws[player][textdrawID].usebox = usebox
Expand Down
17 changes: 8 additions & 9 deletions amx/server/natives/a_samp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,7 @@ function AddStaticVehicleEx(amx, model, x, y, z, angle, color1, color2, respawnD
end

if not g_PoliceVehicles[model] then
if(color1 <= 0 and color1 >= 126) then color1 = math.random(1, 126) end
if(color2 <= 0 and color2 >= 126) then color2 = math.random(1, 126) end

-- WARNING: [amx]\amx\server\natives\a_samp.lua:311: Expected positive value, got negative. This warning may be an error in future versions.
setVehicleColor(vehicle, color1, color2, 0, 0)
setVehicleColorClamped(vehicle, color1, color2)
end
local vehID = addElem(g_Vehicles, vehicle)
if respawnDelay < 0 then
Expand Down Expand Up @@ -615,7 +611,9 @@ end
function TextDrawCreate(amx, x, y, text)
outputDebugString('TextDrawCreate called with args ' .. x .. ' ' .. y .. ' ' .. text)
local textdraw = { x = x, y = y, shadow = {align=1, text=text, font=1, lwidth=0.5, lheight = 0.5} }
textdraw.clientTDId = #g_TextDraws + 1
local id = table.insert(g_TextDraws, textdraw)

setmetatable(
textdraw,
{
Expand All @@ -632,7 +630,8 @@ function TextDrawCreate(amx, x, y, text)
end
end
if different then
clientCall(root, 'TextDrawPropertyChanged', id, k, v)
--outputDebugString(string.format('A property changed for %s string: %s', textdraw.clientTDId, textdraw.text))
clientCall(root, 'TextDrawPropertyChanged', textdraw.clientTDId, k, v)
t.shadow[k] = v
end
end
Expand Down Expand Up @@ -671,7 +670,7 @@ function TextDrawDestroy(amx, textdrawID)
if not g_TextDraws[textdrawID] then
return
end
clientCall(root, 'TextDrawDestroy', textdrawID)
clientCall(root, 'TextDrawDestroy', g_TextDraws[textdrawID].clientTDId)
g_TextDraws[textdrawID] = nil
end

Expand Down Expand Up @@ -727,15 +726,15 @@ function TextDrawShowForPlayer(amx, player, textdrawID)
if not textdraw then
return
end
clientCall(player, 'TextDrawShowForPlayer', textdrawID)
clientCall(player, 'TextDrawShowForPlayer', textdraw.clientTDId)
end

function TextDrawHideForPlayer(amx, player, textdrawID)
local textdraw = g_TextDraws[textdrawID]
if not textdraw then
return
end
clientCall(player, 'TextDrawHideForPlayer', textdrawID)
clientCall(player, 'TextDrawHideForPlayer', textdraw.clientTDId)
end

function TextDrawShowForAll(amx, textdrawID)
Expand Down
11 changes: 10 additions & 1 deletion amx/server/natives/a_vehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,16 @@ function RemoveVehicleComponent(amx, vehicle, upgradeID)
end

function ChangeVehicleColor(amx, vehicle, color1, color2)
setVehicleColor(vehicle, color1, color2, 0, 0)
setVehicleColorClamped(vehicle, color1, color2)
end

function setVehicleColorClamped(vehicle, color1, color2)
--This is to prevent negative color behavior, keep the original color if they sent -1 (I believe this is what samp does)
if color1 ~= -1 and color2 ~= -1 then
color1 = clamp(color1, 0, 126)
color2 = clamp(color2, 0, 126)
setVehicleColor(vehicle, color1, color2, 0, 0)
end
end

function ChangeVehiclePaintjob(amx, vehicle, paintjob)
Expand Down
2 changes: 1 addition & 1 deletion amx/server/syscalls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ g_SAMPSyscallPrototypes = {
TextDrawShowForAll = {'i'},
TextDrawShowForPlayer = {'p', 'i'},
TextDrawTextSize = {'x', 'f', 'f'},
TextDrawUseBox = {'x', 'b'},
TextDrawUseBox = {'x', 'i'},
--Player textdraws
PlayerTextDrawDestroy = {'p', 'i'},
PlayerTextDrawShow = {'p', 'i'},
Expand Down
5 changes: 5 additions & 0 deletions amx/server/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ function setBotState(bot, state)
procCallOnAll('OnBotStateChange', botID, state, oldState)
end

-- Clamping values
function clamp(n, min, max)
return math.max(min, math.min(max, n))
end

-- Table extensions

local _table_insert = table.insert
Expand Down

0 comments on commit f5d01d2

Please sign in to comment.