Skip to content

Commit f5d01d2

Browse files
committedJul 1, 2020
Clamp vehicle colors, better textdraw rendering and logic (to some extent), hide vehicle names when entering vehicles
1 parent 12f9de4 commit f5d01d2

File tree

7 files changed

+98
-48
lines changed

7 files changed

+98
-48
lines changed
 

‎amx/client/client.lua

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -849,16 +849,16 @@ function hideTextDraw(textdraw)
849849
end
850850

851851
function hudGetVerticalScale()
852-
return 0.002232143
852+
return 1.0 / 448.0
853853
end
854854

855855
function hudGetHorizontalScale()
856-
return 0.0015625
856+
return 1.0 / 640.0
857857
end
858858

859859
function initTextDraw(textdraw)
860-
textdraw.id = textdraw.id or (#g_TextDraws + 1)
861-
g_TextDraws[textdraw.id] = textdraw
860+
textdraw.clientTDId = textdraw.clientTDId or (#g_TextDraws + 1)
861+
g_TextDraws[textdraw.clientTDId] = textdraw
862862

863863
-- GTA replaces underscores with spaces
864864
textdraw.text = textdraw.text:gsub("_", " ")
@@ -937,14 +937,13 @@ function initTextDraw(textdraw)
937937
end
938938

939939
local textWidth = dxGetTextWidth(line:gsub('#%x%x%x%x%x%x', ''), scale, font)
940-
textdraw.width = math.max(textdraw.width, textWidth)
940+
941941
if textdraw.align == 1 then
942942
-- left
943-
TDXPos = textdraw.x
943+
TDXPos = textdraw.x-- - textWidth
944944
elseif textdraw.align == 2 or not textdraw.align then
945945
-- center
946-
--outputConsole(string.format("Got centered text %d %d %s", TDXPos, TDYPos, textdraw.text))
947-
TDXPos = 640/2 - textWidth/2
946+
TDXPos = textdraw.x - textWidth / 2
948947
elseif textdraw.align == 3 then
949948
-- right
950949
TDXPos = textdraw.x - textWidth
@@ -974,53 +973,55 @@ function renderTextDraws()
974973
for id,textdraw in pairs(g_TextDraws) do
975974
if textdraw.visible and textdraw.parts and not (textdraw.text:match('^%s*$')) then-- and not textdraw.usebox) then
976975
local font = textDrawFonts[textdraw.font and textdraw.font >= 0 and textdraw.font <= #textDrawFonts and textdraw.font or 0]
977-
if textdraw.upscalex == nil then
978-
textdraw.upscalex = 1.0
979-
end
980-
if textdraw.upscaley == nil then
981-
textdraw.upscaley = 1.0
982-
end
976+
977+
textdraw.upscalex = textdraw.upscalex or 1.0
978+
textdraw.upscaley = textdraw.upscaley or 1.0
979+
980+
textdraw.lheight = textdraw.lheight or 0.5
981+
textdraw.lwidth = textdraw.lwidth or 0.5
983982

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

987986
local vertHudScale = hudGetVerticalScale()
988987
local horHudScale = hudGetHorizontalScale()
989988

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

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

996995
font = font.font
997996
--Process box alignments
998-
if textdraw.usebox then
997+
if textdraw.usebox ~= nil and textdraw.usebox ~= 0 then
998+
--outputConsole('textdraw uses box: ' .. textdraw.text)
999999
local boxcolor = textdraw.boxcolor or tocolor(0, 0, 0, 120*(textdraw.alpha or 1))
10001000
local x, y, w, h
1001+
w = textdraw.width
10011002
if textdraw.align == 1 then --left
1002-
x = textdraw.x
1003+
x = sourceX
10031004
if textdraw.boxsize then
10041005
w = textdraw.boxsize[1]-- - x
10051006
else
10061007
w = textdraw.width
10071008
end
10081009
elseif textdraw.align == 2 then --centered
1009-
x = textdraw.x
1010+
x = sourceX-- / 2
10101011
if textdraw.boxsize then
10111012
w = textdraw.boxsize[1]
10121013
else
10131014
w = textdraw.width
10141015
end
10151016
elseif textdraw.align == 3 then --right
1016-
x = textdraw.x - w
1017+
x = sourceX - w
10171018
if textdraw.boxsize then
1018-
w = textdraw.x - textdraw.boxsize[1]
1019+
w = sourceX - textdraw.boxsize[1]
10191020
else
10201021
w = textdraw.width
10211022
end
10221023
end
1023-
y = textdraw.y
1024+
y = sourceY
10241025

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

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

@@ -1060,7 +1061,8 @@ function destroyTextDraw(textdraw)
10601061
return
10611062
end
10621063
hideTextDraw(textdraw)
1063-
table.removevalue(g_TextDraws, textdraw)
1064+
g_TextDraws[textdraw.clientTDId] = nil
1065+
--table.removevalue(g_TextDraws, textdraw)
10641066
end
10651067

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

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

1086+
--[[
1087+
alignments
1088+
1 = left
1089+
2 = center
1090+
3 = right
1091+
]]
10841092
gameText[gIndex] = { text = text, font = 2 }
10851093
if style == 1 then
10861094
gameText[gIndex].x = 0.9 * 640
@@ -1176,7 +1184,7 @@ function Create3DTextLabel(id, textlabel)
11761184
textlabel.id = id
11771185
textlabel.enabled = false
11781186
g_TextLabels[id] = textlabel
1179-
outputConsole('Created text label with id ' .. textlabel.id)
1187+
--outputConsole('Created text label with id ' .. textlabel.id)
11801188
end
11811189

11821190
function Delete3DTextLabel(id)
@@ -1192,9 +1200,9 @@ function Attach3DTextLabel(textlabel)
11921200
end
11931201

11941202
function TextDrawCreate(id, textdraw)
1195-
textdraw.id = id
1203+
textdraw.clientTDId = id
11961204
textdraw.visible = false
1197-
--outputConsole('Got TextDrawCreate, textdraw.visible is ' .. textdraw.visible)
1205+
--outputConsole('Got TextDrawCreate, textdraw id ' .. id)
11981206

11991207
g_TextDraws[id] = textdraw
12001208
if textdraw.x then
@@ -1207,26 +1215,50 @@ function TextDrawCreate(id, textdraw)
12071215
initTextDraw(textdraw)
12081216
end
12091217

1218+
function findTextDrawHandleByID(id)
1219+
for _,textdraw in pairs(g_TextDraws) do
1220+
if textdraw.clientTDId == id then
1221+
return textdraw.clientTDId
1222+
end
1223+
end
1224+
return -1
1225+
end
1226+
12101227
function TextDrawDestroy(id)
1211-
destroyTextDraw(g_TextDraws[id])
1228+
local clientTDIdx = findTextDrawHandleByID(id)
1229+
if clientTDIdx ~= -1 then
1230+
destroyTextDraw(g_TextDraws[clientTDIdx])
1231+
end
12121232
end
12131233

12141234
function TextDrawHideForPlayer(id)
1215-
hideTextDraw(g_TextDraws[id])
1235+
local clientTDIdx = findTextDrawHandleByID(id)
1236+
if clientTDIdx ~= -1 then
1237+
hideTextDraw(g_TextDraws[clientTDIdx])
1238+
end
12161239
end
12171240

12181241
function TextDrawPropertyChanged(id, prop, newval, skipInit)
12191242
if g_TextDraws == nil then
1220-
outputConsole('Error: g_TextDraws is nil')
1243+
outputConsole('[TextDrawPropertyChanged] Error: g_TextDraws is nil')
1244+
return
1245+
end
1246+
1247+
local clientTDIdx = findTextDrawHandleByID(id)
1248+
if clientTDIdx == -1 then
1249+
outputConsole('[TextDrawPropertyChanged] Error: g_TextDraws couldn\'t find handle for id: ' .. id)
12211250
return
12221251
end
12231252

1224-
if g_TextDraws[id] == nil then
1225-
outputConsole('Error: g_TextDraws is nil at index: ' .. id)
1253+
if g_TextDraws[clientTDIdx] == nil then
1254+
outputConsole('[TextDrawPropertyChanged] Error: g_TextDraws is nil at index: ' .. clientTDIdx)
12261255
return
12271256
end
12281257

1229-
local textdraw = g_TextDraws[id]
1258+
local textdraw = g_TextDraws[clientTDIdx]
1259+
1260+
--outputConsole('[TextDrawPropertyChanged]: Received new property ' .. prop .. ' - ' .. newval .. ' for ' .. textdraw.text)
1261+
12301262
textdraw[prop] = newval
12311263
if prop == 'boxsize' then
12321264
textdraw.boxsize[1] = textdraw.boxsize[1]
@@ -1242,8 +1274,10 @@ end
12421274
function TextDrawShowForPlayer(id)
12431275
--outputConsole(string.format("TextDrawShowForPlayer trying to show textdraw with id %d", id))
12441276
--outputConsole(string.format("TextDrawShowForPlayer trying to show textdraw with text %s", g_TextDraws[id].text))
1245-
1246-
showTextDraw(g_TextDraws[id])
1277+
local clientTDIdx = findTextDrawHandleByID(id)
1278+
if clientTDIdx ~= -1 then
1279+
showTextDraw(g_TextDraws[clientTDIdx])
1280+
end
12471281
end
12481282

12491283
function displayFadingMessage(text, r, g, b, fadeInTime, stayTime, fadeOutTime)

‎amx/server/events.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function gameModeInit(player)
1717
local r, g, b = math.random(50, 255), math.random(50, 255), math.random(50, 255)
1818
ShowPlayerMarker(false, player, g_ShowPlayerMarkers)
1919
setPlayerHudComponentVisible(player, 'area_name', g_ShowZoneNames)
20+
setPlayerHudComponentVisible(player, 'vehicle_name', false) --Samp doesn't show vehicle names when entering vehicles
2021
SetPlayerColor(false, player, r, g, b)
2122
setElementData(player, 'Score', 0)
2223
toggleAllControls(player, false, true, false)

‎amx/server/natives/a_players.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function CreatePlayerTextDraw(amx, player, x, y, text)
348348
local serverTDId = #g_PlayerTextDraws[player]+1
349349
local clientTDId = #g_TextDraws + serverTDId
350350

351-
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} }
351+
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 } }
352352
textdraw.clientTDId = clientTDId
353353
textdraw.serverTDId = serverTDId
354354
textdraw.visible = 0
@@ -372,7 +372,7 @@ function CreatePlayerTextDraw(amx, player, x, y, text)
372372
end
373373
if different then
374374
--table.dump(v, 1, nil) --Dump the data
375-
--outputDebugString(string.format('A property changed for %s string: %s visibility is %d', textdraw.serverTDId, textdraw.text, textdraw.visible))
375+
--outputDebugString(string.format('A property changed for %d string: %s', textdraw.clientTDId, textdraw.text))
376376
clientCall(player, 'TextDrawPropertyChanged', textdraw.clientTDId, k, v)
377377
t.shadow[k] = v
378378
end
@@ -389,6 +389,7 @@ function PlayerTextDrawDestroy(amx, player, textdrawID)
389389
if not IsPlayerTextDrawValid(player, textdrawID) then
390390
return false
391391
end
392+
outputDebugString('Sending textdraw id s->' .. g_PlayerTextDraws[player][textdrawID].serverTDId .. ' c->' .. g_PlayerTextDraws[player][textdrawID].clientTDId .. ' for destruction')
392393
clientCall(player, 'TextDrawDestroy', g_PlayerTextDraws[player][textdrawID].clientTDId)
393394
g_PlayerTextDraws[player][textdrawID] = nil
394395
end
@@ -428,6 +429,7 @@ end
428429

429430
function PlayerTextDrawUseBox(amx, player, textdrawID, usebox)
430431
if not IsPlayerTextDrawValid(player, textdrawID) then
432+
outputDebugString('textdraw is invalid, not setting usebox ' .. textdrawID)
431433
return false
432434
end
433435
g_PlayerTextDraws[player][textdrawID].usebox = usebox

‎amx/server/natives/a_samp.lua

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,7 @@ function AddStaticVehicleEx(amx, model, x, y, z, angle, color1, color2, respawnD
299299
end
300300

301301
if not g_PoliceVehicles[model] then
302-
if(color1 <= 0 and color1 >= 126) then color1 = math.random(1, 126) end
303-
if(color2 <= 0 and color2 >= 126) then color2 = math.random(1, 126) end
304-
305-
-- WARNING: [amx]\amx\server\natives\a_samp.lua:311: Expected positive value, got negative. This warning may be an error in future versions.
306-
setVehicleColor(vehicle, color1, color2, 0, 0)
302+
setVehicleColorClamped(vehicle, color1, color2)
307303
end
308304
local vehID = addElem(g_Vehicles, vehicle)
309305
if respawnDelay < 0 then
@@ -615,7 +611,9 @@ end
615611
function TextDrawCreate(amx, x, y, text)
616612
outputDebugString('TextDrawCreate called with args ' .. x .. ' ' .. y .. ' ' .. text)
617613
local textdraw = { x = x, y = y, shadow = {align=1, text=text, font=1, lwidth=0.5, lheight = 0.5} }
614+
textdraw.clientTDId = #g_TextDraws + 1
618615
local id = table.insert(g_TextDraws, textdraw)
616+
619617
setmetatable(
620618
textdraw,
621619
{
@@ -632,7 +630,8 @@ function TextDrawCreate(amx, x, y, text)
632630
end
633631
end
634632
if different then
635-
clientCall(root, 'TextDrawPropertyChanged', id, k, v)
633+
--outputDebugString(string.format('A property changed for %s string: %s', textdraw.clientTDId, textdraw.text))
634+
clientCall(root, 'TextDrawPropertyChanged', textdraw.clientTDId, k, v)
636635
t.shadow[k] = v
637636
end
638637
end
@@ -671,7 +670,7 @@ function TextDrawDestroy(amx, textdrawID)
671670
if not g_TextDraws[textdrawID] then
672671
return
673672
end
674-
clientCall(root, 'TextDrawDestroy', textdrawID)
673+
clientCall(root, 'TextDrawDestroy', g_TextDraws[textdrawID].clientTDId)
675674
g_TextDraws[textdrawID] = nil
676675
end
677676

@@ -727,15 +726,15 @@ function TextDrawShowForPlayer(amx, player, textdrawID)
727726
if not textdraw then
728727
return
729728
end
730-
clientCall(player, 'TextDrawShowForPlayer', textdrawID)
729+
clientCall(player, 'TextDrawShowForPlayer', textdraw.clientTDId)
731730
end
732731

733732
function TextDrawHideForPlayer(amx, player, textdrawID)
734733
local textdraw = g_TextDraws[textdrawID]
735734
if not textdraw then
736735
return
737736
end
738-
clientCall(player, 'TextDrawHideForPlayer', textdrawID)
737+
clientCall(player, 'TextDrawHideForPlayer', textdraw.clientTDId)
739738
end
740739

741740
function TextDrawShowForAll(amx, textdrawID)

‎amx/server/natives/a_vehicles.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ function RemoveVehicleComponent(amx, vehicle, upgradeID)
131131
end
132132

133133
function ChangeVehicleColor(amx, vehicle, color1, color2)
134-
setVehicleColor(vehicle, color1, color2, 0, 0)
134+
setVehicleColorClamped(vehicle, color1, color2)
135+
end
136+
137+
function setVehicleColorClamped(vehicle, color1, color2)
138+
--This is to prevent negative color behavior, keep the original color if they sent -1 (I believe this is what samp does)
139+
if color1 ~= -1 and color2 ~= -1 then
140+
color1 = clamp(color1, 0, 126)
141+
color2 = clamp(color2, 0, 126)
142+
setVehicleColor(vehicle, color1, color2, 0, 0)
143+
end
135144
end
136145

137146
function ChangeVehiclePaintjob(amx, vehicle, paintjob)

‎amx/server/syscalls.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ g_SAMPSyscallPrototypes = {
442442
TextDrawShowForAll = {'i'},
443443
TextDrawShowForPlayer = {'p', 'i'},
444444
TextDrawTextSize = {'x', 'f', 'f'},
445-
TextDrawUseBox = {'x', 'b'},
445+
TextDrawUseBox = {'x', 'i'},
446446
--Player textdraws
447447
PlayerTextDrawDestroy = {'p', 'i'},
448448
PlayerTextDrawShow = {'p', 'i'},

‎amx/server/util.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ function setBotState(bot, state)
428428
procCallOnAll('OnBotStateChange', botID, state, oldState)
429429
end
430430

431+
-- Clamping values
432+
function clamp(n, min, max)
433+
return math.max(min, math.min(max, n))
434+
end
435+
431436
-- Table extensions
432437

433438
local _table_insert = table.insert

0 commit comments

Comments
 (0)
Please sign in to comment.