This commit is contained in:
Nikita Kruglickiy 2021-11-29 22:17:45 +03:00
parent 8d7d399af5
commit b5c524705b
14 changed files with 325 additions and 256 deletions

View File

@ -62,7 +62,7 @@ local Colors =
ReverseLights = Color(120,120,120),
RearLightsGlass = Color(150,40,40),
FrontLightsBase = Color(30,30,30),
FrontLights = Color(10,10,10),
FrontLights = Color(50,50,50),
FrontLightsGlass = Color(140,140,140),
Floor = Color(50,50,50),
Dashboard = Color(50,50,50),
@ -170,19 +170,8 @@ if SERVER then
local result = builder:getResult()
else
function init()
PERMA.onPermissionsGained = function()
builder = MeshBuilder:new(LINK)
end
if hasPermission("http.get") and hasPermission("mesh") and hasPermission("entities.setRenderProperty", chip()) then
init()
else
setupPermissionRequest({"http.get", "mesh", "entities.setRenderProperty"}, "", true)
hook.add("permissionrequest", "_permissionrequest", function()
if permissionRequestSatisfied() then
init()
end
end)
end
PERMA.build()
end

View File

@ -52,19 +52,8 @@ if SERVER then
local result = builder:getResult()
else
function init()
PERMA.onPermissionsGained = function()
builder = MeshBuilder:new(LINK)
end
if hasPermission("http.get") and hasPermission("mesh") and hasPermission("entities.setRenderProperty", chip()) then
init()
else
setupPermissionRequest({"http.get", "mesh", "entities.setRenderProperty"}, "", true)
hook.add("permissionrequest", "_permissionrequest", function()
if permissionRequestSatisfied() then
init()
end
end)
end
PERMA.build()
end

View File

@ -25,19 +25,8 @@ if SERVER then
builder:getResult()
else
function init()
PERMA.onPermissionsGained = function()
builder = MeshBuilder:new(LINK)
end
if hasPermission("http.get") and hasPermission("mesh") and hasPermission("entities.setRenderProperty", chip()) then
init()
else
setupPermissionRequest({"http.get", "mesh", "entities.setRenderProperty"}, "", true)
hook.add("permissionrequest", "_permissionrequest", function()
if permissionRequestSatisfied() then
init()
end
end)
end
PERMA.build()
end

View File

@ -24,19 +24,8 @@ if SERVER then
end)
end
else
function init()
PERMA.onPermissionsGained = function()
builder = MeshBuilder:new(LINK)
end
if hasPermission("http.get") and hasPermission("mesh") and hasPermission("entities.setRenderProperty", chip()) then
init()
else
setupPermissionRequest({"http.get", "mesh", "entities.setRenderProperty"}, "", true)
hook.add("permissionrequest", "_permissionrequest", function()
if permissionRequestSatisfied() then
init()
end
end)
end
PERMA.build()
end

View File

@ -3,7 +3,7 @@
-- @include /koptilnya/mesh_loader/builder.txt
require("/koptilnya/mesh_loader/builder.txt")
local LINK = "https://www.dropbox.com/s/x2esgnl33i8if2w/vaz2106.obj?dl=1"
local LINK = "https://raw.githubusercontent.com/koptilnya/gmod-data/main/vaz2106.obj"
local SCALE = Vector(0.9)
local MATERIALS = {
Exterior = "phoenix_storms/mrref2",
@ -70,7 +70,6 @@ if SERVER then
builder:build("Body__Exterior", Vector(0), Angle(0), SCALE, COLORS.Exterior, MATERIALS.Exterior, chip(), chip())
builder:build("Glass", Vector(0), Angle(0), SCALE, COLORS.Glass, MATERIALS.Glass, chip(), chip())
builder:build("Body__WindowsLine__Rubber", Vector(0), Angle(0), SCALE, COLORS.Insulation, MATERIALS.Insulation, chip(), chip())
builder:build("Body__TrunkLine__Rubber", Vector(0), Angle(0), SCALE, COLORS.Insulation, MATERIALS.Insulation, chip(), chip())
@ -154,21 +153,12 @@ if SERVER then
builder:build("RightRearDoor__Interior__DoorInterior", Vector(0), Angle(0), SCALE, COLORS.DoorInterior, MATERIALS.DoorInterior, chip(), chip())
builder:build("RightRearDoor__Handle__Chrome", Vector(0), Angle(0), SCALE, COLORS.DoorHandle, MATERIALS.DoorHandle, chip(), chip())
builder:build("Glass", Vector(0), Angle(0), SCALE, COLORS.Glass, MATERIALS.Glass, chip(), chip())
builder:getResult()
else
function init()
PERMA.onPermissionsGained = function()
builder = MeshBuilder:new(LINK)
end
if hasPermission("http.get") and hasPermission("mesh") and hasPermission("entities.setRenderProperty", chip()) then
init()
else
setupPermissionRequest({"http.get", "mesh", "entities.setRenderProperty"}, "", true)
hook.add("permissionrequest", "_permissionrequest", function()
if permissionRequestSatisfied() then
init()
end
end)
end
PERMA.build()
end

View File

@ -13,10 +13,10 @@ local CONFIG = {
},
Clutch = {
DeviceId = 0,
InputId = 2,
InputId = 7,
Type = JOYSTICK_INPUT_TYPE.Axis,
Handler = function(value)
return math.remap(value, 0, 65535, 0, 1)
return math.remap(value, 0, 65535, 1, 0)
end
},
Throttle = {
@ -24,7 +24,7 @@ local CONFIG = {
InputId = 1,
Type = JOYSTICK_INPUT_TYPE.Axis,
Handler = function(value)
return math.remap(value, 0, 65535, 0, 1)
return math.remap(value, 0, 65535, 1, 0)
end
},
Brake = {
@ -32,7 +32,7 @@ local CONFIG = {
InputId = 5,
Type = JOYSTICK_INPUT_TYPE.Axis,
Handler = function(value)
return math.remap(value, 0, 65535, 0, 1)
return math.remap(value, 0, 65535, 1, 0)
end
},
Handbrake = {
@ -45,7 +45,7 @@ local CONFIG = {
},
GearUp = {
DeviceId = 0,
InputId = 3,
InputId = 4,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
@ -53,19 +53,75 @@ local CONFIG = {
},
GearDown = {
DeviceId = 0,
InputId = 0,
InputId = 5,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
}
},
Gear1 = {
DeviceId = 0,
InputId = 8,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
},
Gear2 = {
DeviceId = 0,
InputId = 9,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
},
Gear3 = {
DeviceId = 0,
InputId = 10,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
},
Gear4 = {
DeviceId = 0,
InputId = 11,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
},
Gear5 = {
DeviceId = 0,
InputId = 12,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
},
Gear6 = {
DeviceId = 0,
InputId = 13,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
},
GearR = {
DeviceId = 0,
InputId = 14,
Type = JOYSTICK_INPUT_TYPE.Button,
Handler = function(value)
return math.remap(value, 0, 128, 0, 1)
end
},
}
if CLIENT and player() == owner() then
local data = {}
local isDirty = false
hook.add("think", "_think", function()
hook.add("tick", "_tick", function()
for k, v in pairs(CONFIG) do
local value = 0

View File

@ -1,5 +1,6 @@
NULL_ENTITY = entity(0)
CURRENT_PLAYER = player()
OWNER = owner()
IS_ME = CURRENT_PLAYER == OWNER
TICK_INTERVAL = game.getTickInterval()
RAD_TO_RPM = 9.5493

93
koptilnya/libs/perma.txt Normal file
View File

@ -0,0 +1,93 @@
-- @include /koptilnya/libs/table.txt
-- @shared
require("/koptilnya/libs/table.txt")
if not PERMA then
PERMA = {}
if CLIENT then
PERMA.list = {}
end
end
if CLIENT then
function PERMA.add(permission)
if not table.hasValue(PERMA.list, permission) then
table.insert(PERMA.list, permission)
end
end
function PERMA.addTable(permissions)
for _, v in ipairs(permissions) do
PERMA.add(v)
end
end
function PERMA.remove(permission)
table.removeByValue(PERMA.list, permission)
end
function PERMA.removeTable(permissions)
for _, v in ipairs(permissions) do
PERMA.remove(v)
end
end
function PERMA.clear()
PERMA.list = {}
end
function PERMA.check(permission)
return hasPermission(permission)
end
function PERMA.checkTable(permissions)
local prohibited = table.filter(PERMA.list, function(permission)
return not hasPermission(permission)
end)
return #prohibited == 0, prohibited
end
function PERMA:build(description)
description = description or ""
local hasAccess, prohibited = PERMA.checkTable(PERMA.list)
local function onPermissionsGained()
PERMA:onPermissionsGained()
net.start("onPermissionsGained")
net.send()
hook.remove("permissionrequest", "PERMA_permissionrequest")
end
if not hasAccess then
setupPermissionRequest(prohibited, description, true)
hook.add("permissionrequest", "PERMA_permissionrequest", function()
if permissionRequestSatisfied() then
onPermissionsGained()
end
end)
else
onPermissionsGained()
end
end
-- STUB
function PERMA:onPermissionsGained()
end
else
net.receive("onPermissionsGained", function(len, ply)
PERMA:onPermissionsGained(ply)
end)
-- STUB
function PERMA:onPermissionsGained(ply)
end
end

View File

@ -1,60 +0,0 @@
-- @shared
local PERMISSIONS_BUILDER = PERMISSIONS_BUILDER or {}
if CLIENT then
PERMISSIONS_BUILDER.list = PERMISSIONS_BUILDER.list or {}
local function onPermissionsGained()
PERMISSIONS_BUILDER:onPermissionsGained()
net.start("onPermissionsGained")
net.send()
end
function PERMISSIONS_BUILDER:add(permission)
if not table.hasValue(self.list, permission) then
table.insert(self.list, permission)
end
end
function PERMISSIONS_BUILDER:build(description)
description = description or ""
local allPermissionsGained = true
for k, v in pairs(self.list) do
if not hasPermission(v) then
allPermissionsGained = false
break
end
end
if allPermissionsGained then
onPermissionsGained()
else
setupPermissionRequest(self.list, description, true)
hook.add("permissionrequest", "PERMISSIONS_BUILDER_permissionrequest", function()
if permissionRequestSatisfied() then
onPermissionsGained()
end
end)
end
end
-- STUB
function PERMISSIONS_BUILDER:onPermissionsGained()
end
else
net.receive("onPermissionsGained", function(len, ply)
PERMISSIONS_BUILDER:onPermissionsGained(ply)
end)
-- STUB
function PERMISSIONS_BUILDER:onPermissionsGained(ply)
end
end
return PERMISSIONS_BUILDER

View File

@ -1,12 +1,19 @@
-- @name koptilnya/libs/workers
WORKERS = {}
WORKERS_QUOTA = 0.4
WORKERS_QUOTA = 0.7
local function canProcess()
local exp1 = (math.max(chip():getQuotaAverage(), chip():getQuotaUsed()) + (chip():getQuotaUsed() - math.max(chip():getQuotaAverage(), chip():getQuotaUsed())) * 0.01) / chip():getQuotaMax() < WORKERS_QUOTA
local exp2 = math.max(quotaTotalAverage(), quotaTotalUsed()) < quotaMax() * WORKERS_QUOTA
return exp1 and exp2
end
local function execWorker(worker)
local status
while math.max(quotaAverage(), quotaUsed()) < quotaMax() * WORKERS_QUOTA do
while canProcess() do
status = worker()
if status == 1 or status == 2 then

View File

@ -1,8 +1,21 @@
-- @name koptilnya/mesh_loader/builder
-- @include sv_builder.txt
-- @include cl_builder.txt
-- @include /koptilnya/libs/perma.txt
require("/koptilnya/libs/perma.txt")
if SERVER then
require("sv_builder.txt")
else
PERMA.addTable({
"http.get",
"hologram.create",
"material.create",
"material.load",
"material.urlcreate",
"mesh"
})
require("cl_builder.txt")
end

View File

@ -3,9 +3,11 @@
-- @include /koptilnya/libs/table.txt
-- @include obj_parser.txt
-- @include /koptilnya/libs/utils.txt
-- @include /koptilnya/libs/constants.txt
require("/koptilnya/libs/table.txt")
require("obj_parser.txt")
require("/koptilnya/libs/utils.txt")
require("/koptilnya/libs/constants.txt")
MeshBuilder = class("MeshBuilder")
@ -14,23 +16,17 @@ function MeshBuilder:initialize(link)
self.meshData = {}
self._objects = {}
self._cachedMaterials = {}
self._parser = ObjParser:new(link)
self._parser.onLoaded = function(parser, objData)
self:onObjLoaded(objData)
self:_onObjLoaded(objData)
end
self._parser.onParsed = function(parser, meshData, usedTriangles)
net.start("obj_parsed")
net.send()
self:onObjParsed(meshData, usedTriangles)
self.meshData = meshData
self:_applyMeshes()
self:_onObjParsed(meshData, usedTriangles)
end
self._cachedMaterials = {}
self._parser:parse()
if player() == owner() then
if IS_ME then
net.receive("initialized", function()
net.readEntity(function(ply)
notification.addProgress(tostring(ply:getUserID()), string.format("%s is loading mesh", ply:getName()))
@ -42,12 +38,47 @@ function MeshBuilder:initialize(link)
notification.kill(tostring(ply:getUserID()))
end)
end)
net.receive("player_disconnect", function()
notification.kill(net.readString())
end)
net.receive("starfall_error", function()
net.readEntity(function(ply)
notification.kill(tostring(ply:getUserID()))
end)
end)
end
net.start("initialized")
hook.add("starfallerror", "_starfallerror", function(ent, ply, err)
if ent ~= chip() then return end
net.start("starfall_error")
net.writeEntity(ply)
net.send()
end)
net.receive("objects", function()
self:_onReceiveObjects()
end)
end
function MeshBuilder:_onObjLoaded(objData)
self:onObjLoaded(objData)
end
function MeshBuilder:_onObjParsed(meshData, usedTriangles)
net.start("obj_parsed")
net.send()
self:onObjParsed(meshData, usedTriangles)
self.meshData = meshData
self:_applyMeshes()
end
function MeshBuilder:_onReceiveObjects()
self._objects = {}
local hasNext = net.readBit()
@ -81,15 +112,17 @@ function MeshBuilder:initialize(link)
self:onHologramsReceived(self._objects)
self:_applyMeshes()
end)
end)
end
function MeshBuilder:_applyMeshes()
for _, v in pairs(self._objects) do
if self.meshData[v.name] ~= nil then
local color = v.holo:getColor()
v.holo:setRenderBounds(Vector(-200), Vector(200))
v.holo:setMesh(self.meshData[v.name])
v.holo:setMeshMaterial(v.mat)
v.holo:setRenderBounds(Vector(-200), Vector(200))
v.meshApplied = true
end
end
@ -105,6 +138,8 @@ function MeshBuilder:_setTexture(mat, name, texture, layoutOptions)
layoutOptions = layoutOptions or {}
mat:setTextureURL(name, texture, function(_, _, _, _, layout)
if not layout then return end
local x = layoutOptions.x or 0
local y = layoutOptions.y or 0
local w = layoutOptions.w or 1024

View File

@ -11,6 +11,12 @@ local function setStatus(status)
end
function ObjParser:initialize(link)
self.link = link
self.rawData = nil
self.objData = {}
end
function ObjParser:parse()
addWorker(coroutine.wrap(function()
setStatus("Getting file...")
@ -18,24 +24,25 @@ function ObjParser:initialize(link)
coroutine.yield(1)
end
local objData
http.get(link, function(response)
objData = response
local rawObj
http.get(self.link, function(response)
rawObj = response
end)
while not objData do
while not rawObj do
coroutine.yield(1)
end
self:onLoaded(objData)
local triangles = mesh.trianglesLeft()
self.rawObj = rawObj
self:onLoaded(rawObj)
setStatus("File received, start parsing...")
self.meshData = mesh.createFromObj(objData, true)
local triangles = mesh.trianglesLeft()
self:onParsed(self.meshData, triangles - mesh.trianglesLeft())
self.objData = mesh.createFromObj(self.rawObj, true)
self:onParsed(self.objData, triangles - mesh.trianglesLeft())
setName(initialChipName)

View File

@ -3,18 +3,29 @@
-- @include /koptilnya/libs/workers.txt
require("/koptilnya/libs/workers.txt")
local TIMEOUT = 3
MeshBuilder = class("MeshBuilder")
function MeshBuilder:initialize(link, modelPlaceholder)
self.link = link
self.modelPlaceholder = modelPlaceholder or "models/holograms/cube.mdl"
self._objectsNames = {}
self._objects = {}
self._objectsToSend = {}
self._playersWithAccess = {}
self._firstTimePlayers = find.allPlayers()
self._firstTimeSended = false
self._players = {}
self._timeoutPassed = false
PERMA.onPermissionsGained = function(_, ply)
table.insert(self._players, ply)
net.start("initialized")
net.writeEntity(ply)
net.send(owner())
if self._timeoutPassed then
self:_sendObjects(ply)
end
end
net.receive("obj_parsed", function(len, ply)
net.start("obj_parsed")
@ -24,69 +35,31 @@ function MeshBuilder:initialize(link, modelPlaceholder)
self:onPlayerParsedObj(ply)
end)
net.receive("initialized", function(len, ply)
net.start("initialized")
hook.add("PlayerDisconnected", "MeshBuilder_PlayerDisconnected", function(ply)
table.removeByValue(self._players, ply)
net.start("player_disconnect")
net.writeString(tostring(ply:getUserID()))
net.writeString(ply:getName())
net.send(owner())
end)
net.receive("starfall_error", function()
print("starfall_error")
net.readEntity(function(ply)
net.start("starfall_error")
net.writeEntity(ply)
net.send(owner())
table.insert(self._playersWithAccess, ply)
if self._firstTimeSended then
self:_sendObjects(ply)
end
end)
hook.add("ClientInitialized", "MeshBuilder_ClientInitialized", function(ply)
if #self._firstTimePlayers > 0 then
table.removeByValue(self._firstTimePlayers, ply)
if #self._firstTimePlayers == 0 then
self:_sendObjects(self._playersWithAccess)
self._firstTimeSended = true
end
end
end)
hook.add("PlayerConnected", "MeshBuilder_PlayerConnected", function(ply)
if not self._firstTimeSended then
table.insert(self._firstTimePlayers, ply)
end
end)
hook.add("PlayerDisconnected", "MeshBuilder_PlayerDisconnected", function(ply)
table.removeByValue(self._firstTimePlayers, ply)
end)
addWorker(coroutine.wrap(function()
while not http.canRequest() do
coroutine.yield(1)
end
local objData
http.get(link, function(response)
objData = response
end)
while not objData do
coroutine.yield(1)
end
self:onObjectLoaded(objData)
self:onObjectParsed(self._objectsNames)
return 2
end))
end
function MeshBuilder:_sendObjects(target)
if #self._objectsToSend == 0 then
return
end
if #self._objects == 0 then return end
net.start("objects")
for _, v in pairs(self._objectsToSend) do
for _, v in pairs(self._objects) do
net.writeBit(1)
net.writeString(v.name)
net.writeTable(type(v.mat) == "table" and v.mat or { basetexture = tostring(v.mat) })
@ -102,7 +75,6 @@ function MeshBuilder:reset()
end
self._objects = {}
self._objectsToSend = {}
end
function MeshBuilder:build(name, pos, ang, scale, color, mat, parent, relativeTo)
@ -113,6 +85,7 @@ function MeshBuilder:build(name, pos, ang, scale, color, mat, parent, relativeTo
local holo = holograms.create(pos, ang, self.modelPlaceholder, scale)
holo:setColor(color)
holo:setRenderMode(color.a == 255 and RENDERMODE.NORMAL or RENDERMODE.TRANSCOLOR)
if isValid(parent) then
holo:setParent(parent)
@ -124,22 +97,20 @@ function MeshBuilder:build(name, pos, ang, scale, color, mat, parent, relativeTo
end
function MeshBuilder:getResult()
table.copyFromTo(self._objects, self._objectsToSend)
if self._timeoutPassed then
self:_sendObjects(self._players)
else
timer.simple(TIMEOUT, function()
self._timeoutPassed = true
if self._firstTimeSended then
self:_sendObjects(self._playersWithAccess)
self:_sendObjects(self._players)
end)
end
return self._objectsToSend
return self._objects
end
-- STUB
function MeshBuilder:onObjectLoaded(objData)
end
function MeshBuilder:onObjectParsed(objectsNames)
end
function MeshBuilder:onPlayerParsedObj(ply)
end