Skip to content

Commit

Permalink
Working #home flow
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Jan 11, 2023
1 parent f2ed601 commit 9d139ff
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*.png filter=lfs diff=lfs merge=lfs -text
*.ogg filter=lfs diff=lfs merge=lfs -text
*.ico filter=lfs diff=lfs merge=lfs -text
*.textures filter=lfs diff=lfs merge=lfs -text
2 changes: 0 additions & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ cluster:
addbanner "^f7Sour ^f7is available online ^f1www.github.com/cfoust/sour^f7."
addbanner "Queue for duels with #duel or #duel insta."
defaultgamespeed 100
defaultmodename "ffa"
defaultmap "complex"
autosendmap 1
enable_passflag 1
instacoop 0
Expand Down
2 changes: 1 addition & 1 deletion services/client/definitions/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type ModuleType = {
missingModel: (name: string, msg: number) => void
loadRandomMap: () => void
loadWorld: (map: string) => void
receivedMap: (map: string, oldMap: string) => void
receiveMap: (map: string, oldMap: string) => void
installMod: (name: string) => void,
getModProperty: (id: string, property: string) => string,
modsToURL: () => void,
Expand Down
2 changes: 1 addition & 1 deletion services/client/src/assets/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ export default function useAssets(
setTimeout(() => BananaBread.execute(`map ${map}`), 0)
},
loadWorld: (target: string) => loadMapData(target),
receivedMap: (map: string, oldMap: string) => {
receiveMap: (map: string, oldMap: string) => {
if (oldMap != null && !oldMap.startsWith('getmap_')) {
targetMap = map
loadMapData(oldMap)
Expand Down
3 changes: 3 additions & 0 deletions services/go/default.textures
Git LFS file not shown
2 changes: 2 additions & 0 deletions services/go/pkg/maps/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ type GameMap struct {
}

func NewMap() *GameMap {
c := worldio.Empty_world(12)
return &GameMap{
Header: Header{
Version: game.MAP_VERSION,
Expand All @@ -400,5 +401,6 @@ func NewMap() *GameMap {
WorldRoot: EmptyMap(1024),
Vars: make(map[string]game.Variable),
VSlots: make([]*VSlot, 0),
C: c,
}
}
1 change: 1 addition & 0 deletions services/go/pkg/maps/worldio/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ extern void genfaceverts(const cube &c, int orient, ivec v[4]);
extern int calcmergedsize(int orient, const ivec &co, int size, const vertinfo *verts, int numverts);
extern void invalidatemerges(cube &c, const ivec &co, int size, bool msg);
extern void calcmerges();
bool emptymap(int scale);

extern int mergefaces(int orient, facebounds *m, int sz);
extern void mincubeface(const cube &cu, int orient, const ivec &o, int size, const facebounds &orig, facebounds &cf, ushort nmat = MAT_AIR, ushort matmask = MATF_VOLUME);
Expand Down
11 changes: 11 additions & 0 deletions services/go/pkg/maps/worldio/worldio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,17 @@ size_t partial_save_world(
return buf.buf.len;
}

MapState *empty_world(int scale)
{
MapState *state = new MapState;
state->vslots = new vector<VSlot*>;
state->slots = new vector<Slot*>;
emptymap(scale);
state->root = worldroot;

return state;
}

MapState *partial_load_world(
void *p,
size_t len,
Expand Down
2 changes: 2 additions & 0 deletions services/go/pkg/maps/worldio/worldio.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ void freeocta(cube *c);
cube *loadchildren_buf(void *p, size_t len, int size, int _mapversion);
size_t savec_buf(void *p, unsigned int len, cube *c, int size);

MapState *empty_world(int size);

MapState *partial_load_world(
void *p,
size_t len,
Expand Down
9 changes: 4 additions & 5 deletions services/go/svc/cluster/servers/editing.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (e *EditingState) Process(message game.Message) {
e.mutex.Unlock()
}

func (e *EditingState) LoadMap(path string) error {
map_, err := maps.FromFile(path)
func (e *EditingState) LoadMapBytes(data []byte) error {
map_, err := maps.FromGZ(data)
if err != nil {
return err
}

file, err := os.Open("complex.texture")
file, err := os.Open("default.textures")
if err != nil {
return err
}
Expand All @@ -78,7 +78,7 @@ func (e *EditingState) LoadMap(path string) error {
e.Map.C,
)
if !result {
return fmt.Errorf("applying changes failed")
return fmt.Errorf("failed to load texture index")
}

return nil
Expand Down Expand Up @@ -173,7 +173,6 @@ func (e *EditingState) Apply(edits []*Edit) error {
if !result {
return fmt.Errorf("applying changes failed")
}
log.Info().Msgf(e.Map.Header.GameType)

err := e.Map.ToFile("../test.ogz")
if err != nil {
Expand Down
15 changes: 2 additions & 13 deletions services/go/svc/cluster/servers/gameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,6 @@ func (server *GameServer) SendMapResponse(mapName string, mode int32, path strin
if succeeded {
server.IsBuiltMap = false
}

if succeeded && server.Editing != nil {
go func() {
err := server.Editing.LoadMap(path)
if err != nil {
log.Error().Err(err).Msg("failed to load map")
}

go server.Editing.PollEdits(server.Context)
}()
}
server.Mutex.Unlock()

p := game.Packet{}
Expand Down Expand Up @@ -461,7 +450,7 @@ func (server *GameServer) HandleMapChange(ctx context.Context, mapName string, m
}

if mode == game.MODE_COOP {
server.Editing = NewEditingState()
// TODO not supported generally, only on homeworlds
}

server.Mutex.Unlock()
Expand Down Expand Up @@ -535,7 +524,7 @@ func (server *GameServer) PollEvents(ctx context.Context) {
eventType := ServerEvent(type_)

if eventType != SERVER_EVENT_PONG {
logger.Debug().Str("type", eventType.String()).Msg("server -> cluster")
//logger.Debug().Str("type", eventType.String()).Msg("server -> cluster")
}

if eventType == SERVER_EVENT_REQUEST_MAP {
Expand Down
3 changes: 2 additions & 1 deletion services/go/svc/cluster/servers/qserv/fpsgame/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,7 @@ namespace server {
healthy();
}
ICOMMAND(changemap, "si", (char *target, int *mode), { changemap(target, *mode); });
ICOMMAND(emptymap, "", (), { changemap("", 1); });
ICOMMAND(setmode, "i", (int *mode), { changemap(smapname, *mode); });
ICOMMAND(setmap, "s", (char *target), { changemap(target, gamemode); });

Expand Down Expand Up @@ -3775,7 +3776,7 @@ best.add(clients[i]); \
{
if(m_demo) enddemoplayback();

if(!hasmap(ci)) rotatemap(false);
//if(!hasmap(ci)) rotatemap(false);

shouldstep = true;

Expand Down
10 changes: 7 additions & 3 deletions services/go/svc/cluster/service/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func getModeNames() []string {
func getModeNumber(mode string) opt.Option[int] {
for i, name := range getModeNames() {
if name == mode {
return opt.Some[int](i)
return opt.Some(i)
}
}

Expand All @@ -81,13 +81,13 @@ func (server *Cluster) inferCreateParams(args []string) (*CreateParams, error) {

map_ := server.manager.Maps.FindMap(arg)
if opt.IsSome(map_) {
params.Map = opt.Some[string](arg)
params.Map = opt.Some(arg)
continue
}

preset := server.manager.FindPreset(arg, false)
if opt.IsSome(preset) {
params.Preset = opt.Some[string](preset.Value.Name)
params.Preset = opt.Some(preset.Value.Name)
continue
}

Expand Down Expand Up @@ -234,6 +234,10 @@ func (server *Cluster) RunCommand(ctx context.Context, command string, client *c
server.matches.Dequeue(client)
return true, "", nil

case "home":
server.GoHome(server.serverCtx, client)
return true, "", nil

case "help":
messages := []string{
fmt.Sprintf("%s: create a private game", game.Blue("#creategame")),
Expand Down
66 changes: 66 additions & 0 deletions services/go/svc/cluster/service/home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package service

import (
"context"
"fmt"

"github.com/cfoust/sour/pkg/game"
"github.com/cfoust/sour/pkg/maps"
"github.com/cfoust/sour/svc/cluster/clients"
"github.com/cfoust/sour/svc/cluster/servers"

"github.com/rs/zerolog/log"
)

func (server *Cluster) GoHome(ctx context.Context, client *clients.Client) error {
gameServer, err := server.manager.NewServer(ctx, "", true)
if err != nil {
log.Error().Err(err).Msg("failed to create home server")
return err
}

err = gameServer.StartAndWait(ctx)
if err != nil {
return err
}

gameServer.SendCommand(fmt.Sprintf("serverdesc \"Sour %s\"", game.Blue("Home")))
gameServer.SendCommand("publicserver 1")

// New empty map
gameServer.SendCommand("emptymap")

// Load the user's world or create a new one
editing := servers.NewEditingState()
gameServer.Editing = editing

map_ := maps.NewMap()
gz, err := map_.EncodeOGZ()
if err != nil {
return err
}

err = editing.LoadMapBytes(gz)
if err != nil {
return err
}

go editing.PollEdits(ctx)

connected, err := client.ConnectToServer(gameServer, false, true)
result := <-connected
if result == false || err != nil {
return fmt.Errorf("client never joined")
}

p := game.Packet{}
p.Put(game.N_SENDMAP)
p = append(p, gz...)

client.Send(game.GamePacket{
Channel: 2,
Data: p,
})

return nil
}
2 changes: 1 addition & 1 deletion services/go/svc/cluster/service/matchmaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func (m *Matchmaker) ReceiveQueues() <-chan DuelQueue {
func (m *Matchmaker) FindDuelType(name string) opt.Option[config.DuelType] {
for _, duelType := range m.duelTypes {
if duelType.Name == name || (len(name) == 0 && duelType.Default) {
return opt.Some[config.DuelType](duelType)
return opt.Some(duelType)
}
}

Expand Down
20 changes: 14 additions & 6 deletions services/go/svc/cluster/service/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (server *Cluster) GetClientInfo() []*servers.ClientExtInfo {
newClient := *client

// Replace with clientID
for client, _ := range server.Clients.State {
for client := range server.Clients.State {
if client.GetServer() == gameServer && int(client.GetClientNum()) == newClient.Client {
newClient.Client = int(client.Id)
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func (server *Cluster) PollDuels(ctx context.Context) {
}

server.Clients.Mutex.Lock()
for client, _ := range server.Clients.State {
for client := range server.Clients.State {
if client == winner || client == loser {
continue
}
Expand All @@ -216,7 +216,7 @@ func (server *Cluster) PollDuels(ctx context.Context) {
server.Clients.Mutex.Unlock()
case queue := <-queues:
server.Clients.Mutex.Lock()
for client, _ := range server.Clients.State {
for client := range server.Clients.State {
if client == queue.Client {
continue
}
Expand Down Expand Up @@ -498,7 +498,7 @@ func (server *Cluster) NotifyClientChange(ctx context.Context, client *clients.C
message := fmt.Sprintf("%s: %s (%s)", event, name, serverName)

server.Clients.Mutex.Lock()
for other, _ := range server.Clients.State {
for other := range server.Clients.State {
if other == client {
continue
}
Expand Down Expand Up @@ -529,7 +529,7 @@ func (server *Cluster) NotifyNameChange(ctx context.Context, client *clients.Cli
message := fmt.Sprintf("%s now known as %s [%s]", oldName, newName, serverName)

server.Clients.Mutex.Lock()
for other, _ := range server.Clients.State {
for other := range server.Clients.State {
if other == client {
continue
}
Expand Down Expand Up @@ -564,7 +564,7 @@ func (server *Cluster) ForwardGlobalChat(ctx context.Context, sender *clients.Cl
// To users on another server
otherMessage := fmt.Sprintf("%s [%s]: %s", name, serverName, game.Green(message))

for client, _ := range server.Clients.State {
for client := range server.Clients.State {
if client == sender {
continue
}
Expand Down Expand Up @@ -809,6 +809,14 @@ func (server *Cluster) PollClient(ctx context.Context, client *clients.Client) {
}
}

if message.Type() == game.N_TELEPORT {
//teleport := message.Contents().(*game.Teleport)
//log.Info().Msgf("client %s teleported to %d", client.Reference(), teleport.Destination)
//if teleport.Destination == 10 {
//go server.RunCommandWithTimeout(clientCtx, "creategame complex", client)
//}
}

if message.Type() == game.N_MAPCRC {
client.RestoreMessages()

Expand Down

0 comments on commit 9d139ff

Please sign in to comment.