Skip to content

Commit

Permalink
First working version of "set_gametile"
Browse files Browse the repository at this point in the history
Console command to place gametiles in the current map.
Ground work for the server sending map patches to the client.

The current implementation is horrible. It is spawning a thread
which runs a python script that the user has to place in the maps folder

So this needs some work to be polished and portable.
  • Loading branch information
ChillerDragon committed Jul 23, 2022
1 parent cc6ead0 commit 3399718
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ disable=
R0915, # too-many-statements
W0511, # fixme
W0603, # global-statement

[TYPECHECK]
ignored-modules=twmap

2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,8 @@ if(CLIENT)
components/chillerbot/chillpw.h
components/chillerbot/city.cpp
components/chillerbot/city.h
components/chillerbot/edit_map.cpp
components/chillerbot/edit_map.h
components/chillerbot/playerpics.cpp
components/chillerbot/playerpics.h
components/chillerbot/remotecontrol.cpp
Expand Down
22 changes: 22 additions & 0 deletions scripts/set_gametile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

import sys
import twmap

if len(sys.argv) != 6:
print("usage: set_gametile.py IN_MAP OUT_MAP x y index")
sys.exit(0)

arg_map_in = sys.argv[1]
arg_map_out = sys.argv[2]
arg_x = int(sys.argv[3])
arg_y = int(sys.argv[4])
arg_index = int(sys.argv[5])

m = twmap.Map(arg_map_in)

tiles = m.game_layer().tiles
tiles[arg_y][arg_x][0] = arg_index
m.game_layer().tiles = tiles

m.save(arg_map_out)
10 changes: 0 additions & 10 deletions src/game/client/components/chillerbot/chillerbotux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ void CChillerBotUX::OnConsoleInit()
Console()->Register("load_map", "s[file]", CFGFLAG_CLIENT, ConLoadMap, this, "Load mapfile");
Console()->Register("dump_players", "?s[search]", CFGFLAG_CLIENT, ConDumpPlayers, this, "Prints players to console");
Console()->Register("force_quit", "", CFGFLAG_CLIENT, ConForceQuit, this, "Forces a dirty client quit all data will be lost");
// Console()->Register("set_gametile", "i[x]i[y]i[index]", CFGFLAG_CLIENT, ConSetGametile, this, "Some ugly wip map edit hack");

Console()->Chain("cl_camp_hack", ConchainCampHack, this);
Console()->Chain("cl_chillerbot_hud", ConchainChillerbotHud, this);
Expand All @@ -575,15 +574,6 @@ void CChillerBotUX::OnConsoleInit()
Console()->Chain("cl_show_last_ping", ConchainShowLastPing, this);
}

void CChillerBotUX::ConSetGametile(IConsole::IResult *pResult, void *pUserData)
{
// CChillerBotUX *pSelf = (CChillerBotUX *)pUserData;
// char aBuf[IO_MAX_PATH_LENGTH];
// TODO: call some twmap py script in other thread let it edit loaded map and reload it using ConLoadMap
// str_format(aBuf, sizeof(aBuf), "maps/tmp/chillerbot-ux-%d.map.tmp", pid());
// pSelf->m_pClient->Client()->ChillerBotLoadMap(aBuf);
}

void CChillerBotUX::ConForceQuit(IConsole::IResult *pResult, void *pUserData)
{
exit(0);
Expand Down
1 change: 0 additions & 1 deletion src/game/client/components/chillerbot/chillerbotux.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class CChillerBotUX : public CComponent
static void ConLoadMap(IConsole::IResult *pResult, void *pUserData);
static void ConDumpPlayers(IConsole::IResult *pResult, void *pUserData);
static void ConForceQuit(IConsole::IResult *pResult, void *pUserData);
static void ConSetGametile(IConsole::IResult *pResult, void *pUserData);

static void ConchainCampHack(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainChillerbotHud(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
Expand Down
127 changes: 127 additions & 0 deletions src/game/client/components/chillerbot/edit_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// ChillerDragon 2022 - chillerbot ux

#include "engine/shared/protocol.h"
#include <engine/client/notifications.h>
#include <engine/config.h>
#include <engine/console.h>
#include <engine/graphics.h>
#include <engine/keys.h>
#include <engine/textrender.h>
#include <game/client/animstate.h>
#include <game/client/components/camera.h>
#include <game/client/components/chat.h>
#include <game/client/components/chillerbot/chathelper.h>
#include <game/client/components/chillerbot/version.h>
#include <game/client/components/controls.h>
#include <game/client/components/menus.h>
#include <game/client/components/voting.h>
#include <game/client/gameclient.h>
#include <game/client/prediction/entities/character.h>
#include <game/client/race.h>
#include <game/client/render.h>
#include <game/generated/client_data.h>
#include <game/generated/protocol.h>
#include <game/version.h>

#include "edit_map.h"

/*
needed setup
mkdir -p ~/.teeworlds/maps/tmp
cd scripts/
ln -s "$(pwd)/set_gametile.py" ~/.teeworlds/maps/tmp/
*/

void CEditMapJob::Run()
{
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "cd $HOME/.teeworlds/ && /usr/bin/python3 ./maps/tmp/set_gametile.py %s %s %d %d %d",
m_aSource, m_aDestination, m_X, m_Y, m_Index);
if(system(aBuf))
{
dbg_msg("edit_map", "process failed: %s", aBuf);
}
m_State = STATE_DONE;
}

CEditMap::CEditMap()
{
m_pSetGameTileJob = nullptr;
}

void CEditMap::OnInit()
{
char aDest[IO_MAX_PATH_LENGTH];
GetEditedMapPath(aDest, sizeof(aDest));
Storage()->RemoveFile(aDest, IStorage::TYPE_SAVE);
}

void CEditMap::OnShutdown()
{
char aDest[IO_MAX_PATH_LENGTH];
GetEditedMapPath(aDest, sizeof(aDest));
Storage()->RemoveFile(aDest, IStorage::TYPE_SAVE);
}

void CEditMap::OnConsoleInit()
{
Console()->Register("set_gametile", "i[x]i[y]i[index]", CFGFLAG_CLIENT, ConSetGametile, this, "Some ugly wip map edit hack");
}

void CEditMap::OnRender()
{
if(m_pSetGameTileJob)
{
if(m_pSetGameTileJob->State() == CEditMapJob::STATE_DONE)
{
char aDest[IO_MAX_PATH_LENGTH];
GetEditedMapPath(aDest, sizeof(aDest));
dbg_msg("edit_map", "thread finished load map: %s", aDest);
m_pClient->Client()->ChillerBotLoadMap(aDest);
m_pSetGameTileJob = nullptr;
}
}
}

void CEditMap::GetEditedMapPath(char *pPath, int Size)
{
str_format(pPath, Size, "maps/tmp/chillerbot-ux-%d.map.tmp", pid());
}

void CEditMap::GetSourceMapPath(char *pPath, int Size)
{
GetEditedMapPath(pPath, Size);
char aBuf[IO_MAX_PATH_LENGTH];
if(Storage()->FindFile(pPath + 9, "maps", IStorage::TYPE_SAVE, aBuf, sizeof(aBuf)))
{
if(!str_comp(pPath, aBuf))
{
dbg_msg("edit_map", "edit map found add to it '%s'", aBuf);
str_copy(pPath, aBuf, Size);
return;
}
else
{
dbg_msg("edit_map", "found map does not match '%s' != '%s'", pPath, aBuf);
}
}
dbg_msg("edit_map", "'%s' not found in maps/ use current map", pPath + 9);
str_copy(pPath, m_pClient->Client()->GetCurrentMapPath(), Size);
}

void CEditMap::ConSetGametile(IConsole::IResult *pResult, void *pUserData)
{
CEditMap *pSelf = (CEditMap *)pUserData;
char aDest[IO_MAX_PATH_LENGTH];
pSelf->GetEditedMapPath(aDest, sizeof(aDest));
char aSrc[IO_MAX_PATH_LENGTH];
pSelf->GetSourceMapPath(aSrc, sizeof(aSrc));
dbg_msg("edit_map", "load map from %s", aSrc);
dbg_msg("edit_map", "save to %s", aDest);
int x = pResult->GetInteger(0);
int y = pResult->GetInteger(1);
int Index = pResult->GetInteger(2);
pSelf->m_pSetGameTileJob = std::make_shared<CEditMapJob>(aSrc, aDest, x, y, Index);
pSelf->m_pClient->Engine()->AddJob(pSelf->m_pSetGameTileJob);
}
60 changes: 60 additions & 0 deletions src/game/client/components/chillerbot/edit_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef GAME_CLIENT_COMPONENTS_CHILLERBOT_EDIT_MAP_H
#define GAME_CLIENT_COMPONENTS_CHILLERBOT_EDIT_MAP_H

#include <engine/console.h>
#include <game/client/component.h>

#include <engine/shared/jobs.h>

class CEditMapJob : public IJob
{
char m_aSource[IO_MAX_PATH_LENGTH];
char m_aDestination[IO_MAX_PATH_LENGTH];
int m_X;
int m_Y;
int m_Index;

public:
CEditMapJob(const char *pSource, const char *pDestination, int x, int y, int Index)
{
str_copy(m_aSource, pSource, sizeof(m_aSource));
str_copy(m_aDestination, pDestination, sizeof(m_aDestination));
m_X = x;
m_Y = y;
m_Index = Index;
m_State = STATE_RUNNING;
}
void Run() override;

enum
{
STATE_DONE,
STATE_RUNNING,
};

int m_State;

int State() { return m_State; }
};

class CEditMap : public CComponent
{
virtual void OnRender() override;
virtual void OnInit() override;
virtual void OnShutdown() override;
virtual void OnConsoleInit() override;

static void ConSetGametile(IConsole::IResult *pResult, void *pUserData);

std::shared_ptr<CEditMapJob> m_pSetGameTileJob;

void GetEditedMapPath(char *pPath, int Size);
void GetSourceMapPath(char *pPath, int Size);

public:
virtual int Sizeof() const override { return sizeof(*this); }

CEditMap();
};

#endif
2 changes: 2 additions & 0 deletions src/game/client/gameclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
#include "components/chillerbot/chillerbotux.h"
#include "components/chillerbot/chillpw.h"
#include "components/chillerbot/city.h"
#include "components/chillerbot/edit_map.h"
#include "components/chillerbot/playerpics.h"
#include "components/chillerbot/remotecontrol.h"
#include "components/chillerbot/stresser.h"
Expand Down Expand Up @@ -167,6 +168,7 @@ void CGameClient::OnConsoleInit()
&m_WarList,
&m_VibeBot,
&m_CityHelper,
&m_EditMap,
&m_TerminalUI,
&m_Stresser,
/* &m_ChillConsole, */
Expand Down
2 changes: 2 additions & 0 deletions src/game/client/gameclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "components/chillerbot/chillerbotux.h"
#include "components/chillerbot/chillpw.h"
#include "components/chillerbot/city.h"
#include "components/chillerbot/edit_map.h"
#include "components/chillerbot/playerpics.h"
#include "components/chillerbot/remotecontrol.h"
#include "components/chillerbot/stresser.h"
Expand Down Expand Up @@ -175,6 +176,7 @@ class CGameClient : public IGameClient
CWarList m_WarList;
CVibeBot m_VibeBot;
CCityHelper m_CityHelper;
CEditMap m_EditMap;
CTerminalUI m_TerminalUI;
CStresser m_Stresser;

Expand Down

0 comments on commit 3399718

Please sign in to comment.