DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Looking back (Some of your first code)  (Read 5,112 times)

p3lb0x

  • Beer, beer, beer, I'm going for a beer!
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 424
    • Cody Award
    • Participated in NANY Challenge Once
Looking back (Some of your first code)
« on: January 21, 2016, 02:02 AM »
So, I recently stumbled on my first real C project, back from high school. It is pretty bad. And I thought it would be fun to share with you guys and maybe get to see some of the first code you guys did in whatever capacity you can find. I also have code from the first major Lua project I did. Both were written around the same time.

C code:
The below code is for a master microcontroller that calculates game of life and sends the "game" field to another microcontroller that had a charlieplexed 8x7 led display that displayed it. A video of it in action can be found here
comments are translated from danish and I have commented on the comments in parentheses (pastebin link)
Spoiler (click to show/hide)
Code
#include <htc.h>		//PIC hardware mapping
#include <delay.c>
#include <I2CMaster.h>
#include <stdlib.h>
//config bits

	__CONFIG(HS & PWRTDIS & WDTDIS & LVPDIS);

/////////// Constants ///////////
#define LEDPLADE8PIN 1					// Defines how many light plates with 8pins that's attached (Not ever actually used, was supposed to be able to handle more than one)

/////////// Bit manipulation functions ///////////

void clear_bit(unsigned char *x, char bitNum){
	*x &= ~(1 << bitNum);
}
void set_bit(unsigned char *x, char bitNum) {
	*x |= (1 << bitNum);
}
unsigned char get_bit(unsigned char x, char bitNum) {
	return x & (1 << bitNum);
}

/////////////// Function prototypes for patterns //////////////////

void glider(void);
void lwss(void);
void toad(void);
void kasse(void);
void blinker(void);
void randompat(void);

/////////// Variables ///////////

////////// GAME OF LIFE ///////////

	char LifeGameBuf[2][7];
	char LiveCount;
	char FirstEvol;
	char Pattern;
	char NewPattern;

///////////////////////////////////

	char i;								// Indexes
	char j;
	
	char I2C_Address;					// Defines the i2c address we want to write to
	char I2C_i;							// Defines what type of byte we want to send (I don't remember what this is, and it is apparently unused)

/////////// Main program ///////////
void main(void){						// Start

	srand(70);

/////////// Opsætning ///////////

/////////// Opsætning af SSP modul ///////////

	TRISC = 0b00011000;					// Sets port C pin 3 and 4 to be inputs
	SSPADD = 39;						// Defines the baudrate of the i2c communications
	SSPCON = 0b00101000;				// Configuration and start of SSP module
	SSPCON2 = 0b00000000;
	SSPSTAT = 0b11000000;
	SSPIE = 1;							// Enable interrupt on i2c communication start
	SSPIF = 0;

/////////// Setup of interrupts ///////////

	PEIE = 1;
	GIE = 1;							// Global interrupt enable

/////////// GAME OF LIFE ///////////
	
	LiveCount = 0;
	Pattern = 0;
	NewPattern = 0;
	
	glider();

/////////// Runs in a loop from here (Obviously not) ///////////

/////////// Pattern change ///////////
	TRISD = 0b00000010;				// Set pins on port D as input and output 
	while(!0){
	PORTD = 0b00001000;				// Drive current to RD3 pin
	if(RD3 == 0){					// I HAVE NO IDEA WHY THIS WORKS (Basically, the code here sets a pin, then checks if it is set. It worked, I still have no idea why)
	Pattern = Pattern + 1;
	NewPattern = 1;
	DelayMs(255);
	}
	if(NewPattern){
		if(Pattern > 5) Pattern = 0;
		if(Pattern == 0) glider();
		else if(Pattern == 1) lwss();
		else if(Pattern == 2) toad();
		else if(Pattern == 3) kasse();
		else if(Pattern == 4) blinker();
		else if(Pattern == 5) randompat();
		NewPattern = 0;
	}

//////////////// Blink light to indicate when the next generations of game of life is calculated /////////////////
	PORTD = 0;
	DelayMs(50);
	PORTD = 1;

//////////////// Game of Life /////////////////
		if(!FirstEvol){
			for(i=0; i<7; i++){
				for(j=0; j<8; j++){
					if(i == 0){
						if(j == 0){
							if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],7)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],7)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],7)) LiveCount = LiveCount + 1;
						}
						else if (j == 7){
							if(get_bit(LifeGameBuf[0][i],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
						}
						else{
							if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][6],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
						}
					}
					else if(i == 6){
						if(j == 0){
							if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],7)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],7)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],7)) LiveCount = LiveCount + 1;
						}
						else if (j == 7){
							if(get_bit(LifeGameBuf[0][i],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],j-1)) LiveCount = LiveCount + 1;
						}
						else{
							if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][0],j-1)) LiveCount = LiveCount + 1;
						}
					}
					else{
						if(j == 0){
							if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],7)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],7)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],7)) LiveCount = LiveCount + 1;
						}
						else if (j == 7){
							if(get_bit(LifeGameBuf[0][i],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],0)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
						}
						else{
							if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
							if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
						}
					}
					
				if(LiveCount < 2){clear_bit(&LifeGameBuf[1][i],j);}
				else if(LiveCount == 2 && get_bit(LifeGameBuf[1][i],j) == 1){set_bit(&LifeGameBuf[1][i],j);}
				else if(LiveCount == 2 && get_bit(LifeGameBuf[1][i],j) == 0){clear_bit(&LifeGameBuf[1][i],j);}
				else if(LiveCount == 3){set_bit(&LifeGameBuf[1][i],j);}
				else if(LiveCount > 3){clear_bit(&LifeGameBuf[1][i],j);}
				LiveCount = 0;
				
				}
			}
			for(i=0; i<7; i++){
			LifeGameBuf[0][i] = LifeGameBuf[1][i];
			}
		}
		FirstEvol = 0;				// No longer first generation

////////////// SEND TO DISPLAY /////////////////

	i2c_Start();
	i2c_Address(15,0);
	i2c_Write(LifeGameBuf[0][0]);
	i2c_Write(LifeGameBuf[0][1]);
	i2c_Write(LifeGameBuf[0][2]);
	i2c_Write(LifeGameBuf[0][3]);
	i2c_Write(LifeGameBuf[0][4]);
	i2c_Write(LifeGameBuf[0][5]);
	i2c_Write(LifeGameBuf[0][6]);
	i2c_Stop();
	}
}													// End

static void interrupt isr(void)
{

	if(SSPIF){										// Was it a i2c interrupt?
	SSPIF = 0;										// Clear SSP interrupt flag
	}
}

/////////// Game of Life pattern funftions ///////////

void glider() {

	LifeGameBuf[0][0] = 0b00100000;
	LifeGameBuf[0][1] = 0b10100000;
	LifeGameBuf[0][2] = 0b01100000;
	LifeGameBuf[0][3] = 0b00000000;
	LifeGameBuf[0][4] = 0b00000000;
	LifeGameBuf[0][5] = 0b00000000;
	LifeGameBuf[0][6] = 0b00000000;

	LifeGameBuf[1][0] = LifeGameBuf[0][0];
	LifeGameBuf[1][1] = LifeGameBuf[0][1];
	LifeGameBuf[1][2] = LifeGameBuf[0][2];
	LifeGameBuf[1][3] = LifeGameBuf[0][3];
	LifeGameBuf[1][4] = LifeGameBuf[0][4];
	LifeGameBuf[1][5] = LifeGameBuf[0][5];
	LifeGameBuf[1][6] = LifeGameBuf[0][6];

	FirstEvol = 1;
}


void lwss() {

	LifeGameBuf[0][0] = 0b00000000;
	LifeGameBuf[0][1] = 0b00000000;
	LifeGameBuf[0][2] = 0b00110000;
	LifeGameBuf[0][3] = 0b11011000;
	LifeGameBuf[0][4] = 0b11110000;
	LifeGameBuf[0][5] = 0b01100000;
	LifeGameBuf[0][6] = 0b00000000;

	LifeGameBuf[1][0] = LifeGameBuf[0][0];
	LifeGameBuf[1][1] = LifeGameBuf[0][1];
	LifeGameBuf[1][2] = LifeGameBuf[0][2];
	LifeGameBuf[1][3] = LifeGameBuf[0][3];
	LifeGameBuf[1][4] = LifeGameBuf[0][4];
	LifeGameBuf[1][5] = LifeGameBuf[0][5];
	LifeGameBuf[1][6] = LifeGameBuf[0][6];

	FirstEvol = 1;
}

void toad() {

	LifeGameBuf[0][0] = 0b00000000;
	LifeGameBuf[0][1] = 0b00000000;
	LifeGameBuf[0][2] = 0b00000000;
	LifeGameBuf[0][3] = 0b00111000;
	LifeGameBuf[0][4] = 0b00011100;
	LifeGameBuf[0][5] = 0b00000000;
	LifeGameBuf[0][6] = 0b00000000;

	LifeGameBuf[1][0] = LifeGameBuf[0][0];
	LifeGameBuf[1][1] = LifeGameBuf[0][1];
	LifeGameBuf[1][2] = LifeGameBuf[0][2];
	LifeGameBuf[1][3] = LifeGameBuf[0][3];
	LifeGameBuf[1][4] = LifeGameBuf[0][4];
	LifeGameBuf[1][5] = LifeGameBuf[0][5];
	LifeGameBuf[1][6] = LifeGameBuf[0][6];

	FirstEvol = 1;
}

void kasse() {

	LifeGameBuf[0][0] = 0b00000000;
	LifeGameBuf[0][1] = 0b00110000;
	LifeGameBuf[0][2] = 0b01001000;
	LifeGameBuf[0][3] = 0b01001000;
	LifeGameBuf[0][4] = 0b00110000;
	LifeGameBuf[0][5] = 0b00000000;
	LifeGameBuf[0][6] = 0b00000000;

	LifeGameBuf[1][0] = LifeGameBuf[0][0];
	LifeGameBuf[1][1] = LifeGameBuf[0][1];
	LifeGameBuf[1][2] = LifeGameBuf[0][2];
	LifeGameBuf[1][3] = LifeGameBuf[0][3];
	LifeGameBuf[1][4] = LifeGameBuf[0][4];
	LifeGameBuf[1][5] = LifeGameBuf[0][5];
	LifeGameBuf[1][6] = LifeGameBuf[0][6];

	FirstEvol = 1;
}

void blinker() {

	LifeGameBuf[0][0] = 0b01000000;
	LifeGameBuf[0][1] = 0b01000000;
	LifeGameBuf[0][2] = 0b01000000;
	LifeGameBuf[0][3] = 0b00001000;
	LifeGameBuf[0][4] = 0b00001000;
	LifeGameBuf[0][5] = 0b00001000;
	LifeGameBuf[0][6] = 0b00000000;

	LifeGameBuf[1][0] = LifeGameBuf[0][0];
	LifeGameBuf[1][1] = LifeGameBuf[0][1];
	LifeGameBuf[1][2] = LifeGameBuf[0][2];
	LifeGameBuf[1][3] = LifeGameBuf[0][3];
	LifeGameBuf[1][4] = LifeGameBuf[0][4];
	LifeGameBuf[1][5] = LifeGameBuf[0][5];
	LifeGameBuf[1][6] = LifeGameBuf[0][6];

	FirstEvol = 1;
}

void randompat() {

	LifeGameBuf[0][0] = (char)rand();
	LifeGameBuf[0][1] = (char)rand();
	LifeGameBuf[0][2] = (char)rand();
	LifeGameBuf[0][3] = (char)rand();
	LifeGameBuf[0][4] = (char)rand();
	LifeGameBuf[0][5] = (char)rand();
	LifeGameBuf[0][6] = (char)rand();

	LifeGameBuf[1][0] = LifeGameBuf[0][0];
	LifeGameBuf[1][1] = LifeGameBuf[0][1];
	LifeGameBuf[1][2] = LifeGameBuf[0][2];
	LifeGameBuf[1][3] = LifeGameBuf[0][3];
	LifeGameBuf[1][4] = LifeGameBuf[0][4];
	LifeGameBuf[1][5] = LifeGameBuf[0][5];
	LifeGameBuf[1][6] = LifeGameBuf[0][6];

	FirstEvol = 1;
}
Fun things:
while(!0)
The "I HAVE NO IDEA WHY THIS WORKS" comment

Lua code:
The below code is for a randomized gun generation thing for the game Cortex Command, a video of which can be found here. If I recall correctly, it was also my first NANY.
removed a few new lines, otherwise did nothing (pastebin link)
Spoiler (click to show/hide)
Code
function Create(self)

-- Initialize variables and timers

self.smgbodylist = {};
self.smgbarrellist = {};
self.smgmaglist = {};
self.smgbulletlist = {};
self.smgsoundlist = {};
self.smgsoundlist["fire"] = {};
self.smgsoundlist["reload"] = {};
self.smgmuzzlelist = {};

self.projectileteam = -1;
self.allmodulesfound = false;
self.ammocounter = 0;
self.reloading = false;

self.Body = {};
self.Mag = {};
self.Barrel = {};
self.Bullet = {};
self.FireSound = nil;
self.ReloadSound = nil;
self.MuzzleFlash = nil;

self.firingtimer = Timer();

-- Initial stats
-- Basic stats
self.rateoffire = 700;
self.ammocapacity = 20;
self.accuracy = 25;

-- Initial modifiers
self.rofmod = 0.8 + math.random()*0.4;
self.magmod = 0.8 + math.random()*0.4;
self.accmod = 0.8 + math.random()*0.4;
self.firevelmod = 0.8 + math.random()*0.4;
self.massmod = 0.8 + math.random()*0.4;
self.sharpnessmod = 0.8 + math.random()*0.4;

-- Load body modules
dofile("Brolands.rte/smg/bodymodules.lua");
BrolandsBodies(self);

-- Load barrel modules
dofile("Brolands.rte/smg/barrelmodules.lua");
BrolandsBarrels(self);

-- Load magazines modules
dofile("Brolands.rte/smg/magazinemodules.lua");
BrolandsMagazines(self);

-- Load bullets
dofile("Brolands.rte/smg/bulletmodules.lua");
BrolandsBullets(self);

-- Load muzzleflashes
dofile("Brolands.rte/smg/muzzleflashmodules.lua");
BrolandsMuzzleflashes(self);

-- Load sounds
dofile("Brolands.rte/smg/soundmodules.lua");
BrolandsSounds(self);

end

function Update(self)

-- Generate gun
	if self.allmodulesfound ~= true then
	-- Find the attachments
		for i=1,MovableMan:GetMOIDCount()-1 do
			local mo = MovableMan:GetMOFromID(i);
			if mo.PresetName == "SmgBody" and mo.RootID == self.RootID then
				self.Body["pointer"] = ToAttachable(mo);
				self.Body["pointer"].GetsHitByMOs = false;
			elseif mo.PresetName == "SmgMag" and mo.RootID == self.RootID then
				self.Mag["pointer"] = ToAttachable(mo);
				self.Mag["pointer"].GetsHitByMOs = false;
			elseif mo.PresetName == "SmgBarrel" and mo.RootID == self.RootID then
				self.Barrel["pointer"] = ToAttachable(mo);
				self.Barrel["pointer"].GetsHitByMOs = false;
			end
			if self.Body["pointer"] and self.Mag["pointer"] and self.Barrel["pointer"] then
			self.allmodulesfound = true;
				break;
			end
		end

-- Body
	for k,v in pairs(self.smgbodylist[math.random(1,#self.smgbodylist)]) do self.Body[k] = v end
--	for k,v in pairs(self.smgbodylist[15]) do self.Body[k] = v end
	self.Body["pointer"].Frame = self.Body["sprite"];

-- Modify stat modifier with module modifier
	self.rofmod = self.rofmod * self.Body["rofmod"];
	self.magmod = self.magmod * self.Body["magmod"];
	self.accmod = self.accmod * self.Body["accmod"];
	self.firevelmod = self.firevelmod * self.Body["firevelmod"];
	self.massmod = self.massmod * self.Body["massmod"];
	self.sharpnessmod = self.sharpnessmod * self.Body["sharpnessmod"];

-- Mag
	for k,v in pairs(self.smgmaglist[math.random(1,#self.smgmaglist)]) do self.Mag[k] = v end
--	for k,v in pairs(self.smgmaglist[16]) do self.Mag[k] = v end
	self.Mag["pointer"].Frame = self.Mag["sprite"];
	self.Mag["offset"] = self.Body["magoffset"];

-- Modify stat modifier with module modifier
	self.rofmod = self.rofmod * self.Mag["rofmod"];
	self.magmod = self.magmod * self.Mag["magmod"];
	self.accmod = self.accmod * self.Mag["accmod"];
	self.firevelmod = self.firevelmod * self.Mag["firevelmod"];
	self.massmod = self.massmod * self.Mag["massmod"];
	self.sharpnessmod = self.sharpnessmod * self.Mag["sharpnessmod"];

-- Barrel
	for k,v in pairs(self.smgbarrellist[math.random(1,#self.smgbarrellist)]) do self.Barrel[k] = v end
--	for k,v in pairs(self.smgbarrellist[18]) do self.Barrel[k] = v end
	self.Barrel["pointer"].Frame = self.Barrel["sprite"];
	self.Barrel["offset"] = self.Body["barreloffset"];

-- Modify gun stat modifier with module modifier
	self.rofmod = self.rofmod * self.Barrel["rofmod"];
	self.magmod = self.magmod * self.Barrel["magmod"];
	self.accmod = self.accmod * self.Barrel["accmod"];
	self.firevelmod = self.firevelmod * self.Barrel["firevelmod"];
	self.massmod = self.massmod * self.Barrel["massmod"];
	self.sharpnessmod = self.sharpnessmod * self.Barrel["sharpnessmod"];

-- Bullet
	for k,v in pairs(self.smgbulletlist[math.random(1,#self.smgbulletlist)]) do self.Bullet[k] = v end
--	for k,v in pairs(self.smgbulletlist[3]) do self.Bullet[k] = v end

-- Modify gun stat modifier with module modifier
	self.rofmod = self.rofmod * self.Bullet["rofmod"];
	self.magmod = self.magmod * self.Bullet["magmod"];
	self.accmod = self.accmod * self.Bullet["accmod"];
	self.firevelmod = self.firevelmod * self.Bullet["firevelmod"];
	self.massmod = self.massmod * self.Bullet["massmod"];
	self.sharpnessmod = self.sharpnessmod * self.Bullet["sharpnessmod"];

-- Fire sound

self.FireSound = self.smgsoundlist["fire"][self.Bullet["firesounds"][math.random(1,#self.Bullet["firesounds"])]];

-- Reload sound

self.ReloadSound = self.smgsoundlist["reload"][self.Mag["reloadsounds"][math.random(1,#self.Mag["reloadsounds"])]];

-- Muzzle flash

self.MuzzleFlash = self.smgmuzzlelist[self.Bullet["muzzleflash"][math.random(1,#self.Bullet["muzzleflash"])]];

-- Done generating gun, apply gun stat modifiers
	self.rateoffire = Round(self.rateoffire * self.rofmod);
	self.ammocapacity = Round(self.ammocapacity * self.magmod);

-- You can't be more precise than 100%
if self.accuracy > 100 then
	self.accuracy = 100;
else
	self.accuracy = Round(self.accuracy * self.accmod);
end
	
	self.Bullet["firevel"] = Round(self.Bullet["firevel"] * self.firevelmod);
	self.Bullet["mass"] = self.Bullet["mass"] * self.massmod;
	self.Bullet["sharpness"] = Round(self.Bullet["sharpness"] * self.sharpnessmod);
	
	
-- Output gun stats to console
--	print("\nRate of fire: "..self.rateoffire.." Rounds Per Minute\nMagazine capacity: "..self.ammocapacity.." Rounds\nAccuracy: "..self.accuracy.."%\nProjectile type: "..self.Bullet["particle"].."\nProjectile mass: "..self.Bullet["mass"].."\nProjectile speed: "..self.Bullet["firevel"].."\nProjectile sharpness: "..self.Bullet["sharpness"].."\n");

	self.ammocounter = self.ammocapacity;

-- Clear unneeded tables and variables
self.smgbodylist = nil;
self.smgbarrellist = nil;
self.smgmaglist = nil;
self.smgbulletlist = nil;
self.smgsoundlist = nil;
self.rofmod = nil;
self.magmod = nil;
self.accmod = nil;
self.firevelmod = nil;
self.massmod = nil;
self.sharpnessmod = nil;
end

-- Figure out what team is currently holding the gun
self.projectileteam = MovableMan:GetMOFromID(self.RootID).Team;

-- Move around attachables
	if self.Body["pointer"].ToDelete ~= true and self.Body["pointer"].PresetName ~= "" then
		if self.HFlipped then
			self.Body["pointer"].Pos = self.Pos + Vector(self.Body["offset"].X * -1,self.Body["offset"].Y):RadRotate(self.RotAngle);
		else
			self.Body["pointer"].Pos = self.Pos + Vector(self.Body["offset"].X,self.Body["offset"].Y):RadRotate(self.RotAngle);
		end
		self.Body["pointer"].RotAngle = self.RotAngle;
		self.Body["pointer"].HFlipped = self.HFlipped;
	end

	if self.Mag["pointer"].ToDelete ~= true and self.Mag["pointer"].PresetName ~= "" then
		if self.HFlipped then
			self.Mag["pointer"].Pos = self.Pos + Vector(self.Mag["offset"].X * -1,self.Mag["offset"].Y):RadRotate(self.RotAngle);
		else
			self.Mag["pointer"].Pos = self.Pos + Vector(self.Mag["offset"].X,self.Mag["offset"].Y):RadRotate(self.RotAngle);
		end
		self.Mag["pointer"].RotAngle = self.RotAngle;
		self.Mag["pointer"].HFlipped = self.HFlipped;
	end

	if self.Barrel["pointer"].ToDelete ~= true and self.Barrel["pointer"].PresetName ~= "" then
		if self.HFlipped then
			self.Barrel["pointer"].Pos = self.Pos + Vector(self.Barrel["offset"].X * -1,self.Barrel["offset"].Y):RadRotate(self.RotAngle);
		else
			self.Barrel["pointer"].Pos = self.Pos + Vector(self.Barrel["offset"].X,self.Barrel["offset"].Y):RadRotate(self.RotAngle);
		end
		self.Barrel["pointer"].RotAngle = self.RotAngle;
		self.Barrel["pointer"].HFlipped = self.HFlipped;
	end

	if self:IsActivated() and self.firingtimer:IsPastSimMS(60000 / self.rateoffire) and self.ammocounter > 0 and self.Magazine ~= nil then
	self.ammocounter = self.ammocounter - 1;
	self.firingtimer:Reset()

-- Decide which kind of particle to create depending on the bullets particle type
		if self.Bullet["particletype"] == "MOPixel" then
			self.particle = CreateMOPixel(self.Bullet["particle"],"Brolands.rte");
		elseif self.Bullet["particletype"] == "MOSRotating" then
			self.particle = CreateMOSRotating(self.Bullet["particle"],"Brolands.rte");
		elseif self.Bullet["particletype"] == "MOSParticle" then
			self.particle = CreateMOSParticle(self.Bullet["particle"],"Brolands.rte");
		end

-- Decide which kind of particle to create depending on the muzzleflashs particle type
		if self.MuzzleFlash["particletype"] == "MOPixel" then
			self.particle2 = CreateMOPixel(self.MuzzleFlash["particle"],"Brolands.rte");
		elseif self.MuzzleFlash["particletype"] == "MOSRotating" then
			self.particle2 = CreateMOSRotating(self.MuzzleFlash["particle"],"Brolands.rte");
		elseif self.MuzzleFlash["particletype"] == "MOSParticle" then
			self.particle2 = CreateMOSParticle(self.MuzzleFlash["particle"],"Brolands.rte");
		end

-- Figure out if the actor is sharpaiming or not.
	local MO = MovableMan:GetMOFromID(self.RootID)
	local Aiming = 1;
	if MO then
		local Actor = ToActor(MO)
		if Actor:GetController():IsState(Controller.AIM_SHARP) then
		Aiming = 0.5;
		else
		Aiming = 1;
		end
	end

-- Create a direction vector for the projectile depending on if the gun is HFlipped or not
		if self.HFlipped == true then
			self.particle.Pos = self.Barrel["pointer"].Pos + Vector(self.Barrel["muzzleoffset"].X * -1,self.Barrel["muzzleoffset"].Y):RadRotate(self.RotAngle);
			if math.random() > 0.5 then
				self.firingvector = Vector(-1,0):RadRotate(self.RotAngle + math.random() * (math.pi/8) * ((100 - self.accuracy)/100) * Aiming);
			else
				self.firingvector = Vector(-1,0):RadRotate(self.RotAngle - math.random() * (math.pi/8) * ((100 - self.accuracy)/100) * Aiming);
			end
		else
			self.particle.Pos = self.Barrel["pointer"].Pos + Vector(self.Barrel["muzzleoffset"].X,self.Barrel["muzzleoffset"].Y):RadRotate(self.RotAngle);
			if math.random() > 0.5 then
				self.firingvector = Vector(1,0):RadRotate(self.RotAngle + math.random() * (math.pi/8) * ((100 - self.accuracy)/100) * Aiming);
			else
				self.firingvector = Vector(1,0):RadRotate(self.RotAngle - math.random() * (math.pi/8) * ((100 - self.accuracy)/100) * Aiming);
			end
		end
		self.particle.Mass = self.Bullet["mass"]
		self.particle.Sharpness = self.Bullet["sharpness"];
		self.particle.Vel = self.firingvector * self.Bullet["firevel"];
		self.particle.LifeTime = 1500;
		self.particle.Team = self.projectileteam;
		self.particle.IgnoresTeamHits = true;
		MovableMan:AddParticle(self.particle);

-- Create firing sound emitter.
		self.particle = CreateAEmitter(self.FireSound,"Brolands.rte");
		self.particle.Pos = self.Pos;
		MovableMan:AddParticle(self.particle);

-- Create muzzleflash
		self.particle2.Pos = self.Barrel["pointer"].Pos + Vector(self.Barrel["muzzleoffset"].X * -1,self.Barrel["muzzleoffset"].Y):RadRotate(self.RotAngle);
		self.particle2.Vel = self.Vel;
		MovableMan:AddParticle(self.particle2);
	end
	
-- Keeps track of the magazine
	if self.Magazine == nil then
		self.Mag["pointer"].Scale = 0;
		self.ammocounter = self.ammocapacity;
		self.reloading = true;
	else
		self.Mag["pointer"].Scale = 1;
		self.Magazine.RoundCount = self.ammocounter;
	end

	if self.reloading == true and self.Magazine ~= nil then
	self.reloading = false;
	-- When the gun is done reloading, play reload sound
	self.particle = CreateAEmitter(self.ReloadSound,"Brolands.rte");
	self.particle.Pos = self.Pos;
	MovableMan:AddParticle(self.particle);
	end

end

function Destroy(self)
end

function Round(num, idp)
	local mult = 10^(idp or 0)
	return math.floor(num * mult + 0.5) / mult
end
Fun things:
"pointers" in lua
Stop mousering people so much - Mouser

x16wda

  • what am I doing in this handbasket?
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
    • Participated in NANY Challenge Once
Re: Looking back (Some of your first code)
« Reply #1 on: January 21, 2016, 02:52 AM »
Heh... nice that you have that! I saved the executables of my Atari programs from the early 90s when I was sysopping on Compuserve, but even though I posted the source online I haven't ever found it again. I'm sure I'd cringe to see it again... but it's nice to run across an old post about my big hit, and I even found the whole package in case anyone has an old Atari ST laying around. :P
vi vi vi - editor of the beast

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Looking back (Some of your first code)
« Reply #2 on: January 21, 2016, 03:39 AM »
I had one particular project that I'd update whenever I started learning a new language.  I have it in VB (the first language that I started programming UIs in), Delphi, VB.NET (ick), C#.  I don't think I have the code for that anymore... but you've spurred me to look :)

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,888
    • Participated in NANY Challenge Six Times
    • Coding Snacks
    • Gathering Badge
    • Cody Award
    • Volunteers/Staff
Re: Looking back (Some of your first code)
« Reply #3 on: January 21, 2016, 09:53 AM »
My first C code.  :D Compiled executable for Windows, available here. There's been a page on my website dedicated to it, for almost 10 years.

Spoiler (click to show/hide)
Code: ( c)
// ---------------------------------------------------------------------------------------

#include <stdio.h>

int main()
{
	char noun1[20];
	char number1[20];
	char verbing1[20];
	char verbing2[20];
	char verbing3[20];
	char noun2[20];
	char adjective1[20];
	char pluralnoun1[20];
	char noun3[20];
	char adjective2[20];
	char noun4[20];
	char adjective3[20];
	char verb1[20];
	char personinroom1[20];
	char noun5[20];
	char noun6[20];
	char noun7[20];
	char noun8[20];
	char sillyword1[20];
	char noun9[20];
	
	
	//explain game
	printf("Let's play a game of Madlibs.\n");
	printf("All you have to do is supply the words I ask for.\n");
	printf("Let's begin.\n");
	
	//get word list
	printf("Enter a noun: ");
	scanf("%s",noun1);
	printf("Enter a number: ");
	scanf("%s",number1);
	printf("Enter a verb ending in \"ing\": ");
	scanf("%s",verbing1);
	printf("Enter another verb ending in \"ing\": ");
	scanf("%s",verbing2);
	printf("Enter one more verb ending in \"ing\": ");
	scanf("%s",verbing3);
	printf("Enter a noun: ");
	scanf("%s",noun2);
	printf("Enter an adjective: ");
	scanf("%s",adjective1);
	printf("Enter a plural noun: ");
	scanf("%s",pluralnoun1);
	printf("Enter a noun: ");
	scanf("%s",noun3);
	printf("Enter a adjective: ");
	scanf("%s",adjective2);
	printf("Enter a noun: ");
	scanf("%s",noun4);
	printf("Enter a adjective: ");
	scanf("%s",adjective3);
	printf("Enter a verb: ");
	scanf("%s",verb1);
	printf("Enter the name of a person in the room: ");
	scanf("%s",personinroom1);
	printf("Enter a noun: ");
	scanf("%s",noun5);
	printf("Enter a noun: ");
	scanf("%s",noun6);
	printf("Enter a noun: ");
	scanf("%s",noun7);
	printf("Enter a noun: ");
	scanf("%s",noun8);
	printf("Enter a silly word: ");
	scanf("%s",sillyword1);
	printf("Enter a noun: ");
	scanf("%s",noun9);
	
	//display story
	
	printf("-----Career Advertisement-----\n\n");
	printf("Seeking a new career?\n\
Be a %s or just look like one!\n\
In just %s sessions, we can have you %s, %s, and %s like a\n\
top-paying %s.\n\
Opportunities in this %s field are limited.\n\
There is no fee!\n\
Just come in for a free consultation.\n\
Our expert %s will analyze your %s and determine your\n\
potential for success in this %s field.\n\
Use your natural %s to earn %s money and\n\
have time to %s your dreams too.\n\
Just ask %s who came to us looking like a\n\
%s out of %s and in just ten days we improved their\n\
%s 100 percent.\n\
We even corrected their horrible %s.\n\
It was just in the nick of time because the %s Squad\n\
was ready to ban them from the %s.\n\
Don't wait annother day.\n\
Time is running out.\n",\
noun1,number1,verbing1,verbing2,verbing3,noun2,adjective1,pluralnoun1,noun3,adjective2,\
noun4,adjective3,verb1,personinroom1,noun5,noun6,noun7,noun8,sillyword1,noun9);

// keep console window open for Windows users.
// won't work for other OS's, but Windows users are the only ones that need idiot-proofing 
// because no matter how many times you tell them to run it from a command prompt they will just click the exe file any way.

system("pause");
	
	return(0);
}

Original address: https://www.donationcoder.com/forum/index.php?topic=42231.0