aboutsummaryrefslogtreecommitdiff
path: root/.github/copilot-instructions.md
blob: 2249ade171306ab985824683159a047efbadf6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# GitHub Copilot Instructions for OpenMB

This document provides context and instructions for GitHub Copilot and other AI coding assistants when working on the OpenMB project.

## Project Overview

OpenMB is a C++20 game engine project that follows strict coding standards and conventions. When generating code or suggestions, always adhere to the guidelines outlined in this document.

## Core Principles

1. **Consistency over personal preference** - Follow existing patterns in the codebase
2. **Modern C++20** - Use modern C++ features appropriately
3. **Cross-platform compatibility** - Code must work on Windows, Linux, and macOS
4. **Clear documentation** - All public APIs should have Doxygen comments
5. **Memory safety** - Prefer RAII and smart pointers over manual memory management

## Code Style Requirements

### Formatting

**CRITICAL: All code must follow these formatting rules:**

```cpp
// Braces - ALWAYS on their own line (Allman/BSD style)
void myFunction()
{
    if (condition)
    {
        doSomething();
    }
    else
    {
        doSomethingElse();
    }
}

// NOT this:
void myFunction() {
    if (condition) {
        doSomething();
    } else {
        doSomethingElse();
    }
}
```

- **Indentation**: 4 spaces (NEVER tabs)
- **Line length**: Maximum 120 columns
- **Pointer alignment**: Left-aligned with type (`Ptr* ptr`, not `Ptr *ptr`)
- **Spacing**: Space after control keywords (`if (`, `for (`, `while (`), no space after function names

### Naming Conventions

**CRITICAL: Follow these naming rules exactly:**

```cpp
// Classes and Structs - PascalCase
class PlayerManager { };
struct GameState { };

// Functions and Methods - camelCase
void processInput();
bool isGameRunning() const;

// Member Variables - 'm' prefix + PascalCase
class MyClass
{
private:
    int mHealthPoints;
    std::string mPlayerName;
    bool mIsActive;
};

// Local Variables and Parameters - camelBack
void updatePlayer(const Ptr& playerPtr, int deltaTime)
{
    int currentHealth = getHealth(playerPtr);
    float movementSpeed = calculateSpeed();
}

// Namespaces - PascalCase with MB prefix for project namespaces
namespace MBWorld { }
namespace MBMechanics { }
namespace MBBase { }

// Constants and Enums - PascalCase
enum class GameState
{
    Loading,
    Running,
    Paused
};

constexpr int MaxPlayers = 64;
```

### Include Guards

**ALWAYS use this format for include guards:**

```cpp
#ifndef OPENMB_<PATH_TO_FILE>_H
#define OPENMB_<PATH_TO_FILE>_H

// Header content

#endif
```

Example: For file `apps/openmb/mbworld/player.hpp`
```cpp
#ifndef OPENMB_APPS_OPENMB_MBWORLD_PLAYER_H
#define OPENMB_APPS_OPENMB_MBWORLD_PLAYER_H
```

### Include Order

**ALWAYS organize includes in this order:**

```cpp
#include "relatedheader.hpp"  // Related header first (for .cpp files)

#include <cstdint>            // C system headers
#include <cmath>

#include <string>             // C++ standard library
#include <vector>
#include <memory>

#include <osg/Vec3f>          // External libraries
#include <MyGUI_Widget.h>

#include <components/esm/refid.hpp>  // Project headers
#include "../mbbase/environment.hpp"
```

### Namespace Structure

```cpp
namespace MBWorld
{
    // All namespace content indented
    class MyClass
    {
    public:  // Access modifiers outdented by 4 spaces
        MyClass();
        
    private:
        int mData;
    };
    
    void freeFunction();
}
```

## Code Generation Guidelines

### When Creating New Classes

**ALWAYS generate headers like this:**

```cpp
#ifndef OPENMB_APPS_OPENMB_MBWORLD_MYCLASS_H
#define OPENMB_APPS_OPENMB_MBWORLD_MYCLASS_H

#include <string>
#include <vector>

namespace MBWorld
{
    /// Brief description of what this class does
    class MyClass
    {
    public:
        MyClass();
        ~MyClass() = default;

        /// Brief description of method
        /// \param input Description of parameter
        /// \return Description of return value
        bool processData(int input);

        /// Get the current value
        int getValue() const;

    private:
        int mValue;
        std::string mName;
        std::vector<int> mData;
    };
}

#endif
```

**And implementations like this:**

```cpp
#include "myclass.hpp"

#include <algorithm>
#include <stdexcept>

namespace MBWorld
{
    MyClass::MyClass()
        : mValue(0)
        , mName()
        , mData()
    {
    }

    bool MyClass::processData(int input)
    {
        if (input < 0)
        {
            throw std::runtime_error("Invalid input");
        }

        mValue = input;
        return true;
    }

    int MyClass::getValue() const
    {
        return mValue;
    }
}
```

### Modern C++ Patterns to Use

```cpp
// Smart pointers instead of raw pointers
std::unique_ptr<Dialog> mDialog;
std::shared_ptr<Resource> mResource;

// Range-based for loops
for (const auto& item : container)
{
    process(item);
}

// Auto for complex types
auto iterator = myMap.find(key);
auto result = complexFunction();

// nullptr instead of NULL
Ptr* ptr = nullptr;

// override keyword for virtual functions
void myMethod() override;

// constexpr for compile-time constants
constexpr int BufferSize = 1024;

// Const correctness
void processData(const std::string& input) const;

// Structured bindings (C++17)
for (const auto& [key, value] : myMap)
{
    // ...
}
```

### Patterns to AVOID

```cpp
// DON'T use manual memory management
MyClass* obj = new MyClass();  // BAD
delete obj;                     // BAD

// DO use smart pointers or stack allocation
auto obj = std::make_unique<MyClass>();  // GOOD
MyClass obj;                              // GOOD (when appropriate)

// DON'T omit braces for single-line blocks
if (condition)
    doSomething();  // BAD

// DO always use braces
if (condition)
{
    doSomething();  // GOOD
}

// DON'T use raw pointers for ownership
void takeOwnership(MyClass* ptr);  // BAD

// DO use smart pointers
void takeOwnership(std::unique_ptr<MyClass> ptr);  // GOOD

// DON'T ignore const correctness
void getData(Ptr& ptr);  // BAD if not modifying

// DO use const when appropriate
void getData(const Ptr& ptr) const;  // GOOD
```

### Virtual Functions and Inheritance

```cpp
class Base
{
public:
    virtual ~Base() = default;
    
    /// Pure virtual function
    virtual void mustImplement() = 0;
    
    /// Virtual function with default implementation
    virtual void canOverride();
};

class Derived : public Base
{
public:
    // Always use override keyword
    void mustImplement() override;
    void canOverride() override;
    
    // Use final to prevent further overriding
    void finalMethod() final;
};
```

### Exception Handling

```cpp
// Throw exceptions for error conditions
void MyClass::validate()
{
    if (!isValid())
    {
        throw std::runtime_error("Invalid state detected");
    }
    
    if (mData.empty())
    {
        throw std::logic_error("Data cannot be empty");
    }
}

// Use specific exception types
void processFile(const std::string& filename)
{
    if (!fileExists(filename))
    {
        throw std::invalid_argument("File does not exist: " + filename);
    }
}
```

### Documentation Comments

**ALWAYS add Doxygen comments for:**
- All public classes
- All public methods
- All public member variables
- Complex private methods

```cpp
/// Manages the game's resource loading and caching
///
/// This class handles asynchronous loading of game resources
/// and maintains a cache to avoid redundant disk access.
class ResourceManager
{
public:
    /// Load a resource by its identifier
    ///
    /// \param resourceId The unique identifier of the resource
    /// \param async If true, load asynchronously
    /// \return Pointer to the loaded resource, or nullptr if failed
    /// \throws std::runtime_error if resource format is invalid
    Resource* loadResource(const ESM::RefId& resourceId, bool async = false);
    
    int mCacheSize; ///< Maximum number of cached resources
};
```

## Common OpenMB Patterns

### Using Project Types

```cpp
// ESM::RefId for game object identifiers
ESM::RefId mItemId;
ESM::RefId mSpellId;

// Ptr and ConstPtr for game object references
void processObject(const MBWorld::ConstPtr& ptr);
void modifyObject(MBWorld::Ptr& ptr);

// Common project classes
MBWorld::CellStore* cell;
MBBase::Environment& env = MBBase::Environment::get();
```

### Initialization Lists

**ALWAYS use initialization lists for constructors:**

```cpp
MyClass::MyClass(int value, const std::string& name)
    : mValue(value)
    , mName(name)
    , mCounter(0)
    , mIsActive(false)
{
    // Constructor body for additional logic only
}
```

### Const Correctness

```cpp
class DataManager
{
public:
    // Non-const methods for modification
    void addData(int value);
    void clearData();
    
    // Const methods for reading only
    int getDataCount() const;
    bool hasData() const;
    const std::vector<int>& getData() const;
    
    // Return const reference when not modifying
    const std::string& getName() const { return mName; }
    
private:
    std::vector<int> mData;
    std::string mName;
};
```

### Error Handling in Base Classes

```cpp
// Use exceptions for unsupported operations in base classes
class GameObjectBase
{
public:
    virtual InventoryStore& getInventoryStore(const Ptr& ptr) const
    {
        throw std::runtime_error("This object type does not have an inventory");
    }
    
    virtual float getWeight(const Ptr& ptr) const
    {
        throw std::runtime_error("This object type does not have weight");
    }
};
```

## File Organization

### Header File Template

```cpp
#ifndef OPENMB_<PATH>_H
#define OPENMB_<PATH>_H

// Includes in proper order
#include <standard/library>

#include <external/library>

#include <components/project/header.hpp>

// Forward declarations
namespace MBWorld
{
    class Ptr;
}

namespace <YourNamespace>
{
    /// Class documentation
    class ClassName
    {
    public:
        // Public interface
        
    protected:
        // Protected members
        
    private:
        // Private members
        // All member variables with 'm' prefix
    };
}

#endif
```

### Implementation File Template

```cpp
#include "classname.hpp"

#include <algorithm>
#include <stdexcept>

#include "../other/project/headers.hpp"

namespace <YourNamespace>
{
    ClassName::ClassName()
        : mMember1()
        , mMember2(0)
    {
    }
    
    void ClassName::method()
    {
        // Implementation
    }
}
```

## Platform Considerations

```cpp
// Avoid platform-specific code when possible
// If necessary, use clear preprocessor guards

#ifdef _WIN32
    // Windows-specific code
#elif defined(__linux__)
    // Linux-specific code
#elif defined(__APPLE__)
    // macOS-specific code
#else
    #error "Unsupported platform"
#endif

// Prefer cross-platform solutions
#include <filesystem>  // C++17 cross-platform filesystem
namespace fs = std::filesystem;
```

## Testing and Validation

When generating code, consider:

1. **Is the code cross-platform compatible?**
2. **Are all member variables initialized?**
3. **Is const correctness maintained?**
4. **Are there proper Doxygen comments?**
5. **Does it follow the naming conventions?**
6. **Is memory managed safely (RAII/smart pointers)?**
7. **Are exceptions used appropriately?**
8. **Does it follow the brace style (Allman)?**

## Quick Reference Checklist

Before generating code, verify:

- [ ] Braces on their own line (Allman style)
- [ ] 4 space indentation (no tabs)
- [ ] Member variables have 'm' prefix
- [ ] Functions use camelCase
- [ ] Classes use PascalCase
- [ ] Include guards follow OPENMB_<PATH>_H format
- [ ] Includes are in correct order
- [ ] Const correctness is maintained
- [ ] Smart pointers used instead of raw pointers for ownership
- [ ] Override keyword used for virtual functions
- [ ] Doxygen comments on public APIs
- [ ] Initialization lists used in constructors
- [ ] Namespaces properly indented
- [ ] Line length under 120 columns
- [ ] Exception handling for error conditions

## Example: Complete Class Generation

When asked to create a new class, generate both header and implementation following this pattern:

**Header (playermanager.hpp):**
```cpp
#ifndef OPENMB_APPS_OPENMB_MBWORLD_PLAYERMANAGER_H
#define OPENMB_APPS_OPENMB_MBWORLD_PLAYERMANAGER_H

#include <memory>
#include <string>
#include <vector>

#include <components/esm/refid.hpp>

namespace MBWorld
{
    class Ptr;
    class CellStore;

    /// Manages player state and interactions
    ///
    /// This class handles player initialization, state updates,
    /// and interactions with the game world.
    class PlayerManager
    {
    public:
        PlayerManager();
        ~PlayerManager() = default;

        /// Initialize the player in the game world
        /// \param playerId The unique identifier for the player
        /// \param startCell The starting cell for the player
        /// \return True if initialization succeeded
        bool initialize(const ESM::RefId& playerId, CellStore* startCell);

        /// Update player state
        /// \param deltaTime Time elapsed since last update in seconds
        void update(float deltaTime);

        /// Get the current player pointer
        /// \return Const reference to the player pointer
        const Ptr& getPlayer() const;

        /// Check if player is initialized
        bool isInitialized() const;

    private:
        /// Load player data from save
        void loadPlayerData(const ESM::RefId& playerId);

        /// Validate player state
        bool validateState() const;

        Ptr mPlayer;
        ESM::RefId mPlayerId;
        bool mIsInitialized;
        float mTimeSinceLastUpdate;
    };
}

#endif
```

**Implementation (playermanager.cpp):**
```cpp
#include "playermanager.hpp"

#include <stdexcept>

#include "../mbbase/environment.hpp"
#include "cellstore.hpp"
#include "ptr.hpp"

namespace MBWorld
{
    PlayerManager::PlayerManager()
        : mPlayer()
        , mPlayerId()
        , mIsInitialized(false)
        , mTimeSinceLastUpdate(0.0f)
    {
    }

    bool PlayerManager::initialize(const ESM::RefId& playerId, CellStore* startCell)
    {
        if (!startCell)
        {
            throw std::invalid_argument("Start cell cannot be null");
        }

        if (playerId.empty())
        {
            throw std::invalid_argument("Player ID cannot be empty");
        }

        mPlayerId = playerId;
        loadPlayerData(playerId);

        // Additional initialization logic here

        mIsInitialized = validateState();
        return mIsInitialized;
    }

    void PlayerManager::update(float deltaTime)
    {
        if (!mIsInitialized)
        {
            throw std::runtime_error("Cannot update uninitialized player");
        }

        mTimeSinceLastUpdate += deltaTime;

        // Update logic here
    }

    const Ptr& PlayerManager::getPlayer() const
    {
        if (!mIsInitialized)
        {
            throw std::runtime_error("Player not initialized");
        }

        return mPlayer;
    }

    bool PlayerManager::isInitialized() const
    {
        return mIsInitialized;
    }

    void PlayerManager::loadPlayerData(const ESM::RefId& playerId)
    {
        // Load implementation
    }

    bool PlayerManager::validateState() const
    {
        // Validation implementation
        return true;
    }
}
```

## Summary

When generating code for OpenMB:
1. Follow Allman brace style (braces on own lines)
2. Use proper naming (mMemberVars, camelCaseFunctions, PascalCaseClasses)
3. Include Doxygen documentation
4. Use modern C++20 features
5. Maintain const correctness
6. Use smart pointers and RAII
7. Follow include order and guards
8. Keep code cross-platform

**Remember: Consistency with existing code is paramount. When in doubt, follow the patterns already established in the codebase.**