Send data faster

Send data faster

--
-- Copyright (C) 2017 DBot
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

PPM2.PLAYER_VIEW_OFFSET = 64 * .7
PPM2.PLAYER_VIEW_OFFSET_DUCK = 28 * 1.2

hook.Add 'PlayerSpawn', 'PPM2.Hooks', =>
    timer.Simple 0.5, ->
        return unless @IsValid()
        return unless @IsPony()

        @SetViewOffset(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET))
        @SetViewOffsetDucked(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET_DUCK))
        if @GetPonyData()
            @GetPonyData()\PlayerRespawn()
            net.Start('PPM2.PlayerRespawn')
            net.WriteEntity(@)
            net.Broadcast()
            return

        timer.Simple 0.5, ->
            net.Start('PPM2.RequestPonyData')
            net.Send(@)

hook.Add 'PlayerInitialSpawn', 'PPM2.Hooks', =>
    timer.Simple 0.5, ->
        return unless @IsValid()
        return unless @IsPony()

        timer.Simple 10, ->
            net.Start('PPM2.RequestPonyData')
            net.Send(@)

do
    REQUIRE_CLIENTS = {}

    safeSendFunction = ->
        for ply, data in pairs REQUIRE_CLIENTS
            if not IsValid(ply)
                REQUIRE_CLIENTS[ply] = nil
                continue

            ent = table.remove(data, 1)
            if not ent
                REQUIRE_CLIENTS[ply] = nil
                continue

            data = ent\GetPonyData()
            continue if not data
            data\NetworkTo(ply)

    errorTrack = (err) ->
        PPM2.Message 'Networking Error: ', err
        PPM2.Message debug.traceback()

    timer.Create 'PPM2.Require', 0.25, 0, -> xpcall(safeSendFunction, errorTrack)
    net.Receive 'PPM2.Require', (len = 0, ply = NULL) ->
        return if not IsValid(ply)
        REQUIRE_CLIENTS[ply] = for ent in *ents.GetAll()
            continue if ent == ply
            data = ent\GetPonyData()
            continue if not data
            ent

net.Receive 'PPM2.EditorStatus', (len = 0, ply = NULL) ->
    return if not IsValid(ply)
    ply\SetNWBool('PPM2.InEditor', net.ReadBool())

hook.Add 'PostPlayerDeath', 'PPM2.Hooks', =>
    return if not @GetPonyData()
    @GetPonyData()\PlayerDeath()
    net.Start('PPM2.PlayerDeath')
    net.WriteEntity(@)
    net.Broadcast()

hook.Add 'PlayerDisconnected', 'PPM2.NotifyClients', =>
    data = @GetPonyData()
    return if not data
    net.Start('PPM2.NotifyDisconnect')
    net.WriteUInt(data.netID, 16)
    net.Broadcast()

BOTS_ARE_PONIES = CreateConVar('ppm2_bots', '1', {FCVAR_ARCHIVE, FCVAR_NOTIFY}, 'Whatever spawn bots as ponies')

hook.Add 'PlayerSetModel', 'PPM2.Bots', =>
    return if not BOTS_ARE_PONIES\GetBool()
    return if not @IsBot()
    @SetModel('models/ppm/player_default_base_new.mdl')
    return true

hook.Add 'PlayerSpawn', 'PPM2.Bots', =>
    return if not BOTS_ARE_PONIES\GetBool()
    return if not @IsBot()
    timer.Simple 1, ->
        return if not IsValid(@)
        @SetViewOffset(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET))
        @SetViewOffsetDucked(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET_DUCK))
        if not @GetPonyData()
            data = PPM2.NetworkedPonyData(nil, @)
            PPM2.Randomize(data)
            data\Create()

--
-- Copyright (C) 2017 DBot
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

PPM2.PLAYER_VIEW_OFFSET = 64 * .7
PPM2.PLAYER_VIEW_OFFSET_DUCK = 28 * 1.2

hook.Add 'PlayerSpawn', 'PPM2.Hooks', =>
    timer.Simple 0.5, ->
        return unless @IsValid()
        return unless @IsPony()

        @SetViewOffset(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET))
        @SetViewOffsetDucked(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET_DUCK))
        if @GetPonyData()
            @GetPonyData()\PlayerRespawn()
            net.Start('PPM2.PlayerRespawn')
            net.WriteEntity(@)
            net.Broadcast()
            return

        timer.Simple 0.5, ->
            net.Start('PPM2.RequestPonyData')
            net.Send(@)

hook.Add 'PlayerInitialSpawn', 'PPM2.Hooks', =>
    timer.Simple 0.5, ->
        return unless @IsValid()
        return unless @IsPony()

        timer.Simple 10, ->
            net.Start('PPM2.RequestPonyData')
            net.Send(@)

do
    REQUIRE_CLIENTS = {}

    safeSendFunction = ->
        for ply, data in pairs REQUIRE_CLIENTS
            if not IsValid(ply)
                REQUIRE_CLIENTS[ply] = nil
                continue

            ent = table.remove(data, 1)
            if not ent
                REQUIRE_CLIENTS[ply] = nil
                continue

            data = ent\GetPonyData()
            continue if not data
            data\NetworkTo(ply)

    errorTrack = (err) ->
        PPM2.Message 'Networking Error: ', err
        PPM2.Message debug.traceback()

    timer.Create 'PPM2.Require', 1, 0, -> xpcall(safeSendFunction, errorTrack)
    net.Receive 'PPM2.Require', (len = 0, ply = NULL) ->
        return if not IsValid(ply)
        REQUIRE_CLIENTS[ply] = for ent in *ents.GetAll()
            continue if ent == ply
            data = ent\GetPonyData()
            continue if not data
            ent

net.Receive 'PPM2.EditorStatus', (len = 0, ply = NULL) ->
    return if not IsValid(ply)
    ply\SetNWBool('PPM2.InEditor', net.ReadBool())

hook.Add 'PostPlayerDeath', 'PPM2.Hooks', =>
    return if not @GetPonyData()
    @GetPonyData()\PlayerDeath()
    net.Start('PPM2.PlayerDeath')
    net.WriteEntity(@)
    net.Broadcast()

hook.Add 'PlayerDisconnected', 'PPM2.NotifyClients', =>
    data = @GetPonyData()
    return if not data
    net.Start('PPM2.NotifyDisconnect')
    net.WriteUInt(data.netID, 16)
    net.Broadcast()

BOTS_ARE_PONIES = CreateConVar('ppm2_bots', '1', {FCVAR_ARCHIVE, FCVAR_NOTIFY}, 'Whatever spawn bots as ponies')

hook.Add 'PlayerSetModel', 'PPM2.Bots', =>
    return if not BOTS_ARE_PONIES\GetBool()
    return if not @IsBot()
    @SetModel('models/ppm/player_default_base_new.mdl')
    return true

hook.Add 'PlayerSpawn', 'PPM2.Bots', =>
    return if not BOTS_ARE_PONIES\GetBool()
    return if not @IsBot()
    timer.Simple 1, ->
        return if not IsValid(@)
        @SetViewOffset(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET))
        @SetViewOffsetDucked(Vector(0, 0, PPM2.PLAYER_VIEW_OFFSET_DUCK))
        if not @GetPonyData()
            data = PPM2.NetworkedPonyData(nil, @)
            PPM2.Randomize(data)
            data\Create()


It's update not __update for LinearMotion

require "LunoPunk.tween.motion.Motion"

export ^

-- Determines motion along a line, from one point to another.
class LinearMotion extends Motion
	-- Constructor.
	-- @param	complete	Optional completion callback.
	-- @param	type		Tween type.
	new: (complete = nil, type = nil) =>
		-- Line information.
		@__fromX, @__fromY = 0, 0
		@__moveX, @__moveY = 0, 0
		@__distance = -1

		super 0, complete, type, nil

	-- Starts moving along a line.
	-- @param	fromX		X start.
	-- @param	fromY		Y start.
	-- @param	toX			X finish.
	-- @param	toY			Y finish.
	-- @param	duration	Duration of the movement.
	-- @param	ease		Optional easer function.
	setMotion: (fromX, fromY, toX, toY, duration, ease = nil) =>
		@__distance = -1
		@x, @__fromX = fromX, fromX
		@y, @__fromY = fromY, fromY
		@__moveX = toX - fromX
		@__moveY = toY - fromY
		@__target = duration
		@__ease = ease
		@start!

	-- Starts moving along a line at the speed.
	-- @param	fromX		X start.
	-- @param	fromY		Y start.
	-- @param	toX			X finish.
	-- @param	toY			Y finish.
	-- @param	speed		Speed of the movement.
	-- @param	ease		Optional easer function.
	setMotionSpeed: (fromX, fromY, toX, toY, speed, ease = nil) =>
		@__distance = -1
		@x, @__fromX = fromX, fromX
		@y, @__fromY = fromY, fromY
		@__moveX = toX - fromX
		@__moveY = toY - fromY
		@__target = @distance! / speed
		@__ease = ease
		@start!

	-- @private Updates the Tween.
	update: =>
		super!
		@x = @__fromX + @__moveX * @t
		@y = @__fromY + @__moveY * @t
		if @x == @__fromX + @__moveX and @y == @__fromY + @__moveY and @active
			super!
			@finish!

	-- Length of the current line of movement.
	distance: =>
		return @__distance if @__distance >= 0
		@__distance = math.sqrt @__moveX * @__moveX + @__moveY * @__moveY
		return @__distance

require "LunoPunk.tween.motion.Motion"

export ^

-- Determines motion along a line, from one point to another.
class LinearMotion extends Motion
	-- Constructor.
	-- @param	complete	Optional completion callback.
	-- @param	type		Tween type.
	new: (complete = nil, type = nil) =>
		-- Line information.
		@__fromX, @__fromY = 0, 0
		@__moveX, @__moveY = 0, 0
		@__distance = -1

		super 0, complete, type, nil

	-- Starts moving along a line.
	-- @param	fromX		X start.
	-- @param	fromY		Y start.
	-- @param	toX			X finish.
	-- @param	toY			Y finish.
	-- @param	duration	Duration of the movement.
	-- @param	ease		Optional easer function.
	setMotion: (fromX, fromY, toX, toY, duration, ease = nil) =>
		@__distance = -1
		@x, @__fromX = fromX, fromX
		@y, @__fromY = fromY, fromY
		@__moveX = toX - fromX
		@__moveY = toY - fromY
		@__target = duration
		@__ease = ease
		@start!

	-- Starts moving along a line at the speed.
	-- @param	fromX		X start.
	-- @param	fromY		Y start.
	-- @param	toX			X finish.
	-- @param	toY			Y finish.
	-- @param	speed		Speed of the movement.
	-- @param	ease		Optional easer function.
	setMotionSpeed: (fromX, fromY, toX, toY, speed, ease = nil) =>
		@__distance = -1
		@x, @__fromX = fromX, fromX
		@y, @__fromY = fromY, fromY
		@__moveX = toX - fromX
		@__moveY = toY - fromY
		@__target = @distance! / speed
		@__ease = ease
		@start!

	-- @private Updates the Tween.
	__update: =>
		super!
		@x = @__fromX + @__moveX * @t
		@y = @__fromY + @__moveY * @t
		if @x == @__fromX + @__moveX and @y == @__fromY + @__moveY and @active
			super!
			@finish!

	-- Length of the current line of movement.
	distance: =>
		return @__distance if @__distance >= 0
		@__distance = math.sqrt @__moveX * @__moveX + @__moveY * @__moveY
		return @__distance