From 34b31eb77fd9b73329ecaba618034f96ba58848f Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Thu, 20 Mar 2025 13:21:56 +0000 Subject: [PATCH] Make more use of convenience methods FossilOrigin-Name: 89fbd7a1520ecb65809b4c1bd10006ce9bb3a662cb7e7804abaf85d1ee1da63a --- src/Alias.h | 3 ++ src/Alias.m | 9 ++++ src/Client.h | 15 ++++++ src/Client.mm | 8 +++ src/Command.h | 3 ++ src/Command.mm | 9 ++++ src/ConsoleLine.h | 9 ++++ src/ConsoleLine.m | 18 +++++++ src/KeyMapping.h | 2 + src/KeyMapping.m | 5 ++ src/MD2.h | 5 +- src/MD2.mm | 11 +++-- src/MapModelInfo.h | 6 +++ src/MapModelInfo.m | 9 ++++ src/Menu.h | 2 + src/Menu.m | 5 ++ src/MenuItem.h | 2 + src/MenuItem.m | 5 ++ src/Projectile.h | 2 + src/Projectile.m | 4 ++ src/ResolverResult.h | 12 +++++ src/ResolverResult.mm | 18 +++++++ src/ResolverThread.h | 12 +++++ src/ResolverThread.mm | 44 +++++++++++++++++ src/ServerInfo.h | 1 + src/ServerInfo.mm | 5 ++ src/Variable.h | 6 +++ src/Variable.mm | 15 ++++++ src/clientextras.mm | 7 +-- src/clientgame.mm | 4 -- src/{client.mm => clients.mm} | 0 src/commands.mm | 22 ++++----- src/console.mm | 35 +++---------- src/entities.mm | 11 +++-- src/menus.mm | 8 +-- src/meson.build | 7 ++- src/protos.h | 4 +- src/renderextras.mm | 10 ++-- src/rendergl.mm | 4 +- src/rendermd2.mm | 29 ++++++----- src/server.mm | 17 ++----- src/serverbrowser.mm | 92 ++++++----------------------------- src/serverms.mm | 3 +- src/weapon.mm | 10 ++-- 44 files changed, 320 insertions(+), 188 deletions(-) create mode 100644 src/Client.h create mode 100644 src/Client.mm create mode 100644 src/ConsoleLine.h create mode 100644 src/ConsoleLine.m create mode 100644 src/ResolverResult.h create mode 100644 src/ResolverResult.mm create mode 100644 src/ResolverThread.h create mode 100644 src/ResolverThread.mm rename src/{client.mm => clients.mm} (100%) diff --git a/src/Alias.h b/src/Alias.h index 85b80bd..74788cf 100644 --- a/src/Alias.h +++ b/src/Alias.h @@ -6,6 +6,9 @@ OF_ASSUME_NONNULL_BEGIN @property (copy, nonatomic) OFString *action; @property (readonly, nonatomic) bool persisted; ++ (instancetype)aliasWithName:(OFString *)name + action:(OFString *)action + persisted:(bool)persisted; - (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name action:(OFString *)action diff --git a/src/Alias.m b/src/Alias.m index d8750f6..e1d0a81 100644 --- a/src/Alias.m +++ b/src/Alias.m @@ -1,6 +1,15 @@ #import "Alias.h" @implementation Alias ++ (instancetype)aliasWithName:(OFString *)name + action:(OFString *)action + persisted:(bool)persisted; +{ + return [[self alloc] initWithName:name + action:action + persisted:persisted]; +} + - (instancetype)initWithName:(OFString *)name action:(OFString *)action persisted:(bool)persisted diff --git a/src/Client.h b/src/Client.h new file mode 100644 index 0000000..28fddb8 --- /dev/null +++ b/src/Client.h @@ -0,0 +1,15 @@ +#import + +#import "cube.h" + +// server side version of "dynent" type +@interface Client: OFObject +@property (nonatomic) int type; +@property (nonatomic) ENetPeer *peer; +@property (copy, nonatomic) OFString *hostname; +@property (copy, nonatomic) OFString *mapvote; +@property (copy, nonatomic) OFString *name; +@property (nonatomic) int modevote; + ++ (instancetype)client; +@end diff --git a/src/Client.mm b/src/Client.mm new file mode 100644 index 0000000..32d1c88 --- /dev/null +++ b/src/Client.mm @@ -0,0 +1,8 @@ +#import "Client.h" + +@implementation Client ++ (instancetype)client +{ + return [[self alloc] init]; +} +@end diff --git a/src/Command.h b/src/Command.h index d22c778..6758593 100644 --- a/src/Command.h +++ b/src/Command.h @@ -6,6 +6,9 @@ OF_ASSUME_NONNULL_BEGIN @property (readonly, nonatomic) void (*function)(); @property (readonly, nonatomic) int argumentsTypes; ++ (instancetype)commandWithName:(OFString *)name + function:(void (*)())function + argumentsTypes:(int)argumentsTypes; - (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name function:(void (*)())function diff --git a/src/Command.mm b/src/Command.mm index d0d2d7c..be5b754 100644 --- a/src/Command.mm +++ b/src/Command.mm @@ -20,6 +20,15 @@ padArguments(OFArray *arguments, size_t count) } @implementation Command ++ (instancetype)commandWithName:(OFString *)name + function:(void (*)())function + argumentsTypes:(int)argumentsTypes +{ + return [[self alloc] initWithName:name + function:function + argumentsTypes:argumentsTypes]; +} + - (instancetype)initWithName:(OFString *)name function:(void (*)())function argumentsTypes:(int)argumentsTypes diff --git a/src/ConsoleLine.h b/src/ConsoleLine.h new file mode 100644 index 0000000..5648e9d --- /dev/null +++ b/src/ConsoleLine.h @@ -0,0 +1,9 @@ +#import + +@interface ConsoleLine: OFObject +@property (readonly, copy) OFString *text; +@property (readonly) int outtime; + ++ (instancetype)lineWithText:(OFString *)text outtime:(int)outtime; +- (instancetype)initWithText:(OFString *)text outtime:(int)outtime; +@end diff --git a/src/ConsoleLine.m b/src/ConsoleLine.m new file mode 100644 index 0000000..364eb2b --- /dev/null +++ b/src/ConsoleLine.m @@ -0,0 +1,18 @@ +#import "ConsoleLine.h" + +@implementation ConsoleLine ++ (instancetype)lineWithText:(OFString *)text outtime:(int)outtime +{ + return [[self alloc] initWithText:text outtime:outtime]; +} + +- (instancetype)initWithText:(OFString *)text outtime:(int)outtime +{ + self = [super init]; + + _text = [text copy]; + _outtime = outtime; + + return self; +} +@end diff --git a/src/KeyMapping.h b/src/KeyMapping.h index da020ea..955ad4f 100644 --- a/src/KeyMapping.h +++ b/src/KeyMapping.h @@ -7,6 +7,8 @@ OF_ASSUME_NONNULL_BEGIN @property (readonly, nonatomic) OFString *name; @property (copy, nonatomic) OFString *action; ++ (instancetype)mappingWithCode:(int)code name:(OFString *)name; +- (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithCode:(int)code name:(OFString *)name; @end diff --git a/src/KeyMapping.m b/src/KeyMapping.m index 1c03a25..f18c84b 100644 --- a/src/KeyMapping.m +++ b/src/KeyMapping.m @@ -1,6 +1,11 @@ #import "KeyMapping.h" @implementation KeyMapping ++ (instancetype)mappingWithCode:(int)code name:(OFString *)name +{ + return [[self alloc] initWithCode:code name:name]; +} + - (instancetype)initWithCode:(int)code name:(OFString *)name { self = [super init]; diff --git a/src/MD2.h b/src/MD2.h index 70a93b4..82095f7 100644 --- a/src/MD2.h +++ b/src/MD2.h @@ -10,13 +10,12 @@ OF_ASSUME_NONNULL_BEGIN @property (nonatomic) int mdlnum; @property (nonatomic) bool loaded; ++ (instancetype)md2; - (bool)loadWithIRI:(OFIRI *)IRI; - (void)renderWithLight:(OFVector3D)light frame:(int)frame range:(int)range - x:(float)x - y:(float)y - z:(float)z + position:(OFVector3D)position yaw:(float)yaw pitch:(float)pitch scale:(float)scale diff --git a/src/MD2.mm b/src/MD2.mm index a1a1ea3..1a2fd68 100644 --- a/src/MD2.mm +++ b/src/MD2.mm @@ -44,6 +44,11 @@ snap(int sn, float f) int _displaylistverts; } ++ (instancetype)md2 +{ + return [[self alloc] init]; +} + - (void)dealloc { if (_glCommands) @@ -126,9 +131,7 @@ snap(int sn, float f) - (void)renderWithLight:(OFVector3D)light frame:(int)frame range:(int)range - x:(float)x - y:(float)y - z:(float)z + position:(OFVector3D)position yaw:(float)yaw pitch:(float)pitch scale:(float)sc @@ -141,7 +144,7 @@ snap(int sn, float f) snap:sn]; glPushMatrix(); - glTranslatef(x, y, z); + glTranslatef(position.x, position.y, position.z); glRotatef(yaw + 180, 0, -1, 0); glRotatef(pitch, 0, 0, 1); diff --git a/src/MapModelInfo.h b/src/MapModelInfo.h index e82e764..67723bc 100644 --- a/src/MapModelInfo.h +++ b/src/MapModelInfo.h @@ -6,6 +6,12 @@ OF_ASSUME_NONNULL_BEGIN @property (nonatomic) int rad, h, zoff, snap; @property (copy, nonatomic) OFString *name; ++ (instancetype)infoWithRad:(int)rad + h:(int)h + zoff:(int)zoff + snap:(int)snap + name:(OFString *)name; +- (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithRad:(int)rad h:(int)h zoff:(int)zoff diff --git a/src/MapModelInfo.m b/src/MapModelInfo.m index e1254e9..f7cbe6b 100644 --- a/src/MapModelInfo.m +++ b/src/MapModelInfo.m @@ -1,6 +1,15 @@ #import "MapModelInfo.h" @implementation MapModelInfo ++ (instancetype)infoWithRad:(int)rad + h:(int)h + zoff:(int)zoff + snap:(int)snap + name:(OFString *)name +{ + return [[self alloc] initWithRad:rad h:h zoff:zoff snap:snap name:name]; +} + - (instancetype)initWithRad:(int)rad h:(int)h zoff:(int)zoff diff --git a/src/Menu.h b/src/Menu.h index 493234b..abd817c 100644 --- a/src/Menu.h +++ b/src/Menu.h @@ -10,6 +10,8 @@ OF_ASSUME_NONNULL_BEGIN @property (nonatomic) int mwidth; @property (nonatomic) int menusel; ++ (instancetype)menuWithName:(OFString *)name; +- (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name; @end diff --git a/src/Menu.m b/src/Menu.m index 930777d..4f14bfc 100644 --- a/src/Menu.m +++ b/src/Menu.m @@ -1,6 +1,11 @@ #import "Menu.h" @implementation Menu ++ (instancetype)menuWithName:(OFString *)name +{ + return [[self alloc] initWithName:name]; +} + - (instancetype)initWithName:(OFString *)name { self = [super init]; diff --git a/src/MenuItem.h b/src/MenuItem.h index 4ff4709..b9b4d6f 100644 --- a/src/MenuItem.h +++ b/src/MenuItem.h @@ -3,5 +3,7 @@ @interface MenuItem: OFObject @property (readonly, nonatomic) OFString *text, *action; ++ (instancetype)itemWithText:(OFString *)text action:(OFString *)action; +- (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithText:(OFString *)text action:(OFString *)action; @end diff --git a/src/MenuItem.m b/src/MenuItem.m index bba4e96..11b556f 100644 --- a/src/MenuItem.m +++ b/src/MenuItem.m @@ -1,6 +1,11 @@ #import "MenuItem.h" @implementation MenuItem ++ (instancetype)itemWithText:(OFString *)text action:(OFString *)action +{ + return [[self alloc] initWithText:text action:action]; +} + - (instancetype)initWithText:(OFString *)text action:(OFString *)action { self = [super init]; diff --git a/src/Projectile.h b/src/Projectile.h index fd98d64..73d8a8c 100644 --- a/src/Projectile.h +++ b/src/Projectile.h @@ -8,4 +8,6 @@ @property (nonatomic) DynamicEntity *owner; @property (nonatomic) int gun; @property (nonatomic) bool inuse, local; + ++ (instancetype)projectile; @end diff --git a/src/Projectile.m b/src/Projectile.m index 6e73a88..d14d72c 100644 --- a/src/Projectile.m +++ b/src/Projectile.m @@ -1,4 +1,8 @@ #import "Projectile.h" @implementation Projectile ++ (instancetype)projectile +{ + return [[self alloc] init]; +} @end diff --git a/src/ResolverResult.h b/src/ResolverResult.h new file mode 100644 index 0000000..2ae1481 --- /dev/null +++ b/src/ResolverResult.h @@ -0,0 +1,12 @@ +#import + +#import "cube.h" + +@interface ResolverResult: OFObject +@property (readonly, nonatomic) OFString *query; +@property (readonly, nonatomic) ENetAddress address; + ++ (instancetype)resultWithQuery:(OFString *)query address:(ENetAddress)address; +- (instancetype)init OF_UNAVAILABLE; +- (instancetype)initWithQuery:(OFString *)query address:(ENetAddress)address; +@end diff --git a/src/ResolverResult.mm b/src/ResolverResult.mm new file mode 100644 index 0000000..98391c7 --- /dev/null +++ b/src/ResolverResult.mm @@ -0,0 +1,18 @@ +#import "ResolverResult.h" + +@implementation ResolverResult ++ (instancetype)resultWithQuery:(OFString *)query address:(ENetAddress)address +{ + return [[self alloc] initWithQuery:query address:address]; +} + +- (instancetype)initWithQuery:(OFString *)query address:(ENetAddress)address +{ + self = [super init]; + + _query = query; + _address = address; + + return self; +} +@end diff --git a/src/ResolverThread.h b/src/ResolverThread.h new file mode 100644 index 0000000..884bc74 --- /dev/null +++ b/src/ResolverThread.h @@ -0,0 +1,12 @@ +#import + +@interface ResolverThread: OFThread +{ + volatile bool _stop; +} + +@property (copy, nonatomic) OFString *query; +@property (nonatomic) int starttime; + +- (void)stop; +@end diff --git a/src/ResolverThread.mm b/src/ResolverThread.mm new file mode 100644 index 0000000..3dd75c9 --- /dev/null +++ b/src/ResolverThread.mm @@ -0,0 +1,44 @@ +#import "ResolverThread.h" + +#import "ResolverResult.h" + +extern SDL_sem *resolversem; +extern OFMutableArray *resolverqueries; +extern OFMutableArray *resolverresults; + +@implementation ResolverThread +- (id)main +{ + while (!_stop) { + SDL_SemWait(resolversem); + + @synchronized(ResolverThread.class) { + if (resolverqueries.count == 0) + continue; + + _query = resolverqueries.lastObject; + [resolverqueries removeLastObject]; + _starttime = lastmillis; + } + + ENetAddress address = { ENET_HOST_ANY, CUBE_SERVINFO_PORT }; + enet_address_set_host(&address, _query.UTF8String); + + @synchronized(ResolverThread.class) { + [resolverresults + addObject:[ResolverResult resultWithQuery:_query + address:address]]; + + _query = NULL; + _starttime = 0; + } + } + + return nil; +} + +- (void)stop +{ + _stop = true; +} +@end diff --git a/src/ServerInfo.h b/src/ServerInfo.h index 57a6a15..ddad319 100644 --- a/src/ServerInfo.h +++ b/src/ServerInfo.h @@ -10,6 +10,7 @@ @property (nonatomic) int mode, numplayers, ping, protocol, minremain; @property (nonatomic) ENetAddress address; ++ (instancetype)infoWithName:(OFString *)name; - (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name; @end diff --git a/src/ServerInfo.mm b/src/ServerInfo.mm index 229d464..b774b1d 100644 --- a/src/ServerInfo.mm +++ b/src/ServerInfo.mm @@ -3,6 +3,11 @@ #include "cube.h" @implementation ServerInfo ++ (instancetype)infoWithName:(OFString *)name; +{ + return [[self alloc] initWithName:name]; +} + - (instancetype)initWithName:(OFString *)name { self = [super init]; diff --git a/src/Variable.h b/src/Variable.h index 92c4bff..2796945 100644 --- a/src/Variable.h +++ b/src/Variable.h @@ -8,6 +8,12 @@ OF_ASSUME_NONNULL_BEGIN @property (readonly, nonatomic) void (*__cdecl function)(); @property (readonly, nonatomic) bool persisted; ++ (instancetype)variableWithName:(OFString *)name + min:(int)min + max:(int)max + storage:(int *)storage + function:(void (*__cdecl)())function + persisted:(bool)persisted; - (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name min:(int)min diff --git a/src/Variable.mm b/src/Variable.mm index e3920e7..7289942 100644 --- a/src/Variable.mm +++ b/src/Variable.mm @@ -3,6 +3,21 @@ #include "cube.h" @implementation Variable ++ (instancetype)variableWithName:(OFString *)name + min:(int)min + max:(int)max + storage:(int *)storage + function:(void (*__cdecl)())function + persisted:(bool)persisted +{ + return [[self alloc] initWithName:name + min:min + max:max + storage:storage + function:function + persisted:persisted]; +} + - (instancetype)initWithName:(OFString *)name min:(int)min max:(int)max diff --git a/src/clientextras.mm b/src/clientextras.mm index 1a37bd8..07a89ac 100644 --- a/src/clientextras.mm +++ b/src/clientextras.mm @@ -71,8 +71,9 @@ renderclient( scale *= 32; mz -= 1.9f; } - rendermodel(mdlname, frame[n], range[n], 0, 1.5f, d.o.x, mz, d.o.y, - d.yaw + 90, d.pitch / 2, team, scale, speed, 0, basetime); + rendermodel(mdlname, frame[n], range[n], 0, 1.5f, + OFMakeVector3D(d.o.x, mz, d.o.y), d.yaw + 90, d.pitch / 2, team, + scale, speed, 0, basetime); } extern int democlientnum; @@ -161,7 +162,7 @@ renderscores() addteamscore(player); if (!demoplayback) addteamscore(player1); - OFMutableString *teamScores = [[OFMutableString alloc] init]; + OFMutableString *teamScores = [OFMutableString string]; for (size_t j = 0; j < teamsUsed; j++) [teamScores appendFormat:@"[ %@: %d ]", teamName[j], teamScore[j]]; diff --git a/src/clientgame.mm b/src/clientgame.mm index 527c686..49e1755 100644 --- a/src/clientgame.mm +++ b/src/clientgame.mm @@ -471,8 +471,6 @@ getclient(int cn) // ensure valid entity neterr(@"clientnum"); return nil; } - if (players == nil) - players = [[OFMutableArray alloc] init]; while (cn >= players.count) [players addObject:[OFNull null]]; return (players[cn] != [OFNull null] ? players[cn] @@ -484,8 +482,6 @@ setclient(int cn, id client) { if (cn < 0 || cn >= MAXCLIENTS) neterr(@"clientnum"); - if (players == nil) - players = [[OFMutableArray alloc] init]; while (cn >= players.count) [players addObject:[OFNull null]]; players[cn] = client; diff --git a/src/client.mm b/src/clients.mm similarity index 100% rename from src/client.mm rename to src/clients.mm diff --git a/src/commands.mm b/src/commands.mm index 695d477..c862470 100644 --- a/src/commands.mm +++ b/src/commands.mm @@ -20,9 +20,7 @@ alias(OFString *name, OFString *action) Alias *alias = identifiers[name]; if (alias == nil) { - alias = [[Alias alloc] initWithName:name - action:action - persisted:true]; + alias = [Alias aliasWithName:name action:action persisted:true]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; @@ -42,12 +40,12 @@ int variable(OFString *name, int min, int cur, int max, int *storage, void (*function)(), bool persisted) { - Variable *variable = [[Variable alloc] initWithName:name - min:min - max:max - storage:storage - function:function - persisted:persisted]; + Variable *variable = [Variable variableWithName:name + min:min + max:max + storage:storage + function:function + persisted:persisted]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; @@ -89,9 +87,9 @@ getalias(OFString *name) bool addcommand(OFString *name, void (*function)(), int argumentsTypes) { - Command *command = [[Command alloc] initWithName:name - function:function - argumentsTypes:argumentsTypes]; + Command *command = [Command commandWithName:name + function:function + argumentsTypes:argumentsTypes]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; diff --git a/src/console.mm b/src/console.mm index 65c717b..e4be446 100644 --- a/src/console.mm +++ b/src/console.mm @@ -4,35 +4,12 @@ #include +#import "ConsoleLine.h" #import "KeyMapping.h" #import "OFString+Cube.h" -@interface ConsoleLine: OFObject -@property (readonly, copy) OFString *text; -@property (readonly) int outtime; - -- (instancetype)initWithText:(OFString *)text outtime:(int)outtime; -@end - static OFMutableArray *conlines; -@implementation ConsoleLine -- (instancetype)initWithText:(OFString *)text outtime:(int)outtime -{ - self = [super init]; - - _text = [text copy]; - _outtime = outtime; - - return self; -} - -- (OFString *)description -{ - return _text; -} -@end - const int ndraw = 5; const int WORDWRAP = 80; int conskip = 0; @@ -59,7 +36,7 @@ conline(OFString *sf, bool highlight) // add a line to the console buffer text = [conlines.lastObject.text mutableCopy]; [conlines removeLastObject]; } else - text = [[OFMutableString alloc] init]; + text = [OFMutableString string]; if (highlight) // show line in a different colour, for chat etc. @@ -70,8 +47,8 @@ conline(OFString *sf, bool highlight) // add a line to the console buffer if (conlines == nil) conlines = [[OFMutableArray alloc] init]; - [conlines insertObject:[[ConsoleLine alloc] initWithText:text - outtime:lastmillis] + [conlines insertObject:[ConsoleLine lineWithText:text + outtime:lastmillis] atIndex:0]; puts(text.UTF8String); @@ -135,8 +112,8 @@ keymap(OFString *code, OFString *key, OFString *action) if (keyMappings == nil) keyMappings = [[OFMutableArray alloc] init]; - KeyMapping *mapping = - [[KeyMapping alloc] initWithCode:code.cube_intValue name:key]; + KeyMapping *mapping = [KeyMapping mappingWithCode:code.cube_intValue + name:key]; mapping.action = action; [keyMappings addObject:mapping]; } diff --git a/src/entities.mm b/src/entities.mm index 436c3b2..078fede 100644 --- a/src/entities.mm +++ b/src/entities.mm @@ -26,8 +26,9 @@ void renderent(entity &e, OFString *mdlname, float z, float yaw, int frame = 0, int numf = 1, int basetime = 0, float speed = 10.0f) { - rendermodel(mdlname, frame, numf, 0, 1.1f, e.x, z + S(e.x, e.y)->floor, - e.y, yaw, 0, false, 1.0f, speed, 0, basetime); + rendermodel(mdlname, frame, numf, 0, 1.1f, + OFMakeVector3D(e.x, z + S(e.x, e.y)->floor, e.y), yaw, 0, false, + 1.0f, speed, 0, basetime); } void @@ -43,8 +44,10 @@ renderentities() if (mmi == nil) continue; rendermodel(mmi.name, 0, 1, e.attr4, (float)mmi.rad, - e.x, (float)S(e.x, e.y)->floor + mmi.zoff + e.attr3, - e.y, (float)((e.attr1 + 7) - (e.attr1 + 7) % 15), 0, + OFMakeVector3D(e.x, + (float)S(e.x, e.y)->floor + mmi.zoff + e.attr3, + e.y), + (float)((e.attr1 + 7) - (e.attr1 + 7) % 15), 0, false, 1.0f, 10.0f, mmi.snap); } else { if (OUTBORD(e.x, e.y)) diff --git a/src/menus.mm b/src/menus.mm index 2cc7ae3..013fce5 100644 --- a/src/menus.mm +++ b/src/menus.mm @@ -95,7 +95,7 @@ newmenu(OFString *name) if (menus == nil) menus = [[OFMutableArray alloc] init]; - [menus addObject:[[Menu alloc] initWithName:name]]; + [menus addObject:[Menu menuWithName:name]]; } COMMAND(newmenu, ARG_1STR) @@ -105,7 +105,7 @@ menumanual(int m, int n, OFString *text) if (n == 0) [menus[m].items removeAllObjects]; - MenuItem *item = [[MenuItem alloc] initWithText:text action:@""]; + MenuItem *item = [MenuItem itemWithText:text action:@""]; [menus[m].items addObject:item]; } @@ -115,8 +115,8 @@ menuitem(OFString *text, OFString *action) Menu *menu = menus.lastObject; MenuItem *item = - [[MenuItem alloc] initWithText:text - action:(action.length > 0 ? action : text)]; + [MenuItem itemWithText:text + action:(action.length > 0 ? action : text)]; [menu.items addObject:item]; } COMMAND(menuitem, ARG_2STR) diff --git a/src/meson.build b/src/meson.build index d969438..3d2e1bf 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,7 +1,9 @@ executable('client', [ 'Alias.m', + 'Client.mm', 'Command.mm', + 'ConsoleLine.m', 'Cube.mm', 'DynamicEntity.mm', 'Identifier.m', @@ -12,9 +14,11 @@ executable('client', 'MenuItem.m', 'OFString+Cube.mm', 'Projectile.m', + 'ResolverResult.mm', + 'ResolverThread.mm', 'ServerInfo.mm', 'Variable.mm', - 'client.mm', + 'clients.mm', 'clientextras.mm', 'clientgame.mm', 'clients2c.mm', @@ -62,6 +66,7 @@ executable('client', executable('server', [ + 'Client.mm', 'server.mm', 'serverms.mm', 'serverutil.mm', diff --git a/src/protos.h b/src/protos.h index b863eb5..17ba55a 100644 --- a/src/protos.h +++ b/src/protos.h @@ -210,8 +210,8 @@ extern void cleansound(); // rendermd2 extern void rendermodel(OFString *mdl, int frame, int range, int tex, float rad, - float x, float y, float z, float yaw, float pitch, bool teammate, - float scale, float speed, int snap = 0, int basetime = 0); + OFVector3D position, float yaw, float pitch, bool teammate, float scale, + float speed, int snap = 0, int basetime = 0); @class MapModelInfo; extern MapModelInfo *getmminfo(int i); diff --git a/src/renderextras.mm b/src/renderextras.mm index ffe2a47..54a3be7 100644 --- a/src/renderextras.mm +++ b/src/renderextras.mm @@ -192,11 +192,11 @@ renderents() // show sparkly thingies for map entities in edit mode int e = closestent(); if (e >= 0) { entity &c = ents[e]; - closeent = [[OFString alloc] - initWithFormat:@"closest entity = %@ (%d, %d, %d, %d), " - @"selection = (%d, %d)", - entnames[c.type], c.attr1, c.attr2, c.attr3, c.attr4, - getvar(@"selxs"), getvar(@"selys")]; + closeent = + [OFString stringWithFormat:@"closest entity = %@ (%d, %d, " + @"%d, %d), selection = (%d, %d)", + entnames[c.type], c.attr1, c.attr2, c.attr3, + c.attr4, getvar(@"selxs"), getvar(@"selys")]; } } diff --git a/src/rendergl.mm b/src/rendergl.mm index 5c36aa5..3f9c9b0 100644 --- a/src/rendergl.mm +++ b/src/rendergl.mm @@ -359,8 +359,8 @@ void drawhudmodel(int start, int end, float speed, int base) { rendermodel(hudgunnames[player1.gunselect], start, end, 0, 1.0f, - player1.o.x, player1.o.z, player1.o.y, player1.yaw + 90, - player1.pitch, false, 1.0f, speed, 0, base); + OFMakeVector3D(player1.o.x, player1.o.z, player1.o.y), + player1.yaw + 90, player1.pitch, false, 1.0f, speed, 0, base); } void diff --git a/src/rendermd2.mm b/src/rendermd2.mm index 124ca37..0d09a07 100644 --- a/src/rendermd2.mm +++ b/src/rendermd2.mm @@ -41,9 +41,9 @@ loadmodel(OFString *name) if (m != nil) return m; - m = [[MD2 alloc] init]; + m = [MD2 md2]; m.mdlnum = modelnum++; - m.mmi = [[MapModelInfo alloc] initWithRad:2 h:2 zoff:0 snap:0 name:@""]; + m.mmi = [MapModelInfo infoWithRad:2 h:2 zoff:0 snap:0 name:@""]; m.loadname = name; if (mdllookup == nil) @@ -60,11 +60,11 @@ mapmodel( { MD2 *m = loadmodel([name stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]); - m.mmi = [[MapModelInfo alloc] initWithRad:rad.cube_intValue - h:h.cube_intValue - zoff:zoff.cube_intValue - snap:snap.cube_intValue - name:m.loadname]; + m.mmi = [MapModelInfo infoWithRad:rad.cube_intValue + h:h.cube_intValue + zoff:zoff.cube_intValue + snap:snap.cube_intValue + name:m.loadname]; if (mapmodels == nil) mapmodels = [[OFMutableArray alloc] init]; @@ -87,13 +87,14 @@ getmminfo(int i) } void -rendermodel(OFString *mdl, int frame, int range, int tex, float rad, float x, - float y, float z, float yaw, float pitch, bool teammate, float scale, +rendermodel(OFString *mdl, int frame, int range, int tex, float rad, + OFVector3D position, float yaw, float pitch, bool teammate, float scale, float speed, int snap, int basetime) { MD2 *m = loadmodel(mdl); - if (isoccluded(player1.o.x, player1.o.y, x - rad, z - rad, rad * 2)) + if (isoccluded(player1.o.x, player1.o.y, position.x - rad, + position.z - rad, rad * 2)) return; delayedload(m); @@ -102,8 +103,8 @@ rendermodel(OFString *mdl, int frame, int range, int tex, float rad, float x, glBindTexture(GL_TEXTURE_2D, tex ? lookuptexture(tex, &xs, &ys) : FIRSTMDL + m.mdlnum); - int ix = (int)x; - int iy = (int)z; + int ix = (int)position.x; + int iy = (int)position.z; OFVector3D light = OFMakeVector3D(1, 1, 1); if (!OUTBORD(ix, iy)) { @@ -124,9 +125,7 @@ rendermodel(OFString *mdl, int frame, int range, int tex, float rad, float x, [m renderWithLight:light frame:frame range:range - x:x - y:y - z:z + position:position yaw:yaw pitch:pitch scale:scale diff --git a/src/server.mm b/src/server.mm index e56753c..eb30738 100644 --- a/src/server.mm +++ b/src/server.mm @@ -3,21 +3,10 @@ #include "cube.h" +#import "Client.h" + enum { ST_EMPTY, ST_LOCAL, ST_TCPIP }; -// server side version of "dynent" type -@interface Client: OFObject -@property (nonatomic) int type; -@property (nonatomic) ENetPeer *peer; -@property (copy, nonatomic) OFString *hostname; -@property (copy, nonatomic) OFString *mapvote; -@property (copy, nonatomic) OFString *name; -@property (nonatomic) int modevote; -@end - -@implementation Client -@end - static OFMutableArray *clients; int maxclients = 8; @@ -359,7 +348,7 @@ addclient() if (client.type == ST_EMPTY) return client; - Client *client = [[Client alloc] init]; + Client *client = [Client client]; if (clients == nil) clients = [[OFMutableArray alloc] init]; diff --git a/src/serverbrowser.mm b/src/serverbrowser.mm index 39e4ade..04bcc63 100644 --- a/src/serverbrowser.mm +++ b/src/serverbrowser.mm @@ -4,80 +4,16 @@ #include "SDL_thread.h" #include "cube.h" +#import "ResolverResult.h" +#import "ResolverThread.h" #import "ServerInfo.h" -@interface ResolverThread: OFThread -{ - volatile bool _stop; -} - -@property (copy, nonatomic) OFString *query; -@property (nonatomic) int starttime; -@end - -@interface ResolverResult: OFObject -@property (readonly, nonatomic) OFString *query; -@property (readonly, nonatomic) ENetAddress address; - -- (instancetype)init OF_UNAVAILABLE; -- (instancetype)initWithQuery:(OFString *)query address:(ENetAddress)address; -@end - static OFMutableArray *resolverthreads; -static OFMutableArray *resolverqueries; -static OFMutableArray *resolverresults; -static SDL_sem *resolversem; +OFMutableArray *resolverqueries; +OFMutableArray *resolverresults; +SDL_sem *resolversem; static int resolverlimit = 1000; -@implementation ResolverThread -- (id)main -{ - while (!_stop) { - SDL_SemWait(resolversem); - - @synchronized(ResolverThread.class) { - if (resolverqueries.count == 0) - continue; - - _query = resolverqueries.lastObject; - [resolverqueries removeLastObject]; - _starttime = lastmillis; - } - - ENetAddress address = { ENET_HOST_ANY, CUBE_SERVINFO_PORT }; - enet_address_set_host(&address, _query.UTF8String); - - @synchronized(ResolverThread.class) { - [resolverresults addObject:[[ResolverResult alloc] - initWithQuery:_query - address:address]]; - - _query = NULL; - _starttime = 0; - } - } - - return nil; -} - -- (void)stop -{ - _stop = true; -} -@end - -@implementation ResolverResult -- (instancetype)initWithQuery:(OFString *)query address:(ENetAddress)address -{ - self = [super init]; - - _query = query; - _address = address; - - return self; -} -@end - void resolverinit(int threads, int limit) { @@ -88,7 +24,7 @@ resolverinit(int threads, int limit) resolversem = SDL_CreateSemaphore(0); while (threads > 0) { - ResolverThread *rt = [[ResolverThread alloc] init]; + ResolverThread *rt = [ResolverThread thread]; rt.name = @"resolverthread"; [resolverthreads addObject:rt]; [rt start]; @@ -104,7 +40,7 @@ resolverstop(size_t i, bool restart) [rt stop]; if (restart) { - rt = [[ResolverThread alloc] init]; + rt = [ResolverThread thread]; rt.name = @"resolverthread"; resolverthreads[i] = rt; @@ -187,7 +123,7 @@ addserver(OFString *servername) if (servers == nil) servers = [[OFMutableArray alloc] init]; - [servers addObject:[[ServerInfo alloc] initWithName:servername]]; + [servers addObject:[ServerInfo infoWithName:servername]]; } void @@ -280,19 +216,19 @@ refreshservers() ServerInfo *si, size_t i, bool *stop) { if (si.address.host != ENET_HOST_ANY && si.ping != 9999) { if (si.protocol != PROTOCOL_VERSION) - si.full = [[OFString alloc] - initWithFormat: + si.full = [OFString + stringWithFormat: @"%@ [different cube protocol]", si.name]; else - si.full = [[OFString alloc] - initWithFormat:@"%d\t%d\t%@, %@: %@ %@", + si.full = [OFString + stringWithFormat:@"%d\t%d\t%@, %@: %@ %@", si.ping, si.numplayers, si.map.length > 0 ? si.map : @"[unknown]", modestr(si.mode), si.name, si.sdesc]; } else - si.full = [[OFString alloc] - initWithFormat: + si.full = [OFString + stringWithFormat: (si.address.host != ENET_HOST_ANY ? @"%@ [waiting for server response]" : @"%@ [unknown host]\t"), diff --git a/src/serverms.mm b/src/serverms.mm index 1a0be2c..91e79ad 100644 --- a/src/serverms.mm +++ b/src/serverms.mm @@ -158,8 +158,7 @@ servermsinit(OFString *master_, OFString *sdesc, bool listen) if (!mid) mid = master; masterpath = @(mid); - masterbase = [[OFString alloc] initWithUTF8String:master - length:mid - master]; + masterbase = [OFString stringWithUTF8String:master length:mid - master]; serverdesc = sdesc; if (listen) { diff --git a/src/weapon.mm b/src/weapon.mm index d6e2411..6f6e6c9 100644 --- a/src/weapon.mm +++ b/src/weapon.mm @@ -148,10 +148,8 @@ newprojectile(OFVector3D &from, OFVector3D &to, float speed, bool local, for (size_t i = 0; i < MAXPROJ; i++) { Projectile *p = projs[i]; - if (p == nil) { - p = [[Projectile alloc] init]; - projs[i] = p; - } + if (p == nil) + projs[i] = p = [Projectile projectile]; if (p.inuse) continue; @@ -354,8 +352,8 @@ hitpush(int target, int damage, DynamicEntity *d, DynamicEntity *at, } void -raydamage( - DynamicEntity *o, const OFVector3D &from, const OFVector3D &to, DynamicEntity *d, int i) +raydamage(DynamicEntity *o, const OFVector3D &from, const OFVector3D &to, + DynamicEntity *d, int i) { if (o.state != CS_ALIVE) return;