Where is the source code ?

Discuss API design here.
Frank_der_Golf_Fahrer
Junior Member
Posts: 10
Joined: Sat Feb 12, 2022 10:26 am

Where is the source code ?

Postby Frank_der_Golf_Fahrer » Tue Feb 22, 2022 8:34 pm

I am searching for the actual source code of sp bc all that I can find are those weird python files that are so helpful like a brownout during a voice call:
they look always like this:

The name of the file.
# ../players/bots.py

Code: Select all

"""Provides bot specific functionality."""

# =============================================================================
# >> FORWARD IMPORTS
# =============================================================================
# Source.Python Imports
#   Bots
from _players._bots import BotCmd
from _players._bots import BotController
from _players._bots import bot_manager

# =============================================================================
# >> ALL DECLARATION
# =============================================================================
__all__ = ('BotCmd',
           'BotController',
           'bot_manager',
           )


My question is now where is the code that should be in this file?
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

Re: Where is the source code ?

Postby satoon101 » Tue Feb 22, 2022 9:12 pm

Image
Frank_der_Golf_Fahrer
Junior Member
Posts: 10
Joined: Sat Feb 12, 2022 10:26 am

Re: Where is the source code ?

Postby Frank_der_Golf_Fahrer » Wed Feb 23, 2022 12:36 am

Thy a lot!! So there is no actual python codebase so all the source code is written in c++
I have already checked out the wiki page. But those parts of real interest to me have documentation like the following


Bildschirmfoto von 2022-02-23 02-06-41.png
Bildschirmfoto von 2022-02-23 02-06-41.png (14.9 KiB) Viewed 76373 times
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Where is the source code ?

Postby Ayuto » Wed Feb 23, 2022 1:05 pm

Yes, a lot is written in C++, but not everything. Some packages like the plugins package are completely Python based. Modules/packages that start with an underscore are implemented in C++. However, we have wrapped them in Python packages, so there are no "hidden" packages.

As for the commands.player module: it's rather an internal modules, actually. If you are interested in creating commands, I would recommend using commands.typed, which provides decorators to easily create typed commands:
http://wiki.sourcepython.com/developing ... mands.html
Frank_der_Golf_Fahrer
Junior Member
Posts: 10
Joined: Sat Feb 12, 2022 10:26 am

Re: Where is the source code ?

Postby Frank_der_Golf_Fahrer » Wed Feb 23, 2022 4:35 pm

Thy a lot for the explanation. and thy for linking the command module. It helped me a lot. :). Hope I bother you too much but could give any advice on how I can make a Player(Bot) throw a grenade. So far I can create a Player(Bot) and give it a Smoke and set it as the current weapon. The problem is now how the Player(Bot) can throw it. Here is the code that I have so far.

Code: Select all

@Event('player_say')
def on_player_say(event):
   try:
   
      for index in range(3, 25): # Start at 3 so that myself does get affected

         player = Player.from_userid(index) # create player
         player.set_team(2) # move player to terrorist team
         player.client_command("give weapon_smokegrenade", True) # give the player a smoke grenade
         player.client_command("use weapon_smokegrenade", True)

   except Execption as e :
      pass


I know this is bad coding practice to try and except all Exceptions but I don't know a way to get the current number of players on the server. So I simply iterate over it to a high amount where I am sure that it could access all Players(Bots).

I have tried 2 other Methods so far to control them. 1. use the bot manager and BotCmd. Using this method I can create a bot but nothing more I can create it but I can't do anything with it I always need to access it over the player "API" is another way to set the team and current weapon of a but then going the player rout? 2nd Method I could create a UserCmd object and set the "buttons" to "PlayerButtons.ATTACK" but I could not find a way to execute it as you do with the Botmanager.

The code for Method1:

Code: Select all

@Event('player_say')
def on_player_say(event):
    edict = bot_manager.create_bot('Foo')

    bcmd = BotCmd()
    bcmd.buttons |= PlayerButtons.ATTACK

    controller = bot_manager.get_bot_controller(edict)
    controller.run_player_move(bcmd)


The code for Method2:

Code: Select all

@Event('player_say')
def on_player_say(event):
    usercmd = UserCmd()
    usercmd.buttons = PlayerButtons.ATTACK
User avatar
Kami
Global Moderator
Posts: 264
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

Re: Where is the source code ?

Postby Kami » Wed Feb 23, 2022 5:24 pm

Hey,

for your iteration problem you can try
http://wiki.sourcepython.com/developing ... PlayerIter

You can do something simple like:

Syntax: Select all

for player in PlayerIter('bot'):
player.set_team(2) # move player to terrorist team
player.client_command("give weapon_smokegrenade", True) # give the player a smoke grenade
player.client_command("use weapon_smokegrenade", True)


if you want to loop over everyone you can just use PlayerIter() or if you want to loop over everyone alive you can use PlayerIter('alive').

For the throwing problem I think you will have to use

http://wiki.sourcepython.com/developing ... RunCommand

and in that use the PlayerButtons.ATTACK like:

Syntax: Select all

@OnPlayerRunCommand
def on_player_run_command(player, user_cmd):
user_cmd.buttons |= PlayerButtons.ATTACK


This would force the player to always attack I think so you might need some kind of delay / restrictions there.

Not 100% sure this is right, but I hope it helps :D
Frank_der_Golf_Fahrer
Junior Member
Posts: 10
Joined: Sat Feb 12, 2022 10:26 am

Re: Where is the source code ?

Postby Frank_der_Golf_Fahrer » Wed Feb 23, 2022 7:23 pm

Thy, Thy a lot @Kami for helping me. Your suggestion worked great :) Here is the code I evolved from your ideas so I can fire bots via a chat message.

Syntax: Select all

from players.constants import PlayerButtons
from listeners import OnPlayerRunCommand
from filters.players import PlayerIter
from events import Event

attack = 1

@OnPlayerRunCommand
def on_player_run_command(player, user_cmd):
global attack
if attack == 1:

user_cmd.buttons |= PlayerButtons.ATTACK
attack = 0
# user_cmd.buttons &= ~PlayerButtons.ATTACK


@Event('player_say')
def on_player_say(event):
global attack

if event['text'] == '!bot':
for player in PlayerIter('bot'):

player.set_team(2) # move player to terrorist team
player.client_command("give weapon_smokegrenade", True) # give the player a smoke grenade
player.client_command("use weapon_smokegrenade", True)

if event['text'] == '!a':
attack = 1

Return to “API Design”

Who is online

Users browsing this forum: No registered users and 7 guests