Skip to content

Examples

Gaspar edited this page Feb 12, 2024 · 8 revisions

import goldsrcmap as gsm

INPUT = 'src/test.map'
OUTPUT = 'src/test_edited.map'
TEXTURE = 'light_green'
CLASS_ENTITY_TO_TEXTURIZE = 'kz_climb_block'
Z_AXIS = gsm.Vector3(0,0,1)

m = gsm.load_map(INPUT)

for entity in m:
    if entity == CLASS_ENTITY_TO_TEXTURIZE:
        for brush in entity:
            for face in brush:
                if face.normal.is_parallel(Z_AXIS):
                    face.texture.name = TEXTURE

gsm.save_map(m, OUTPUT)

This script identifies faces within a specific entity whose plane normal is parallel to the Z-axis and changes the texture of those faces to a specified texture

Before After
image image

import goldsrcmap as gsm
import math

INPUT = 'src/test.map'
OUTPUT = 'src/test_edited.map'

m = gsm.load_map(INPUT)

def curve(t):
    # curve equation ...
    #x(t) = ...
    #y(t) = ...
    #z(t) = ...
    return [x, y, z]

def curve_array(brush, curve_function, start_param, end_param, spacing):
    """Creates an array of brushes along a parametric curve"""
    array = []
    param = start_param
    while param <= end_param:
        point = curve_function(param)
        brush_copy = brush.copy()
        brush_copy.move_by(*point)
        array.append(brush_copy)
        param += spacing
    return array

brush = m.brushes[0]
result = curve_array(brush, curve, 0, 20, 0.1)
m.add_brush(result)

gsm.save_map(m, OUTPUT)

This script defines a parametric curve function, and creates an array of brushes along that curve. The parametric curve is used to generate a series of points, and for each point, a copy of the original brush is moved to that position

def torus_knot(t):
    R = 1024  # Radius of the torus
    r = 256  # Radius of the tube
    a = math.sqrt(r)
    x = (R + r * math.cos(a * t)) * math.cos(t)
    y = (R + r * math.cos(a * t)) * math.sin(t)
    z = r * math.sin(a * t)
    return [x, y, z]
Torus knot
image R=1024 r=512 start_param=0 end_param=20 spacing=0.1 image
end_param=100
image image
def helix(t):
    radius = 512
    pitch = 128
    x = radius * math.cos(t)
    y = radius * math.sin(t)
    z = pitch * t
    return [x, y, z]
Helix
image start_param=0 end_param=20 spacing=0.1
image

import goldsrcmap as gsm
import random

OUTPUT = 'src/test_edited.map'

def brush_cluster(num_towers, base_size_range, height_range, block_size, space_between_towers, position, texture):
    # random implementation, there are better ways to do it
    towers = []
    tower_width = max(base_size_range) + space_between_towers

    for i in range(num_towers):
        base_size = random.uniform(base_size_range[0], base_size_range[1])
        height = random.uniform(height_range[0], height_range[1])

        row = i // int(block_size[0] / tower_width)
        col = i % int(block_size[0] / tower_width)

        tower_position = gsm.Point(
            position[0] + col * tower_width,
            position[1] + row * tower_width,
            position[2]
        )

        tower = gsm.BrushGenerator.cuboid(base_size, base_size, height, tower_position, texture=texture)
        towers.append(tower)

    return towers


start_position = gsm.Point(-4096, -4096, 0)
position = start_position
num_towers = 200
base_size_range = (428, 256)
height_range = (256, 1500)
block_size = (6 * 1024, 3 * 2048)
space_between_towers = 50
texture = 'measure128_02'

towers = brush_cluster(num_towers, base_size_range, height_range, block_size, space_between_towers, position, texture)

m = gsm.Map()
m.add_brush(towers)

gsm.save_map(m, OUTPUT)

This script generates a cluster of brushes, resembling a collection of city towers, by creating a specified number of brushes with randomly assigned base sizes and heights based on preset ranges.

Brush cluster
start_position=gsm.Point(-4096, -4096, 0)
num_towers=200
base_size_range=(428, 256)
height_range=(256, 1500)
block_size=(6*1024, 3*2048)
image
start_position=gsm.Point(-4096, -4096, 0)
num_towers=50
base_size_range=(512, 512)
height_range=(512, 2048)
block_size=(10*900, 2)
space_between_towers=256
image

Extra: https://github.com/G2Pavon/goldsrcmapextensions

Projects using goldsrcmap:

https://github.com/G2Pavon/TAB

https://github.com/G2Pavon/kz_fgd

Clone this wiki locally