Extending the plugin with C++
Posted: Mon Jul 20, 2015 9:29 pm
Hi everyone,
I'm thinking about converting an already existing Python code (spawnpoints module, part of a SP Plugin) to C++ - just to learn more of the API. Here's the code:As you can see, the only thing needed by the script is basically the path to save the spawn points. However, this could be passed to the SpawnPoints class aswell.
So, how would I go about turning this into a SP Plugin extension?
Thanks! :D
EDIT: I'm not asking about my code's logic, rather the structure of how I would use includes and the boost library and stuff.
I'm thinking about converting an already existing Python code (spawnpoints module, part of a SP Plugin) to C++ - just to learn more of the API. Here's the code:
Syntax: Select all
# ../seekme/spawnpoints.py
"""Provides a dynamic way to save and load map-based spawn points."""
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# JSON
from json import dump as json_dump
from json import load as json_load
# OS
from os import makedirs
# Random
from random import choice as random_choice
# Source.Pyton Imports
# Engines
from engines.server import global_vars
# Filters
from filters.players import PlayerIter
# Mathlib
from mathlib import Vector
# Paths
from paths import PLUGIN_DATA_PATH
# Script Imports
from .info import info
# =============================================================================
# >> CONSTANTS
# =============================================================================
# Miniimum distance for spawning players
DISTANCE_SPAWN = 100.0
# Minimum distance to add vectors
DISTANCE_ADD = 200.0
# Maximum number of vectors per map (5 times 'maxplayers')
VECTORS_MAX = global_vars.max_clients * 5
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# PlayerIter() generator for alive players' locations
playeriter_alive_origins = PlayerIter(is_filters='alive', return_types='location')
# ../addons/source-python/data/plugins/flashfunsp/spawnpoints
spawnpoints_path = PLUGIN_DATA_PATH / info.basename / 'spawnpoints'
# =============================================================================
# >> CLASSES
# =============================================================================
class _SpawnPoints(list):
""" Extends list for handling Vector instances as spawnpoints. """
def __init__(self):
"""Calls list's constructor."""
super(_SpawnPoints, self).__init__()
def append(self, vector):
"""Checks the size of this list before appending the vector."""
# Does the vector already exist?
if vector in self:
# If yes, don't go any further
return
# Do we have enough vectors for the map?
if len(self) >= VECTORS_MAX:
# If yes, don't go any further
return
# If not, add the vector to the list
super(_SpawnPoints, self).append(vector)
def load(self):
"""Loads the spawnpoints/<mapname>.json file if it exists and adds the vectors to this list."""
# Clear the list if it's not empty
if self:
self.clear()
# Does the file exist?
if not self.json_path.isfile():
# If not, don't go any further
return
# Else, open the file for reading
with self.json_path.open() as file_object:
# store the file's contents (lists of [X,Y,Z] coordinates)
contents = json_load(file_object)
# loop through the file's contents
for coords in contents:
# Append the coordinates as a Vector instance
self.append(Vector(*coords))
def save(self):
"""Writes vectors from this list to the spawnpoints/<mapname>.json file."""
# Do we have anything to save?
if not self:
# If not, don't go any further
return
# Does to folder to save the file in exist?
if not spawnpoints_path.exists():
# If not, create the path tree
makedirs(spawnpoints_path)
# Convert the Vector instances of this list into lists of [X, Y, Z] coordinates
coords = list(map(lambda vector: [vector.x, vector.y, vector.z], self))
# Open the file for writing
with self.json_path.open("w") as file_object:
# Dump the [X,Y,Z] lists to it
json_dump(coords, file_object, indent=4)
def check(self, vector):
"""Returns whether the vector can be added to this list."""
# Is the vector already in this list?
if vector in self:
# return False to stop the addition
return False
# Loop through each vector in this list
for check in self:
# Is the check vector too near?
if check.get_distance(vector) < DISTANCE_ADD:
# If yes, return False
return False
# else return True to add it
return True
def get_random(self, tried=0):
"""Returns a random spawn-point."""
# Did we try enough times?
if tried == len(self):
# If yes, we couldn't find a vector, so return None
return None
# Get a random vector
vector = random_choice(self)
# Is any player too near to this vector?
for origin in playeriter_alive_origins:
if vector.get_distance(origin) < DISTANCE_SPAWN:
# If yes, try another time
return self.get_random(tried + 1)
# If not, return the chosen vector
return vector
@property
def json_path(self):
return spawnpoints_path.joinpath('{0}.json'.format(global_vars.map_name))
# Create an instance of the _SpawnPoints class
spawnpoints = _SpawnPoints()
So, how would I go about turning this into a SP Plugin extension?
Thanks! :D
EDIT: I'm not asking about my code's logic, rather the structure of how I would use includes and the boost library and stuff.